// File: /workspace/github_c_makefile_star2_O1/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, %r15d
movl %edx, %ebp
movq %rsi, (%rsp)
movq %rdi, %rbx
leaq 0xff2e(%rip), %rax      # 0x13a58
addq $0x10, %rax
movq %rax, (%rdi)
movl $0x1000, 0x8(%rdi)      # imm = 0x1000
movl %edx, 0xc(%rdi)
leaq 0x2075f(%rip), %rax     # 0x242a1
movb $0x0, (%rax)
leaq 0x2075c(%rip), %rax     # 0x242a8
xorl %ecx, %ecx
movq %rcx, (%rax)
leaq 0xff90(%rip), %rax      # 0x13ae8
movq %rax, (%rdi)
movq %rcx, 0x10(%rdi)
movl $0x10, %edi
callq 0x31d0
testl %eax, %eax
js 0x3c76
movq 0x10438(%rip), %rdi     # 0x13fb0
callq 0xe470
movl $0x20, %edi
callq 0x3230
movq %rax, %r12
movl $0x20, %edi
movq %rax, %r14
callq 0x3230
movq %rax, %r13
movl $0x3e8, %eax            # imm = 0x3E8
xorl %edx, %edx
divl %r15d
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, (%r12)
movl $0x18010, 0x4(%r12)     # imm = 0x18010
movw %dx, 0x8(%r12)
leaq -0x57a(%rip), %rax      # 0x365c
movq %rax, 0x10(%r12)
movq (%rsp), %rax
movq %rax, 0x18(%r12)
movzwl %dx, %eax
movl %eax, 0xc(%r12)
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 0x20683(%rip), %rcx     # 0x24290
movq %rax, (%rcx)
leal 0x20(,%r14,2), %edi
callq 0x3040
leaq 0x20664(%rip), %rcx     # 0x24288
movq %rax, (%rcx)
leaq 0xf(%r14), %rax
leaq 0x20666(%rip), %rcx     # 0x24298
movq %rax, (%rcx)
movq %r12, %rdi
movq %r13, %rsi
callq 0x3310
movq 0x10389(%rip), %rcx     # 0x13fd0
movq (%rcx), %rdi
testl %eax, %eax
jne 0x3c62
addq $0x10, %rbx
movq %r13, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x33e7
jmp 0x3c67
callq 0x33d2
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10373(%rip), %rbx     # 0x13ff0
leaq 0xb3f7(%rip), %rsi      # 0xf07b
movl $0x14, %edx
movq %rbx, %rdi
callq 0x3260
callq 0x3180
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3210
movq %rax, %rdi
callq 0x3110
movl $0x1, %edi
callq 0x3280
movq %rax, %r12
cmpl $0x1, %edx
jne 0x3d0d
movq %r12, %rdi
callq 0x30e0
movq %rax, %r12
movq 0x10323(%rip), %rdi     # 0x13ff0
leaq 0xb392(%rip), %rsi      # 0xf066
movl $0x14, %edx
callq 0x3260
movq 0x1030b(%rip), %rdi     # 0x13ff0
movq %r12, %rsi
callq 0x3210
movq %rax, %rdi
callq 0x3110
callq 0x3330
xorl %r13d, %r13d
movq %r14, %r12
jmp 0x3b9a
movq %rax, %r12
callq 0x3330
movq %r12, %rdi
callq 0x3360
nop