commit 2842523aae8cbeec79f863099fb0886cdc162c60 parent 1ef4095e7e6c6d7bdd9a29c4840af1043fd9a321 Author: Michael Savage <mikejsavage@gmail.com> Date: Wed Jan 25 23:45:10 +0200 Build audio.exe on Windows. Audio output still non-functional Diffstat:
build.bat | | | 4 | ++++ |
win32_audio_output.cc | | | 47 | ++++++++++++++++++++++++++--------------------- |
win32_audio_output.h | | | 19 | +++++++++++++++++++ |
diff --git a/build.bat b/build.bat @@ -29,6 +29,7 @@ @cl memory_arena.cc -c %FLAGS% @cl lz4.cc -c %FLAGS% @cl lz4hc.cc -c %FLAGS% +@cl mixer.cc -c %FLAGS% @cl profiler.cc -c %FLAGS% @cl renderer.cc -c %FLAGS% @cl sha2.cc -c %FLAGS% -wd4244 @@ -39,6 +40,8 @@ @cl strlcpy.cc -c %FLAGS% @cl terrain_manager.cc -c %FLAGS% @cl text_renderer.cc -c %FLAGS% +@cl wave.cc -c %FLAGS% +@cl win32_audio_output.cc -c %FLAGS% @cl work_queue.cc -c %FLAGS% @cl rng\well512.cc -c %FLAGS% -Forng\well512.obj @@ -66,3 +69,4 @@ @cl main.cc gl.obj glad.obj hm.obj terrain_manager.obj heightmap.obj btt.obj gpubtt.obj lz4.obj skybox.obj http.obj -DSTATIC_GAME %COMMONOBJS% %EXEFLAGS% -Femedfall.exe @cl pp.cc heightmap.obj log.obj stb_image.obj lz4.obj lz4hc.obj memory_arena.obj strlcpy.obj %EXEFLAGS% @cl launcher\main.cc log.obj http.obj sha2.obj gl.obj glad.obj work_queue.obj memory_arena.obj strlcpy.obj imgui.lib %EXEFLAGS% -Felauncher.exe +@cl audio.cc memory_arena.obj log.obj strlcpy.obj win32_audio_output.obj mixer.obj wave.obj Ole32.lib %EXEFLAGS% -Feaudio.exe diff --git a/win32_audio_output.cc b/win32_audio_output.cc @@ -2,16 +2,16 @@ #include <windows.h> #include <wrl/client.h> -#include <C:/Program Files (x86)/Microsoft DirectX SDK (June 2010)/Include/comdecl.h> -#include <C:/Program Files (x86)/Microsoft DirectX SDK (June 2010)/Include/xaudio2.h> +#include <comdecl.h> +#include <xaudio2.h> #include "intrinsics.h" #include "log.h" #include "platform_audio_output.h" -// #include "platform_library.h" +#include "platform_library.h" #include "platform_thread.h" -// static Library xaudio_lib = NULL; +static Library xaudio2_lib = NULL; static IXAudio2 * xaudio2 = NULL; static IXAudio2MasteringVoice * mastering_voice = NULL; @@ -21,6 +21,11 @@ void audio_output_init() { FATAL( "CoInitializeEx" ); } + xaudio2_lib = library_open( "XAudio2_7.dll" ); + if( xaudio2_lib == NULL ) { + FATAL( "Couldn't open xaudio2_7.dll: %s", library_last_error() ); + } + if( FAILED( XAudio2Create( &xaudio2, 0, XAUDIO2_DEFAULT_PROCESSOR ) ) ) { FATAL( "XAudio2Create" ); } @@ -28,21 +33,14 @@ void audio_output_init() { if( FAILED( xaudio2->CreateMasteringVoice( &mastering_voice ) ) ) { FATAL( "CreateMasteringVoice" ); } - - // xaudio_lib = library_open( "xaudio2_7.dll" ); - // if( xaudio_lib == NULL ) { - // FATAL( "Couldn't open xaudio2_7.dll: %s", library_last_error() ); - // } } void audio_output_term() { - // library_close( xaudio_lib ); - mastering_voice->DestroyVoice(); - xaudio2->Reset(); - + library_close( xaudio2_lib ); CoUninitialize(); + xaudio2_lib = NULL; xaudio2 = NULL; mastering_voice = NULL; } @@ -51,7 +49,7 @@ static THREAD( audio_output_thread ) { AudioOutputDevice * device = ( AudioOutputDevice * ) data; // double buffering - s16 buffers[ 1024 ][ 2 ]; + s16 buffers[ 256 ][ 2 ]; u32 buffer_idx = 0; for( ;; ) { for( ;; ) { @@ -61,16 +59,15 @@ static THREAD( audio_output_thread ) { break; } - // WaitForSingleObject( voiceContext.hBufferEndEvent, INFINITE ); - // TODO: wait for semaphore or something + WaitForSingleObject( device->callbacks.event, INFINITE ); } device->callback( buffers[ buffer_idx ], ARRAY_COUNT( buffers[ buffer_idx ] ) ); XAUDIO2_BUFFER buf; - zero( &buf ); + zero_struct( &buf ); buf.AudioBytes = sizeof( buffers[ buffer_idx ] ); - buf.pAudioData = buffers[ buffer_idx ]; + buf.pAudioData = ( const BYTE * ) buffers[ buffer_idx ]; if( FAILED( device->voice->SubmitSourceBuffer( &buf ) ) ) { FATAL( "SubmitSourceBuffer" ); @@ -84,7 +81,7 @@ static THREAD( audio_output_thread ) { void audio_output_open( AudioOutputDevice * device, AudioOutputCallback callback ) { WAVEFORMATEX format; - zero( &format ); + zero_struct( &format ); format.wFormatTag = 1; format.nChannels = 1; format.nSamplesPerSec = 44100; @@ -93,14 +90,22 @@ void audio_output_open( AudioOutputDevice * device, AudioOutputCallback callback format.wBitsPerSample = 8; format.cbSize = 0; - if( FAILED( xaudio2->CreateSourceVoice( &device->voice, &format ) ) ) { + if( FAILED( xaudio2->CreateSourceVoice( &device->voice, &format, 0, 0, &device->callbacks ) ) ) { FATAL( "CreateSourceVoice" ); } - device->voice->start( 0, 0 ); + device->callbacks.event = CreateEvent( NULL, FALSE, FALSE, NULL ); + if( device->callbacks.event == NULL ) { + FATAL( "CreateEvent" ); + } + + device->voice->Start( 0 ); } void audio_output_close( AudioOutputDevice * device ) { device->voice->Stop( 0 ); device->voice->DestroyVoice(); + if( CloseHandle( device->callbacks.event ) == 0 ) { + FATAL( "CloseHandle" ); + } } diff --git a/win32_audio_output.h b/win32_audio_output.h @@ -1,9 +1,28 @@ #pragma once +#include "intrinsics.h" #include "platform_thread.h" +#include <xaudio2.h> + +struct AudioCallbacks : public IXAudio2VoiceCallback { + HANDLE event; + + void __stdcall OnBufferEnd( void * ) { + SetEvent( event ); + } + + void __stdcall OnBufferStart( void * ) { } + void __stdcall OnLoopEnd( void * ) { } + void __stdcall OnStreamEnd() { } + void __stdcall OnVoiceError( void *, HRESULT ) { } + void __stdcall OnVoiceProcessingPassStart( u32 ) { } + void __stdcall OnVoiceProcessingPassEnd() { } +}; + struct AudioOutputDevice { IXAudio2SourceVoice * voice; + AudioCallbacks callbacks; Thread thread; AudioOutputCallback * callback; };