// File: /workspace/github_c_makefile_star2_O3/pipde[P]deos-songlib/lib/songlib.c
// Function: dplay
void
dplay(double beats,int *data,int length, int bps)
    {
    static double beatSlop = 0.0;

    double where;
    double front;
    double middle;

    int skip;
    register double samples = beatsToSamples(beats);
    register int noteSamples = (int) samples;
    register int dataLen;
    register int len;
    register double diminishedAmplitude,increasedAmplitude;
    register int i;
    register double fadeLimit;
    register double bpsFactor;
    double startOfFade;
    double emphasis;

    dTrace("entering\n");
    //printf("playing note at %f\n",location - spaces);

    if (beats == 0) 
        {
        oneshot(data,length,bps);
        return;
        }

    samples = beatsToSamples(beats);
    noteSamples = (int) samples;
    where = samplesToMeasures(location - spaces);

    front = (int) (where + 0.011);

    if (beatsPerMeasure % 2 == 0)
        middle = 0.5;
    else
        middle = (beatsPerMeasure + 1.0) / (2 * beatsPerMeasure);

    //printf("where: %f, front: %f, middle: %f, front + middle: %f\n",
        //where,front,middle,front+middle);
    //getchar();

    if (where > front - 0.01 && where < front + 0.01)
        {
        emphasis = primaryEmphasis;
        //printf("primary emphasis at measure %f\n",where);
        }
    else if (where > front + middle - 0.01 && where < front + middle + 0.01)
        {
        emphasis = secondaryEmphasis;
        //printf("secondary emphasis at measure %f\n",where);
        }
    else
        {
        emphasis = 1.0;
        }

    beatSlop += samples - noteSamples;
    while (beatSlop > 1.0)
        {
        //printf("dplay: adding extra sample\n");
        ++noteSamples;
        beatSlop -= 1.0;
        }

    bpsFactor = pow(2,bitsPerSample) / pow(2,bps);

    skip = beatsToSamples(skipAttack);

    len = MIN(length, noteSamples - skip);
    dataLen = length;

    while (location + noteSamples >= output->samples)
        growOutput();

    /* write out all the data we have up to len (len <= noteSamples) */

    increasedAmplitude = 0;
    for (i = 0; i < len; ++i)
        {
        output->data[0][(int)location] +=
            data[i+skip] * amplitude * bpsFactor * emphasis * increasedAmplitude;
        location += 1;
        if (increasedAmplitude >= 1)
            increasedAmplitude = 1;
        else
            increasedAmplitude += attack;
        }

    /* if we run out of data, replicate the last sample to make up */
    /* the shortfall */
    
    if (len < noteSamples)
        {
        register const int lastSample = data[length-1] * amplitude * emphasis;

        diminishedAmplitude = amplitude * emphasis;
        for (; i < noteSamples; ++i)
            {
            output->data[0][(int)location] += lastSample
                * bpsFactor *diminishedAmplitude;
            location += 1;
            diminishedAmplitude *= sustain;
            }
        }

    /* write out fade/sustained portion */

    startOfFade = location;
    diminishedAmplitude = amplitude * emphasis;
    fadeLimit = FADE_LIMIT / MAX(crescendoFactor,1.0);

    for (; i < dataLen && diminishedAmplitude > fadeLimit; ++i)
        {
        //added the next line (probably should compute the
        //number of samples needed in advance)
        if (location >= output->samples) growOutput();
        output->data[0][(int)location] += 
            data[i] * diminishedAmplitude * bpsFactor;
        location += 1;
        diminishedAmplitude *= sustain;
        }

    if (location > maxLocation) maxLocation = location;

    sustainedLocation = location;
    location = startOfFade;
    //printf("ending note at %f\n",location - spaces);
    //printf("    %f samples\n",samples);

    if (TempoChange) setTempo(getTempo() + beats * TempoDelta);
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x30, %rsp
movl %edx, %r14d
movl %esi, %ebp
movq %rdi, %rbx
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x3185
jp 0x3185
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x3779
cvtsi2sdl 0xc2a3(%rip), %xmm2 # 0xf430
movsd 0x70eb(%rip), %xmm3     # 0xa280
movsd %xmm0, 0x28(%rsp)
mulsd %xmm3, %xmm0
movsd 0xc309(%rip), %xmm4     # 0xf4b0
divsd %xmm4, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %r15d
movsd 0x1bb4c(%rip), %xmm1    # 0x1ed08
subsd 0x1be94(%rip), %xmm1    # 0x1f058
divsd %xmm2, %xmm1
divsd %xmm3, %xmm1
movl 0xc27e(%rip), %eax      # 0xf450
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd %xmm4, %xmm1
divsd %xmm2, %xmm1
movsd 0x70af(%rip), %xmm3     # 0xa298
addsd %xmm1, %xmm3
cvttpd2dq %xmm3, %xmm3
cvtdq2pd %xmm3, %xmm3
movsd 0x70a3(%rip), %xmm4     # 0xa2a0
addsd %xmm3, %xmm4
ucomisd %xmm4, %xmm1
jbe 0x3223
movsd 0x7099(%rip), %xmm4     # 0xa2a8
addsd %xmm3, %xmm4
ucomisd %xmm1, %xmm4
jbe 0x3223
movsd 0xc237(%rip), %xmm5     # 0xf458
jmp 0x3276
movsd 0x7035(%rip), %xmm5     # 0xa260
testb $0x1, %al
je 0x3242
addl %eax, %eax
addsd %xmm5, %xmm2
xorps %xmm4, %xmm4
cvtsi2sd %eax, %xmm4
divsd %xmm4, %xmm2
jmp 0x324a
movsd 0x701e(%rip), %xmm2     # 0xa268
addsd %xmm3, %xmm2
movsd 0x704a(%rip), %xmm3     # 0xa2a0
addsd %xmm2, %xmm3
ucomisd %xmm3, %xmm1
jbe 0x3276
addsd 0x7040(%rip), %xmm2     # 0xa2a8
ucomisd %xmm1, %xmm2
jbe 0x3276
movsd 0xc1ea(%rip), %xmm5     # 0xf460
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
subsd %xmm1, %xmm0
addsd 0x1bdc6(%rip), %xmm0    # 0x1f050
ucomisd 0x6fce(%rip), %xmm0     # 0xa260
movsd %xmm5, 0x10(%rsp)
jbe 0x32bf
movsd 0x700e(%rip), %xmm1     # 0xa2b0
movsd 0x6fb6(%rip), %xmm2     # 0xa260
incl %r15d
addsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
ja 0x32aa
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, 0x1bd83(%rip)    # 0x1f050
xorps %xmm0, %xmm0
cvtsi2sdl 0xc160(%rip), %xmm0 # 0xf438
callq 0x2030
movsd %xmm0, 0x20(%rsp)
movsd 0x6f75(%rip), %xmm0     # 0xa260
movl %r14d, %edi
callq 0x2060
movsd %xmm0, 0x18(%rsp)
movsd 0x1bd5f(%rip), %xmm0    # 0x1f060
mulsd 0x6f77(%rip), %xmm0     # 0xa280
divsd 0xc19f(%rip), %xmm0     # 0xf4b0
xorps %xmm1, %xmm1
cvtsi2sdl 0xc114(%rip), %xmm1 # 0xf430
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %r12d
movl %r15d, %r14d
subl %r12d, %r14d
cmpl %ebp, %r14d
cmovgel %ebp, %r14d
movsd 0x1b9cd(%rip), %xmm9    # 0x1ed08
movapd %xmm9, %xmm0
movsd 0x8(%rsp), %xmm2
addsd %xmm2, %xmm0
movq 0x1b9af(%rip), %rax     # 0x1ed00
xorps %xmm1, %xmm1
cvtsi2sdl 0xc(%rax), %xmm1
ucomisd %xmm1, %xmm0
jb 0x3397
movsd %xmm2, 0x8(%rsp)
callq 0x38cb
movsd 0x8(%rsp), %xmm2
movsd 0x1b98f(%rip), %xmm9    # 0x1ed08
movapd %xmm9, %xmm0
addsd %xmm2, %xmm0
movq 0x1b977(%rip), %rax     # 0x1ed00
xorps %xmm1, %xmm1
cvtsi2sdl 0xc(%rax), %xmm1
ucomisd %xmm1, %xmm0
jae 0x3365
movsd 0x20(%rsp), %xmm7
divsd 0x18(%rsp), %xmm7
testl %r14d, %r14d
movsd 0x6eb1(%rip), %xmm8     # 0xa260
jle 0x344a
movsd 0xc0ab(%rip), %xmm0     # 0xf468
movq 0x18(%rax), %rcx
movq (%rcx), %rcx
movslq %r12d, %rsi
movl %r14d, %edx
leaq (%rbx,%rsi,4), %rsi
xorpd %xmm1, %xmm1
xorl %edi, %edi
movsd 0x10(%rsp), %xmm6
xorps %xmm2, %xmm2
cvtsi2sdl (%rsi,%rdi,4), %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm7, %xmm2
cvttsd2si %xmm9, %r8d
mulsd %xmm6, %xmm2
movslq %r8d, %r8
xorps %xmm3, %xmm3
cvtsi2sdl (%rcx,%r8,4), %xmm3
mulsd %xmm1, %xmm2
addsd %xmm3, %xmm2
cvttsd2si %xmm2, %r9d
movl %r9d, (%rcx,%r8,4)
addsd %xmm8, %xmm9
movapd %xmm8, %xmm2
cmpnlesd %xmm1, %xmm2
addsd 0xc049(%rip), %xmm1     # 0xf470
andpd %xmm2, %xmm1
andnpd %xmm8, %xmm2
orpd %xmm2, %xmm1
incq %rdi
cmpq %rdi, %rdx
jne 0x33da
movsd %xmm9, 0x1b8c3(%rip)    # 0x1ed08
movl %r14d, %ecx
jmp 0x3452
xorl %ecx, %ecx
movsd 0x10(%rsp), %xmm6
cmpl %r15d, %r14d
jge 0x34e4
movsd 0xc005(%rip), %xmm0     # 0xf468
cmpl %r15d, %ecx
jge 0x34ec
movapd %xmm6, %xmm1
mulsd %xmm0, %xmm1
movslq %ebp, %rdx
xorps %xmm2, %xmm2
cvtsi2sdl -0x4(%rbx,%rdx,4), %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm6, %xmm2
cvttpd2dq %xmm2, %xmm2
cvtdq2pd %xmm2, %xmm2
mulsd %xmm7, %xmm2
movq 0x18(%rax), %rdx
movq (%rdx), %rdx
movsd 0xbfd5(%rip), %xmm3     # 0xf478
cvttsd2si %xmm9, %esi
movslq %esi, %rsi
xorps %xmm4, %xmm4
cvtsi2sdl (%rdx,%rsi,4), %xmm4
movapd %xmm2, %xmm5
mulsd %xmm1, %xmm5
addsd %xmm4, %xmm5
cvttsd2si %xmm5, %edi
movl %edi, (%rdx,%rsi,4)
addsd %xmm8, %xmm9
mulsd %xmm3, %xmm1
incl %ecx
cmpl %ecx, %r15d
jne 0x34a3
movsd %xmm9, 0x1b829(%rip)    # 0x1ed08
movl %r15d, %ecx
jmp 0x34ec
movsd 0xbf7c(%rip), %xmm0     # 0xf468
movapd %xmm9, %xmm1
cmpl %ebp, %ecx
jge 0x35ed
mulsd %xmm0, %xmm6
movsd 0xbf7b(%rip), %xmm0     # 0xf480
maxsd 0x6d53(%rip), %xmm0     # 0xa260
movsd 0x6da3(%rip), %xmm3     # 0xa2b8
divsd %xmm0, %xmm3
ucomisd %xmm3, %xmm6
movapd %xmm9, %xmm1
jbe 0x35ed
movslq %ecx, %r14
movslq %ebp, %r15
incq %r14
movapd %xmm9, %xmm1
movsd %xmm7, 0x20(%rsp)
movsd %xmm9, 0x8(%rsp)
movsd %xmm3, 0x18(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0xc(%rax), %xmm0
ucomisd %xmm0, %xmm1
jb 0x3593
movsd %xmm6, 0x10(%rsp)
callq 0x38cb
movsd 0x18(%rsp), %xmm3
movsd 0x8(%rsp), %xmm9
movsd 0x6ce8(%rip), %xmm8     # 0xa260
movsd 0x20(%rsp), %xmm7
movsd 0x10(%rsp), %xmm6
movq 0x1b775(%rip), %rax     # 0x1ed00
movsd 0x1b775(%rip), %xmm1    # 0x1ed08
xorps %xmm0, %xmm0
cvtsi2sdl -0x4(%rbx,%r14,4), %xmm0
mulsd %xmm6, %xmm0
movq 0x18(%rax), %rcx
cvttsd2si %xmm1, %edx
movq (%rcx), %rcx
movslq %edx, %rdx
xorps %xmm2, %xmm2
cvtsi2sdl (%rcx,%rdx,4), %xmm2
mulsd %xmm7, %xmm0
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %esi
movl %esi, (%rcx,%rdx,4)
addsd %xmm8, %xmm1
movsd %xmm1, 0x1b735(%rip)    # 0x1ed08
cmpq %r15, %r14
jge 0x35ed
mulsd 0xbe98(%rip), %xmm6     # 0xf478
incq %r14
ucomisd %xmm3, %xmm6
ja 0x3549
xorps %xmm0, %xmm0
cvtsi2sdl 0x1b718(%rip), %xmm0 # 0x1ed10
ucomisd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
jbe 0x3608
movl %eax, 0x1b708(%rip)     # 0x1ed10
movl %eax, 0x1ba5a(%rip)     # 0x1f068
movsd %xmm9, 0x1b6f1(%rip)    # 0x1ed08
cmpl $0x0, 0xbf2a(%rip)      # 0xf548
movsd 0x28(%rsp), %xmm0
je 0x363e
mulsd 0xbf22(%rip), %xmm0     # 0xf550
addsd 0xbe7a(%rip), %xmm0     # 0xf4b0
movsd %xmm0, 0xbe72(%rip)     # 0xf4b0
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq