// File: /workspace/github_c_makefile_star2_O0/baines[P]MiniGBS/audio.c
// Function: audio_update_rate
void audio_update_rate(void){
	audio_rate = 59.7f;

	uint8_t tma = mem[0xff06];
	uint8_t tac = mem[0xff07];

	if(tac & 0x04){
		int rates[] = { 4096, 262144, 65536, 16384 };
		audio_rate = rates[tac & 0x03] / (float)(256 - tma);
		if(tac & 0x80) audio_rate *= 2.0f;
	}

	audio_rate *= cfg.speed;

	debug_msg("Audio rate changed: %.4f", audio_rate);

	size_t new_nsamples = (int)(FREQ / audio_rate) * 2;
	float* new_samples = calloc(new_nsamples, sizeof(float));

	if(samples){
		memcpy(new_samples, samples, MIN(nsamples, new_nsamples));
	}

	free(samples);
	samples    = new_samples;
	nsamples   = new_nsamples;

	free(samples_tmp);
	samples_tmp = calloc(nsamples, sizeof(float));

	// TODO: these should really be adjusted more accurately to not lose samples on speed change
	sample_ptr = samples;
	sample_end = samples + nsamples;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movss 0x5604(%rip), %xmm0     # 0x118a4
movss %xmm0, 0xa9e8(%rip)     # 0x16c90
leaq 0xa661(%rip), %rax      # 0x16910
movq (%rax), %rax
movb 0xff06(%rax), %al
movb %al, -0x1(%rbp)
leaq 0xa64e(%rip), %rax      # 0x16910
movq (%rax), %rax
movb 0xff07(%rax), %al
movb %al, -0x2(%rbp)
movzbl -0x2(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0xc342
movq 0x56af(%rip), %rax      # 0x11990
movq %rax, -0x20(%rbp)
movq 0x56ac(%rip), %rax      # 0x11998
movq %rax, -0x18(%rbp)
movzbl -0x2(%rbp), %eax
andl $0x3, %eax
cltq
cvtsi2ssl -0x20(%rbp,%rax,4), %xmm0
movzbl -0x1(%rbp), %ecx
movl $0x100, %eax            # imm = 0x100
subl %ecx, %eax
cvtsi2ss %eax, %xmm1
divss %xmm1, %xmm0
movss %xmm0, 0xa976(%rip)     # 0x16c90
movzbl -0x2(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0xc340
movss 0x3ce4(%rip), %xmm0     # 0x10014
mulss 0xa958(%rip), %xmm0     # 0x16c90
movss %xmm0, 0xa950(%rip)     # 0x16c90
jmp 0xc342
leaq 0xa5cf(%rip), %rax      # 0x16918
movss 0x20(%rax), %xmm0
mulss 0xa93a(%rip), %xmm0     # 0x16c90
movss %xmm0, 0xa932(%rip)     # 0x16c90
movss 0xa92a(%rip), %xmm0     # 0x16c90
cvtss2sd %xmm0, %xmm0
leaq 0x562f(%rip), %rdi      # 0x119a0
movb $0x1, %al
callq 0xacd0
movss 0x5500(%rip), %xmm0     # 0x11880
divss 0xa908(%rip), %xmm0     # 0x16c90
cvttss2si %xmm0, %eax
shll %eax
cltq
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movl $0x4, %esi
callq 0x54a0
movq %rax, -0x30(%rbp)
cmpq $0x0, 0xa8a2(%rip)      # 0x16c50
je 0xc40b
movq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
movq 0xa891(%rip), %rax      # 0x16c50
movq %rax, -0x50(%rbp)
movq 0xa876(%rip), %rax      # 0x16c40
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
ja 0xc3ea
movq -0x38(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0xc3f2
movq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdx
callq 0x55c0
movq 0xa83e(%rip), %rdi      # 0x16c50
callq 0x5250
movq -0x30(%rbp), %rax
movq %rax, 0xa82e(%rip)      # 0x16c50
movq -0x28(%rbp), %rax
movq %rax, 0xa813(%rip)      # 0x16c40
movq 0xa854(%rip), %rdi      # 0x16c88
callq 0x5250
movq 0xa800(%rip), %rdi      # 0x16c40
movl $0x4, %esi
callq 0x54a0
movq %rax, 0xa837(%rip)      # 0x16c88
movq 0xa7f8(%rip), %rax      # 0x16c50
movq %rax, 0xa801(%rip)      # 0x16c60
movq 0xa7ea(%rip), %rax      # 0x16c50
movq 0xa7d3(%rip), %rcx      # 0x16c40
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0xa7ed(%rip)      # 0x16c68
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)