// File: /workspace/github_c_makefile_star2_O1/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
movl $0x1, %ebp
movl %ebp, 0x38(%rdi)
shll %cl, %ebp
movslq 0x8(%rdi), %rax
movl %eax, %r15d
shrl $0x1f, %r15d
addl %eax, %r15d
cmpq $-0x1, %rax
movl %eax, 0x18(%rdi)
leaq 0x8(,%rax,8), %rdi
movq $-0x1, %r12
cmovlq %r12, %rdi
callq 0x3040
movq %rax, %r14
movq 0x20(%rbx), %rdi
testq %rdi, %rdi
je 0x83b3
callq 0x32a0
sarl %r15d
movslq 0x18(%rbx), %rax
cmpq $-0x1, %rax
leaq 0x4(,%rax,4), %rdi
cmovlq %r12, %rdi
callq 0x3040
movq %rax, 0x20(%rbx)
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x83e1
callq 0x32a0
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x28(%rsp)
movslq 0x18(%rbx), %rax
cmpq $-0x1, %rax
leaq 0x4(,%rax,4), %rax
movq $-0x1, %rdi
cmovgeq %rax, %rdi
callq 0x3040
movq %rax, 0x10(%rbx)
movl 0x18(%rbx), %eax
testl %eax, %eax
jle 0x8596
movsd 0x8(%rsp), %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl %r15d, %r12d
negl %r12d
xorpd %xmm4, %xmm4
xorl %r13d, %r13d
movsd %xmm4, 0x30(%rsp)
leal (%r12,%r13), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
movsd %xmm1, 0x18(%rsp)
movl 0x38(%rbx), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x7bc4(%rip), %xmm1     # 0x10028
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movapd %xmm1, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
callq 0x33a0
movsd 0x18(%rsp), %xmm1
mulsd 0x7b96(%rip), %xmm1     # 0x10030
xorps %xmm0, %xmm0
cvtsi2sdl 0x18(%rbx), %xmm0
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x33a0
xorps %xmm1, %xmm1
cvtsi2sdl 0x18(%rbx), %xmm1
movsd 0x20(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x33a0
xorps %xmm1, %xmm1
cvtsi2sdl 0x18(%rbx), %xmm1
movsd 0x20(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x33a0
addsd 0x6b2b(%rip), %xmm0     # 0xf010
mulsd 0x7b4b(%rip), %xmm0     # 0x10038
movsd %xmm0, 0x18(%rsp)
xorps %xmm1, %xmm1
cvtsi2sdl 0x18(%rbx), %xmm1
movsd 0x20(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x33a0
movsd 0x10(%rsp), %xmm0
mulsd 0x7b28(%rip), %xmm0     # 0x10040
xorps %xmm1, %xmm1
cvtsi2sdl 0x18(%rbx), %xmm1
divsd %xmm1, %xmm0
callq 0x33a0
movsd 0x10(%rsp), %xmm0
mulsd 0x7b11(%rip), %xmm0     # 0x10048
xorps %xmm1, %xmm1
cvtsi2sdl 0x18(%rbx), %xmm1
divsd %xmm1, %xmm0
callq 0x33a0
movsd 0x8(%rsp), %xmm0
cmpq %r13, %r15
je 0x856a
movsd 0x8(%rsp), %xmm0
mulsd 0x10(%rsp), %xmm0
callq 0x30a0
divsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r14,%r13,8)
movsd 0x30(%rsp), %xmm4
addsd %xmm1, %xmm4
incq %r13
movslq 0x18(%rbx), %rax
cmpq %rax, %r13
jl 0x843a
jmp 0x859a
xorpd %xmm4, %xmm4
movsd 0x28(%rsp), %xmm3
addsd 0x7a78(%rip), %xmm3     # 0x10020
cmpl $0x0, 0x18(%rbx)
jle 0x85f5
movsd 0x48(%rbx), %xmm0
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
xorl %edx, %edx
movsd 0x7a73(%rip), %xmm1     # 0x10038
movsd (%r14,%rdx,8), %xmm2
divsd %xmm4, %xmm2
mulsd %xmm3, %xmm2
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %esi
movl %esi, (%rax,%rdx,4)
movl $0x0, (%rcx,%rdx,4)
incq %rdx
movslq 0x18(%rbx), %rsi
cmpq %rsi, %rdx
jl 0x85c5
movq 0x10(%rbx), %rax
movslq %r15d, %rsi
movl $0x4, %edx
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
xorl %r8d, %r8d
leaq 0x1(%r8), %rdi
addq $-0x4, %rdx
addl $0x2, %ecx
cmpq %rsi, %r8
jge 0x8623
cmpl $0x0, (%rax,%r8,4)
movq %rdi, %r8
je 0x8609
cmpq $0x1, %rdi
je 0x868f
leaq -0x1(%rdi), %r8
xorl %esi, %esi
cmpl %r8d, %r15d
jl 0x8658
movq %rax, %r8
subq %rdx, %r8
subq %rdi, %r15
addq $0x2, %r15
movl %r15d, %edx
xorl %esi, %esi
movl (%r8), %edi
movl %edi, (%rax,%rsi,4)
incq %rsi
addq $0x4, %r8
cmpq %rsi, %rdx
jne 0x8646
subl %ecx, 0x8(%rbx)
cmpl $0x2, %esi
jb 0x867b
movl %esi, %ecx
leaq -0x2(%rcx), %rdx
movl (%rax,%rdx,4), %esi
movl %esi, (%rax,%rcx,4)
incq %rcx
movslq 0x8(%rbx), %rsi
decq %rdx
cmpq %rsi, %rcx
jl 0x8666
movl 0x8(%rbx), %ecx
movl %ecx, %edx
decl %edx
shrl $0x1f, %edx
leal (%rcx,%rdx), %r15d
decl %r15d
sarl %r15d
movl 0x38(%rbx), %ecx
imull 0x8(%rbx), %ecx
movl %ecx, 0x18(%rbx)
testl %ecx, %ecx
jne 0x86b1
movl $0x1, 0x18(%rbx)
cvttsd2si %xmm3, %ecx
movl %ecx, (%rax)
movl $0x0, 0x4(%rax)
movl $0x0, 0x28(%rbx)
leal 0x1(,%r15,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 0x32a0
nop