refactor player
[carveJwlIkooP6JGAAIwe30JlM.git] / player_audio.h
1 #ifndef PLAYER_AUDIO_H
2 #define PLAYER_AUDIO_H
3
4 #include "player.h"
5
6 /*
7 * Audio
8 */
9 static void player_audio(void)
10 {
11 static int _ding = 0;
12
13 int last = _ding;
14 _ding = glfwGetKey(vg_window, GLFW_KEY_C);
15
16 int trigger_ding = 0;
17 if( _ding && !last )
18 trigger_ding = 1;
19
20 static int _air = 0;
21
22 int l2 = _air;
23 _air = player.in_air;
24
25 static double last_revert = -2000.0;
26
27
28
29
30 audio_lock();
31
32 double revert_delta = vg_time - last_revert;
33 if( player.on_board && (!_air && l2) && (fabsf(player.slip) > 0.5f) &&
34 (revert_delta > 0.7) )
35 {
36 audio_player_set_position( &audio_player_extra, player.rb.co );
37 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
38 audio_player_set_vol( &audio_player_extra, 2.0f );
39 audio_player_playclip( &audio_player_extra, &audio_lands[rand()%5] );
40
41 last_revert = vg_time;
42 }
43
44 static float air = 0.0f;
45 air = vg_lerpf(air, player.in_air? 1.0f: 0.0f, 5.0f*ktimestep);
46
47 /* Spacial info */
48 v3f ears = { 1.0f,0.0f,0.0f };
49 v3f delta;
50
51 float *cam = player.camera[3],
52 *pos = player.rb.co;
53
54 if( trigger_ding )
55 audio_player_playclip( &audio_player_extra, &audio_ding );
56
57 audio_player_set_position( &audio_player0, player.rb.co );
58 audio_player_set_position( &audio_player1, player.rb.co );
59 audio_player_set_position( &audio_player2, player.rb.co );
60 audio_player_set_position( &audio_player_gate, world.render_gate_pos );
61
62 v3_sub( player.rb.co, player.camera[3], delta );
63 v3_normalize( delta );
64 m3x3_mulv( player.camera, ears, ears );
65
66 /* TODO, Make function */
67 v3_copy( ears, vg_audio.listener_ears );
68 v3_copy( player.camera[3], vg_audio.listener_pos );
69
70 /* Tunnel / occlusion */
71 audio_sample_occlusion( player.camera[3] );
72
73 int sprite_avail = -1;
74 for( int i=0; i<vg_list_size(ambient_sprites); i++ )
75 {
76 if( !audio_player_is_playing( &ambient_sprites[i] ) )
77 {
78 sprite_avail = i;
79 break;
80 }
81 }
82
83 if( sprite_avail != -1 )
84 {
85 v3f waterpos;
86 enum audio_sprite_type sprite_type =
87 audio_sample_sprite_random( player.rb.co, waterpos );
88
89 if( sprite_type != k_audio_sprite_type_none )
90 {
91 audio_player *avail = &ambient_sprites[ sprite_avail ];
92
93 audio_player_set_vol( avail, 20.0f );
94 audio_player_set_flags( avail, AUDIO_FLAG_SPACIAL_3D );
95 audio_player_set_position( avail, waterpos );
96
97 if( sprite_type == k_audio_sprite_type_grass )
98 {
99 audio_player_playclip( avail, &audio_grass[rand()%4] );
100 }
101 else if( sprite_type == k_audio_sprite_type_water )
102 {
103 audio_player_playclip( avail, &audio_water[rand()%6] );
104 }
105 }
106 }
107
108 if( freecam || player.is_dead || !player.on_board )
109 {
110 audio_player_set_vol( &audio_player0, 0.0f );
111 audio_player_set_vol( &audio_player1, 0.0f );
112 audio_player_set_vol( &audio_player2, 0.0f );
113
114 int walk_phase = 0;
115 if( vg_fractf(player.walk_timer) > 0.5f )
116 walk_phase = 1;
117 else
118 walk_phase = 0;
119
120 if( (player.step_phase != walk_phase) && !player.in_air )
121 {
122 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
123 audio_player_set_position( &audio_player_extra, player.rb.co );
124 audio_player_set_vol( &audio_player_extra, 6.0f );
125 audio_player_playclip( &audio_player_extra,
126 &audio_footsteps[rand()%4] );
127 }
128
129 player.step_phase = walk_phase;
130 }
131 else
132 {
133 /* Composite */
134 float speed = vg_minf(v3_length( player.rb.v )*0.1f,1.0f),
135 attn = speed,
136 slide = vg_clampf( fabsf(player.slip), 0.0f, 1.0f ),
137 vol0 = (1.0f-air)*attn*(1.0f-slide),
138 vol1 = air *attn,
139 vol2 = (1.0f-air)*attn*slide;
140
141 audio_player_set_vol( &audio_player0, vol0 );
142 audio_player_set_vol( &audio_player1, vol1 );
143 audio_player_set_vol( &audio_player2, vol2 );
144
145 float reverb_amt = vol0 * audio_occlusion_current * 0.5f;
146 audio_player_set_pan( &audio_player3, 0.0f );
147 audio_player_set_vol( &audio_player3, reverb_amt );
148 }
149
150 #if 0
151 world_audio_update( cam, ears );
152 #endif
153 audio_unlock();
154 }
155
156 #endif /* PLAYER_AUDIO_H */