add motion vectors to all shaders
[carveJwlIkooP6JGAAIwe30JlM.git] / audio.h
diff --git a/audio.h b/audio.h
index 212ed9f19d8231a13e004122d5c5b7a39d8ae994..8282c068183669efddbea0437be3fa96e56cdb10 100644 (file)
--- a/audio.h
+++ b/audio.h
+/*
+ * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
+ */
+
 #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 "world.h"
 
-static float audio_occlusion_current = 0.0f,
+VG_STATIC float audio_occlusion_current = 0.0f,
              k_audio_occlusion_rate  = 1.0f;
 
-static int   k_audio_debug_soundscape = 0;
+VG_STATIC int   k_audio_debug_soundscape = 0;
+
+audio_clip audio_board[] =
+{
+   { .path="sound/skate.ogg" },
+   { .path="sound/wheel.ogg" },
+   { .path="sound/slide.ogg" },
+   { .path="sound/reverb.ogg" },
+   { .path="sound/grind_loop.ogg" },
+   { .path="sound/grind_enter.ogg" },
+   { .path="sound/grind_exit.ogg" }
+};
+
+audio_clip audio_splash =
+{ .path = "sound/splash.ogg" };
+
+audio_clip audio_jumps[] = {
+   { .path = "sound/jump0.ogg" },
+   { .path = "sound/jump1.ogg" },
+};
+
+audio_clip audio_footsteps[] = {
+ {.path = "sound/step_concrete0.ogg" },
+ {.path = "sound/step_concrete1.ogg" },
+ {.path = "sound/step_concrete2.ogg" },
+ {.path = "sound/step_concrete3.ogg" }
+};
+
+audio_clip audio_footsteps_grass[] = {
+ {.path = "sound/step_bush0.ogg" },
+ {.path = "sound/step_bush1.ogg" },
+ {.path = "sound/step_bush2.ogg" },
+ {.path = "sound/step_bush3.ogg" },
+ {.path = "sound/step_bush4.ogg" },
+ {.path = "sound/step_bush5.ogg" }
+};
+
+audio_clip audio_footsteps_wood[] = {
+ {.path = "sound/step_wood0.ogg" },
+ {.path = "sound/step_wood1.ogg" },
+ {.path = "sound/step_wood2.ogg" },
+ {.path = "sound/step_wood3.ogg" },
+ {.path = "sound/step_wood4.ogg" },
+ {.path = "sound/step_wood5.ogg" }
+};
+
+audio_clip audio_lands[] = {
+   { .path = "sound/land0.ogg" },
+   { .path = "sound/land1.ogg" },
+   { .path = "sound/land2.ogg" },
+   { .path = "sound/landsk0.ogg" },
+   { .path = "sound/landsk1.ogg" },
+   { .path = "sound/onto.ogg" },
+   { .path = "sound/outo.ogg" },
+};
+
+audio_clip audio_water[] = {
+   { .path = "sound/wave0.ogg" },
+   { .path = "sound/wave1.ogg" },
+   { .path = "sound/wave2.ogg" },
+   { .path = "sound/wave3.ogg" },
+   { .path = "sound/wave4.ogg" },
+   { .path = "sound/wave5.ogg" }
+};
+
+audio_clip audio_grass[] = {
+   { .path = "sound/grass0.ogg" },
+   { .path = "sound/grass1.ogg" },
+   { .path = "sound/grass2.ogg" },
+   { .path = "sound/grass3.ogg" },
+};
+
+audio_clip audio_ambience[] =
+{
+   { .path="sound/town_generic.ogg" }
+};
+
+audio_clip audio_gate_pass = {
+   .path = "sound/gate_pass.ogg"
+};
+
+audio_clip audio_gate_lap = {
+   .path = "sound/gate_lap.ogg"
+};
 
-sfx_set audio_board =
+audio_clip audio_gate_ambient = {
+.path = "sound/gate_ambient.ogg"
+};
+
+audio_player ambient_player =
 {
-   .sources = "sound/skate.ogg\0"
-              "sound/wheel.ogg\0"
-              "sound/slide.ogg\0"
-              "sound/reverb.ogg\0"
+   .name = "Ambience"
 };
 
-sfx_system audio_player0 =
+audio_player audio_rewind_player =
+{
+   .name = "Rewind"
+};
+
+audio_clip audio_rewind[] = {
+{ .path = "sound/rewind_start.ogg" },
+{ .path = "sound/rewind_end_1.5.ogg" },
+{ .path = "sound/rewind_end_2.5.ogg" },
+{ .path = "sound/rewind_end_6.5.ogg" },
+{ .path = "sound/rewind_clack.ogg" },
+};
+
+audio_clip audio_ui[] = {
+   { .path = "sound/ui_click.ogg" },
+   { .path = "sound/ui_ding.ogg" },
+};
+
+audio_player ambient_sprites[4] =
+{
+   { .name = "Ambient Sprites 0" },
+   { .name = "Ambient Sprites 1" },
+   { .name = "Ambient Sprites 2" },
+   { .name = "Ambient Sprites 3" },
+};
+
+audio_player audio_player0 =
 {
-   .vol = 0.0f,
-   .ch = 1,
-   .vol_src = &audio_vol_all,
    .name = "Player0",
-   .flags = SFX_FLAG_REPEAT | SFX_FLAG_PERSISTENT 
 };
 
