// File: /workspace/github_c_makefile_star2_O2/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 0x3747
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbp
movq %rdi, %r15
movl 0xb71c8(%rdi), %r12d
pushq $0x2
popq %rcx
movl %edx, 0x4(%rsp)
movl %edx, %eax
cltd
idivl %ecx
movl %eax, %r14d
movq 0x20cc5(%rip), %rdi     # 0x242a8
movq 0x20cb6(%rip), %rcx     # 0x242a0
xorl %r13d, %r13d
movl %eax, (%rsp)
movq %rsi, %rbx
incq %rdi
movq %rcx, %rsi
shrq %rsi
movq %rdi, %rax
xorl %edx, %edx
divq %rsi
testq %rdx, %rdx
jne 0x3626
cmpq %rcx, %rdi
cmovaeq %r13, %rsi
addq %rsi, %rsi
addq 0x20c7e(%rip), %rsi     # 0x24298
shrl %ecx
movq %r15, %rdi
movl %ecx, %edx
callq 0xae24
movl 0x20ca0(%rip), %esi     # 0x242cc
addl %r12d, %esi
cmpl $0x361f0, %esi          # imm = 0x361F0
jb 0x36dc
addl $0xfffc9e10, %esi       # imm = 0xFFFC9E10
cvtsi2sd %rsi, %xmm0
divsd 0xa9ba(%rip), %xmm0     # 0xe008
movsd 0xa9ba(%rip), %xmm1     # 0xe010
movq 0x20c3b(%rip), %r8      # 0x24298
movq 0x20c44(%rip), %rax     # 0x242a8
movswl (%r8,%rax,2), %ecx
cvtsi2sd %ecx, %xmm2
incq %rax
movq 0x20c29(%rip), %rcx     # 0x242a0
xorl %edx, %edx
divq %rcx
movq %rdx, %rdi
movswl (%r8,%rdx,2), %eax
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
cvtsi2sd %eax, %xmm0
mulsd 0xa968(%rip), %xmm0     # 0xe018
movsd 0x20c18(%rip), %xmm1    # 0x242d0
mulsd 0xa960(%rip), %xmm1     # 0xe020
addsd %xmm0, %xmm1
movsd %xmm1, 0x20c04(%rip)    # 0x242d0
cvttsd2si %xmm1, %eax
movw %ax, 0x20bf1(%rip)      # 0x242c8
decl %r14d
jmp 0x36f5
movq 0x20bc5(%rip), %rax     # 0x242a8
movq 0x20bb6(%rip), %rcx     # 0x242a0
incq %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %rdi
movl %esi, 0x20bd1(%rip)     # 0x242cc
movq %rdi, 0x20ba6(%rip)     # 0x242a8
testl %r14d, %r14d
jne 0x35f3
cmpb $0x0, 0x20b9e(%rip)     # 0x242b0
movl 0x4(%rsp), %edx
jne 0x3739
cmpb $0x1, 0x20b92(%rip)     # 0x242b1
jne 0x3730
movq 0x20b90(%rip), %rdi     # 0x242b8
movq %rbp, %rsi
callq 0x3774
cmpb $0x0, 0x20ba1(%rip)     # 0x242d8
je 0x3748
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x89d, %eax            # imm = 0x89D
movl (%rsp), %ecx
cmpl %eax, %ecx
cmovbl %ecx, %eax
addl %eax, %eax
xorl %ecx, %ecx
leaq 0x20b80(%rip), %rdx     # 0x242e0
cmpq %rcx, %rax
je 0x3739
movzwl (%rbp,%rcx), %esi
movw %si, (%rcx,%rdx)
addq $0x2, %rcx
jmp 0x3760