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