-sfx_system audio_player1 =
+audio_player audio_player1 =
 {
-   .vol = 0.0f,
-   .ch = 1,
-   .vol_src = &audio_vol_all,
    .name = "Player1",
-   .flags = SFX_FLAG_REPEAT | SFX_FLAG_PERSISTENT
 };
 
-sfx_system audio_player2 =
+audio_player audio_player2 =
 {
-   .vol = 0.0f,
-   .ch = 1,
-   .vol_src = &audio_vol_all,
    .name = "Player2",
-   .flags = SFX_FLAG_REPEAT | SFX_FLAG_PERSISTENT
 };
 
-sfx_system audio_player3 =
+audio_player audio_player3 =
 {
-   .vol = 0.0f,
-   .ch = 1,
-   .vol_src = &audio_vol_all,
    .name = "Player3",
-   .flags = SFX_FLAG_REPEAT | SFX_FLAG_PERSISTENT
 };
 
-static void audio_init(void)
+audio_player audio_player4 =
 {
-   sfx_set_init( &audio_board, NULL );
-   sfx_set_play( &audio_board, &audio_player0, 0 );
-   sfx_set_play( &audio_board, &audio_player1, 1 );
-   sfx_set_play( &audio_board, &audio_player2, 2 );
-   sfx_set_play( &audio_board, &audio_player3, 3 );
+   .name = "Player4",
+};
+
+audio_player audio_player_extra =
+{
+   .name = "PlayerInst"
+};
+
+audio_player audio_player_gate =
+{
+   .name = "Gate"
+};
+
+VG_STATIC void audio_init(void)
+{
+   audio_player_init( &audio_player0 );
+   audio_player_init( &audio_player1 );
+   audio_player_init( &audio_player2 );
+   audio_player_init( &audio_player3 );
+   audio_player_init( &audio_player4 );
+   audio_player_init( &audio_player_gate );
+   audio_player_init( &ambient_player );
+   audio_player_init( &ambient_sprites[0] );
+   audio_player_init( &ambient_sprites[1] );
+   audio_player_init( &ambient_sprites[2] );
+   audio_player_init( &ambient_sprites[3] );
+   audio_player_init( &audio_player_extra );
+   audio_player_init( &audio_rewind_player );
+
+   audio_clip_loadn( audio_board, vg_list_size(audio_board), NULL );
+   audio_clip_loadn( audio_ambience, vg_list_size(audio_ambience), NULL );
+   audio_clip_loadn( &audio_splash, 1, NULL );
+   audio_clip_loadn( &audio_gate_pass, 1, NULL );
+   audio_clip_loadn( &audio_gate_lap, 1, NULL );
+   audio_clip_loadn( &audio_gate_ambient, 1, NULL );
+   audio_clip_loadn( audio_jumps, vg_list_size(audio_jumps), NULL );
+   audio_clip_loadn( audio_lands, vg_list_size(audio_lands), NULL );
+   audio_clip_loadn( audio_water, vg_list_size(audio_water), NULL );
+   audio_clip_loadn( audio_grass, vg_list_size(audio_grass), NULL );
+   audio_clip_loadn( audio_footsteps, vg_list_size(audio_footsteps), NULL );
+   audio_clip_loadn( audio_footsteps_grass, 
+                     vg_list_size(audio_footsteps_grass), NULL );
+   audio_clip_loadn( audio_footsteps_wood, 
+                     vg_list_size(audio_footsteps_wood), NULL );
+   audio_clip_loadn( audio_rewind, vg_list_size(audio_rewind), NULL );
+   audio_clip_loadn( audio_ui, vg_list_size(audio_ui), NULL );
+
+   audio_lock();
+   u32 flags = AUDIO_FLAG_LOOP|AUDIO_FLAG_SPACIAL_3D;
+
+   audio_player_set_flags( &audio_player0, flags );
+   audio_player_set_flags( &audio_player1, flags );
+   audio_player_set_flags( &audio_player2, flags );
+   audio_player_set_flags( &audio_player4, flags );
+   audio_player_set_flags( &audio_player_gate, flags );
+   audio_player_set_flags( &audio_player3, AUDIO_FLAG_LOOP );
+   audio_player_set_flags( &ambient_player, AUDIO_FLAG_LOOP );
+   audio_player_set_flags( &ambient_sprites[0], AUDIO_FLAG_SPACIAL_3D );
+   audio_player_set_flags( &ambient_sprites[1], AUDIO_FLAG_SPACIAL_3D );
+   audio_player_set_flags( &ambient_sprites[2], AUDIO_FLAG_SPACIAL_3D );
+   audio_player_set_flags( &ambient_sprites[3], AUDIO_FLAG_SPACIAL_3D );
+   audio_player_set_vol( &ambient_player, 1.0f );
+   audio_player_set_vol( &audio_player_gate, 0.0f );
+   audio_player_set_vol( &audio_player_extra, 1.0f );
+   audio_player_set_vol( &audio_rewind_player, 0.2f );
+   audio_player_set_flags( &audio_rewind_player, 0x00 );
+
+   audio_player_playclip( &audio_player0, &audio_board[0] );
+   audio_player_playclip( &audio_player1, &audio_board[1] );
+   audio_player_playclip( &audio_player2, &audio_board[2] );
+   audio_player_playclip( &audio_player3, &audio_board[3] );
+   audio_player_playclip( &audio_player4, &audio_board[4] );
+   audio_player_playclip( &ambient_player, &audio_ambience[0] );
+   audio_player_playclip( &audio_player_gate, &audio_gate_ambient );
+
+   audio_unlock();
 
    vg_convar_push( (struct vg_convar){
       .name = "aud_debug_soundscape",
@@ -81,33 +248,23 @@ static void audio_init(void)
    });
 }
 
