// File: /workspace/github_c_makefile_star2_O2/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 0x2e9c
jp 0x2e9c
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x33d6
cvtsi2sdl 0xb58c(%rip), %xmm1 # 0xe430
movsd 0x63cc(%rip), %xmm2     # 0x9278
movsd %xmm0, 0x28(%rsp)
mulsd %xmm0, %xmm2
divsd 0xb5f2(%rip), %xmm2     # 0xe4b0
mulsd %xmm1, %xmm2
movsd %xmm2, 0x8(%rsp)
movsd 0x1ae38(%rip), %xmm0    # 0x1dd08
subsd 0x1b180(%rip), %xmm0    # 0x1e058
callq 0x34ce
movsd 0x63ab(%rip), %xmm1     # 0x9290
addsd %xmm0, %xmm1
cvttpd2dq %xmm1, %xmm1
cvtdq2pd %xmm1, %xmm2
movl 0xb559(%rip), %eax      # 0xe450
testb $0x1, %al
jne 0x2f05
movsd 0x6365(%rip), %xmm1     # 0x9268
jmp 0x2f1b
cvtsi2sd %eax, %xmm1
addsd 0x634f(%rip), %xmm1     # 0x9260
addl %eax, %eax
cvtsi2sd %eax, %xmm3
divsd %xmm3, %xmm1
movsd 0x8(%rsp), %xmm4
cvttsd2si %xmm4, %r15d
movsd 0x636a(%rip), %xmm3     # 0x9298
addsd %xmm2, %xmm3
ucomisd %xmm3, %xmm0
jbe 0x2f54
movsd 0x6360(%rip), %xmm3     # 0x92a0
addsd %xmm2, %xmm3
ucomisd %xmm0, %xmm3
jbe 0x2f54
movsd 0xb506(%rip), %xmm2     # 0xe458
jmp 0x2f88
addsd %xmm2, %xmm1
movsd 0x6338(%rip), %xmm2     # 0x9298
addsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
movsd 0x62f0(%rip), %xmm2     # 0x9260
jbe 0x2f88
addsd 0x6326(%rip), %xmm1     # 0x92a0
ucomisd %xmm0, %xmm1
jbe 0x2f88
movsd 0xb4d8(%rip), %xmm2     # 0xe460
cvtsi2sd %r15d, %xmm0
subsd %xmm0, %xmm4
addsd 0x1b0b7(%rip), %xmm4    # 0x1e050
movsd 0x62bf(%rip), %xmm0     # 0x9260
movsd 0x6307(%rip), %xmm1     # 0x92b0
ucomisd %xmm0, %xmm4
jbe 0x2fb8
incl %r15d
addsd %xmm1, %xmm4
jmp 0x2fa9
movsd %xmm2, 0x8(%rsp)
movsd %xmm4, 0x1b08a(%rip)    # 0x1e050
cvtsi2sdl 0xb46a(%rip), %xmm0 # 0xe438
callq 0x2030
movsd %xmm0, 0x18(%rsp)
movsd 0x627f(%rip), %xmm0     # 0x9260
movl %r14d, %edi
callq 0x2060
movsd 0x1b06f(%rip), %xmm1    # 0x1e060
mulsd 0x627f(%rip), %xmm1     # 0x9278
divsd 0xb4af(%rip), %xmm1     # 0xe4b0
cvtsi2sdl 0xb427(%rip), %xmm2 # 0xe430
movsd %xmm0, 0x10(%rsp)
mulsd %xmm1, %xmm2
cvttsd2si %xmm2, %r12d
movl %r15d, %r14d
subl %r12d, %r14d
cvtsi2sd %r15d, %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x1acd6(%rip), %xmm8    # 0x1dd08
movapd %xmm8, %xmm0
addsd 0x20(%rsp), %xmm0
movq 0x1acbc(%rip), %rax     # 0x1dd00
cvtsi2sdl 0xc(%rax), %xmm1
ucomisd %xmm1, %xmm0
jb 0x3056
callq 0x34f7
jmp 0x3029
cmpl %ebp, %r14d
cmovgel %ebp, %r14d
movsd 0x18(%rsp), %xmm6
divsd 0x10(%rsp), %xmm6
movsd 0xb3f7(%rip), %xmm0     # 0xe468
movsd 0xb3f7(%rip), %xmm1     # 0xe470
movslq %r14d, %rdx
movslq %r12d, %rcx
leaq (%rbx,%rcx,4), %rsi
xorpd %xmm2, %xmm2
xorl %ecx, %ecx
movsd 0x8(%rsp), %xmm5
movsd 0x61c9(%rip), %xmm7     # 0x9260
cmpq %rdx, %rcx
jge 0x30ff
cvtsi2sdl (%rsi,%rcx,4), %xmm3
mulsd %xmm0, %xmm3
mulsd %xmm6, %xmm3
mulsd %xmm5, %xmm3
movq 0x18(%rax), %rdi
movq (%rdi), %rdi
cvttsd2si %xmm8, %r8d
movslq %r8d, %r8
cvtsi2sdl (%rdi,%r8,4), %xmm4
mulsd %xmm2, %xmm3
addsd %xmm4, %xmm3
cvttsd2si %xmm3, %r9d
movl %r9d, (%rdi,%r8,4)
addsd %xmm7, %xmm8
movsd %xmm8, 0x1ac27(%rip)    # 0x1dd08
movapd %xmm7, %xmm3
cmpnlesd %xmm2, %xmm3
addsd %xmm1, %xmm2
andpd %xmm3, %xmm2
andnpd %xmm7, %xmm3
orpd %xmm3, %xmm2
incq %rcx
jmp 0x3097
cmpl %r15d, %r14d
jge 0x3174
movslq %ebp, %r14
cvtsi2sdl -0x4(%rbx,%r14,4), %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm5, %xmm1
cvttpd2dq %xmm1, %xmm1
cvtdq2pd %xmm1, %xmm1
mulsd %xmm0, %xmm5
mulsd %xmm6, %xmm1
movsd 0xb34a(%rip), %xmm0     # 0xe478
movapd %xmm5, %xmm2
cmpl %r15d, %ecx
jge 0x317b
movq 0x18(%rax), %rdx
movq (%rdx), %rdx
cvttsd2si %xmm8, %esi
movslq %esi, %rsi
cvtsi2sdl (%rdx,%rsi,4), %xmm3
movapd %xmm1, %xmm4
mulsd %xmm2, %xmm4
addsd %xmm3, %xmm4
cvttsd2si %xmm4, %edi
movl %edi, (%rdx,%rsi,4)
addsd %xmm7, %xmm8
movsd %xmm8, 0x1ab9c(%rip)    # 0x1dd08
mulsd %xmm0, %xmm2
incl %ecx
jmp 0x3132
mulsd %xmm0, %xmm5
movslq %ebp, %r14
movsd 0xb2fd(%rip), %xmm0     # 0xe480
maxsd 0x60d5(%rip), %xmm0     # 0x9260
movsd 0x6115(%rip), %xmm3     # 0x92a8
divsd %xmm0, %xmm3
movl %ecx, %r15d
movapd %xmm8, %xmm0
movsd %xmm6, 0x18(%rsp)
movsd %xmm8, 0x20(%rsp)
movsd %xmm3, 0x10(%rsp)
cmpq %r14, %r15
jge 0x3253
ucomisd %xmm3, %xmm5
jbe 0x3253
cvtsi2sdl 0xc(%rax), %xmm1
ucomisd %xmm1, %xmm0
jb 0x320b
movsd %xmm5, 0x8(%rsp)
callq 0x34f7
movsd 0x10(%rsp), %xmm3
movsd 0x20(%rsp), %xmm8
movsd 0x6070(%rip), %xmm7     # 0x9260
movsd 0x18(%rsp), %xmm6
movsd 0x8(%rsp), %xmm5
movq 0x1aafd(%rip), %rax     # 0x1dd00
movsd 0x1aafd(%rip), %xmm0    # 0x1dd08
cvtsi2sdl (%rbx,%r15,4), %xmm1
mulsd %xmm5, %xmm1
movq 0x18(%rax), %rcx
movq (%rcx), %rcx
cvttsd2si %xmm0, %edx
movslq %edx, %rdx
cvtsi2sdl (%rcx,%rdx,4), %xmm2
mulsd %xmm6, %xmm1
addsd %xmm2, %xmm1
cvttsd2si %xmm1, %esi
movl %esi, (%rcx,%rdx,4)
addsd %xmm7, %xmm0
movsd %xmm0, 0x1aac5(%rip)    # 0x1dd08
mulsd 0xb22d(%rip), %xmm5     # 0xe478
incq %r15
jmp 0x31b2
cvtsi2sdl 0x1aab5(%rip), %xmm1 # 0x1dd10
ucomisd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
jbe 0x326b
movl %eax, 0x1aaa5(%rip)     # 0x1dd10
movl %eax, 0x1adf7(%rip)     # 0x1e068
movsd %xmm8, 0x1aa8e(%rip)    # 0x1dd08
cmpl $0x0, 0xb2c7(%rip)      # 0xe548
movsd 0x28(%rsp), %xmm0
je 0x32a1
mulsd 0xb2bf(%rip), %xmm0     # 0xe550
addsd 0xb217(%rip), %xmm0     # 0xe4b0
movsd %xmm0, 0xb20f(%rip)     # 0xe4b0
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq