X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=vg_audio.h;h=fc09d103aa5338167100acdd0738679d656e3aba;hb=80c63a082132aa37bf0578d4d6757880db748a79;hp=46c9e7331a90f2c4ec5534eec9754699a3c5e404;hpb=4c48fe01a5d1983be89b7dce6f08e6b708cfbb05;p=vg.git diff --git a/vg_audio.h b/vg_audio.h index 46c9e73..fc09d10 100644 --- a/vg_audio.h +++ b/vg_audio.h @@ -3,6 +3,8 @@ #ifndef VG_AUDIO_H #define VG_AUDIO_H +#define VG_GAME + #include "vg/vg.h" #include "vg/vg_stdint.h" #include "vg/vg_platform.h" @@ -11,8 +13,10 @@ #include "vg/vg_ui.h" #include "vg/vg_console.h" #include "vg/vg_store.h" +#include "vg/vg_profiler.h" #include +#include #ifdef __GNUC__ #ifndef __clang__ @@ -149,8 +153,13 @@ static struct vg_audio_system v3f listener_pos, listener_ears; + + float volume, + volume_target, + volume_target_internal, + volume_console; } -vg_audio; +vg_audio = { .volume_console = 1.0f }; static struct vg_profile _vg_prof_audio_decode = {.mode = k_profile_mode_accum, @@ -216,6 +225,14 @@ VG_STATIC void vg_audio_init(void) .persistent = 1 }); + vg_convar_push( (struct vg_convar){ + .name = "volume", + .data = &vg_audio.volume_console, + .data_type = k_convar_dtype_f32, + .opt_f32 = { .min=0.0f, .max=2.0f, .clamp=1 }, + .persistent = 1 + }); + /* allocate memory */ /* 32mb fixed */ @@ -267,7 +284,7 @@ VG_STATIC void vg_audio_init(void) vg_success( "Ready\n" ); } -VG_STATIC void vg_audio_free(void * nothing) +VG_STATIC void vg_audio_free(void) { SDL_CloseAudioDevice( vg_audio.sdl_output_device ); } @@ -378,6 +395,8 @@ VG_STATIC void audio_system_enque(void) { /* Process incoming sound queue */ audio_lock(); + + vg_audio.volume_target_internal = vg_audio.volume_target; int wr = 0; for( int i=0; iinfo.vol < 0.01f ) { @@ -479,6 +499,19 @@ VG_STATIC void audio_entity_spacialize( audio_entity *ent, float *vol, float *pa return; } + if( !vg_validf(vg_audio.listener_pos[0]) || + !vg_validf(vg_audio.listener_pos[1]) || + !vg_validf(vg_audio.listener_pos[2]) || + !vg_validf(ent->info.world_position[0]) || + !vg_validf(ent->info.world_position[1]) || + !vg_validf(ent->info.world_position[2]) ) + { + vg_error( "NaN listener/world position (%s)\n", ent->name ); + *vol = 0.0f; + *pan = 0.0f; + return; + } + v3f delta; v3_sub( ent->info.world_position, vg_audio.listener_pos, delta ); @@ -676,8 +709,7 @@ VG_STATIC void audio_entity_mix( aatree_ptr id, float *buffer, for( u32 j=0; jfadeout ) { /* Force this system to be removed now */ @@ -715,21 +747,32 @@ VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int byte_count ) for( int i=0; iactive ) - { audio_entity_mix( i, pOut32F, frame_count ); - } } + float vol_diff = vg_audio.volume_target_internal - vg_audio.volume, + vol_rate = 1.0f / (44100.0f*0.25f), + vol_chg = frame_count * vol_rate; + + if( vol_chg > fabsf( vol_diff ) ) + vg_audio.volume = vg_audio.volume_target_internal; + else + vg_audio.volume += vg_signf( vol_diff ) * vol_chg; + /* redistribute */ audio_system_cleanup(); audio_lock(); + vg_profile_increment( &_vg_prof_audio_decode ); vg_profile_increment( &_vg_prof_audio_mix ); @@ -837,6 +880,7 @@ VG_STATIC void audio_require_init( audio_player *player ) if( player->init ) return; + audio_unlock(); vg_fatal_exit_loop( "Must init audio player before playing! \n" ); } @@ -845,6 +889,7 @@ VG_STATIC void audio_require_clip_loaded( audio_clip *clip ) if( clip->data && clip->size ) return; + audio_unlock(); vg_fatal_exit_loop( "Must load audio clip before playing! \n" ); } @@ -926,6 +971,19 @@ VG_STATIC void audio_player_set_position( audio_player *sys, v3f pos ) VG_STATIC void audio_player_set_vol( audio_player *sys, float vol ) { audio_require_lock(); + + if( !vg_validf(vol) ) + { + vg_warn( "NaN volume (%s)\n", sys->name ); + vol = 0.0f; + } + + if( (vol < 0.0f) || (vol > 100.0f) ) + { + vg_warn( "Invalid volume (%s: %f)\n", sys->name, vol ); + vol = 0.0f; + } + sys->info.vol = vol; } @@ -959,6 +1017,18 @@ VG_STATIC u32 audio_player_get_flags( audio_player *sys ) return sys->info.flags; } +VG_STATIC void audio_set_master_vol( float vol ) +{ + audio_require_lock(); + vg_audio.volume_target = vol; +} + +VG_STATIC void audio_push_console_vol(void) +{ + audio_lock(); + audio_set_master_vol( vg_audio.volume_console ); + audio_unlock(); +} /* * Debugging @@ -1064,7 +1134,7 @@ VG_STATIC void audio_debug_ui( m4x4f mtx_pv ) wpos[3] = 1.0f; m4x4_mulv( mtx_pv, wpos, wpos ); - if( wpos[3] < 0.0f ) + if( wpos[3] <= 0.0f ) goto projected_behind; v2_muls( wpos, (1.0f/wpos[3]) * 0.5f, wpos );