implement repeating sound effects
authorhgn <hgodden00@gmail.com>
Mon, 25 Oct 2021 13:46:53 +0000 (14:46 +0100)
committerhgn <hgodden00@gmail.com>
Mon, 25 Oct 2021 13:46:53 +0000 (14:46 +0100)
16 files changed:
fishladder.c
fishladder_resources.h
sound/random_01.ogg [new file with mode: 0644]
sound/random_02.ogg [new file with mode: 0644]
sound/random_03.ogg [new file with mode: 0644]
sound/random_04.ogg [new file with mode: 0644]
sound/random_05.ogg [new file with mode: 0644]
sound/random_06.ogg [new file with mode: 0644]
sound/random_07.ogg [new file with mode: 0644]
sound/random_08.ogg [new file with mode: 0644]
sound/random_09.ogg [new file with mode: 0644]
sound/rolling_01.ogg [new file with mode: 0644]
sound/rolling_02.ogg [new file with mode: 0644]
sound/rolling_03.ogg [new file with mode: 0644]
sound/splitter_01.ogg [new file with mode: 0644]
vg/vg_audio.h

index dec99e2c266da40d93d327e18c01164bcdf7421c..ac1efcfc19045eec2175c81e72359e0385b43d3e 100644 (file)
@@ -616,6 +616,8 @@ void vg_update(void)
                {
                        vg_success( "Starting simulation!\n" );
                        
+                       sfx_set_playrnd( &audio_rolls, &audio_system_balls_rolling, 0, 1 );
+                       
                        world.simulating = 1;
                        world.num_fishes = 0;
                        world.sim_frame = 0;
@@ -755,7 +757,10 @@ void vg_update(void)
                                        fish->alive = 0;
                                else
                                        if( cell_entry->config == k_cell_type_split )
+                                       {
+                                               sfx_set_playrnd( &audio_splitter, &audio_system_balls_important, 0, 1 );
                                                cell_entry->state |= FLAG_FLIP_ROTATING;
+                                       }
                        }
                        
                        world.sim_frame ++;
index 841cf93ffb366d43a16aaa09f9e4d25290edb3f1..dcb24c763360386c82b6893a150d3b8d52cdde85 100644 (file)
@@ -35,6 +35,47 @@ sound/mod_06.ogg\0",
  .flags = 0
 };
 
+sfx_set audio_splitter =
+{
+ .sources = "\
+sound/splitter_01.ogg\0"
+};
+
+sfx_set audio_rolls = 
+{
+ .sources = "\
+sound/rolling_01.ogg\0\
+sound/rolling_02.ogg\0"
+};
+
+// One two or three layers of rolling noise
+sfx_system audio_system_balls_rolling =
+{
+       .vol = 1.f, .ch = 1, .vol_src = &audio_volume_sfx, 
+       .name = "Balls Rolling", .flags = SFX_FLAG_REPEAT
+};
+
+// Various oneshots
+sfx_system audio_system_balls_switching =
+{
+       .vol = 1.f, .ch = 1, .vol_src = &audio_volume_sfx, 
+       .name = "Balls Switching", .fadeout_length = FADEOUT_LENGTH
+};
+
+// Gameplay critical sounds eg. splitter sound rocking
+sfx_system audio_system_balls_important =
+{
+       .vol = 1.f, .ch = 1, .vol_src = &audio_volume_sfx, 
+       .name = "Balls Gameplay", .fadeout_length = FADEOUT_LENGTH
+};
+
+// Suplemental sounds
+sfx_system audio_system_balls_extra = 
+{
+       .vol = 1.f, .ch = 1, .vol_src = &audio_volume_sfx, 
+       .name = "Balls Extra", .fadeout_length = FADEOUT_LENGTH
+};
+
 static void resource_load_main(void)
 {
        // Textures
@@ -42,12 +83,16 @@ static void resource_load_main(void)
        
        // Audio
        sfx_set_init( &audio_tile_mod, NULL );
+       sfx_set_init( &audio_splitter, NULL );
+       sfx_set_init( &audio_rolls, NULL );
 }
 
 static void resource_free_main(void)
 {
        vg_tex2d_free( texture_list, vg_list_size( texture_list ) );    
        sfx_set_free( &audio_tile_mod );
+       sfx_set_free( &audio_splitter );
+       sfx_set_free( &audio_rolls );
 }
 
 // SHADERS
diff --git a/sound/random_01.ogg b/sound/random_01.ogg
new file mode 100644 (file)
index 0000000..193d24c
Binary files /dev/null and b/sound/random_01.ogg differ
diff --git a/sound/random_02.ogg b/sound/random_02.ogg
new file mode 100644 (file)
index 0000000..f1a46f4
Binary files /dev/null and b/sound/random_02.ogg differ
diff --git a/sound/random_03.ogg b/sound/random_03.ogg
new file mode 100644 (file)
index 0000000..07aee2f
Binary files /dev/null and b/sound/random_03.ogg differ
diff --git a/sound/random_04.ogg b/sound/random_04.ogg
new file mode 100644 (file)
index 0000000..307a714
Binary files /dev/null and b/sound/random_04.ogg differ
diff --git a/sound/random_05.ogg b/sound/random_05.ogg
new file mode 100644 (file)
index 0000000..dbdbc02
Binary files /dev/null and b/sound/random_05.ogg differ
diff --git a/sound/random_06.ogg b/sound/random_06.ogg
new file mode 100644 (file)
index 0000000..65b35c6
Binary files /dev/null and b/sound/random_06.ogg differ
diff --git a/sound/random_07.ogg b/sound/random_07.ogg
new file mode 100644 (file)
index 0000000..401dc25
Binary files /dev/null and b/sound/random_07.ogg differ
diff --git a/sound/random_08.ogg b/sound/random_08.ogg
new file mode 100644 (file)
index 0000000..c190b34
Binary files /dev/null and b/sound/random_08.ogg differ
diff --git a/sound/random_09.ogg b/sound/random_09.ogg
new file mode 100644 (file)
index 0000000..1b55884
Binary files /dev/null and b/sound/random_09.ogg differ
diff --git a/sound/rolling_01.ogg b/sound/rolling_01.ogg
new file mode 100644 (file)
index 0000000..b0f41ac
Binary files /dev/null and b/sound/rolling_01.ogg differ
diff --git a/sound/rolling_02.ogg b/sound/rolling_02.ogg
new file mode 100644 (file)
index 0000000..95c3b3d
Binary files /dev/null and b/sound/rolling_02.ogg differ
diff --git a/sound/rolling_03.ogg b/sound/rolling_03.ogg
new file mode 100644 (file)
index 0000000..c408d3e
Binary files /dev/null and b/sound/rolling_03.ogg differ
diff --git a/sound/splitter_01.ogg b/sound/splitter_01.ogg
new file mode 100644 (file)
index 0000000..2d524fd
Binary files /dev/null and b/sound/splitter_01.ogg differ
index a21a5d5aa292df3faae590c7ea1d7976a887d1a2..457ac13d63ea8dbdb4ca276c26bba46a661591e6 100644 (file)
@@ -6,11 +6,12 @@
 #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        16
+#define SFX_FLAG_ONESHOT       0x1
+#define SFX_FLAG_STEREO                0x2
+#define SFX_FLAG_REPEAT        0x4
+#define FADEOUT_LENGTH                 441
+#define FADEOUT_DIVISOR        (1.f/(float)FADEOUT_LENGTH)
 
 typedef struct sfx_vol_control sfx_vol_control;
 typedef struct sfx_system sfx_system;
@@ -31,7 +32,8 @@ struct sfx_system
        float   vol;
        
        // Info
-       int ch, end, cur, flags;
+       int ch, end, cur;
+       u32 flags;
        
        // Effects
        u32 fadeout, fadeout_length, fadeout_cursor;
@@ -448,42 +450,62 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput
                if( sys->fadeout_cursor != sys->cur )
                        sys->fadeout = sys->fadeout_length;
                
-               while( cursor < vg_min( sys->cur + frameCount, sys->end ) )
+               u32 frames_write = frameCount;
+               
+               while( frames_write )
                {
-                       audio_mixer_getsamples( pcf, sys->source, cursor, sys->ch );
-                       
-                       avgvol += fabs( pcf[0] * sys->vol );
-                       avgvol += fabs( pcf[1] * sys->vol );
+                       u32 samples_this_run = vg_min( frames_write, sys->end - cursor );
+               
+                       for( u32 j = 0; j < samples_this_run; j ++ )
+                       {
+                               audio_mixer_getsamples( pcf, sys->source, cursor, sys->ch );
+                               
+                               avgvol += fabs( pcf[0] * sys->vol );
+                               avgvol += fabs( pcf[1] * sys->vol );
+                               
+                               pOut32F[ bpos*2+0 ] += pcf[0] * sys->vol;
+                               pOut32F[ bpos*2+1 ] += pcf[1] * sys->vol;
+                               
+                               // Blend the fadeout cursor in to prevent popping
+                               if( sys->fadeout )
+                               {
+                                       if( sys->fadeout_cursor < sys->end )
+                                       {
+                                               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 --;
+                                       }
+                                       else
+                                               sys->fadeout = 0;
+                               }
+
+                               cursor ++;
+                               bpos ++;
+                       }
                        
-                       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;
+                       sys->fadeout_cursor = cursor;
+                       sys->signal_average = avgvol / (float)(bpos*2);
+                       
+                       break;
                }
-               
-               sys->signal_average = avgvol / (float)(bpos*2);
-               sys->cur += frameCount;
-               sys->fadeout_cursor = sys->cur;
        }
 
        // Redistribute sound systems
@@ -494,8 +516,8 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput
        {
                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) )