commit f3f4af7dfc5051e894a96326a639491dea64a6e3 parent 56db176eadfa05b60f3eaaaa9cac38ac226498f2 Author: Michael Savage <mikejsavage@gmail.com> Date: Mon Oct 24 20:34:09 +0300 Mixer cleanup Diffstat:
mixer.cc | | | 38 | ++++++++++++++++---------------------- |
diff --git a/mixer.cc b/mixer.cc @@ -1,11 +1,12 @@ #include "intrinsics.h" +#include "int_conversions.h" #include "mixer.h" #include "platform_audio_output.h" #include "nonblocking_fixed_spsc_queue.h" struct PlayingSound { SoundData data; - s32 num_played_samples; // TODO: ssize_t? + s32 num_played_samples; float volume; u16 next_free; // TODO: next_playing? allows us to make playing implicit @@ -42,7 +43,7 @@ static Thread mixer_thread; static PlayingSoundID sound_counter; static bool mixer_sound_id_to_slot( PlayingSoundID sound_id, size_t * slot ) { - for( size_t i = 0; i < array_count( playing_sounds ); i++ ) { + for( size_t i = 0; i < ARRAY_COUNT( playing_sounds ); i++ ) { if( ids[ i ] == sound_id ) { *slot = i; return true; @@ -73,7 +74,7 @@ static bool mixer_find_free_slot( size_t * free_slot ) { static void mixer_stop_all_impl() { first_free_sound = 0; - for( size_t i = 0; i < array_count( playing_sounds ); i++ ) { + for( size_t i = 0; i < ARRAY_COUNT( playing_sounds ); i++ ) { playing_sounds[ i ].playing = false; playing_sounds[ i ].next_free = i + 1; ids[ i ] = INVALID_SOUND_ID; @@ -121,24 +122,21 @@ static void mixer_process_command( MixerCommand cmd ) { static void mixer_mix_sound( PlayingSound * ps, float * samples, size_t num_samples ) { // TODO: simd - // TODO: there is some funny casting in here - s32 samples_to_write = num_samples; + s32 samples_to_write = checked_cast< s32 >( num_samples ); if( !ps->loop ) { - samples_to_write = min( samples_to_write, ( s32 ) ps->data.num_samples - ps->num_played_samples ); + samples_to_write = min( samples_to_write, checked_cast< s32 >( ps->data.num_samples ) - ps->num_played_samples ); } - float scale = 1.0f / float( S16_MAX ); - for( size_t i = 0; i < samples_to_write; i++ ) { - // TODO: this is not quite right - size_t j = ( ps->num_played_samples + i ) % ps->data.num_samples; - samples[ i ] += float( ps->data.samples[ j ] ) * scale * ps->volume; + for( s32 i = 0; i < samples_to_write; i++ ) { + s32 j = ( ps->num_played_samples + i ) % ps->data.num_samples; + samples[ i ] += dequantize11s( ps->data.samples[ j ], 16 ) * ps->volume; } ps->num_played_samples += samples_to_write; if( ps->loop ) { ps->num_played_samples %= ps->data.num_samples; } - else if( ps->num_played_samples == ps->data.num_samples ) { + else if( ps->num_played_samples == checked_cast< s32 >( ps->data.num_samples ) ) { ps->playing = false; } } @@ -147,8 +145,6 @@ static THREAD( mixer_thread_proc ) { AudioOutputDevice * output_device = ( AudioOutputDevice * ) data; for( ;; ) { - // TODO: limit how many commands we process at once so people - // can't jam the queue and make us skip for( size_t i = 0; i < COMMAND_QUEUE_SIZE; i++ ) { MixerCommand cmd; if( !command_queue.dequeue( &cmd ) ) break; @@ -157,24 +153,23 @@ static THREAD( mixer_thread_proc ) { // TODO: channels float samples[ 2048 ]; - for( size_t i = 0; i < array_count( samples ); i++ ) { + for( size_t i = 0; i < ARRAY_COUNT( samples ); i++ ) { samples[ i ] = 0.0f; } - for( size_t i = 0; i < array_count( playing_sounds ); i++ ) { + for( size_t i = 0; i < ARRAY_COUNT( playing_sounds ); i++ ) { PlayingSound & ps = playing_sounds[ i ]; if( ps.playing ) { - mixer_mix_sound( &ps, samples, array_count( samples ) ); + mixer_mix_sound( &ps, samples, ARRAY_COUNT( samples ) ); } } s16 finalised_samples[ 2048 ]; - for( size_t i = 0; i < array_count( samples ); i++ ) { - // TODO: this float -> s16 is not quite right - finalised_samples[ i ] = clamp11( samples[ i ] ) * S16_MAX; + for( size_t i = 0; i < ARRAY_COUNT( samples ); i++ ) { + finalised_samples[ i ] = quantize11s( clamp11( samples[ i ] ), 16 ); } - audio_output_write( output_device, finalised_samples, array_count( finalised_samples ) ); + audio_output_write( output_device, finalised_samples, ARRAY_COUNT( finalised_samples ) ); } THREAD_END; @@ -193,7 +188,6 @@ void mixer_term() { } PlayingSoundID mixer_play( SoundData sound, MixerLoopBool loop, s32 start_pos ) { - // TODO: check start_pos fits in PlayingSound.num_played_samples MixerCommand cmd; cmd.type = MIXER_PLAY; cmd.sound_to_play = sound;