// File: /workspace/github_c_makefile_star2_O0/calmopyrin[P]tedplay/AudioSDL.cpp
// Function: AudioSDL::AudioSDL(void*, unsigned int, unsigned int)
AudioSDL::AudioSDL(void *userData, unsigned int sampleFrq_ = 48000,
				   unsigned int bufDurInMsec = 40) : Audio(sampleFrq_), audiohwspec(0)
{
	if (SDL_Init(SDL_INIT_AUDIO) < 0) { //  SDL_INIT_AUDIO|
		std::cerr << "Unable to init SDL: " << SDL_GetError() << std::endl;
		exit(1);
	}
	atexit(SDL_Quit);

	SDL_AudioSpec *desired, *obtained = NULL;

	try {
		desired = new SDL_AudioSpec;
		obtained = new SDL_AudioSpec;
	} catch(char *txt) {
		std::cerr << "Exception occurred: " << txt << std::endl;
	}

	unsigned int fragsPerSec = 1000 / bufDurInMsec;
	unsigned int bufSize1kbChunk = (sampleFrq_ / fragsPerSec / 1024) * 1024;
	if (!bufSize1kbChunk) bufSize1kbChunk = 512;
	bufferLength = bufSize1kbChunk;

	desired->freq		= sampleFrq_;
	desired->format		= AUDIO_S16;
	desired->channels	= 1;
	desired->samples	= bufferLength;
	desired->callback	= audioCallback;
	desired->userdata	= userData;
	desired->size		= desired->channels * desired->samples * sizeof(Uint8);
	desired->silence	= 0x00;

	unsigned int bfSize = TED_SOUND_CLOCK / fragsPerSec; //(bufferLength * TED_SOUND_CLOCK + sampleFrq_ / 2) / sampleFrq_;
	// double length ring buffer, dividable by 8
	ringBufferSize = (bfSize / 8 + 1) * 16;
	ringBuffer = new short[ringBufferSize];
	// trigger initial buffer fill
	ringBufferIndex = ringBufferSize-1;

	if (SDL_OpenAudio(desired, obtained)) {
		fprintf(stderr,"SDL_OpenAudio failed!\n");
		return;
	} else {
		fprintf(stderr,"SDL_OpenAudio success!\n");
		fprintf(stderr, "Using audio driver : %s\n", SDL_GetCurrentAudioDriver());
		if ( obtained == NULL ) {
			fprintf(stderr, "Great! We have our desired audio format!\n");
			audiohwspec = desired;
			delete obtained;
		} else {
			//fprintf(stderr, "Oops! Failed to get desired audio format!\n");
			audiohwspec = obtained;
			delete desired;
		}
	}
	paused = true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x58(%rbp)
movl -0x14(%rbp), %esi
callq 0x43f0
movq -0x58(%rbp), %rax
leaq 0x1fc44(%rip), %rcx     # 0x23ab8
movq %rcx, (%rax)
movq $0x0, 0x10(%rax)
movl $0x10, %edi
callq 0x3220
movl %eax, -0x50(%rbp)
jmp 0x3e8e
movl -0x50(%rbp), %eax
cmpl $0x0, %eax
jge 0x3efa
movq 0x20153(%rip), %rdi     # 0x23ff0
leaq 0x1a1d7(%rip), %rsi     # 0x1e07b
callq 0x3250
movq %rax, -0x60(%rbp)
jmp 0x3eaf
callq 0x3190
movq %rax, -0x68(%rbp)
jmp 0x3eba
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdi
callq 0x3250
movq %rax, -0x70(%rbp)
jmp 0x3ecd
movq -0x70(%rbp), %rdi
movq 0x200e0(%rip), %rsi     # 0x23fb8
callq 0x3290
jmp 0x3edf
movl $0x1, %edi
callq 0x3300
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x41fa
movq 0x200a7(%rip), %rdi     # 0x23fa8
callq 0x1d9a0
movq $0x0, -0x38(%rbp)
movl $0x20, %edi
callq 0x3270
movq %rax, -0x78(%rbp)
jmp 0x3f1e
movq -0x78(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x20, %edi
callq 0x3270
movq %rax, -0x80(%rbp)
jmp 0x3f36
movq -0x80(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x3fb8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x41fa
movq -0x20(%rbp), %rdi
callq 0x30e0
movq %rax, -0x40(%rbp)
movq 0x20080(%rip), %rdi     # 0x23ff0
leaq 0x1a0ef(%rip), %rsi     # 0x1e066
callq 0x3250
movq %rax, -0x88(%rbp)
jmp 0x3f85
movq -0x88(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3250
movq %rax, -0x90(%rbp)
jmp 0x3f9e
movq -0x90(%rbp), %rdi
movq 0x2000c(%rip), %rsi     # 0x23fb8
callq 0x3290
jmp 0x3fb3
callq 0x33e0
movl $0x3e8, %eax            # imm = 0x3E8
xorl %edx, %edx
divl -0x18(%rbp)
movl %eax, -0x44(%rbp)
movl -0x14(%rbp), %eax
xorl %edx, %edx
divl -0x44(%rbp)
shrl $0xa, %eax
shll $0xa, %eax
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jne 0x3ffb
movl $0x200, -0x48(%rbp)     # imm = 0x200
jmp 0x3ffb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x33e0
jmp 0x41fa
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
movl %ecx, 0x8(%rax)
movl -0x14(%rbp), %edx
movq -0x30(%rbp), %rcx
movl %edx, (%rcx)
movq -0x30(%rbp), %rcx
movw $0x8010, 0x4(%rcx)      # imm = 0x8010
movq -0x30(%rbp), %rcx
movb $0x1, 0x6(%rcx)
movw 0x8(%rax), %cx
movq -0x30(%rbp), %rax
movw %cx, 0x8(%rax)
movq -0x30(%rbp), %rax
leaq -0x977(%rip), %rcx      # 0x36c0
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x30(%rbp), %rax
movzbl 0x6(%rax), %ecx
movzwl 0x8(%rax), %edx
imull %edx, %ecx
movl %ecx, 0xc(%rax)
movq -0x30(%rbp), %rax
movb $0x0, 0x7(%rax)
movl -0x44(%rbp), %ecx
xorl %edx, %edx
movl $0x361f0, %eax          # imm = 0x361F0
divl %ecx
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %ecx
andl $-0x8, %ecx
movl %ecx, %eax
leal 0x10(%rax,%rax), %eax
movl %eax, %ecx
leaq 0x3029b(%rip), %rax     # 0x34320
movq %rcx, (%rax)
movq (%rax), %rdi
addq %rdi, %rdi
movq $-0x1, %rax
cmovbq %rax, %rdi
callq 0x3040
movq %rax, -0x98(%rbp)
jmp 0x40a7
movq -0x98(%rbp), %rcx
leaq 0x30263(%rip), %rax     # 0x34318
movq %rcx, (%rax)
leaq 0x30261(%rip), %rax     # 0x34320
movq (%rax), %rcx
decq %rcx
leaq 0x3025c(%rip), %rax     # 0x34328
movq %rcx, (%rax)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x33c0
movl %eax, -0x9c(%rbp)
jmp 0x40e4
movl -0x9c(%rbp), %eax
cmpl $0x0, %eax
je 0x410c
movq 0x1feda(%rip), %rax     # 0x23fd0
movq (%rax), %rdi
leaq 0x19f90(%rip), %rsi     # 0x1e090
movb $0x0, %al
callq 0x3380
jmp 0x41f1
movq 0x1febd(%rip), %rax     # 0x23fd0
movq %rax, -0xb8(%rbp)
movq (%rax), %rdi
leaq 0x19f83(%rip), %rsi     # 0x1e0a7
xorl %eax, %eax
callq 0x3380
movq -0xb8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
callq 0x3060
movq %rax, -0xa8(%rbp)
jmp 0x414a
movq -0xa8(%rbp), %rdx
movq -0xb0(%rbp), %rdi
leaq 0x19f60(%rip), %rsi     # 0x1e0bf
movb $0x0, %al
callq 0x3380
cmpq $0x0, -0x38(%rbp)
jne 0x41b5
movq 0x1fe5c(%rip), %rax     # 0x23fd0
movq (%rax), %rdi
leaq 0x19f5a(%rip), %rsi     # 0x1e0d8
movb $0x0, %al
callq 0x3380
movq -0x58(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rax
movq %rax, -0xc0(%rbp)
cmpq $0x0, %rax
je 0x41b3
movq -0xc0(%rbp), %rdi
movl $0x20, %esi
callq 0x3280
jmp 0x41e5
movq -0x58(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, %rax
je 0x41e3
movq -0xc8(%rbp), %rdi
movl $0x20, %esi
callq 0x3280
jmp 0x41e5
jmp 0x41e7
leaq 0x30142(%rip), %rax     # 0x34330
movb $0x1, (%rax)
addq $0xd0, %rsp
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x3bf0
movq -0x20(%rbp), %rdi
callq 0x3410
nopl (%rax)