medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit aac5a39d5ea6e48def097fc47ca5786c0dd5f307
parent 770b6211add827d724bdd49f79b0633f30e06a9e
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sun Dec 11 14:41:07 +0200

Use Pool in the mixer

Diffstat:
mixer.cc | 84++++++++++++++++++++++---------------------------------------------------------
1 file changed, 23 insertions(+), 61 deletions(-)
diff --git a/mixer.cc b/mixer.cc @@ -1,16 +1,15 @@ #include "intrinsics.h" #include "int_conversions.h" #include "mixer.h" +#include "pool.h" #include "nonblocking_fixed_spsc_queue.h" struct PlayingSound { + PlayingSoundID id; SoundData data; s32 num_played_samples; float volume; - u16 next_free; - // TODO: next_playing? allows us to make playing implicit bool loop; - bool playing; }; enum MixerCommandType { @@ -34,81 +33,47 @@ struct MixerCommand { #define RESET_SOUND_COUNTER 0 STATIC_ASSERT( RESET_SOUND_COUNTER != INVALID_SOUND_ID ); -static PlayingSound playing_sounds[ MAX_CONCURRENT_SOUNDS ]; -static PlayingSoundID ids[ MAX_CONCURRENT_SOUNDS ]; -static u16 first_free_sound; +static Pool< PlayingSound, MAX_CONCURRENT_SOUNDS > playing_sounds; static NonblockingFixedSPSCQueue< MixerCommand, COMMAND_QUEUE_SIZE > command_queue; 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++ ) { - if( ids[ i ] == sound_id ) { - *slot = i; - return true; +static PlayingSound * find_playing_sound( PlayingSoundID sound_id ) { + for( PlayingSound * ps : playing_sounds ) { + if( ps->id == sound_id ) { + return ps; } } - return false; -} - -// TODO: replace this with generic memory pool -static bool mixer_find_free_slot( size_t * free_slot ) { - u16 slot = first_free_sound; - u16 * prev = &first_free_sound; - - while( slot < MAX_CONCURRENT_SOUNDS ) { - PlayingSound & ps = playing_sounds[ slot ]; - if( !ps.playing ) { - *prev = ps.next_free; - *free_slot = slot; - return true; - } - - prev = &ps.next_free; - } - - return false; + return NULL; } static void mixer_stop_all_impl() { - first_free_sound = 0; - 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; - } + playing_sounds.clear(); } static void mixer_process_command( MixerCommand cmd ) { // TODO: most of these should make smooth adjustments to avoid popping switch( cmd.type ) { case MIXER_PLAY: { - size_t slot; - if( mixer_find_free_slot( &slot ) ) { - PlayingSound & ps = playing_sounds[ slot ]; - ps.data = cmd.sound_to_play; - ps.num_played_samples = cmd.start_pos; - ps.volume = 1.0f; - ps.loop = cmd.loop; - ps.playing = true; - ids[ slot ] = cmd.sound_id; + PlayingSound * ps = playing_sounds.acquire(); + if( ps != NULL ) { + ps->id = cmd.sound_id; + ps->data = cmd.sound_to_play; + ps->num_played_samples = cmd.start_pos; + ps->volume = 1.0f; + ps->loop = cmd.loop; } } break; case MIXER_VOLUME: { - size_t slot; - if( mixer_sound_id_to_slot( cmd.sound_id, &slot ) ) { - playing_sounds[ slot ].volume = cmd.volume; + PlayingSound * ps = find_playing_sound( cmd.sound_id ); + if( ps != NULL ) { + ps->volume = cmd.volume; } } break; case MIXER_STOP: { - size_t slot; - if( mixer_sound_id_to_slot( cmd.sound_id, &slot ) ) { - playing_sounds[ slot ].playing = false; - playing_sounds[ slot ].next_free = first_free_sound; - first_free_sound = slot; - } + playing_sounds.release( find_playing_sound( cmd.sound_id ) ); } break; case MIXER_STOP_ALL: { @@ -135,7 +100,7 @@ static void mixer_mix_sound( PlayingSound * ps, float * samples, size_t num_samp ps->num_played_samples %= ps->data.num_samples; } else if( ps->num_played_samples == checked_cast< s32 >( ps->data.num_samples ) ) { - ps->playing = false; + playing_sounds.release( ps ); } } @@ -158,11 +123,8 @@ AUDIO_OUTPUT_CALLBACK( mixer_mix ) { samples[ i ] = 0.0f; } - for( size_t i = 0; i < ARRAY_COUNT( playing_sounds ); i++ ) { - PlayingSound & ps = playing_sounds[ i ]; - if( ps.playing ) { - mixer_mix_sound( &ps, samples, num_output_samples ); - } + for( PlayingSound * ps : playing_sounds ) { + mixer_mix_sound( ps, samples, num_output_samples ); } for( s32 i = 0; i < num_output_samples; i++ ) {