// File: /workspace/github_c_makefile_star2_O2/calmopyrin[P]tedplay/Filter.cpp
// Function: Filter::reCalcWindowTable()
void Filter::reCalcWindowTable()
{
	int i;
	const double pi = 4 * atan(1.0);
	const double f = fc_;
	double gain = double(1 << precision_) - 1.0; // 4095.0
	double kernelSum = 0;
	
	// #intervals between time steps for resampling
	timeStep = 1U;

	windowTableSize = order_ * int(timeStep);
	int midorder = windowTableSize / 2;
	double *dblCoeffs = new double[windowTableSize + 1];

	if (sampleHistory_) delete [] sampleHistory_;
	sampleHistory_ = new int [windowTableSize + 1];

	if (windowTable_) delete [] windowTable_;
	windowTable_ = new int[windowTableSize + 1];

	// this is used for resampling
	for (i = 0; i < windowTableSize; i++) {
		double j = i - midorder;
		double x = pi * j / double(timeStep);

		// Blackman has better stop-band attenuation
		double blackman = 0.42 + 0.5 * cos(2 * x / double(windowTableSize)) 
			- 0.08*cos(4 * pi * j / double(windowTableSize));
		// Hamming has 20% faster roll-off
		double hamming = 0.54 + 0.46 * cos(2 * x / double(windowTableSize));
		// von Hann is in between
		double vonHann = 0.5 * (1.0 + cos(2 * x / double(windowTableSize)));
		// Blackman-Harris window
		double a0 = 0.35875, a1 = 0.48229, a2 = 0.14128, a3 = 0.01168;
		double blackmanHarris = a0 + a1 * cos(2 * x / double(windowTableSize)) 
			- a2 * cos(4 * x / double(windowTableSize))
			+ a3 * cos(6 * x / double(windowTableSize));

		double c = (j == 0) ? 2.0 * f : sin(2.0 * f * x ) / x;

		c *= vonHann;
		dblCoeffs[i] = c;
		kernelSum += c;
	}

	for (i = 0; i < windowTableSize; i++) {
		double rebasedCoeff = dblCoeffs[i] / kernelSum * gain * mixingVolume;
		windowTable_[i] = (int)(rebasedCoeff + 0.5);
		sampleHistory_[i] = 0;
	}
	// Now decrease filter order if side coeffs are zero...
	i = 0;
	while (0 == windowTable_[i] && i < midorder) i++;
	if (i) {
		int k;
		int drop = i;

		i = 0;
		for(k = drop; k <= midorder; k++, i++)
			windowTable_[i] = windowTable_[k];
		order_ = order_ - drop * 2;

		if (i > 1) {
			int *b = windowTable_ + i - 2;
			do {
				windowTable_[i] = *b--;
			} while (++i < order_);
		}
		midorder = (order_ - 1) / 2;
	}
	windowTableSize = order_ * int(timeStep);
	if (!windowTableSize) {
		windowTableSize = 1;
		windowTable_[0] = int(gain);
		windowTable_[1] = 0;
	}

	sampleBufPtr_ = 0;
	sampleBufMask_ = (midorder << 1) + 1;

	delete [] dblCoeffs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movsd 0x30(%rdi), %xmm0
movsd %xmm0, 0x8(%rsp)
movb 0x40(%rdi), %cl
pushq $0x1
popq %rbp
movl %ebp, 0x38(%rdi)
shll %cl, %ebp
movslq 0x8(%rdi), %r12
pushq $0x2
popq %rcx
movl %r12d, %eax
cltd
idivl %ecx
movq %rax, (%rsp)
cmpq $-0x1, %r12
movl %r12d, 0x18(%rdi)
leaq 0x8(,%r12,8), %rdi
pushq $-0x1
popq %r15
cmovlq %r15, %rdi
callq 0x3040
movq %rax, %r14
movq 0x20(%rbx), %rdi
testq %rdi, %rdi
je 0x81de
callq 0x3290
movslq 0x18(%rbx), %r12
movq %r12, %r13
incq %r13
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x18(%rsp)
shlq $0x2, %r13
cmpl $-0x1, %r12d
movq %r13, %rdi
cmovlq %r15, %rdi
callq 0x3040
movq %rax, 0x20(%rbx)
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x8220
callq 0x3290
movslq 0x18(%rbx), %r12
leaq 0x4(,%r12,4), %r13
movsd 0x18(%rsp), %xmm0
addsd 0x6e02(%rip), %xmm0     # 0xf030
movsd %xmm0, 0x18(%rsp)
cmpl $-0x1, %r12d
cmovlq %r15, %r13
movq %r13, %rdi
callq 0x3040
movq %rax, 0x10(%rbx)
movsd 0x8(%rsp), %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movslq (%rsp), %rbp
movl %ebp, %r13d
negl %r13d
xorpd %xmm3, %xmm3
xorl %r15d, %r15d
movslq %r12d, %rax
cmpq %rax, %r15
jge 0x83b2
movsd %xmm3, 0x30(%rsp)
leal (%r15,%r13), %eax
cvtsi2sd %eax, %xmm1
movsd %xmm1, 0x20(%rsp)
movl 0x38(%rbx), %eax
cvtsi2sd %rax, %xmm0
mulsd 0x6da7(%rip), %xmm1     # 0xf040
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movapd %xmm1, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
cvtsi2sd %r12d, %xmm1
divsd %xmm1, %xmm0
callq 0x3380
movsd 0x20(%rsp), %xmm1
mulsd 0x6d7b(%rip), %xmm1     # 0xf048
cvtsi2sdl 0x18(%rbx), %xmm0
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x3380
cvtsi2sdl 0x18(%rbx), %xmm1
movsd 0x28(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x3380
cvtsi2sdl 0x18(%rbx), %xmm1
movsd 0x28(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x3380
addsd 0x5d01(%rip), %xmm0     # 0xe010
mulsd 0x6d21(%rip), %xmm0     # 0xf038
movsd %xmm0, 0x20(%rsp)
cvtsi2sdl 0x18(%rbx), %xmm1
movsd 0x28(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x3380
movsd 0x10(%rsp), %xmm0
mulsd 0x6d11(%rip), %xmm0     # 0xf050
cvtsi2sdl 0x18(%rbx), %xmm1
divsd %xmm1, %xmm0
callq 0x3380
movsd 0x10(%rsp), %xmm0
mulsd 0x6cfd(%rip), %xmm0     # 0xf058
cvtsi2sdl 0x18(%rbx), %xmm1
divsd %xmm1, %xmm0
callq 0x3380
movsd 0x8(%rsp), %xmm0
cmpq %r15, (%rsp)
je 0x838c
movsd 0x8(%rsp), %xmm0
mulsd 0x10(%rsp), %xmm0
callq 0x3090
divsd 0x10(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r14,%r15,8)
movsd 0x30(%rsp), %xmm3
addsd %xmm1, %xmm3
incq %r15
movl 0x18(%rbx), %r12d
jmp 0x8269
movsd 0x48(%rbx), %xmm0
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
xorl %edx, %edx
movsd 0x18(%rsp), %xmm2
movslq %r12d, %rsi
cmpq %rsi, %rdx
jge 0x83fd
movsd (%r14,%rdx,8), %xmm1
divsd %xmm3, %xmm1
mulsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
addsd 0x6c4f(%rip), %xmm1     # 0xf038
cvttsd2si %xmm1, %esi
movl %esi, (%rax,%rdx,4)
andl $0x0, (%rcx,%rdx,4)
incq %rdx
movl 0x18(%rbx), %r12d
jmp 0x83c7
movq 0x10(%rbx), %rcx
pushq $-0x2
popq %rax
xorl %edx, %edx
leaq 0x1(%rdx), %rsi
addl $0x2, %eax
cmpq %rbp, %rdx
jge 0x841b
cmpl $0x0, (%rcx,%rdx,4)
movq %rsi, %rdx
je 0x8406
cmpq $0x1, %rsi
jne 0x8426
movl 0x8(%rbx), %esi
jmp 0x8472
decq %rsi
xorl %edx, %edx
cmpq %rbp, %rsi
jg 0x843e
movl (%rcx,%rsi,4), %edi
movl %edi, (%rcx,%rdx,4)
incq %rsi
incq %rdx
jmp 0x842b
movl 0x8(%rbx), %esi
subl %eax, %esi
movl %esi, 0x8(%rbx)
cmpq $0x2, %rdx
jb 0x8465
leaq -0x2(%rdx), %rax
movl (%rcx,%rax,4), %esi
movl %esi, (%rcx,%rdx,4)
incq %rdx
movslq 0x8(%rbx), %rsi
decq %rax
cmpq %rsi, %rdx
jl 0x8450
leal -0x1(%rsi), %eax
cltd
pushq $0x2
popq %rdi
idivl %edi
movq %rax, (%rsp)
imull 0x38(%rbx), %esi
movl %esi, 0x18(%rbx)
testl %esi, %esi
jne 0x848e
movl $0x1, 0x18(%rbx)
cvttsd2si %xmm2, %eax
movl %eax, (%rcx)
andl $0x0, 0x4(%rcx)
andl $0x0, 0x28(%rbx)
movq (%rsp), %rax
leal 0x1(,%rax,2), %eax
movl %eax, 0x2c(%rbx)
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3290