// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %rax
movq %rdi, %rbx
leaq 0x18(%rdi), %rax
movq %rdi, 0x20(%rdi)
movb $0x1, %cl
movb %cl, 0x28(%rdi)
andl $0x0, 0x2c(%rdi)
movl $0xac44, 0x70(%rdi)     # imm = 0xAC44
movw $0x8, 0x74(%rdi)
movb %cl, 0x76(%rdi)
movw $0x400, 0x78(%rdi)      # imm = 0x400
leaq -0xe4(%rip), %rcx       # 0xe878
movq %rcx, 0x80(%rdi)
movq %rax, 0x88(%rdi)
testl %esi, %esi
je 0xe9e5
leaq 0x5679(%rip), %rdi      # 0x13fee
callq 0x4540
leaq 0x5484(%rip), %rdi      # 0x13e05
callq 0x4520
movl %eax, %r13d
leaq 0x70(%rbx), %r14
testl %r13d, %r13d
je 0xea72
leaq 0x90(%rbx), %rcx
xorl %edi, %edi
xorl %esi, %esi
movq %r14, %rdx
xorl %r8d, %r8d
callq 0x4400
movl %eax, 0x68(%rbx)
testl %eax, %eax
je 0xeafe
callq 0x40a0
movq 0xc5ed(%rip), %rdi      # 0x1afb0
testq %rax, %rax
je 0xeb21
leaq 0x5504(%rip), %rsi      # 0x13ed7
callq 0x4390
movq %rax, %r14
callq 0x40a0
jmp 0xeb19
leaq 0x541f(%rip), %r14      # 0x13e0b
leaq 0x5407(%rip), %r15      # 0x13dfa
xorl %r12d, %r12d
xorl %r13d, %r13d
callq 0x40f0
cmpl %eax, %r12d
jge 0xe989
movl %r12d, %edi
callq 0x4650
movq %rax, %rbp
movq %r14, %rdi
movl %r12d, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x4040
movq %rbp, %rdi
movq %r15, %rsi
callq 0x44c0
testl %eax, %eax
jne 0xea6d
movq 0xc57c(%rip), %rdi      # 0x1afb0
leaq 0x53e6(%rip), %rsi      # 0x13e21
callq 0x4390
movq %rax, %rdi
movq %r15, %rsi
callq 0x4390
movq %rax, %rdi
leaq 0x55c4(%rip), %rsi      # 0x14019
callq 0x4390
movq %rax, %rdi
callq 0x41c0
movq %r15, %rdi
callq 0x4520
movl %eax, %r13d
incl %r12d
jmp 0xe9f9
movq 0xc537(%rip), %rdi      # 0x1afb0
leaq 0x53b0(%rip), %rsi      # 0x13e30
callq 0x4390
movq %rax, %r15
xorl %edi, %edi
callq 0x42e0
movq %r15, %rdi
movl %eax, %esi
callq 0x4610
leaq 0x53c8(%rip), %rsi      # 0x13e68
movq %rax, %rdi
callq 0x4390
movq %rax, %rdi
callq 0x41c0
leaq 0x53e4(%rip), %r15      # 0x13e9b
leaq 0x53c7(%rip), %r12      # 0x13e85
xorl %ebp, %ebp
xorl %edi, %edi
callq 0x42e0
cmpl %eax, %ebp
jge 0xe996
movl %ebp, %edi
xorl %esi, %esi
callq 0x4120
testq %rax, %rax
je 0xeaee
movq %r12, %rdi
movl %ebp, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x4040
jmp 0xeafa
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x4040
incl %ebp
jmp 0xeac0
movq 0xc4eb(%rip), %rdi      # 0x1aff0
leaq 0x53aa(%rip), %rsi      # 0x13eb6
callq 0x4390
movq %rax, %r14
callq 0x42c0
movq %r14, %rdi
movq %rax, %rsi
jmp 0xeb28
leaq 0x53d1(%rip), %rsi      # 0x13ef9
callq 0x4390
movq %rax, %rdi
callq 0x41c0
cmpl $0x0, 0x68(%rbx)
je 0xebe9
movq 0xc46a(%rip), %rdi      # 0x1afb0
leaq 0x53e0(%rip), %rsi      # 0x13f2d
callq 0x4390
movl 0x90(%rbx), %esi
movq %rax, %rdi
callq 0x4610
leaq 0x53d8(%rip), %rsi      # 0x13f3f
movq %rax, %rdi
callq 0x4390
movzwl 0x94(%rbx), %esi
movq %rax, %rdi
callq 0x45e0
leaq 0x53c5(%rip), %rsi      # 0x13f4a
movq %rax, %rdi
callq 0x4390
movzbl 0x96(%rbx), %esi
movq %rax, %rdi
callq 0x4610
leaq 0x53b4(%rip), %rsi      # 0x13f57
movq %rax, %rdi
callq 0x4390
movzwl 0x98(%rbx), %esi
movq %rax, %rdi
callq 0x45e0
movq %rax, %rdi
callq 0x41c0
movzwl 0x98(%rbx), %eax
imulq $0x2df, %rax, %rsi      # imm = 0x2DF
leaq 0x38(%rbx), %rdi
leaq 0x7(%rsp), %rdx
movb $0x0, (%rdx)
callq 0x8f6a
movq 0x38(%rbx), %rax
movq %rax, 0x18(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq