X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=vg%2Fvg_audio.h;h=e62d5845447e35f09c3e3b6536911a38ff2e3e42;hb=3363633178b1eea582304742ad1202487af0feb1;hp=249bedc8f390452ba9a98adab4d6cbaecee7d535;hpb=026b67150e9bd238709a6aeab656f7d01f3765a8;p=fishladder.git diff --git a/vg/vg_audio.h b/vg/vg_audio.h index 249bedc..e62d584 100644 --- a/vg/vg_audio.h +++ b/vg/vg_audio.h @@ -7,10 +7,10 @@ #include "stb/stb_vorbis.h" #define SFX_MAX_SYSTEMS 32 -#define SFX_FLAG_ONESHOT 0x1 +//#define SFX_FLAG_ONESHOT 0x1 #define SFX_FLAG_STEREO 0x2 #define SFX_FLAG_REPEAT 0x4 -#define SFX_FLAG_GHOST 0x8 +#define SFX_FLAG_PERSISTENT 0x8 #define FADEOUT_LENGTH 4410 #define FADEOUT_DIVISOR (1.f/(float)FADEOUT_LENGTH) @@ -25,26 +25,26 @@ struct sfx_vol_control struct sfx_system { + sfx_system *persisitent_source; + int in_queue; + // Source buffer start float *source, *replacement; + u32 clip_start, clip_end, buffer_length; + // Modifiers sfx_vol_control *vol_src; - float vol; + float vol, cvol; // Info u32 ch, end, cur; u32 flags; - int is_queued; - // Effects u32 fadeout, fadeout_current; - sfx_system *thread_clone; // Memory of this structure is copied into thread 2 - // Diagnostic - float signal_average; // Current signal volume const char *name; }; @@ -55,7 +55,7 @@ struct sfx_set float *main; char *sources; - u32 segments[16]; //from->to,from->to ... + u32 segments[32]; //from->to,from->to ... u32 numsegments; u32 ch; u32 flags; @@ -229,32 +229,31 @@ static int sfx_begin_edit( sfx_system *sys ) { MUTEX_LOCK( sfx_mux_t01 ); - if( sys->is_queued ) + if( sfx_q_len >= SFX_MAX_SYSTEMS && !sys->in_queue ) { 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; } +static void sfx_end_edit( sfx_system *sys ) +{ + MUTEX_UNLOCK( sfx_mux_t01 ); +} + // Mark change to be uploaded to queue system -static int sfx_save( sfx_system *sys ) +static int sfx_push( sfx_system *sys ) { - if( sfx_q_len >= SFX_MAX_SYSTEMS ) + if( !sys->in_queue ) { - 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; + sys->in_queue = 1; } - // Mark change in queue - sfx_q[ sfx_q_len ++ ] = sys; - MUTEX_UNLOCK( sfx_mux_t01 ); return 1; @@ -383,95 +382,64 @@ 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. - if( !src->thread_clone || (src->flags & SFX_FLAG_ONESHOT) ) - { - clone = sfx_alloc(); - if( !clone ) - break; - - src->thread_clone = clone; - } - else - { - clone = src->thread_clone; + src->in_queue = 0; - // 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; - } - } + // Copy + clone = sfx_alloc(); + *clone = *src; - // run replacement routine if one is waiting (todo: what is this?) - if( src->replacement ) - { - free( src->source ); - - src->source = src->replacement; - src->replacement = NULL; - } - - // Localize data to thread 1's memory pool - clone->source = src->source; - clone->ch = src->ch; - clone->end = src->end; - clone->cur = src->cur; - clone->flags = src->flags; - clone->vol_src = src->vol_src; - clone->name = src->name; - clone->fadeout = src->fadeout; - clone->fadeout_current = src->fadeout_current; - - // loopback pointer, mainly used for persistent sound handles - clone->thread_clone = src; + // Links need to exist on persistent sounds + clone->persisitent_source = src->flags & SFX_FLAG_PERSISTENT? src: NULL; } sfx_q_len = 0; - // Pull in volume sliders + // Volume modifiers for( int i = 0; i < sfx_sys_len; i ++ ) { sfx_system *sys = sfx_sys + i; - sys->vol = sys->thread_clone->vol * g_master_volume; + // Apply persistent volume if linked + if( sys->flags & SFX_FLAG_PERSISTENT ) + { + sys->vol = sys->persisitent_source->vol * g_master_volume; + + // Persistent triggers + // ------------------- + + // Fadeout effect ( + remove ) + if( sys->persisitent_source->fadeout ) + { + sys->fadeout_current = sys->persisitent_source->fadeout_current; + sys->fadeout = sys->persisitent_source->fadeout; + + sys->persisitent_source = NULL; + sys->flags &= ~SFX_FLAG_PERSISTENT; + } + } + + // Apply volume slider if it has one linked if( sys->vol_src ) - sys->vol *= sys->vol_src->val; + sys->cvol = sys->vol * sys->vol_src->val; + else + sys->cvol = sys->vol; } MUTEX_UNLOCK( sfx_mux_t01 ); - // Clear buffer ( is necessary ? ) + // Clear buffer float *pOut32F = (float *)pOutBuf; for( int i = 0; i < frameCount * 2; i ++ ){ pOut32F[i] = 0.f; } - // Do something with local.. for( int i = 0; i < sfx_sys_len; i ++ ) { sfx_system *sys = sfx_sys + i; u32 cursor = sys->cur, buffer_pos = 0; - float avgvol = 0.f; float pcf[2] = { 0.f, 0.0f }; u32 frames_write = frameCount; @@ -483,10 +451,10 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput if( sys->fadeout ) { - // Force this system to be removed + // Force this system to be removed now if( sys->fadeout_current == 0 ) { - sys->flags = SFX_FLAG_GHOST; + sys->flags &= 0x00000000; sys->cur = sys->end; break; } @@ -514,9 +482,6 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput 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 ); - cursor ++; buffer_pos ++; } @@ -533,8 +498,6 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput } sys->cur = cursor; - sys->signal_average = avgvol / (float)(buffer_pos*2); - break; } } @@ -550,20 +513,8 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput // 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|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|SFX_FLAG_GHOST)) ) - src->thread_clone->thread_clone = NULL; - } idx ++ ; } @@ -578,8 +529,6 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput // String layout: "sounda.ogg\0soundb.ogg\0soundc.ogg\0\0" static void sfx_set_strings( sfx_set *dest, char *strSources, u32 flags, int bAsync ) { - printf( "Init sfx set\n| start | end | length | name \n" ); - dest->ch = (flags & SFX_FLAG_STEREO)? 2: 1; dest->main = NULL; @@ -612,8 +561,6 @@ static void sfx_set_strings( sfx_set *dest, char *strSources, u32 flags, int bAs dest->segments[ dest->numsegments*2+0 ] = total-samples; dest->segments[ dest->numsegments*2+1 ] = total; - - printf( "| %09u | %09u | %09u | %s\n", total-samples, total, samples, source ); } else { @@ -625,8 +572,6 @@ static void sfx_set_strings( sfx_set *dest, char *strSources, u32 flags, int bAs source += len +1; dest->numsegments ++; } - - vg_info( "finished, numsegments: %u\n", dest->numsegments ); } static void sfx_set_init( sfx_set *dest, char *sources ) @@ -637,26 +582,43 @@ 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->fadeout_current = 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; - sfx_save( sys ); + // Diagnostics + sys->clip_start = sys->cur; + sys->clip_end = sys->end; + sys->buffer_length = source->segments[ (source->numsegments-1)*2 + 1 ]; + + sfx_push( 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 ) ) @@ -664,10 +626,7 @@ static void sfx_system_fadeout( sfx_system *sys, u32 length_samples ) sys->fadeout_current = length_samples; sys->fadeout = length_samples; - if( sys->thread_clone ) - sys->cur = sys->thread_clone->cur; - - sfx_save( sys ); + sfx_end_edit( sys ); } }