add motion vectors to all shaders
[carveJwlIkooP6JGAAIwe30JlM.git] / player_audio.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef PLAYER_AUDIO_H
6 #define PLAYER_AUDIO_H
7
8 #include "player.h"
9
10 /*
11 * Audio
12 */
13 VG_STATIC void player_audio(void)
14 {
15 struct player_phys *phys = &player.phys;
16
17 static int _air = 0;
18
19 int l2 = _air;
20 _air = phys->in_air;
21
22 static double last_revert = -2000.0;
23
24
25
26
27 audio_lock();
28
29 double revert_delta = vg.time - last_revert;
30 if( phys->on_board && (!_air && l2) && (revert_delta > 0.7) &&
31 (player.air_time > 0.5f) )
32 {
33 audio_player_set_position( &audio_player_extra, phys->rb.co );
34 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
35 audio_player_set_vol( &audio_player_extra, 2.0f );
36
37 if( (fabsf(phys->slip) > 0.75f) )
38 {
39 audio_player_playclip( &audio_player_extra, &audio_lands[rand()%2+3] );
40 }
41 else
42 {
43 audio_player_playclip( &audio_player_extra, &audio_lands[rand()%3] );
44 }
45
46 last_revert = vg.time;
47 }
48
49 static float air = 0.0f;
50 air = vg_lerpf( air, phys->in_air? 1.0f: 0.0f, 5.0f*vg.time_delta );
51
52 /* Spacial info */
53 v3f ears = { 1.0f,0.0f,0.0f };
54 v3f delta;
55
56 float *cam = main_camera.transform[3],
57 *pos = phys->rb.co;
58
59 audio_player_set_position( &audio_player0, phys->rb.co );
60 audio_player_set_position( &audio_player1, phys->rb.co );
61 audio_player_set_position( &audio_player2, phys->rb.co );
62 audio_player_set_position( &audio_player4, phys->rb.co );
63 audio_player_set_position( &audio_player_gate, world.render_gate_pos );
64 audio_player_set_vol( &audio_player_gate, 5.0f );
65
66 v3_sub( phys->rb.co, main_camera.transform[3], delta );
67 v3_normalize( delta );
68 m3x3_mulv( main_camera.transform, ears, ears );
69
70 /* TODO, Make function */
71 v3_copy( ears, vg_audio.listener_ears );
72 v3_copy( main_camera.transform[3], vg_audio.listener_pos );
73
74 /* Tunnel / occlusion */
75 audio_sample_occlusion( main_camera.transform[3] );
76
77 int sprite_avail = -1;
78 for( int i=0; i<vg_list_size(ambient_sprites); i++ )
79 {
80 if( !audio_player_is_playing( &ambient_sprites[i] ) )
81 {
82 sprite_avail = i;
83 break;
84 }
85 }
86
87 if( sprite_avail != -1 )
88 {
89 v3f waterpos;
90 enum audio_sprite_type sprite_type =
91 audio_sample_sprite_random( phys->rb.co, waterpos );
92
93 if( sprite_type != k_audio_sprite_type_none )
94 {
95 audio_player *avail = &ambient_sprites[ sprite_avail ];
96
97 audio_player_set_vol( avail, 20.0f );
98 audio_player_set_flags( avail, AUDIO_FLAG_SPACIAL_3D );
99 audio_player_set_position( avail, waterpos );
100
101 if( sprite_type == k_audio_sprite_type_grass )
102 {
103 audio_player_playclip( avail, &audio_grass[rand()%4] );
104 }
105 else if( sprite_type == k_audio_sprite_type_water )
106 {
107 if( world.water.enabled )
108 audio_player_playclip( avail, &audio_water[rand()%6] );
109 }
110 }
111 }
112
113 if( freecam || player.is_dead || !phys->on_board )
114 {
115 audio_player_set_vol( &audio_player0, 0.0f );
116 audio_player_set_vol( &audio_player1, 0.0f );
117 audio_player_set_vol( &audio_player2, 0.0f );
118 audio_player_set_vol( &audio_player3, 0.0f );
119 audio_player_set_vol( &audio_player4, 0.0f );
120
121 int walk_phase = 0;
122 if( vg_fractf(player.walk_timer) > 0.5f )
123 walk_phase = 1;
124 else
125 walk_phase = 0;
126
127 if( (player.step_phase != walk_phase) && !phys->in_air )
128 {
129 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
130 audio_player_set_position( &audio_player_extra, phys->rb.co );
131 audio_player_set_vol( &audio_player_extra, 6.0f );
132
133 if( player.surface_prop == k_surface_prop_concrete )
134 {
135 audio_player_playclip(
136 &audio_player_extra,
137 &audio_footsteps[rand()%vg_list_size(audio_footsteps)]
138 );
139 }
140 else if( player.surface_prop == k_surface_prop_grass )
141 {
142 audio_player_playclip(
143 &audio_player_extra,
144 &audio_footsteps_grass[rand()%vg_list_size(audio_footsteps_grass)]
145 );
146 }
147 else if( player.surface_prop == k_surface_prop_wood )
148 {
149 audio_player_playclip(
150 &audio_player_extra,
151 &audio_footsteps_wood[rand()%vg_list_size(audio_footsteps_wood)]
152 );
153 }
154 }
155
156 player.step_phase = walk_phase;
157 }
158 else
159 {
160 /* Composite */
161 float speed = vg_minf(v3_length( phys->rb.v )*0.1f,1.0f),
162 attn = speed,
163 slide = vg_clampf( fabsf(phys->slip), 0.0f, 1.0f ),
164 grind = phys->grind,
165 vol0 = (1.0f-air)*attn*(1.0f-slide)*(1.0f-grind),
166 vol1 = air *attn*(1.0f-grind),
167 vol2 = (1.0f-air)*attn*slide*(1.0f-grind),
168 vol4 = grind*2.0f;
169
170 audio_player_set_vol( &audio_player0, vol0*vg.time_rate );
171 audio_player_set_vol( &audio_player1, vol1*vg.time_rate );
172 audio_player_set_vol( &audio_player2, vol2*vg.time_rate );
173 audio_player_set_vol( &audio_player4, vol4*vg.time_rate );
174
175 float reverb_amt = vol0 * audio_occlusion_current * 0.5f;
176
177 audio_player_set_pan( &audio_player3, 0.0f );
178 audio_player_set_vol( &audio_player3, reverb_amt*vg.time_rate );
179 }
180
181 #if 0
182 world_audio_update( cam, ears );
183 #endif
184 audio_unlock();
185
186 if( player.phys.in_air )
187 player.air_time += vg.time_delta;
188 else
189 player.air_time = 0.0f;
190
191 }
192
193 #endif /* PLAYER_AUDIO_H */