grind sound
[carveJwlIkooP6JGAAIwe30JlM.git] / audio.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #include "common.h"
6
7 #ifndef AUDIO_H
8 #define AUDIO_H
9
10 #include "world.h"
11
12 VG_STATIC float audio_occlusion_current = 0.0f,
13 k_audio_occlusion_rate = 1.0f;
14
15 VG_STATIC int k_audio_debug_soundscape = 0;
16
17 audio_clip audio_board[] =
18 {
19 { .path="sound/skate.ogg" },
20 { .path="sound/wheel.ogg" },
21 { .path="sound/slide.ogg" },
22 { .path="sound/reverb.ogg" },
23 { .path="sound/grind_loop.ogg" },
24 { .path="sound/grind_enter.ogg" },
25 { .path="sound/grind_exit.ogg" }
26 };
27
28 audio_clip audio_splash =
29 { .path = "sound/splash.ogg" };
30
31 audio_clip audio_jumps[] = {
32 { .path = "sound/jump0.ogg" },
33 { .path = "sound/jump1.ogg" },
34 };
35
36 audio_clip audio_footsteps[] = {
37 {.path = "sound/step_concrete0.ogg" },
38 {.path = "sound/step_concrete1.ogg" },
39 {.path = "sound/step_concrete2.ogg" },
40 {.path = "sound/step_concrete3.ogg" }
41 };
42
43 audio_clip audio_footsteps_grass[] = {
44 {.path = "sound/step_bush0.ogg" },
45 {.path = "sound/step_bush1.ogg" },
46 {.path = "sound/step_bush2.ogg" },
47 {.path = "sound/step_bush3.ogg" },
48 {.path = "sound/step_bush4.ogg" },
49 {.path = "sound/step_bush5.ogg" }
50 };
51
52 audio_clip audio_footsteps_wood[] = {
53 {.path = "sound/step_wood0.ogg" },
54 {.path = "sound/step_wood1.ogg" },
55 {.path = "sound/step_wood2.ogg" },
56 {.path = "sound/step_wood3.ogg" },
57 {.path = "sound/step_wood4.ogg" },
58 {.path = "sound/step_wood5.ogg" }
59 };
60
61 audio_clip audio_lands[] = {
62 { .path = "sound/land0.ogg" },
63 { .path = "sound/land1.ogg" },
64 { .path = "sound/land2.ogg" },
65 { .path = "sound/landsk0.ogg" },
66 { .path = "sound/landsk1.ogg" },
67 { .path = "sound/onto.ogg" },
68 { .path = "sound/outo.ogg" },
69 };
70
71 audio_clip audio_water[] = {
72 { .path = "sound/wave0.ogg" },
73 { .path = "sound/wave1.ogg" },
74 { .path = "sound/wave2.ogg" },
75 { .path = "sound/wave3.ogg" },
76 { .path = "sound/wave4.ogg" },
77 { .path = "sound/wave5.ogg" }
78 };
79
80 audio_clip audio_grass[] = {
81 { .path = "sound/grass0.ogg" },
82 { .path = "sound/grass1.ogg" },
83 { .path = "sound/grass2.ogg" },
84 { .path = "sound/grass3.ogg" },
85 };
86
87 audio_clip audio_ambience[] =
88 {
89 { .path="sound/town_generic.ogg" }
90 };
91
92 audio_clip audio_gate_pass = {
93 .path = "sound/gate_pass.ogg"
94 };
95
96 audio_clip audio_gate_lap = {
97 .path = "sound/gate_lap.ogg"
98 };
99
100 audio_clip audio_gate_ambient = {
101 .path = "sound/gate_ambient.ogg"
102 };
103
104 audio_player ambient_player =
105 {
106 .name = "Ambience"
107 };
108
109 audio_player audio_rewind_player =
110 {
111 .name = "Rewind"
112 };
113
114 audio_clip audio_rewind[] = {
115 { .path = "sound/rewind_start.ogg" },
116 { .path = "sound/rewind_end_1.5.ogg" },
117 { .path = "sound/rewind_end_2.5.ogg" },
118 { .path = "sound/rewind_end_6.5.ogg" },
119 { .path = "sound/rewind_clack.ogg" },
120 };
121
122 audio_clip audio_ui[] = {
123 { .path = "sound/ui_click.ogg" },
124 { .path = "sound/ui_ding.ogg" },
125 };
126
127 audio_player ambient_sprites[4] =
128 {
129 { .name = "Ambient Sprites 0" },
130 { .name = "Ambient Sprites 1" },
131 { .name = "Ambient Sprites 2" },
132 { .name = "Ambient Sprites 3" },
133 };
134
135 audio_player audio_player0 =
136 {
137 .name = "Player0",
138 };
139
140 audio_player audio_player1 =
141 {
142 .name = "Player1",
143 };
144
145 audio_player audio_player2 =
146 {
147 .name = "Player2",
148 };
149
150 audio_player audio_player3 =
151 {
152 .name = "Player3",
153 };
154
155 audio_player audio_player4 =
156 {
157 .name = "Player4",
158 };
159
160 audio_player audio_player_extra =
161 {
162 .name = "PlayerInst"
163 };
164
165 audio_player audio_player_gate =
166 {
167 .name = "Gate"
168 };
169
170 VG_STATIC void audio_init(void)
171 {
172 audio_player_init( &audio_player0 );
173 audio_player_init( &audio_player1 );
174 audio_player_init( &audio_player2 );
175 audio_player_init( &audio_player3 );
176 audio_player_init( &audio_player4 );
177 audio_player_init( &audio_player_gate );
178 audio_player_init( &ambient_player );
179 audio_player_init( &ambient_sprites[0] );
180 audio_player_init( &ambient_sprites[1] );
181 audio_player_init( &ambient_sprites[2] );
182 audio_player_init( &ambient_sprites[3] );
183 audio_player_init( &audio_player_extra );
184 audio_player_init( &audio_rewind_player );
185
186 audio_clip_loadn( audio_board, vg_list_size(audio_board), NULL );
187 audio_clip_loadn( audio_ambience, vg_list_size(audio_ambience), NULL );
188 audio_clip_loadn( &audio_splash, 1, NULL );
189 audio_clip_loadn( &audio_gate_pass, 1, NULL );
190 audio_clip_loadn( &audio_gate_lap, 1, NULL );
191 audio_clip_loadn( &audio_gate_ambient, 1, NULL );
192 audio_clip_loadn( audio_jumps, vg_list_size(audio_jumps), NULL );
193 audio_clip_loadn( audio_lands, vg_list_size(audio_lands), NULL );
194 audio_clip_loadn( audio_water, vg_list_size(audio_water), NULL );
195 audio_clip_loadn( audio_grass, vg_list_size(audio_grass), NULL );
196 audio_clip_loadn( audio_footsteps, vg_list_size(audio_footsteps), NULL );
197 audio_clip_loadn( audio_footsteps_grass,
198 vg_list_size(audio_footsteps_grass), NULL );
199 audio_clip_loadn( audio_footsteps_wood,
200 vg_list_size(audio_footsteps_wood), NULL );
201 audio_clip_loadn( audio_rewind, vg_list_size(audio_rewind), NULL );
202 audio_clip_loadn( audio_ui, vg_list_size(audio_ui), NULL );
203
204 audio_lock();
205 u32 flags = AUDIO_FLAG_LOOP|AUDIO_FLAG_SPACIAL_3D;
206
207 audio_player_set_flags( &audio_player0, flags );
208 audio_player_set_flags( &audio_player1, flags );
209 audio_player_set_flags( &audio_player2, flags );
210 audio_player_set_flags( &audio_player4, flags );
211 audio_player_set_flags( &audio_player_gate, flags );
212 audio_player_set_flags( &audio_player3, AUDIO_FLAG_LOOP );
213 audio_player_set_flags( &ambient_player, AUDIO_FLAG_LOOP );
214 audio_player_set_flags( &ambient_sprites[0], AUDIO_FLAG_SPACIAL_3D );
215 audio_player_set_flags( &ambient_sprites[1], AUDIO_FLAG_SPACIAL_3D );
216 audio_player_set_flags( &ambient_sprites[2], AUDIO_FLAG_SPACIAL_3D );
217 audio_player_set_flags( &ambient_sprites[3], AUDIO_FLAG_SPACIAL_3D );
218 audio_player_set_vol( &ambient_player, 1.0f );
219 audio_player_set_vol( &audio_player_gate, 0.0f );
220 audio_player_set_vol( &audio_player_extra, 1.0f );
221 audio_player_set_vol( &audio_rewind_player, 0.2f );
222 audio_player_set_flags( &audio_rewind_player, 0x00 );
223
224 audio_player_playclip( &audio_player0, &audio_board[0] );
225 audio_player_playclip( &audio_player1, &audio_board[1] );
226 audio_player_playclip( &audio_player2, &audio_board[2] );
227 audio_player_playclip( &audio_player3, &audio_board[3] );
228 audio_player_playclip( &audio_player4, &audio_board[4] );
229 audio_player_playclip( &ambient_player, &audio_ambience[0] );
230 audio_player_playclip( &audio_player_gate, &audio_gate_ambient );
231
232 audio_unlock();
233
234 vg_convar_push( (struct vg_convar){
235 .name = "aud_debug_soundscape",
236 .data = &k_audio_debug_soundscape,
237 .data_type = k_convar_dtype_i32,
238 .opt_i32 = { .min=0, .max=1, .clamp=0 },
239 .persistent = 1
240 });
241
242 vg_convar_push( (struct vg_convar){
243 .name = "aud_occlusion_rate",
244 .data = &k_audio_occlusion_rate,
245 .data_type = k_convar_dtype_f32,
246 .opt_f32 = { .clamp = 0 },
247 .persistent = 1
248 });
249 }
250
251 VG_STATIC void audio_free(void)
252 {
253 /* TODO! */
254 vg_warn( "UNIMPLEMENTED: audio_free()\n" );
255 }
256
257 VG_STATIC void audio_sample_occlusion( v3f origin )
258 {
259 float d = 0.0f,
260 sample_dist = 880.0f;
261
262 int sample_count = 8;
263
264 int lv = 0;
265 v3f last;
266 v3_zero(last);
267
268 for( int i=0; i<sample_count; i++ )
269 {
270 v3f dir;
271 vg_rand_dir( dir );
272
273 ray_hit contact;
274 contact.dist = 15.0f;
275
276 if( ray_world( origin, dir, &contact ) )
277 {
278 d += contact.dist;
279
280 #if 0
281 vg_line( origin, contact.pos, 0xff0000ff );
282 vg_line_pt3( contact.pos, 0.1f, 0xff0000ff );
283
284 if( lv )
285 vg_line( contact.pos, last, 0xffffffff );
286 #endif
287
288 v3_copy( contact.pos, last );
289 lv = 1;
290 }
291 else
292 {
293 v3f p1;
294 v3_muladds( origin, dir, sample_dist, p1 );
295
296 #if 0
297 vg_line( origin, p1, 0xffcccccc );
298 #endif
299
300 d += sample_dist;
301 lv = 0;
302 }
303
304 }
305
306 float occlusion = 1.0f - (d * (1.0f/(sample_dist*(float)sample_count))),
307 rate = VG_TIMESTEP_FIXED * k_audio_occlusion_rate,
308 target = powf( occlusion, 6.0f );
309 audio_occlusion_current = vg_lerpf( audio_occlusion_current, target, rate );
310 }
311
312 enum audio_sprite_type
313 {
314 k_audio_sprite_type_none,
315 k_audio_sprite_type_grass,
316 k_audio_sprite_type_water
317 };
318
319 /*
320 * Trace out a random point, near the player to try and determine water areas
321 */
322 VG_STATIC enum audio_sprite_type audio_sample_sprite_random( v3f origin,
323 v3f output )
324 {
325 v3f chance = { (vg_randf()-0.5f) * 30.0f,
326 8.0f,
327 (vg_randf()-0.5f) * 30.0f };
328
329 v3f pos;
330 v3_add( chance, origin, pos );
331
332 ray_hit contact;
333 contact.dist = vg_minf( 16.0f, pos[1] );
334
335
336 if( ray_world( pos, (v3f){0.0f,-1.0f,0.0f}, &contact ) )
337 {
338 struct world_material *mat = ray_hit_material( &contact );
339
340 if( mat->info.surface_prop == k_surface_prop_grass)
341 {
342 v3_copy( contact.pos, output );
343 return k_audio_sprite_type_grass;
344 }
345 else
346 {
347 #if 0
348 vg_line( pos, contact.pos, 0xff0000ff );
349 vg_line_pt3( contact.pos, 0.3f, 0xff0000ff );
350 #endif
351 return k_audio_sprite_type_none;
352 }
353 }
354
355 output[0] = pos[0];
356 output[1] = 0.0f;
357 output[2] = pos[2];
358
359 if( world.water.enabled )
360 return k_audio_sprite_type_water;
361 else
362 return k_audio_sprite_type_none;
363 }
364
365 VG_STATIC void audio_debug_soundscapes(void)
366 {
367 if( !k_audio_debug_soundscape ) return;
368
369 char buf[64];
370 snprintf( buf, 31, "occlusion: %.5f", audio_occlusion_current );
371
372 vg_uictx.cursor[0] = 450;
373 vg_uictx.cursor[1] = 10;
374 vg_uictx.cursor[2] = audio_occlusion_current * 200.0f;
375 vg_uictx.cursor[3] = 20;
376
377 ui_fill_rect( vg_uictx.cursor, 0x55cccccc );
378 ui_text( vg_uictx.cursor, buf, 1, 0 );
379 }
380
381 #endif /* AUDIO_H */