// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x3972
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3bd0
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x30(%rbp)
movsd 0x1a8f5(%rip), %xmm0    # 0x1e008
movsd %xmm0, -0x38(%rbp)
movq 0x30c09(%rip), %rax     # 0x34328
addq $0x1, %rax
movq 0x30bf6(%rip), %rcx     # 0x34320
shrq %rcx
xorl %edx, %edx
divq %rcx
cmpq $0x0, %rdx
jne 0x378f
movq 0x30be9(%rip), %rax     # 0x34328
addq $0x1, %rax
cmpq 0x30bd6(%rip), %rax     # 0x34320
jb 0x3754
xorl %eax, %eax
movq %rax, -0x68(%rbp)
jmp 0x3762
movq 0x30bc5(%rip), %rax     # 0x34320
shrq %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rdi
movq 0x30ba3(%rip), %rsi     # 0x34318
movq -0x40(%rbp), %rax
shlq %rax
addq %rax, %rsi
movq 0x30b9a(%rip), %rax     # 0x34320
movl %eax, %edx
shrl %edx
callq 0x18b00
movl 0x30bb7(%rip), %eax     # 0x3434c
addl -0x2c(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $0x361f0, -0x44(%rbp)   # imm = 0x361F0
jb 0x389f
movl -0x44(%rbp), %eax
addl $0xfffc9e10, %eax       # imm = 0xFFFC9E10
movl %eax, 0x30b96(%rip)     # 0x3434c
movl 0x30b90(%rip), %eax     # 0x3434c
cvtsi2sd %rax, %xmm0
movsd 0x1a84f(%rip), %xmm1    # 0x1e018
divsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x50(%rbp), %xmm1
movsd 0x1a841(%rip), %xmm0    # 0x1e020
subsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x50(%rbp), %xmm0
movq 0x30b24(%rip), %rcx     # 0x34318
movq %rcx, -0x70(%rbp)
movq 0x30b29(%rip), %rax     # 0x34328
movswl (%rcx,%rax,2), %ecx
cvtsi2sd %ecx, %xmm2
movsd -0x58(%rbp), %xmm1
incq %rax
movq 0x30b0a(%rip), %rcx     # 0x34320
xorl %edx, %edx
divq %rcx
movq -0x70(%rbp), %rax
movswl (%rax,%rdx,2), %eax
cvtsi2sd %eax, %xmm3
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movw %ax, -0x5a(%rbp)
movw -0x5a(%rbp), %cx
movq -0x28(%rbp), %rax
movq %rax, %rdx
addq $0x2, %rdx
movq %rdx, -0x28(%rbp)
movw %cx, (%rax)
movsd 0x30af7(%rip), %xmm0    # 0x34350
movswl -0x5a(%rbp), %eax
cvtsi2sd %eax, %xmm2
movsd 0x1a7a7(%rip), %xmm1    # 0x1e010
mulsd %xmm2, %xmm1
movsd 0x1a793(%rip), %xmm2    # 0x1e008
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x30acb(%rip)    # 0x34350
cvttsd2si 0x30ac3(%rip), %eax # 0x34350
movw %ax, 0x30ab4(%rip)      # 0x34348
movl -0x30(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x38a8
movl -0x44(%rbp), %eax
movl %eax, 0x30aa4(%rip)     # 0x3434c
movq 0x30a79(%rip), %rax     # 0x34328
addq $0x1, %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x30a62(%rip)           # 0x34320
movq %rdx, 0x30a63(%rip)     # 0x34328
cmpl $0x0, -0x30(%rbp)
jne 0x3718
testb $0x1, 0x30a5a(%rip)     # 0x34330
jne 0x3970
testb $0x1, 0x30a4e(%rip)     # 0x34331
je 0x38f8
movq 0x30a4c(%rip), %rdi     # 0x34338
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
callq 0x3980
testb $0x1, 0x30a59(%rip)     # 0x34358
jne 0x396c
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x14(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x89d, %eax            # imm = 0x89D
jae 0x3930
movl -0x14(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x78(%rbp)
jmp 0x393a
movl $0x89d, %eax            # imm = 0x89D
movl %eax, -0x78(%rbp)
jmp 0x393a
movl -0x74(%rbp), %eax
movl -0x78(%rbp), %ecx
cmpl %ecx, %eax
jae 0x396a
movq -0x10(%rbp), %rax
movl -0x60(%rbp), %ecx
movw (%rax,%rcx,2), %dx
movl -0x60(%rbp), %eax
movl %eax, %ecx
leaq 0x30a05(%rip), %rax     # 0x34360
movw %dx, (%rax,%rcx,2)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x3908
jmp 0x396e
jmp 0x396e
jmp 0x3970
jmp 0x3972
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)