cross compile build script
[fishladder.git] / vg / vg_audio.h
index a21a5d5aa292df3faae590c7ea1d7976a887d1a2..e62d5845447e35f09c3e3b6536911a38ff2e3e42 100644 (file)
@@ -6,11 +6,13 @@
 #define STB_VORBIS_MAX_CHANNELS 2
 #include "stb/stb_vorbis.h"
 
-#define SFX_MAX_SYSTEMS 16
-#define SFX_FLAG_ONESHOT 0x1
-#define SFX_FLAG_STEREO         0x2
-#define FADEOUT_LENGTH 441
-#define FADEOUT_DIVISOR (1.f/(float)FADEOUT_LENGTH)
+#define SFX_MAX_SYSTEMS        32
+//#define SFX_FLAG_ONESHOT     0x1
+#define SFX_FLAG_STEREO                0x2
+#define SFX_FLAG_REPEAT        0x4
+#define SFX_FLAG_PERSISTENT 0x8
+#define FADEOUT_LENGTH                 4410
+#define FADEOUT_DIVISOR        (1.f/(float)FADEOUT_LENGTH)
 
 typedef struct sfx_vol_control sfx_vol_control;
 typedef struct sfx_system sfx_system;
@@ -23,23 +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
-       int ch, end, cur, flags;
+       u32 ch, end, cur;
+       u32 flags;
        
        // Effects
-       u32 fadeout, fadeout_length, fadeout_cursor;
-       
-       sfx_system *thread_clone; // Memory of this structure is copied into thread 2
+       u32 fadeout, fadeout_current;
        
        // Diagnostic
-       float   signal_average; // Current signal volume
        const char *name;
 };
 
@@ -50,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;
@@ -180,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
@@ -214,26 +220,39 @@ 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 )
+       
+       if( sfx_q_len >= SFX_MAX_SYSTEMS && !sys->in_queue )
        {
-               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;
        }
        
