// File: /workspace/github_c_makefile_star2_O1/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 0x3832
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
movl %ebp, %r15d
movq %rsi, %rbx
movq 0x20c01(%rip), %rdi     # 0x24298
incq %rdi
movq 0x20bef(%rip), %rcx     # 0x24290
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 0x20bbe(%rip), %rsi     # 0x24288
shrl %ecx
movq %r14, %rdi
movl %ecx, %edx
callq 0xb236
movl 0x20be0(%rip), %eax     # 0x242bc
addl %r12d, %eax
cmpl $0x361f0, %eax          # imm = 0x361F0
jb 0x3794
addl $0xfffc9e10, %eax       # imm = 0xFFFC9E10
movl %eax, 0x20bc7(%rip)     # 0x242bc
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd 0xb903(%rip), %xmm0     # 0xf008
movsd 0xb903(%rip), %xmm1     # 0xf010
movq 0x20b74(%rip), %rcx     # 0x24288
movq 0x20b7d(%rip), %rax     # 0x24298
movswl (%rcx,%rax,2), %edx
xorps %xmm2, %xmm2
cvtsi2sd %edx, %xmm2
incq %rax
xorl %edx, %edx
divq 0x20b5e(%rip)           # 0x24290
movswl (%rcx,%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 0xb8b0(%rip), %xmm0     # 0xf018
movsd 0x20b50(%rip), %xmm1    # 0x242c0
mulsd 0xb8a8(%rip), %xmm1     # 0xf020
addsd %xmm0, %xmm1
movsd %xmm1, 0x20b3c(%rip)    # 0x242c0
cvttsd2si %xmm1, %eax
movw %ax, 0x20b29(%rip)      # 0x242b8
decl %r15d
jmp 0x379a
movl %eax, 0x20b22(%rip)     # 0x242bc
movq 0x20af7(%rip), %rax     # 0x24298
incq %rax
xorl %edx, %edx
divq 0x20ae3(%rip)           # 0x24290
movq %rdx, 0x20ae4(%rip)     # 0x24298
testl %r15d, %r15d
jne 0x3690
cmpb $0x0, 0x20adc(%rip)     # 0x242a0
movl 0x4(%rsp), %ebx
jne 0x3824
cmpb $0x1, 0x20ad0(%rip)     # 0x242a1
jne 0x37e4
movq 0x20ace(%rip), %rdi     # 0x242a8
movq %r13, %rsi
movl %ebx, %edx
callq 0x3834
incl %ebx
cmpl $0x3, %ebx
setb %al
orb 0x20ad6(%rip), %al      # 0x242c8
testb $0x1, %al
jne 0x3824
movl $0x89d, %eax            # imm = 0x89D
cmpl %eax, %ebp
cmovbl %ebp, %eax
cmpl $0x1, %eax
adcl $0x0, %eax
addl %eax, %eax
xorl %ecx, %ecx
leaq 0x20abf(%rip), %rdx     # 0x242d0
movzwl (%r13,%rcx), %esi
movw %si, (%rcx,%rdx)
addq $0x2, %rcx
cmpq %rcx, %rax
jne 0x3811
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop