stuff
authorhgn <hgodden00@gmail.com>
Sat, 24 Sep 2022 00:31:21 +0000 (01:31 +0100)
committerhgn <hgodden00@gmail.com>
Sat, 24 Sep 2022 00:31:21 +0000 (01:31 +0100)
28 files changed:
audio.h
blender_export.py
common.h
main.c
player.h
sound_src/bird0.ogg [new file with mode: 0644]
sound_src/bird1.ogg [new file with mode: 0644]
sound_src/bird2.ogg [new file with mode: 0644]
sound_src/docks_generic.ogg [new file with mode: 0644]
sound_src/field_generic.ogg [new file with mode: 0644]
sound_src/grass0.ogg [new file with mode: 0644]
sound_src/grass1.ogg [new file with mode: 0644]
sound_src/grass2.ogg [new file with mode: 0644]
sound_src/grass3.ogg [new file with mode: 0644]
sound_src/grobplab.ogg [new file with mode: 0644]
sound_src/leaves0.ogg [new file with mode: 0644]
sound_src/leaves1.ogg [new file with mode: 0644]
sound_src/leaves2.ogg [new file with mode: 0644]
sound_src/leaves3.ogg [new file with mode: 0644]
sound_src/town_generic.ogg [new file with mode: 0644]
sound_src/wave0.ogg [new file with mode: 0644]
sound_src/wave1.ogg [new file with mode: 0644]
sound_src/wave2.ogg [new file with mode: 0644]
sound_src/wave3.ogg [new file with mode: 0644]
sound_src/wave4.ogg [new file with mode: 0644]
sound_src/wave5.ogg [new file with mode: 0644]
world.h
world_audio.h [new file with mode: 0644]

diff --git a/audio.h b/audio.h
index 0572d6dcf52b623fc7a3f313c370e0d70e0f4d7c..212ed9f19d8231a13e004122d5c5b7a39d8ae994 100644 (file)
--- a/audio.h
+++ b/audio.h
@@ -1,7 +1,10 @@
+#include "common.h"
+static void audio_spacialize( sfx_system *sys,
+                              v3f pos, v3f camera, v3f ears, float vol );
+
 #ifndef AUDIO_H
 #define AUDIO_H
 
-#include "common.h"
 #include "world.h"
 
 static float audio_occlusion_current = 0.0f,
@@ -9,8 +12,6 @@ static float audio_occlusion_current = 0.0f,
 
 static int   k_audio_debug_soundscape = 0;
 
-sfx_vol_control audio_vol_all = { .val = 1.0f, .name = "All" };
-
 sfx_set audio_board =
 {
    .sources = "sound/skate.ogg\0"
@@ -85,6 +86,21 @@ static void audio_free(void)
    sfx_set_free( &audio_board );
 }
 
+static void audio_spacialize( sfx_system *sys,
+                              v3f pos, v3f camera, v3f ears, float vol )
+{
+   float attn = (v3_dist( pos, camera ) / vol) +1.0f;
+
+   v3f delta;
+   v3_sub( pos, camera, delta );
+   v3_normalize( delta );
+   
+   float pan = v3_dot( ears, delta );
+
+   sys->vol = 1.0f/(attn*attn);
+   sys->pan = pan;
+}
+
 static void audio_sample_occlusion( v3f origin )
 {
    float d = 0.0f,
@@ -95,14 +111,7 @@ static void audio_sample_occlusion( v3f origin )
    for( int i=0; i<sample_count; i++ )
    {
       v3f dir;
-      dir[0] = vg_randf();
-      dir[1] = vg_randf();
-      dir[2] = vg_randf();
-
-      v3_muls( dir, 2.0f, dir );
-      v3_sub( dir, (v3f){1.0f,1.0f,1.0f}, dir );
-
-      v3_normalize( dir );
+      vg_rand_dir( dir );
 
       ray_hit contact;
       contact.dist = 15.0f;
@@ -137,7 +146,7 @@ static void audio_debug_soundscapes(void)
    char buf[64];
    snprintf( buf, 31, "occlusion: %.5f", audio_occlusion_current );
 
-   ui_global_ctx.cursor[0] = 10;
+   ui_global_ctx.cursor[0] = 250;
    ui_global_ctx.cursor[1] = 10;
    ui_global_ctx.cursor[2] = audio_occlusion_current * 200.0f;
    ui_global_ctx.cursor[3] = 20;
index 05d1ec06aa5482a1ff35a40ac5cd5771dfca55a0..a8061e4ed9241baa12cd77c379284a94fcb9339e 100644 (file)
@@ -166,7 +166,8 @@ class subclass_audio_channel(Structure):
 
 class classtype_audio_system(Structure):
    _pack_ = 1
-   _fields_ = [("")]
+   _fields_ = [("sounds", subclass_audio_channel * 4),
+               ("len", c_uint32)]
 
 class classtype_audio_zone(Structure):
    _pack_ = 1
index 587c31b551011c3e60f2b1d100bbfdb72b20a8a3..a9197a4a9a0bf65fa75c991d5170092841c3045c 100644 (file)
--- a/common.h
+++ b/common.h
@@ -47,4 +47,7 @@ static void eval_bezier_time( v3f p0, v3f p1, v3f h0, v3f h1, float t, v3f p )
    v3_muladds( p, p0, 3.0f*tt  -ttt -3.0f*t +1.0f, p );
 }
 
+/* TODO: he needs a home somewhere */
+sfx_vol_control audio_vol_all = { .val = 1.0f, .name = "All" };
+
 #endif /* COMMON_H */
diff --git a/main.c b/main.c
index 266e080803cc4b50190e795b33eb39854151d0cd..c306aa50f1292a0701fae233776fe14b7a4a67ee 100644 (file)
--- a/main.c
+++ b/main.c
@@ -502,6 +502,7 @@ void vg_ui(void)
    //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
    
    audio_debug_soundscapes();
+   world_audio_debug();
 
 #if 0
    static double last_b_press = 0.0;
index 8619308a0cffe686a929574701353ca2d3be4c23..3d42096d2f420296566a897ebe2f77e5d0079ede 100644 (file)
--- a/player.h
+++ b/player.h
@@ -1137,53 +1137,59 @@ static void player_animate_camera(void)
  */
 static void player_audio(void)
 {
-   audio_sample_occlusion( player.camera[3] );
-
-   float speed = vg_minf(v3_length( player.rb.v )*0.1f,1.0f),
-         attn = v3_dist( player.rb.co, player.camera[3] )+1.0f;
-   attn = (1.0f/(attn*attn)) * speed;
+   sfx_lock();
 
    static float air = 0.0f;
    air = vg_lerpf(air, player.in_air? 1.0f: 0.0f, 5.0f*ktimestep);
-   
+
+   /* Spacial info */
    v3f ears = { 1.0f,0.0f,0.0f };
    v3f delta;
 
+   float *cam = player.camera[3],
+         *pos = player.rb.co;
+
    v3_sub( player.rb.co, player.camera[3], delta );
    v3_normalize( delta );
    m3x3_mulv( player.camera, ears, ears );
    
-   float pan = v3_dot( ears, delta );
-   audio_player0.pan = pan;
-   audio_player1.pan = pan;
-   audio_player2.pan = pan;
-
-   audio_player3.pan = 0.0f;
-
-   if( freecam )
+   if( freecam || player.is_dead || !player.on_board )
    {
       audio_player0.vol = 0.0f;
       audio_player1.vol = 0.0f;
       audio_player2.vol = 0.0f;
+
+      audio_player0.pan = 0.0f;
+      audio_player1.pan = 0.0f;
+      audio_player2.pan = 0.0f;
    }
    else
    {
-      if( player.is_dead )
-      {
-         audio_player0.vol = 0.0f;
-         audio_player1.vol = 0.0f;
-         audio_player2.vol = 0.0f;
-      }
-      else
-      {
-         float slide = vg_clampf( fabsf(player.slip), 0.0f, 1.0f );
-         audio_player0.vol = (1.0f-air)*attn*(1.0f-slide);
-         audio_player1.vol =       air *attn;
-         audio_player2.vol = (1.0f-air)*attn*slide;
-      }
+      /* Spacialize 0 */
+      audio_spacialize( &audio_player0, pos, cam, ears, 1.0f );
+      audio_player1.pan = audio_player0.pan;
+      audio_player2.pan = audio_player0.pan;
+
+      /* Composite */
+      float speed = vg_minf(v3_length( player.rb.v )*0.1f,1.0f),
+            attn  = audio_player0.vol * speed,
+            slide = vg_clampf( fabsf(player.slip), 0.0f, 1.0f ),
+            vol0  = (1.0f-air)*attn*(1.0f-slide),
+            vol1  =       air *attn,
+            vol2  = (1.0f-air)*attn*slide;
+
+      audio_player1.vol = vol1;
+      audio_player2.vol = vol2;
+      audio_player0.vol = vol0;
    }
 
+   /* Tunnel / occlusion */
+   audio_sample_occlusion( player.camera[3] );
+   audio_player3.pan = 0.0f;
    audio_player3.vol = audio_player0.vol * audio_occlusion_current * 2.0f;
+   
+   world_audio_update( cam, ears );
+   sfx_unlock();
 }
 
 /*
diff --git a/sound_src/bird0.ogg b/sound_src/bird0.ogg
new file mode 100644 (file)
index 0000000..8d16ffd
Binary files /dev/null and b/sound_src/bird0.ogg differ
diff --git a/sound_src/bird1.ogg b/sound_src/bird1.ogg
new file mode 100644 (file)
index 0000000..4597258
Binary files /dev/null and b/sound_src/bird1.ogg differ
diff --git a/sound_src/bird2.ogg b/sound_src/bird2.ogg
new file mode 100644 (file)
index 0000000..5872341
Binary files /dev/null and b/sound_src/bird2.ogg differ
diff --git a/sound_src/docks_generic.ogg b/sound_src/docks_generic.ogg
new file mode 100644 (file)
index 0000000..9bd8111
Binary files /dev/null and b/sound_src/docks_generic.ogg differ
diff --git a/sound_src/field_generic.ogg b/sound_src/field_generic.ogg
new file mode 100644 (file)
index 0000000..11f6bc0
Binary files /dev/null and b/sound_src/field_generic.ogg differ
diff --git a/sound_src/grass0.ogg b/sound_src/grass0.ogg
new file mode 100644 (file)
index 0000000..184d599
Binary files /dev/null and b/sound_src/grass0.ogg differ
diff --git a/sound_src/grass1.ogg b/sound_src/grass1.ogg
new file mode 100644 (file)
index 0000000..71b22ee
Binary files /dev/null and b/sound_src/grass1.ogg differ
diff --git a/sound_src/grass2.ogg b/sound_src/grass2.ogg
new file mode 100644 (file)
index 0000000..fedb06e
Binary files /dev/null and b/sound_src/grass2.ogg differ
diff --git a/sound_src/grass3.ogg b/sound_src/grass3.ogg
new file mode 100644 (file)
index 0000000..b6537fb
Binary files /dev/null and b/sound_src/grass3.ogg differ
diff --git a/sound_src/grobplab.ogg b/sound_src/grobplab.ogg
new file mode 100644 (file)
index 0000000..eb68d45
Binary files /dev/null and b/sound_src/grobplab.ogg differ
diff --git a/sound_src/leaves0.ogg b/sound_src/leaves0.ogg
new file mode 100644 (file)
index 0000000..cf0a7d9
Binary files /dev/null and b/sound_src/leaves0.ogg differ
diff --git a/sound_src/leaves1.ogg b/sound_src/leaves1.ogg
new file mode 100644 (file)
index 0000000..2acc99e
Binary files /dev/null and b/sound_src/leaves1.ogg differ
diff --git a/sound_src/leaves2.ogg b/sound_src/leaves2.ogg
new file mode 100644 (file)
index 0000000..3da688a
Binary files /dev/null and b/sound_src/leaves2.ogg differ
diff --git a/sound_src/leaves3.ogg b/sound_src/leaves3.ogg
new file mode 100644 (file)
index 0000000..905a02e
Binary files /dev/null and b/sound_src/leaves3.ogg differ
diff --git a/sound_src/town_generic.ogg b/sound_src/town_generic.ogg
new file mode 100644 (file)
index 0000000..8a5d3c2
Binary files /dev/null and b/sound_src/town_generic.ogg differ
diff --git a/sound_src/wave0.ogg b/sound_src/wave0.ogg
new file mode 100644 (file)
index 0000000..2009b90
Binary files /dev/null and b/sound_src/wave0.ogg differ
diff --git a/sound_src/wave1.ogg b/sound_src/wave1.ogg
new file mode 100644 (file)
index 0000000..bca14df
Binary files /dev/null and b/sound_src/wave1.ogg differ
diff --git a/sound_src/wave2.ogg b/sound_src/wave2.ogg
new file mode 100644 (file)
index 0000000..c3678b5
Binary files /dev/null and b/sound_src/wave2.ogg differ
diff --git a/sound_src/wave3.ogg b/sound_src/wave3.ogg
new file mode 100644 (file)
index 0000000..cbde47c
Binary files /dev/null and b/sound_src/wave3.ogg differ
diff --git a/sound_src/wave4.ogg b/sound_src/wave4.ogg
new file mode 100644 (file)
index 0000000..7336171
Binary files /dev/null and b/sound_src/wave4.ogg differ
diff --git a/sound_src/wave5.ogg b/sound_src/wave5.ogg
new file mode 100644 (file)
index 0000000..08740ea
Binary files /dev/null and b/sound_src/wave5.ogg differ
diff --git a/world.h b/world.h
index 3734dbf353963295c2b75eb8ec25aa4436509436..b76086f8c152a798bbcd65022028ba43ae918747 100644 (file)
--- a/world.h
+++ b/world.h
@@ -18,6 +18,7 @@ static int ray_world( v3f pos, v3f dir, ray_hit *hit );
 #include "traffic.h" /*TODO: -> world_traffic.h */
 #include "world_routes.h"
 #include "world_sfd.h"
+#include "world_audio.h"
 
 #include "shaders/terrain.h"
 #include "shaders/sky.h"
@@ -540,6 +541,7 @@ static void world_init(void)
 
    /* Other systems */
    world_sfd_init();
+   world_audio_init();
 }
 
 static void world_update(void)
diff --git a/world_audio.h b/world_audio.h
new file mode 100644 (file)
index 0000000..e6ee331
--- /dev/null
@@ -0,0 +1,105 @@
+#ifndef WORLD_AUDIO_H
+#define WORLD_AUDIO_H
+
+#include "common.h"
+#include "audio.h"
+
+sfx_set audio_birds =
+{
+   .sources = "sound/bird0.ogg\0"
+              "sound/bird1.ogg\0"
+              "sound/bird2.ogg\0"
+};
+
+static float k_audio_random_freq = 1.0f/10.0f,
+             k_audio_random_hz   = 1.0f;
+
+static double world_audio_last_tick = 0.0;
+
+static struct world_ambient_layer
+{
+   sfx_system sys;
+   v3f pos;
+}
+world_ambient_layers[8];
+
+static void world_audio_init(void)
+{
+   sfx_set_init( &audio_birds, NULL );
+
+   vg_convar_push( (struct vg_convar){
+      .name = "aud_random_freq",
+      .data = &k_audio_random_freq,
+      .data_type = k_convar_dtype_f32,
+      .opt_f32 = { .clamp = 0 },
+      .persistent = 1
+   });
+
+   for( int i=0; i<vg_list_size(world_ambient_layers); i++ )
+   {
+      struct world_ambient_layer *layer = &world_ambient_layers[i];
+      sfx_system *sys = &layer->sys;
+      sys->vol = 1.0f;
+      sys->ch  = 1;
+      sys->vol_src = &audio_vol_all;
+      sys->name = "ambient";
+      sys->flags = SFX_FLAG_PERSISTENT;
+   }
+}
+
+static void world_audio_spawn_random( v3f around )
+{
+   for( int i=0; i<vg_list_size(world_ambient_layers); i++ )
+   {
+      struct world_ambient_layer *layer = &world_ambient_layers[i];
+
+      if( !layer->sys.is_playing )
+      {
+         sfx_set_playrnd( &audio_birds, &layer->sys, 0, 3 );
+
+         v3f chance;
+         vg_rand_sphere( chance );
+         v3_muladds( around, chance, 100.0f, layer->pos );
+
+         return;
+      }
+   }
+}
+
+static void world_audio_update( v3f around, v3f ears )
+{
+   if( world_audio_last_tick + (double)k_audio_random_hz < vg_time )
+   {
+      world_audio_last_tick = vg_time;
+
+      float chance = vg_randf();
+      if( chance < k_audio_random_freq )
+      {
+         world_audio_spawn_random( around );
+      }
+   }
+
+   /* Spacialize */
+   for( int i=0; i<vg_list_size(world_ambient_layers); i++ )
+   {
+      struct world_ambient_layer *layer = &world_ambient_layers[i];
+
+      if( layer->sys.is_playing )
+         audio_spacialize( &layer->sys, layer->pos, around, ears, 30.0f );
+   }
+}
+
+static void world_audio_debug(void)
+{
+   for( int i=0; i<vg_list_size(world_ambient_layers); i++ )
+   {
+      struct world_ambient_layer *layer = &world_ambient_layers[i];
+
+      if( layer->sys.is_playing )
+      {
+         vg_line_pt3( layer->pos, 1.0f, 0xffff00ff );
+      }
+   }
+}
+
+#endif /* WORLD_AUDIO_H */