X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=vg%2Fvg_audio.h;h=0c9a666691e64f62f4926041d41f4fc0679e33cf;hb=284582f279c0241552d5b989b3207a259baf6046;hp=2191385f438bbf77d017c6d3de115ae59c2da588;hpb=d94e286c52dcfd5faf7cbd12690268f9d68957ef;p=fishladder.git diff --git a/vg/vg_audio.h b/vg/vg_audio.h index 2191385..0c9a666 100644 --- a/vg/vg_audio.h +++ b/vg/vg_audio.h @@ -11,7 +11,7 @@ #define SFX_FLAG_STEREO 0x2 #define SFX_FLAG_REPEAT 0x4 #define SFX_FLAG_GHOST 0x8 -#define FADEOUT_LENGTH 441 +#define FADEOUT_LENGTH 4410 #define FADEOUT_DIVISOR (1.f/(float)FADEOUT_LENGTH) typedef struct sfx_vol_control sfx_vol_control; @@ -28,12 +28,14 @@ struct sfx_system // Source buffer start float *source, *replacement; + u32 clip_start, clip_end, buffer_length; + // Modifiers sfx_vol_control *vol_src; float vol; // Info - int ch, end, cur, cur_lagged; + u32 ch, end, cur; u32 flags; // Effects @@ -183,6 +185,7 @@ struct sfx_vorbis_a_to_inf #define SFX_A_FLAG_AUTOSTART 0x1 #define SFX_A_FLAG_AUTOFREE 0x2 +/* static int sfx_save( sfx_system *sys ); // Asynchronous load-to-system callback @@ -217,24 +220,28 @@ void sfx_vorbis_a_to( sfx_system *sys, const char *strFileName, int channels, u3 if( !sfx_vorbis_a( strFileName, channels, sfx_vorbis_a_to_c, inf ) ) free( inf ); -} +}*/ // 0 // ====================================================== -// Mark change to be uploaded to queue system -static int sfx_save( sfx_system *sys ) +static int sfx_begin_edit( sfx_system *sys ) { MUTEX_LOCK( sfx_mux_t01 ); - + if( sfx_q_len >= SFX_MAX_SYSTEMS ) { - vg_error( "Warning: No free space in sound queue\n" ); - MUTEX_UNLOCK( sfx_mux_t01 ); + vg_warn( "Warning: No free space in sound queue\n" ); return 0; } + return 1; +} + +// Mark change to be uploaded to queue system +static int sfx_save( sfx_system *sys ) +{ // Mark change in queue sfx_q[ sfx_q_len ++ ] = sys; @@ -320,27 +327,12 @@ static void vg_audio_init(void) } } -#ifndef VYGER_RELEASE -u32 num_sfx_sets = 0; -#endif - // Shutdown audio device static void vg_audio_free(void) { ma_device_uninit( &g_aud_device ); } -// (debug) make sure we are shutting down safely -static void sfx_sys_chkerr(void) -{ -#ifndef VYGER_RELEASE - if( num_sfx_sets ) - { - vg_error( "Leaked %u sfx sets\n", num_sfx_sets ); - } -#endif -} - // 1 // ====================================================== @@ -348,9 +340,7 @@ static void sfx_sys_chkerr(void) static sfx_system *sfx_alloc(void) { if( sfx_sys_len >= SFX_MAX_SYSTEMS ) - { return NULL; - } // A conditional is done against this in localization step, // Needs to be initialized. @@ -386,33 +376,38 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput sfx_system *clone; // This is a 'new' sound if thread_clone not set. - if( !src->thread_clone || src->flags & SFX_FLAG_ONESHOT ) + if( !src->thread_clone || (src->flags & SFX_FLAG_ONESHOT) ) { - src->thread_clone = sfx_alloc(); - if( !src->thread_clone ) + clone = sfx_alloc(); + if( !clone ) break; + + src->thread_clone = clone; } else { - // Modifying an active system spawns a small fadeout ghost system - sfx_system *ghost_system = sfx_alloc(); - - if( !ghost_system ) - break; - - ghost_system->source = src->source; - ghost_system->ch = src->ch; - ghost_system->end = src->end; - ghost_system->cur = src->cur_lagged; - ghost_system->flags = SFX_FLAG_GHOST; - ghost_system->fadeout = FADEOUT_LENGTH; - ghost_system->fadeout_current = FADEOUT_LENGTH; - ghost_system->vol_src = src->vol_src; - ghost_system->name = src->name; - ghost_system->thread_clone = src; - } + clone = src->thread_clone; - clone = src->thread_clone; + // Modifying an active system's cursor spawns a small fadeout ghost system + if( clone->cur != src->cur ) + { + sfx_system *ghost_system = sfx_alloc(); + + if( !ghost_system ) + break; + + ghost_system->source = clone->source; + ghost_system->ch = clone->ch; + ghost_system->end = clone->end; + ghost_system->cur = clone->cur; + ghost_system->flags = SFX_FLAG_GHOST; + ghost_system->fadeout = FADEOUT_LENGTH; + ghost_system->fadeout_current = FADEOUT_LENGTH; + ghost_system->vol_src = clone->vol_src; + ghost_system->name = clone->name; + ghost_system->thread_clone = src; + } + } // run replacement routine if one is waiting (todo: what is this?) if( src->replacement ) @@ -463,8 +458,7 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput { sfx_system *sys = sfx_sys + i; - u32 cursor = sys->cur, buffer_pos = 0; - + u32 cursor = sys->cur, buffer_pos = 0; float avgvol = 0.f; float pcf[2] = { 0.f, 0.0f }; @@ -473,7 +467,7 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput while( frames_write ) { - u32 samples_this_run = vg_min( frames_write, sys->end - cursor ); + u32 samples_this_run = VG_MIN( frames_write, sys->end - cursor ); if( sys->fadeout ) { @@ -485,7 +479,7 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput break; } - samples_this_run = vg_min( samples_this_run, sys->fadeout_current ); + samples_this_run = VG_MIN( samples_this_run, sys->fadeout_current ); } for( u32 j = 0; j < samples_this_run; j ++ ) @@ -499,10 +493,15 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput vol *= (float)sys->fadeout_current * fadeout_divisor; sys->fadeout_current --; } + + if( buffer_pos >= frameCount ) + { + break; + } pOut32F[ buffer_pos*2+0 ] += pcf[0] * vol; pOut32F[ buffer_pos*2+1 ] += pcf[1] * vol; - + avgvol += fabs( pcf[0] * vol ); avgvol += fabs( pcf[1] * vol ); @@ -522,7 +521,6 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput } sys->cur = cursor; - sys->cur_lagged = cursor; sys->signal_average = avgvol / (float)(buffer_pos*2); break; @@ -531,25 +529,27 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput // Redistribute sound systems MUTEX_LOCK( sfx_mux_t01 ); - - unsigned int idx = 0, wr = 0; + + u32 idx = 0, wr = 0; while( idx != sfx_sys_len ) { sfx_system *src = sfx_sys + idx; - + // Keep only if cursor is before end or repeating if( src->cur < src->end || (src->flags & SFX_FLAG_REPEAT) ) { // Correct source pointer on persisitent originals since we shifted ID's - if( !(src->flags & SFX_FLAG_ONESHOT) ) + if( !(src->flags & (SFX_FLAG_ONESHOT|SFX_FLAG_GHOST)) ) + { src->thread_clone->thread_clone = sfx_sys + wr; + } sfx_sys[ wr ++ ] = sfx_sys[ idx ]; } else { // Clear link on persistent sources (done playing) - if( !(src->flags & SFX_FLAG_ONESHOT) ) + if( !(src->flags & (SFX_FLAG_ONESHOT|SFX_FLAG_GHOST)) ) src->thread_clone->thread_clone = NULL; } @@ -564,7 +564,7 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput // Load strings into sfx_set's memory // String layout: "sounda.ogg\0soundb.ogg\0soundc.ogg\0\0" -void sfx_set_strings( sfx_set *dest, char *strSources, u32 flags, int bAsync ) +static void sfx_set_strings( sfx_set *dest, char *strSources, u32 flags, int bAsync ) { printf( "Init sfx set\n| start | end | length | name \n" ); @@ -617,54 +617,63 @@ void sfx_set_strings( sfx_set *dest, char *strSources, u32 flags, int bAsync ) vg_info( "finished, numsegments: %u\n", dest->numsegments ); } -void sfx_set_init( sfx_set *dest, char *sources ) +static void sfx_set_init( sfx_set *dest, char *sources ) { -#ifndef VYGER_RELEASE - num_sfx_sets ++; -#endif - if( !sources ) - { sfx_set_strings( dest, dest->sources, dest->flags, 0 ); - } else - { sfx_set_strings( dest, sources, dest->flags, 0 ); - } } // Pick a random sound from the buffer and play it into system -void sfx_set_playrnd( sfx_set *source, sfx_system *sys, int min_id, int max_id ) +static void sfx_set_playrnd( sfx_set *source, sfx_system *sys, int min_id, int max_id ) { if( !source->numsegments ) - { return; - } int pick = (rand() % (max_id-min_id)) + min_id; - sys->fadeout = 0; - sys->source = source->main; - sys->cur = source->segments[ pick*2 + 0 ]; - sys->end = source->segments[ pick*2 + 1 ]; - sys->ch = source->ch; - - sfx_save( sys ); + 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->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 ); + } } static void sfx_system_fadeout( sfx_system *sys, u32 length_samples ) { - sys->fadeout_current = length_samples; - sys->fadeout = length_samples; - - sfx_save( sys ); + if( sfx_begin_edit( sys ) ) + { + if( sys->end ) + { + sys->fadeout_current = length_samples; + sys->fadeout = length_samples; + + if( sys->thread_clone ) + sys->cur = sys->thread_clone->cur; + + sfx_save( sys ); + } + else + { + // Sound was not initialized + MUTEX_UNLOCK( sfx_mux_t01 ); + } + } } // Free set resources -void sfx_set_free( sfx_set *set ) +static void sfx_set_free( sfx_set *set ) { -#ifndef VYGER_RELEASE - num_sfx_sets --; -#endif free( set->main ); }