-static void audio_free(void)
+VG_STATIC void audio_free(void)
 {
-   sfx_set_free( &audio_board );
+   /* TODO! */
+   vg_warn( "UNIMPLEMENTED: audio_free()\n" );
 }
 
-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 )
+VG_STATIC void audio_sample_occlusion( v3f origin )
 {
    float d = 0.0f,
          sample_dist = 880.0f;
 
    int sample_count = 8;
 
+   int lv = 0;
+   v3f last;
+   v3_zero(last);
+
    for( int i=0; i<sample_count; i++ )
    {
       v3f dir;
@@ -120,39 +277,105 @@ static void audio_sample_occlusion( v3f origin )
       {
          d += contact.dist;
 
+#if 0
          vg_line( origin, contact.pos, 0xff0000ff );
          vg_line_pt3( contact.pos, 0.1f, 0xff0000ff );
+
+         if( lv )
+            vg_line( contact.pos, last, 0xffffffff );
+#endif
+
+         v3_copy( contact.pos, last );
+         lv = 1;
       }
       else
       {
          v3f p1;
          v3_muladds( origin, dir, sample_dist, p1 );
+
+#if 0
          vg_line( origin, p1, 0xffcccccc );
+#endif
 
          d += sample_dist;
+         lv = 0;
       }
+
    }
 
    float occlusion = 1.0f - (d * (1.0f/(sample_dist*(float)sample_count))),
-         rate = ktimestep * k_audio_occlusion_rate,
-         target = powf( occlusion, 6.0f );
+         rate = VG_TIMESTEP_FIXED * k_audio_occlusion_rate,
+         target = powf( vg_maxf(occlusion,0.0f), 6.0f );
    audio_occlusion_current = vg_lerpf( audio_occlusion_current, target, rate );
 }
 
-static void audio_debug_soundscapes(void)
+enum audio_sprite_type 
+{
+   k_audio_sprite_type_none,
+   k_audio_sprite_type_grass,
+   k_audio_sprite_type_water
+};
+
+/*
+ * Trace out a random point, near the player to try and determine water areas
+ */
+VG_STATIC enum audio_sprite_type audio_sample_sprite_random( v3f origin, 
+                                                             v3f output )
+{
+   v3f chance = { (vg_randf()-0.5f) * 30.0f, 
+                  8.0f,
+                  (vg_randf()-0.5f) * 30.0f };
+   
+   v3f pos;
+   v3_add( chance, origin, pos );
+
+   ray_hit contact;
+   contact.dist = vg_minf( 16.0f, pos[1] );
+
+   
+   if( ray_world( pos, (v3f){0.0f,-1.0f,0.0f}, &contact ) )
+   {
+      struct world_material *mat = ray_hit_material( &contact );
+
+      if( mat->info.surface_prop == k_surface_prop_grass) 
+      {
+         v3_copy( contact.pos, output );
+         return k_audio_sprite_type_grass;
+      }
+      else
+      {
+#if 0
+         vg_line( pos, contact.pos, 0xff0000ff );
+         vg_line_pt3( contact.pos, 0.3f, 0xff0000ff );
+#endif
+         return k_audio_sprite_type_none;
+      }
+   }
+
+   output[0] = pos[0];
+   output[1] = 0.0f;
+   output[2] = pos[2];
+   
+   if( world.water.enabled )
+      return k_audio_sprite_type_water;
+   else
+      return k_audio_sprite_type_none;
+}
+
+VG_STATIC void audio_debug_soundscapes(void)
 {
    if( !k_audio_debug_soundscape ) return;
 
    char buf[64];
    snprintf( buf, 31, "occlusion: %.5f", audio_occlusion_current );
 
-   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;
+   vg_uictx.cursor[0] = 450;
+   vg_uictx.cursor[1] = 10;
+   vg_uictx.cursor[2] = audio_occlusion_current * 200.0f;
+   vg_uictx.cursor[3] = 20;
 
-   gui_fill_rect( ui_global_ctx.cursor, 0x55cccccc );
-   gui_text( ui_global_ctx.cursor, buf, 1, 0 );
+   ui_fill_rect( vg_uictx.cursor, 0x55cccccc );
+   ui_text( vg_uictx.cursor, buf, 1, 0 );
 }
 
 #endif /* AUDIO_H */