// File: /workspace/github_c_makefile_star2_O1/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 0x31ae
jp 0x31ae
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x37aa
cvtsi2sdl 0xc27a(%rip), %xmm2 # 0xf430
movsd 0x70c2(%rip), %xmm3     # 0xa280
movsd %xmm0, 0x28(%rsp)
mulsd %xmm3, %xmm0
movsd 0xc2e0(%rip), %xmm4     # 0xf4b0
divsd %xmm4, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %r15d
movsd 0x1bb23(%rip), %xmm1    # 0x1ed08
subsd 0x1be6b(%rip), %xmm1    # 0x1f058
divsd %xmm2, %xmm1
divsd %xmm3, %xmm1
movl 0xc255(%rip), %eax      # 0xf450
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd %xmm4, %xmm1
divsd %xmm2, %xmm1
movsd 0x7086(%rip), %xmm3     # 0xa298
addsd %xmm1, %xmm3
cvttpd2dq %xmm3, %xmm3
cvtdq2pd %xmm3, %xmm3
movsd 0x707a(%rip), %xmm4     # 0xa2a0
addsd %xmm3, %xmm4
ucomisd %xmm4, %xmm1
jbe 0x324c
movsd 0x7070(%rip), %xmm4     # 0xa2a8
addsd %xmm3, %xmm4
ucomisd %xmm1, %xmm4
jbe 0x324c
movsd 0xc20e(%rip), %xmm5     # 0xf458
jmp 0x329f
movsd 0x700c(%rip), %xmm5     # 0xa260
testb $0x1, %al
je 0x326b
addl %eax, %eax
addsd %xmm5, %xmm2
xorps %xmm4, %xmm4
cvtsi2sd %eax, %xmm4
divsd %xmm4, %xmm2
jmp 0x3273
movsd 0x6ff5(%rip), %xmm2     # 0xa268
addsd %xmm3, %xmm2
movsd 0x7021(%rip), %xmm3     # 0xa2a0
addsd %xmm2, %xmm3
ucomisd %xmm3, %xmm1
jbe 0x329f
addsd 0x7017(%rip), %xmm2     # 0xa2a8
ucomisd %xmm1, %xmm2
jbe 0x329f
movsd 0xc1c1(%rip), %xmm5     # 0xf460
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
subsd %xmm1, %xmm0
addsd 0x1bd9d(%rip), %xmm0    # 0x1f050
ucomisd 0x6fa5(%rip), %xmm0     # 0xa260
movsd %xmm5, 0x8(%rsp)
jbe 0x32e0
movsd 0x6fe5(%rip), %xmm1     # 0xa2b0
movsd 0x6f8d(%rip), %xmm2     # 0xa260
incl %r15d
addsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
ja 0x32d3
movsd %xmm0, 0x1bd68(%rip)    # 0x1f050
xorps %xmm0, %xmm0
cvtsi2sdl 0xc145(%rip), %xmm0 # 0xf438
callq 0x2030
movsd %xmm0, 0x18(%rsp)
movsd 0x6f5a(%rip), %xmm0     # 0xa260
movl %r14d, %edi
callq 0x2060
movsd 0x1bd4a(%rip), %xmm1    # 0x1f060
mulsd 0x6f62(%rip), %xmm1     # 0xa280
divsd 0xc18a(%rip), %xmm1     # 0xf4b0
xorps %xmm2, %xmm2
cvtsi2sdl 0xc0ff(%rip), %xmm2 # 0xf430
mulsd %xmm1, %xmm2
cvttsd2si %xmm2, %r12d
movl %r15d, %r14d
subl %r12d, %r14d
cmpl %ebp, %r14d
cmovgel %ebp, %r14d
xorps %xmm2, %xmm2
cvtsi2sd %r15d, %xmm2
movsd %xmm0, 0x10(%rsp)
movsd 0x1b9ab(%rip), %xmm0    # 0x1ed08
addsd %xmm2, %xmm0
movq 0x1b998(%rip), %rcx     # 0x1ed00
xorps %xmm1, %xmm1
cvtsi2sdl 0xc(%rcx), %xmm1
ucomisd %xmm1, %xmm0
jb 0x33a8
movsd %xmm2, 0x20(%rsp)
callq 0x3905
movsd 0x20(%rsp), %xmm2
movsd 0x1b979(%rip), %xmm0    # 0x1ed08
addsd %xmm2, %xmm0
movq 0x1b966(%rip), %rcx     # 0x1ed00
xorps %xmm1, %xmm1
cvtsi2sdl 0xc(%rcx), %xmm1
ucomisd %xmm1, %xmm0
jae 0x337c
movsd 0x18(%rsp), %xmm7
divsd 0x10(%rsp), %xmm7
testl %r14d, %r14d
movsd 0x6ea0(%rip), %xmm8     # 0xa260
jle 0x345f
movsd 0x1b93a(%rip), %xmm0    # 0x1ed08
movsd 0xc092(%rip), %xmm1     # 0xf468
movq 0x18(%rcx), %rax
movq (%rax), %rdx
movslq %r12d, %rax
movl %r14d, %esi
leaq (%rbx,%rax,4), %rdi
xorpd %xmm2, %xmm2
xorl %eax, %eax
movsd 0x8(%rsp), %xmm6
xorps %xmm3, %xmm3
cvtsi2sdl (%rdi,%rax,4), %xmm3
mulsd %xmm1, %xmm3
mulsd %xmm7, %xmm3
cvttsd2si %xmm0, %r8d
mulsd %xmm6, %xmm3
movslq %r8d, %r8
xorps %xmm4, %xmm4
cvtsi2sdl (%rdx,%r8,4), %xmm4
mulsd %xmm2, %xmm3
addsd %xmm4, %xmm3
cvttsd2si %xmm3, %r9d
movl %r9d, (%rdx,%r8,4)
addsd %xmm8, %xmm0
movapd %xmm8, %xmm3
cmpnlesd %xmm2, %xmm3
addsd 0xc030(%rip), %xmm2     # 0xf470
andpd %xmm3, %xmm2
andnpd %xmm8, %xmm3
orpd %xmm3, %xmm2
incq %rax
cmpq %rax, %rsi
jne 0x33f3
movsd %xmm0, 0x1b8ab(%rip)    # 0x1ed08
jmp 0x3467
xorl %eax, %eax
movsd 0x8(%rsp), %xmm6
cmpl %r15d, %r14d
setl %dl
cmpl %r15d, %eax
setl %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x3504
movsd 0x1b87f(%rip), %xmm0    # 0x1ed08
movsd 0xbfd7(%rip), %xmm2     # 0xf468
movapd %xmm6, %xmm1
mulsd %xmm2, %xmm1
movslq %ebp, %rdx
xorps %xmm3, %xmm3
cvtsi2sdl -0x4(%rbx,%rdx,4), %xmm3
mulsd %xmm2, %xmm3
mulsd %xmm6, %xmm3
cvttpd2dq %xmm3, %xmm2
cvtdq2pd %xmm2, %xmm2
mulsd %xmm7, %xmm2
movq 0x18(%rcx), %rcx
movq (%rcx), %rcx
movsd 0xbfb0(%rip), %xmm3     # 0xf478
cvttsd2si %xmm0, %edx
movslq %edx, %rdx
xorps %xmm4, %xmm4
cvtsi2sdl (%rcx,%rdx,4), %xmm4
movapd %xmm2, %xmm5
mulsd %xmm1, %xmm5
addsd %xmm4, %xmm5
cvttsd2si %xmm5, %esi
movl %esi, (%rcx,%rdx,4)
addsd %xmm8, %xmm0
mulsd %xmm3, %xmm1
incl %eax
cmpl %eax, %r15d
jne 0x34c8
movsd %xmm0, 0x1b806(%rip)    # 0x1ed08
jmp 0x3507
movl %eax, %r15d
movsd 0x1b7f9(%rip), %xmm0    # 0x1ed08
movsd %xmm0, 0x10(%rsp)
cmpl %ebp, %r15d
jge 0x360d
mulsd 0xbf42(%rip), %xmm6     # 0xf468
movsd 0xbf52(%rip), %xmm0     # 0xf480
maxsd 0x6d2a(%rip), %xmm0     # 0xa260
movsd 0x6d7a(%rip), %xmm3     # 0xa2b8
divsd %xmm0, %xmm3
ucomisd %xmm3, %xmm6
jbe 0x360d
movslq %r15d, %r14
movslq %ebp, %r15
incq %r14
movsd %xmm7, 0x18(%rsp)
movsd %xmm3, 0x20(%rsp)
movsd 0x1b79f(%rip), %xmm0    # 0x1ed08
movq 0x1b790(%rip), %rax     # 0x1ed00
xorps %xmm1, %xmm1
cvtsi2sdl 0xc(%rax), %xmm1
ucomisd %xmm1, %xmm0
jb 0x35a4
movsd %xmm6, 0x8(%rsp)
callq 0x3905
movsd 0x20(%rsp), %xmm3
movsd 0x6cc8(%rip), %xmm8     # 0xa260
movsd 0x18(%rsp), %xmm7
movsd 0x8(%rsp), %xmm6
xorps %xmm0, %xmm0
cvtsi2sdl -0x4(%rbx,%r14,4), %xmm0
mulsd %xmm6, %xmm0
movq 0x1b747(%rip), %rax     # 0x1ed00
movq 0x18(%rax), %rax
movsd 0x1b743(%rip), %xmm1    # 0x1ed08
cvttsd2si %xmm1, %ecx
movq (%rax), %rax
movslq %ecx, %rcx
xorps %xmm2, %xmm2
cvtsi2sdl (%rax,%rcx,4), %xmm2
mulsd %xmm7, %xmm0
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %edx
movl %edx, (%rax,%rcx,4)
addsd %xmm8, %xmm1
movsd %xmm1, 0x1b715(%rip)    # 0x1ed08
cmpq %r15, %r14
jge 0x360d
mulsd 0xbe78(%rip), %xmm6     # 0xf478
incq %r14
ucomisd %xmm3, %xmm6
ja 0x3561
movsd 0x1b6f3(%rip), %xmm0    # 0x1ed08
xorps %xmm1, %xmm1
cvtsi2sdl 0x1b6f0(%rip), %xmm1 # 0x1ed10
ucomisd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
jbe 0x3630
movl %eax, 0x1b6e0(%rip)     # 0x1ed10
movl %eax, 0x1ba32(%rip)     # 0x1f068
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x1b6c4(%rip)    # 0x1ed08
cmpl $0x0, 0xbefd(%rip)      # 0xf548
movsd 0x28(%rsp), %xmm0
je 0x366b
mulsd 0xbef5(%rip), %xmm0     # 0xf550
addsd 0xbe4d(%rip), %xmm0     # 0xf4b0
movsd %xmm0, 0xbe45(%rip)     # 0xf4b0
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq