// File: /workspace/github_c_makefile_star2_O3/calmopyrin[P]tedplay/Audio.cpp
// Function: Audio::audioCallback(void*, unsigned char*, int)
void Audio::audioCallback(void *userData, unsigned char *stream, int len)
{
	TED *ted = reinterpret_cast<TED *>(userData);

	if (ted) {

		short *playerBuffer = (short*) stream;
		unsigned int sampleRate = ted->getSampleRate();
		unsigned int sampleCnt = len / 2;
		static unsigned int remainder = 0;
		const double avw = 0.90;
		static double avg = 0;

		do {
			// since we double buffer, fill only the half if the one we're playing is depleted
			if ((ringBufferIndex + 1) % (ringBufferSize / 2) == 0) {
				size_t half = ringBufferIndex + 1 >= ringBufferSize ? 0 : ringBufferSize / 2;
				ted->ted_process(ringBuffer + half, (unsigned int) ringBufferSize / 2);
			}
			//
			unsigned int newCount = remainder + sampleRate;
			if ( newCount >= TED_SOUND_CLOCK) {
				remainder = newCount - TED_SOUND_CLOCK;
				double weightPrev = double(remainder)/double(TED_SOUND_CLOCK);
				double weightCurr = 1.00 - weightPrev;
				// interpolate sample
				short sample = short(weightPrev * double(ringBuffer[ringBufferIndex]) + 
						weightCurr * double(ringBuffer[(ringBufferIndex + 1) % ringBufferSize]) );
				*playerBuffer++ = sample;
				avg = avw * avg + (1 - avw) * sample;
				lastSample = int(avg);
				sampleCnt--;
			} else {
				remainder = newCount;
			}
			ringBufferIndex = (ringBufferIndex + 1) % ringBufferSize;
		} while (sampleCnt);
		// FIXME: it is not a very good idea to do this here
		if (!paused) {
			if (recording)
				dumpWavData(wavFileHandle, stream, len);
			//
			if (!usingPlayedBuffer)
				for (unsigned int i = 0; i < ((len / 2) < playedBufSize ? len / 2 : playedBufSize); i++)
					playedBuffer[i] = *(((short*)stream) + i);
			else {
				;
			}
		}
#if 0
		lastSample = *(((short*)stream) + len / 2 - 1);
#endif
	}
}

// Assembly:
testq %rdi, %rdi
je 0x383c
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r13
movq %rdi, %r14
movl 0xb71c8(%rdi), %r12d
movl %edx, %ebp
shrl $0x1f, %ebp
movl %edx, 0x4(%rsp)
addl %edx, %ebp
sarl %ebp
movq 0x21c07(%rip), %rdi     # 0x25298
movq 0x21bf8(%rip), %rcx     # 0x25290
movl %ebp, %r15d
movq %rsi, %rbx
incq %rdi
movq %rcx, %rsi
shrq %rsi
movq %rdi, %rax
xorl %edx, %edx
divq %rsi
testq %rdx, %rdx
jne 0x36d6
cmpq %rcx, %rdi
movl $0x0, %eax
cmovaeq %rax, %rsi
addq %rsi, %rsi
addq 0x21bbe(%rip), %rsi     # 0x25288
shrl %ecx
movq %r14, %rdi
movl %ecx, %edx
callq 0xb380
movl 0x21be0(%rip), %esi     # 0x252bc
addl %r12d, %esi
cmpl $0x361f0, %esi          # imm = 0x361F0
jb 0x3798
addl $0xfffc9e10, %esi       # imm = 0xFFFC9E10
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
divsd 0xb907(%rip), %xmm0     # 0xf008
movsd 0xb907(%rip), %xmm1     # 0xf010
movq 0x21b78(%rip), %r8      # 0x25288
movq 0x21b81(%rip), %rax     # 0x25298
movswl (%r8,%rax,2), %ecx
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
incq %rax
movq 0x21b63(%rip), %rcx     # 0x25290
xorl %edx, %edx
divq %rcx
movq %rdx, %rdi
movswl (%r8,%rdx,2), %eax
xorps %xmm3, %xmm3
cvtsi2sd %eax, %xmm3
subsd %xmm0, %xmm1
mulsd %xmm1, %xmm3
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
cvttsd2si %xmm2, %eax
movw %ax, (%rbx)
addq $0x2, %rbx
cwtl
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0xb8ac(%rip), %xmm0     # 0xf018
movsd 0x21b4c(%rip), %xmm1    # 0x252c0
mulsd 0xb8a4(%rip), %xmm1     # 0xf020
addsd %xmm0, %xmm1
movsd %xmm1, 0x21b38(%rip)    # 0x252c0
cvttsd2si %xmm1, %eax
movw %ax, 0x21b25(%rip)      # 0x252b8
decl %r15d
jmp 0x37b1
movq 0x21af9(%rip), %rax     # 0x25298
movq 0x21aea(%rip), %rcx     # 0x25290
incq %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %rdi
movl %esi, 0x21b05(%rip)     # 0x252bc
movq %rdi, 0x21ada(%rip)     # 0x25298
testl %r15d, %r15d
jne 0x369e
cmpb $0x0, 0x21ad2(%rip)     # 0x252a0
movl 0x4(%rsp), %ebx
jne 0x382e
cmpb $0x1, 0x21ac6(%rip)     # 0x252a1
jne 0x37ee
movq 0x21ac4(%rip), %rdi     # 0x252a8
movq %r13, %rsi
movl %ebx, %edx
callq 0x383e
incl %ebx
cmpl $0x3, %ebx
setb %al
orb 0x21acc(%rip), %al      # 0x252c8
testb $0x1, %al
jne 0x382e
movl $0x89d, %eax            # imm = 0x89D
cmpl %eax, %ebp
cmovbl %ebp, %eax
cmpl $0x1, %eax
adcl $0x0, %eax
addl %eax, %eax
xorl %ecx, %ecx
leaq 0x21ab5(%rip), %rdx     # 0x252d0
movzwl (%r13,%rcx), %esi
movw %si, (%rcx,%rdx)
addq $0x2, %rcx
cmpq %rcx, %rax
jne 0x381b
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop