X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=vg%2Fvg_audio.h;h=aefc0fc81cd0693688a125304877a15ae354be39;hb=5293bf2e9c4f30d5b01f1e1638c00509c1674768;hp=249bedc8f390452ba9a98adab4d6cbaecee7d535;hpb=026b67150e9bd238709a6aeab656f7d01f3765a8;p=fishladder.git diff --git a/vg/vg_audio.h b/vg/vg_audio.h index 249bedc..aefc0fc 100644 --- a/vg/vg_audio.h +++ b/vg/vg_audio.h @@ -28,6 +28,8 @@ struct sfx_system // Source buffer start float *source, *replacement; + u32 clip_start, clip_end, buffer_length; + // Modifiers sfx_vol_control *vol_src; float vol; @@ -36,8 +38,6 @@ struct sfx_system u32 ch, end, cur; u32 flags; - int is_queued; - // Effects u32 fadeout, fadeout_current; @@ -229,29 +229,19 @@ static int sfx_begin_edit( sfx_system *sys ) { MUTEX_LOCK( sfx_mux_t01 ); - if( sys->is_queued ) + if( sfx_q_len >= SFX_MAX_SYSTEMS ) { MUTEX_UNLOCK( sfx_mux_t01 ); - - vg_warn( "Sfx system locked for writing.. Spam is being created!\n" ); + vg_warn( "Warning: No free space in sound queue\n" ); return 0; } - sys->is_queued = 1; return 1; } // Mark change to be uploaded to queue system static int sfx_save( sfx_system *sys ) { - if( sfx_q_len >= SFX_MAX_SYSTEMS ) - { - vg_error( "Warning: No free space in sound queue\n" ); - - MUTEX_UNLOCK( sfx_mux_t01 ); - return 0; - } - // Mark change in queue sfx_q[ sfx_q_len ++ ] = sys; @@ -383,8 +373,6 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput while( sfx_q_len --> 0 ) { sfx_system *src = sfx_q[sfx_q_len]; - src->is_queued = 0; - sfx_system *clone; // This is a 'new' sound if thread_clone not set. @@ -637,37 +625,61 @@ static void sfx_set_init( sfx_set *dest, char *sources ) sfx_set_strings( dest, sources, dest->flags, 0 ); } -// Pick a random sound from the buffer and play it into system -static void sfx_set_playrnd( sfx_set *source, sfx_system *sys, int min_id, int max_id ) +static void sfx_set_play( sfx_set *source, sfx_system *sys, int id ) { - if( !source->numsegments ) - return; - - int pick = (rand() % (max_id-min_id)) + min_id; - if( sfx_begin_edit( sys ) ) { sys->fadeout = 0; sys->source = source->main; - sys->cur = source->segments[ pick*2 + 0 ]; - sys->end = source->segments[ pick*2 + 1 ]; + sys->cur = source->segments[ id*2 + 0 ]; + sys->end = source->segments[ id*2 + 1 ]; sys->ch = source->ch; + // Diagnostics + sys->clip_start = sys->cur; + sys->clip_end = sys->end; + sys->buffer_length = source->segments[ (source->numsegments-1)*2 + 1 ]; + sfx_save( sys ); } } +// Pick a random sound from the buffer and play it into system +static void sfx_set_playrnd( sfx_set *source, sfx_system *sys, int min_id, int max_id ) +{ + if( !source->numsegments ) + return; + + if( max_id > source->numsegments ) + { + vg_error( "Max ID out of range for playrnd\n" ); + return; + } + + int pick = (rand() % (max_id-min_id)) + min_id; + + sfx_set_play( source, sys, pick ); +} + static void sfx_system_fadeout( sfx_system *sys, u32 length_samples ) { if( sfx_begin_edit( sys ) ) { - sys->fadeout_current = length_samples; - sys->fadeout = length_samples; + if( sys->end ) + { + sys->fadeout_current = length_samples; + sys->fadeout = length_samples; + + if( sys->thread_clone ) + sys->cur = sys->thread_clone->cur; - if( sys->thread_clone ) - sys->cur = sys->thread_clone->cur; - - sfx_save( sys ); + sfx_save( sys ); + } + else + { + // Sound was not initialized + MUTEX_UNLOCK( sfx_mux_t01 ); + } } }