medfall

A super great game engine
Log | Files | Refs

commit a0b0ba0dd9fe6d87d7f53644dacc31277eec5928
parent c1a62c253f8ffdd8b910498bfcf0ee8da1d232d7
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Mon, 29 Oct 2018 11:46:24 +0200

Fix Windows audio output

Diffstat:
audio.cc | 2+-
win32_audio_output.cc | 22++++++----------------
2 files changed, 7 insertions(+), 17 deletions(-)

diff --git a/audio.cc b/audio.cc @@ -68,7 +68,7 @@ int main( int argc, char ** argv ) { printf( "%f %s %u\n", volume, playing ? "playing" : "stopped", sound_sin ); #if PLATFORM_WINDOWS - Sleep( 100 ); + Sleep( 185 ); #else getline( &line, &n, stdin ); diff --git a/win32_audio_output.cc b/win32_audio_output.cc @@ -71,21 +71,14 @@ void audio_output_term() { static THREAD( audio_output_thread ) { AudioOutputDevice * device = ( AudioOutputDevice * ) data; - // TODO: can probably get rid of this, don't need double buffers when - // we have a big ring buffer - - // double buffering - constexpr u32 num_samples = 512; - STATIC_ASSERT( ARRAY_COUNT( device->buffer.samples ) % num_samples == 0 ); - - s16 buffers[ 2 ][ num_samples ]; - u32 buffer_idx = 0; - while( load_acquire( &device->shutting_down ) == 0 ) { + constexpr u32 num_samples = 512; + STATIC_ASSERT( ARRAY_COUNT( device->buffer.samples ) % num_samples == 0 ); + for( ;; ) { XAUDIO2_VOICE_STATE state; device->voice->GetState( &state ); - if( state.BuffersQueued < ARRAY_COUNT( buffers ) ) { + if( state.BuffersQueued < 2 ) { break; } @@ -93,19 +86,16 @@ static THREAD( audio_output_thread ) { } u32 cursor = load_acquire( &device->buffer.cursor ); - memcpy( buffers[ buffer_idx ], &device->buffer[ cursor ], sizeof( buffers[ buffer_idx ] ) ); XAUDIO2_BUFFER buf = { }; - buf.AudioBytes = sizeof( buffers[ buffer_idx ] ); - buf.pAudioData = ( const BYTE * ) buffers[ buffer_idx ]; + buf.AudioBytes = num_samples * sizeof( device->buffer.samples[ 0 ] ); + buf.pAudioData = ( const BYTE * )( device->buffer.samples + cursor ); buf.pContext = device->event; if( FAILED( device->voice->SubmitSourceBuffer( &buf ) ) ) { FATAL( "SubmitSourceBuffer" ); } - buffer_idx = ( buffer_idx + 1 ) % ARRAY_COUNT( buffers ); - store_release( &device->buffer.cursor, ( cursor + num_samples ) % ARRAY_COUNT( device->buffer.samples ) ); }