refactor player
[carveJwlIkooP6JGAAIwe30JlM.git] / player_audio.h
diff --git a/player_audio.h b/player_audio.h
new file mode 100644 (file)
index 0000000..01c13fc
--- /dev/null
@@ -0,0 +1,156 @@
+#ifndef PLAYER_AUDIO_H
+#define PLAYER_AUDIO_H
+
+#include "player.h"
+
+/* 
+ * Audio
+ */
+static void player_audio(void)
+{
+   static int _ding = 0;
+   
+   int last = _ding;
+   _ding = glfwGetKey(vg_window, GLFW_KEY_C);
+
+   int trigger_ding = 0;
+   if( _ding && !last )
+      trigger_ding = 1;
+
+   static int _air = 0;
+
+   int l2 = _air;
+   _air = player.in_air;
+
+   static double last_revert = -2000.0;
+
+
+
+
+   audio_lock();
+   
+   double revert_delta = vg_time - last_revert;
+   if( player.on_board && (!_air && l2) && (fabsf(player.slip) > 0.5f) && 
+         (revert_delta > 0.7) )
+   {
+      audio_player_set_position( &audio_player_extra, player.rb.co );
+      audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
+      audio_player_set_vol( &audio_player_extra, 2.0f );
+      audio_player_playclip( &audio_player_extra, &audio_lands[rand()%5] );
+
+      last_revert = vg_time;
+   }
+
+   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;
+   
+   if( trigger_ding )
+      audio_player_playclip( &audio_player_extra, &audio_ding );
+
+   audio_player_set_position( &audio_player0, player.rb.co );
+   audio_player_set_position( &audio_player1, player.rb.co );
+   audio_player_set_position( &audio_player2, player.rb.co );
+   audio_player_set_position( &audio_player_gate, world.render_gate_pos );
+
+   v3_sub( player.rb.co, player.camera[3], delta );
+   v3_normalize( delta );
+   m3x3_mulv( player.camera, ears, ears );
+
+   /* TODO, Make function */
+   v3_copy( ears, vg_audio.listener_ears );
+   v3_copy( player.camera[3], vg_audio.listener_pos );
+
+   /* Tunnel / occlusion */
+   audio_sample_occlusion( player.camera[3] );
+
+   int sprite_avail = -1;
+   for( int i=0; i<vg_list_size(ambient_sprites); i++ )
+   {
+      if( !audio_player_is_playing( &ambient_sprites[i] ) )
+      {
+         sprite_avail = i;
+         break;
+      }
+   }
+   
+   if( sprite_avail != -1 )
+   {
+      v3f waterpos;
+      enum audio_sprite_type sprite_type = 
+         audio_sample_sprite_random( player.rb.co, waterpos );
+
+      if( sprite_type != k_audio_sprite_type_none )
+      {
+         audio_player *avail = &ambient_sprites[ sprite_avail ];
+
+         audio_player_set_vol( avail, 20.0f );
+         audio_player_set_flags( avail, AUDIO_FLAG_SPACIAL_3D );
+         audio_player_set_position( avail, waterpos );
+
+         if( sprite_type == k_audio_sprite_type_grass )
+         {
+            audio_player_playclip( avail, &audio_grass[rand()%4] );
+         }
+         else if( sprite_type == k_audio_sprite_type_water )
+         {
+            audio_player_playclip( avail, &audio_water[rand()%6] );
+         }
+      }
+   }
+   
+   if( freecam || player.is_dead || !player.on_board )
+   {
+      audio_player_set_vol( &audio_player0, 0.0f );
+      audio_player_set_vol( &audio_player1, 0.0f );
+      audio_player_set_vol( &audio_player2, 0.0f );
+
+      int walk_phase = 0;
+      if( vg_fractf(player.walk_timer) > 0.5f )
+         walk_phase = 1;
+      else
+         walk_phase = 0;
+
+      if( (player.step_phase != walk_phase) && !player.in_air )
+      {
+         audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
+         audio_player_set_position( &audio_player_extra, player.rb.co );
+         audio_player_set_vol( &audio_player_extra, 6.0f );
+         audio_player_playclip( &audio_player_extra, 
+                                          &audio_footsteps[rand()%4] );
+      }
+
+      player.step_phase = walk_phase;
+   }
+   else
+   {
+      /* Composite */
+      float speed = vg_minf(v3_length( player.rb.v )*0.1f,1.0f),
+            attn  = 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_player_set_vol( &audio_player0, vol0 );
+      audio_player_set_vol( &audio_player1, vol1 );
+      audio_player_set_vol( &audio_player2, vol2 );
+
+      float reverb_amt = vol0 * audio_occlusion_current * 0.5f;
+      audio_player_set_pan( &audio_player3, 0.0f );
+      audio_player_set_vol( &audio_player3, reverb_amt );
+   }
+   
+#if 0
+   world_audio_update( cam, ears );
+#endif
+   audio_unlock();
+}
+
+#endif /* PLAYER_AUDIO_H */