// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %ecx, %r12d
movl %edx, %ebp
movq %rsi, (%rsp)
movq %rdi, %rbx
movl $0x1000, 0x8(%rdi)      # imm = 0x1000
movl %edx, 0xc(%rdi)
leaq 0x20879(%rip), %rax     # 0x242b1
movb $0x0, (%rax)
leaq 0x20876(%rip), %rax     # 0x242b8
andq $0x0, (%rax)
leaq 0x1009b(%rip), %rax     # 0x13ae8
movq %rax, (%rdi)
andq $0x0, 0x10(%rdi)
pushq $0x10
popq %rdi
callq 0x31d0
testl %eax, %eax
js 0x3bd1
movq 0x10544(%rip), %rdi     # 0x13fb0
callq 0xd990
pushq $0x20
popq %rdi
callq 0x3220
movq %rax, %r15
pushq $0x20
popq %rdi
movq %rax, %r14
callq 0x3220
movq %rax, %r13
movl $0x3e8, %eax            # imm = 0x3E8
xorl %edx, %edx
divl %r12d
movl %eax, %ecx
movl %ebp, %eax
xorl %edx, %edx
divl %ecx
andl $0xfffffc00, %eax       # imm = 0xFFFFFC00
movl $0x200, %edx            # imm = 0x200
cmovnel %eax, %edx
movl %edx, 0x8(%rbx)
movl %ebp, (%r15)
movl $0x18010, 0x4(%r15)     # imm = 0x18010
movw %dx, 0x8(%r15)
leaq -0x517(%rip), %rax      # 0x35ac
movq %rax, 0x10(%r15)
movq (%rsp), %rax
movq %rax, 0x18(%r15)
movzwl %dx, %eax
movl %eax, 0xc(%r15)
movl $0x361f0, %eax          # imm = 0x361F0
xorl %edx, %edx
divl %ecx
movl %eax, %r14d
addl %r14d, %r14d
andl $0x7fff0, %r14d         # imm = 0x7FFF0
leaq 0x10(%r14), %rax
leaq 0x207a9(%rip), %rcx     # 0x242a0
movq %rax, (%rcx)
leal 0x20(,%r14,2), %edi
callq 0x3040
leaq 0x2078a(%rip), %rcx     # 0x24298
movq %rax, (%rcx)
leaq 0xf(%r14), %rax
leaq 0x2078c(%rip), %rcx     # 0x242a8
movq %rax, (%rcx)
movq %r15, %rdi
movq %r13, %rsi
callq 0x32f0
movq 0x1049f(%rip), %r12     # 0x13fd0
movq (%r12), %rcx
testl %eax, %eax
jne 0x3b95
leaq 0xa567(%rip), %rdi      # 0xe0a7
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
callq 0x33a0
movq (%r12), %r14
callq 0x3060
leaq 0xa564(%rip), %rsi      # 0xe0bf
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x32c0
testq %r13, %r13
je 0x3bb5
movq %r13, 0x10(%rbx)
pushq $0x20
popq %rsi
movq %r15, %rdi
callq 0x3230
leaq 0x2072d(%rip), %rax     # 0x242b0
movb $0x1, (%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa4f4(%rip), %rdi      # 0xe090
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x33a0
movq (%r12), %rcx
leaq 0xa518(%rip), %rdi      # 0xe0d8
pushq $0x29
popq %rsi
pushq $0x1
popq %rdx
callq 0x33a0
movq %r15, 0x10(%rbx)
jmp 0x3b7c
movq 0x10418(%rip), %rdi     # 0x13ff0
leaq 0xa49c(%rip), %rsi      # 0xe07b
callq 0x3200
movq %rax, %rbx
callq 0x3170
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3200
movq %rax, %rdi
callq 0x3110
pushq $0x1
popq %rdi
callq 0x3270
movq %rax, %r15
cmpl $0x1, %edx
jne 0x3c58
movq %r15, %rdi
callq 0x30d0
movq %rax, %r15
movq 0x103cf(%rip), %rdi     # 0x13ff0
leaq 0xa43e(%rip), %rsi      # 0xe066
callq 0x3200
movq %rax, %rdi
movq %r15, %rsi
callq 0x3200
movq %rax, %rdi
callq 0x3110
callq 0x3310
xorl %r13d, %r13d
movq %r14, %r15
jmp 0x3a8a
movq %rax, %r15
callq 0x3310
movq %r15, %rdi
callq 0x3340