-       // Mark change in queue
-       sfx_q[ sfx_q_len ++ ] = sys;
+       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_push( sfx_system *sys )
+{
+       if( !sys->in_queue )
+       {
+               // Mark change in queue
+               sfx_q[ sfx_q_len ++ ] = sys;
+               sys->in_queue = 1;
+       }
        
        MUTEX_UNLOCK( sfx_mux_t01 );
        
@@ -317,27 +336,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
 // ======================================================
 
@@ -345,11 +349,7 @@ static void sfx_sys_chkerr(void)
 static sfx_system *sfx_alloc(void)
 {
        if( sfx_sys_len >= SFX_MAX_SYSTEMS )
-       {
-               vg_error( "Warning: No free space in sound system\n" );
-               
                return NULL;
-       }
        
        // A conditional is done against this in localization step,
        // Needs to be initialized.
@@ -382,133 +382,139 @@ 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];
+               sfx_system *clone;
                
-               // This is a 'new' sound if thread_clone not set.
-               if( !src->thread_clone || src->flags & SFX_FLAG_ONESHOT )
-               {
-                       src->thread_clone = sfx_alloc();
-               }
+               src->in_queue = 0;
                
-               // run replacement routine if one is waiting
-               if( src->replacement )
-               {
-                       free( src->source );
-                       
-                       src->source = src->replacement;
-                       src->replacement = NULL;
-               }
-
-               src->thread_clone->source = src->source;
+               // Copy
+               clone = sfx_alloc();
+               *clone = *src;
                
-               // Localize data to thread 1's memory pool
-               src->thread_clone->ch = src->ch;
-               src->thread_clone->end = src->end;
-               src->thread_clone->cur = src->cur;
-               src->thread_clone->flags = src->flags;
-               src->thread_clone->fadeout = src->fadeout;
-               src->thread_clone->fadeout_length = src->fadeout_length;
-               src->thread_clone->vol_src = src->vol_src;
-               src->thread_clone->name = src->name;
-
-               // loopback pointer, mainly used for persistent sound handles
-               src->thread_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;
-               u32 bpos = 0;
-               
-               float avgvol = 0.f;
+               u32 cursor = sys->cur, buffer_pos = 0;          
                float pcf[2] = { 0.f, 0.0f };
                
-               if( sys->fadeout_cursor != sys->cur )
-                       sys->fadeout = sys->fadeout_length;
+               u32 frames_write = frameCount;
+               float fadeout_divisor = 1.0f / (float)sys->fadeout;
                
-               while( cursor < vg_min( sys->cur + frameCount, sys->end ) )
+               while( frames_write )
                {
-                       audio_mixer_getsamples( pcf, sys->source, cursor, sys->ch );
+                       u32 samples_this_run = VG_MIN( frames_write, sys->end - cursor );
+               
+                       if( sys->fadeout )
+                       {
+                               // Force this system to be removed now
+                               if( sys->fadeout_current == 0 )
+                               {
+                                       sys->flags &= 0x00000000;
+                                       sys->cur = sys->end;
+                                       break;
+                               }
+                               
+                               samples_this_run = VG_MIN( samples_this_run, sys->fadeout_current );
+                       }
                        
-                       avgvol += fabs( pcf[0] * sys->vol );
-                       avgvol += fabs( pcf[1] * sys->vol );
+                       for( u32 j = 0; j < samples_this_run; j ++ )
+                       {
+                               audio_mixer_getsamples( pcf, sys->source, cursor, sys->ch );
+                               
+                               float vol = sys->vol;
+
+                               if( sys->fadeout )
+                               {
+                                       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;
+                               
+                               cursor ++;
+                               buffer_pos ++;
+                       }
                        
-                       pOut32F[ bpos*2+0 ] += pcf[0] * sys->vol;
-                       pOut32F[ bpos*2+1 ] += pcf[1] * sys->vol;
+                       frames_write -= samples_this_run;
                        
-                       // Blend the fadeout cursor in to prevent popping
-                       if( sys->fadeout )
+                       if( sys->flags & SFX_FLAG_REPEAT )
                        {
-                               if( sys->fadeout_cursor < sys->end )
+                               if( frames_write )
                                {
-                                       audio_mixer_getsamples( pcf, sys->source, sys->fadeout_cursor, sys->ch );
-                                       
-                                       float mul = (float)sys->fadeout * FADEOUT_DIVISOR;
-                                       
-                                       pOut32F[ bpos*2+0 ] += pcf[0] * sys->vol * mul;
-                                       pOut32F[ bpos*2+1 ] += pcf[1] * sys->vol * mul;
-                                       
-                                       sys->fadeout_cursor ++;
-                                       sys->fadeout --;
+                                       cursor = 0;
+                                       continue;
                                }
-                               else
-                                       sys->fadeout = 0;
                        }
 
-                       cursor ++;
-                       bpos ++;
+                       sys->cur = cursor;
+                       break;
                }
-               
-               sys->signal_average = avgvol / (float)(bpos*2);
-               sys->cur += frameCount;
-               sys->fadeout_cursor = sys->cur;
        }
 
        // 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
-               if( src->cur < src->end ) 
+               
+               // 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) )
-                               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) )
-                               src->thread_clone->thread_clone = NULL;
-               }
                
                idx ++ ;
        }
@@ -521,10 +527,8 @@ 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" );
-
        dest->ch = (flags & SFX_FLAG_STEREO)? 2: 1;
        
        dest->main = NULL;
@@ -557,8 +561,6 @@ void sfx_set_strings( sfx_set *dest, char *strSources, u32 flags, int bAsync )
                        
                        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
                {
@@ -570,49 +572,66 @@ void sfx_set_strings( sfx_set *dest, char *strSources, u32 flags, int bAsync )
                source += len +1;
                dest->numsegments ++;
        }
-       
-       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 );
+}
+
+static void sfx_set_play( sfx_set *source, sfx_system *sys, int id )
+{
+       if( sfx_begin_edit( sys ) )
+       {
+               sys->fadeout = 0;
+               sys->fadeout_current = 0;
+               sys->source = source->main;
+               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_push( sys );
        }
 }
 
 // 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;
+
+       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;
-
-       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 );
+       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;
+               
+               sfx_end_edit( sys );
+       }
 }
 
 // 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 );
 }