// File: /workspace/github_cpp_makefile_star2_O1/khedoros[P]khednes/apu.cpp
// Function: apu::init(bool)
void apu::init(bool headless/*=false*/) {

    //dat.buffer = &(buffer[0]); Set this after the number of samples has been determined
    dat.apui = this;
    dat.first = 1;
    dat.buffered = 0;

    desired.freq = SAMPLE_RATE;
    desired.format = AUDIO_U8;
    desired.channels = 1;

    //#ifndef _WIN32
    //desired.samples = SAMPLES_PER_FRAME * 2; //Works for PulseAudio
    //#else
    desired.samples = 1024;                  //Works for DirectSound
    //#endif
    desired.callback = my_callback;
    desired.userdata = &dat;

    int init_success = 0;

    #ifdef _WIN32
    const char * desired_driver = "directsound";
    #else
    const char * desired_driver = "pulseaudio";
    #endif

    if(headless) {
        printf("In headless mode; going to init dummy audio driver.\n");
        init_success = SDL_AudioInit("dummy");
    }
    else {
        for (int i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
            const char* driver_name = SDL_GetAudioDriver(i);
            printf("Audio driver #%d: %s\n", i, driver_name);
            if(strcmp(driver_name, desired_driver) == 0) {
                cout<<"Going to init "<<desired_driver<<" driver."<<endl;
                init_success = SDL_AudioInit(desired_driver);
            }
        }
    }
    
    const char * device_name = "";
    if(init_success == 0) {
        cout<<"I think I successfully init'd the driver. Here are the "<<SDL_GetNumAudioDevices(0)<<" audio devices it provides: "<<endl;
        for(int i=0;i<SDL_GetNumAudioDevices(0);++i) {
            device_name = SDL_GetAudioDeviceName(i,0);
            #ifdef _WIN32
            if(i == 0 && device_name) id = SDL_OpenAudioDevice(device_name, 0, &desired, &obtained, 0);
            #endif
            
            if(device_name) {
                printf("Audio device #%d: %s\n", i, device_name);
            }
            else
                printf("Audio device #%d: NULL!?!\n", i);
        }
    }

    #ifndef _WIN32
    id = SDL_OpenAudioDevice(NULL, 0, &desired, &obtained, 0);
    #endif    

    if(id <= 0) {
        cerr<<"Couldn't open the audio device: "<<SDL_GetError()<<endl;
        //return 1;
    }
    else {
        if(SDL_GetCurrentAudioDriver()) {
            cout<<"Opened audio device with driver: "<<SDL_GetCurrentAudioDriver()<<endl;
        }
        else {
            cout<<"Doesn't seem like a driver was successfully init'd!"<<endl;

        }
    }

    if(id > 0) {
    //if(desired.freq != obtained.freq || desired.format != obtained.format || desired.channels != obtained.channels || desired.samples != obtained.samples) {
        //cout<<"Desired:\n\tFreq: "<<desired.freq<<"\n\tFormat: "<<desired.format<<"\n\tChannels: "<<int(desired.channels)<<"\n\tSamples: "<<desired.samples<<endl<<endl;
        cout<<"Obtained:\n\tFreq: "<<obtained.freq<<"\n\tFormat: "<<obtained.format<<"\n\tChannels: "<<int(obtained.channels)<<"\n\tSamples: "<<obtained.samples<<endl;
            //cout<<"Bufsize: "<<bufsize<<endl;
        size_t bufsize = SAMPLES_PER_FRAME * obtained.samples;
        buffer.resize(bufsize,0);
        dat.buffer = &(buffer[0]);
    //}
    //cout<<"byte size: "<<obtained.size<<endl;

    //if(obtained.freq != SAMPLE_RATE) {
    //    cout<<"Didn't get the frequency I wanted. Should be interesting."<<endl;
    //}
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
leaq 0x18(%rdi), %rax
movq %rdi, 0x20(%rdi)
movb $0x1, %cl
movb %cl, 0x28(%rdi)
movl $0x0, 0x2c(%rdi)
movl $0xac44, 0x70(%rdi)     # imm = 0xAC44
movw $0x8, 0x74(%rdi)
movb %cl, 0x76(%rdi)
movw $0x400, 0x78(%rdi)      # imm = 0x400
leaq -0x12d(%rip), %rcx      # 0x1078c
movq %rcx, 0x80(%rdi)
movq %rax, 0x88(%rdi)
testl %esi, %esi
movq %rdi, 0x10(%rsp)
je 0x108ef
leaq 0x5767(%rip), %rdi      # 0x1603e
callq 0x4540
leaq 0x5572(%rip), %rdi      # 0x15e55
callq 0x4520
movl %eax, %ebp
jmp 0x109bf
callq 0x4100
testl %eax, %eax
jle 0x109bd
leaq 0x5558(%rip), %r14      # 0x15e5b
leaq 0x5540(%rip), %r15      # 0x15e4a
movq 0xb69f(%rip), %r12      # 0x1bfb0
xorl %ebp, %ebp
xorl %r13d, %r13d
movl %r13d, %edi
callq 0x4620
movq %rax, %rbx
movq %r14, %rdi
movl %r13d, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x4050
movq %rbx, %rdi
movq %r15, %rsi
callq 0x44d0
testl %eax, %eax
jne 0x109aa
movl $0xe, %edx
movq %r12, %rdi
leaq 0x5522(%rip), %rsi      # 0x15e71
callq 0x4410
movl $0xa, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x4410
movl $0x8, %edx
movq %r12, %rdi
leaq 0x56f6(%rip), %rsi      # 0x16069
callq 0x4410
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq %r12, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4290
movq %r15, %rdi
callq 0x4520
movl %eax, %ebp
incl %r13d
callq 0x4100
cmpl %eax, %r13d
jl 0x10916
jmp 0x109bf
xorl %ebp, %ebp
movq 0x10(%rsp), %r13
leaq 0x70(%r13), %r14
testl %ebp, %ebp
jne 0x10a8d
movq 0xb5d9(%rip), %rbx      # 0x1bfb0
leaq 0x54a2(%rip), %rsi      # 0x15e80
movl $0x37, %edx
movq %rbx, %rdi
callq 0x4410
xorl %edi, %edi
callq 0x4300
movq %rbx, %rdi
movl %eax, %esi
callq 0x45e0
movq %rax, %rbx
leaq 0x54b2(%rip), %rsi      # 0x15eb8
movl $0x1c, %edx
movq %rax, %rdi
callq 0x4410
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4290
xorl %edi, %edi
callq 0x4300
testl %eax, %eax
jle 0x10a8d
leaq 0x549f(%rip), %r15      # 0x15eeb
leaq 0x5482(%rip), %r12      # 0x15ed5
xorl %ebp, %ebp
movl %ebp, %edi
xorl %esi, %esi
callq 0x4130
testq %rax, %rax
je 0x10a74
movq %r12, %rdi
movl %ebp, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x4050
jmp 0x10a80
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x4050
incl %ebp
xorl %edi, %edi
callq 0x4300
cmpl %eax, %ebp
jl 0x10a55
leaq 0x90(%r13), %rcx
xorl %edi, %edi
xorl %esi, %esi
movq %r14, %rdx
xorl %r8d, %r8d
callq 0x4430
movl %eax, 0x68(%r13)
testl %eax, %eax
je 0x10ae3
callq 0x40b0
movq 0xb4f9(%rip), %r14      # 0x1bfb0
testq %rax, %rax
je 0x10b1e
leaq 0x5464(%rip), %rsi      # 0x15f27
movl $0x21, %edx
movq %r14, %rdi
callq 0x4410
callq 0x40b0
testq %rax, %rax
jne 0x10b08
movq 0xb4cf(%rip), %r14      # 0x1bfb0
jmp 0x10b3b
movq 0xb506(%rip), %r14      # 0x1bff0
leaq 0x5415(%rip), %rsi      # 0x15f06
movl $0x20, %edx
movq %r14, %rdi
callq 0x4410
callq 0x42f0
testq %rax, %rax
je 0x10b34
movq %rax, %r15
movq %rax, %rdi
callq 0x41c0
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
jmp 0x10b2d
leaq 0x5424(%rip), %rsi      # 0x15f49
movl $0x33, %edx
movq %r14, %rdi
callq 0x4410
jmp 0x10b53
movq 0xb4b5(%rip), %r14      # 0x1bff0
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x45d0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq %r14, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4290
cmpl $0x0, 0x68(%r13)
je 0x10c76
movq 0xb424(%rip), %rbx      # 0x1bfb0
leaq 0x53ea(%rip), %rsi      # 0x15f7d
movl $0x11, %edx
movq %rbx, %rdi
callq 0x4410
movl 0x90(%r13), %esi
movq %rbx, %rdi
callq 0x45e0
movq %rax, %rbx
leaq 0x53d6(%rip), %rsi      # 0x15f8f
movl $0xa, %edx
movq %rax, %rdi
callq 0x4410
movzwl 0x94(%r13), %esi
movq %rbx, %rdi
callq 0x4280
movq %rax, %rbx
leaq 0x53ba(%rip), %rsi      # 0x15f9a
movl $0xc, %edx
movq %rax, %rdi
callq 0x4410
movzbl 0x96(%r13), %esi
movq %rbx, %rdi
callq 0x45e0
movq %rax, %rbx
leaq 0x53a0(%rip), %rsi      # 0x15fa7
movl $0xb, %edx
movq %rax, %rdi
callq 0x4410
movzwl 0x98(%r13), %esi
movq %rbx, %rdi
callq 0x4280
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4290
movzwl 0x98(%r13), %eax
imulq $0x2df, %rax, %rsi      # imm = 0x2DF
leaq 0x38(%r13), %rdi
leaq 0xf(%rsp), %rdx
movb $0x0, (%rdx)
callq 0xa1a0
movq 0x38(%r13), %rax
movq %rax, 0x18(%r13)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop