bugs/map edits
[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/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 #if 0
105 audio_player ambient_player =
106 {
107 .name = "Ambience"
108 };
109
110 audio_player audio_rewind_player =
111 {
112 .name = "Rewind"
113 };
114 #endif
115
116 audio_clip audio_rewind[] = {
117 { .path = "sound/rewind_start.ogg" },
118 { .path = "sound/rewind_end_1.5.ogg" },
119 { .path = "sound/rewind_end_2.5.ogg" },
120 { .path = "sound/rewind_end_6.5.ogg" },
121 { .path = "sound/rewind_clack.ogg" },
122 };
123
124 audio_clip audio_ui[] = {
125 { .path = "sound/ui_click.ogg" },
126 { .path = "sound/ui_ding.ogg" },
127 };
128
129 audio_clip audio_music[] = {
130 { .path = "sound/song.ogg", .flags = k_audio_format_vorbis },
131 { .path = "sound/skate.ogg", .flags = k_audio_format_vorbis },
132 };
133
134 #if 0
135 audio_player ambient_sprites[4] =
136 {
137 { .name = "Ambient Sprites 0" },
138 { .name = "Ambient Sprites 1" },
139 { .name = "Ambient Sprites 2" },
140 { .name = "Ambient Sprites 3" },
141 };
142
143 audio_player audio_player0 =
144 {
145 .name = "Player0",
146 };
147
148 audio_player audio_player1 =
149 {
150 .name = "Player1",
151 };
152
153 audio_player audio_player2 =
154 {
155 .name = "Player2",
156 };
157
158 audio_player audio_player3 =
159 {
160 .name = "Player3",
161 };
162
163 audio_player audio_player4 =
164 {
165 .name = "Player4",
166 };
167
168 audio_player audio_player_extra =
169 {
170 .name = "PlayerInst"
171 };
172
173 audio_player audio_player_gate =
174 {
175 .name = "Gate"
176 };
177 #endif
178
179 VG_STATIC void audio_init(void)
180 {
181 #if 0
182 audio_player_init( &audio_player0 );
183 audio_player_init( &audio_player1 );
184 audio_player_init( &audio_player2 );
185 audio_player_init( &audio_player3 );
186 audio_player_init( &audio_player4 );
187 audio_player_init( &audio_player_gate );
188 audio_player_init( &ambient_player );
189 audio_player_init( &ambient_sprites[0] );
190 audio_player_init( &ambient_sprites[1] );
191 audio_player_init( &ambient_sprites[2] );
192 audio_player_init( &ambient_sprites[3] );
193 audio_player_init( &audio_player_extra );
194 audio_player_init( &audio_rewind_player );
195 #endif
196
197 audio_clip_loadn( audio_board, vg_list_size(audio_board), NULL );
198 audio_clip_loadn( audio_ambience, vg_list_size(audio_ambience), NULL );
199 audio_clip_loadn( &audio_splash, 1, NULL );
200 audio_clip_loadn( &audio_gate_pass, 1, NULL );
201 audio_clip_loadn( &audio_gate_lap, 1, NULL );
202 audio_clip_loadn( &audio_gate_ambient, 1, NULL );
203 audio_clip_loadn( audio_music, vg_list_size(audio_music), NULL );
204
205 audio_clip_loadn( audio_jumps, vg_list_size(audio_jumps), NULL );
206 audio_clip_loadn( audio_lands, vg_list_size(audio_lands), NULL );
207 audio_clip_loadn( audio_water, vg_list_size(audio_water), NULL );
208 audio_clip_loadn( audio_grass, vg_list_size(audio_grass), NULL );
209 audio_clip_loadn( audio_footsteps, vg_list_size(audio_footsteps), NULL );
210 audio_clip_loadn( audio_footsteps_grass,
211 vg_list_size(audio_footsteps_grass), NULL );
212 audio_clip_loadn( audio_footsteps_wood,
213 vg_list_size(audio_footsteps_wood), NULL );
214 audio_clip_loadn( audio_rewind, vg_list_size(audio_rewind), NULL );
215 audio_clip_loadn( audio_ui, vg_list_size(audio_ui), NULL );
216
217 #if 0
218 audio_lock();
219 u32 flags = AUDIO_FLAG_LOOP|AUDIO_FLAG_SPACIAL_3D;
220
221 audio_player_set_flags( &audio_player0, flags );
222 audio_player_set_flags( &audio_player1, flags );
223 audio_player_set_flags( &audio_player2, flags );
224 audio_player_set_flags( &audio_player4, flags );
225 audio_player_set_flags( &audio_player_gate, flags );
226 audio_player_set_flags( &audio_player3, AUDIO_FLAG_LOOP );
227 audio_player_set_flags( &ambient_player, AUDIO_FLAG_LOOP );
228 audio_player_set_flags( &ambient_sprites[0], AUDIO_FLAG_SPACIAL_3D );
229 audio_player_set_flags( &ambient_sprites[1], AUDIO_FLAG_SPACIAL_3D );
230 audio_player_set_flags( &ambient_sprites[2], AUDIO_FLAG_SPACIAL_3D );
231 audio_player_set_flags( &ambient_sprites[3], AUDIO_FLAG_SPACIAL_3D );
232
233 audio_player_set_vol( &ambient_player, 1.0f );
234 audio_player_set_vol( &audio_player_gate, 0.0f );
235 audio_player_set_vol( &audio_player_extra, 1.0f );
236 audio_player_set_vol( &audio_rewind_player, 0.2f );
237 audio_player_set_flags( &audio_rewind_player, 0x00 );
238
239 audio_player_playclip( &audio_player0, &audio_board[0] );
240 audio_player_playclip( &audio_player1, &audio_board[1] );
241 audio_player_playclip( &audio_player2, &audio_board[2] );
242 audio_player_playclip( &audio_player3, &audio_board[3] );
243 audio_player_playclip( &audio_player4, &audio_board[4] );
244 audio_player_playclip( &ambient_player, &audio_ambience[0] );
245 audio_player_playclip( &audio_player_gate, &audio_gate_ambient );
246
247 audio_unlock();
248 #endif
249
250 vg_var_push( (struct vg_var){
251 .name = "aud_debug_soundscape",
252 .data = &k_audio_debug_soundscape,
253 .data_type = k_var_dtype_i32,
254 .opt_i32 = { .min=0, .max=1, .clamp=0 },
255 .persistent = 1
256 });
257
258 vg_var_push( (struct vg_var){
259 .name = "aud_occlusion_rate",
260 .data = &k_audio_occlusion_rate,
261 .data_type = k_var_dtype_f32,
262 .opt_f32 = { .clamp = 0 },
263 .persistent = 1
264 });
265
266 audio_lock();
267 audio_set_lfo_wave( 0, k_lfo_polynomial_bipolar, 80.0f );
268 audio_set_lfo_frequency( 0, 20.0f );
269 audio_unlock();
270 }
271
272 VG_STATIC void audio_update(void)
273 {
274 #if 0
275 static u32 flapflop = 0x00;
276 static audio_channel *channel = NULL;
277
278 u32 next = floorf( vg.time / 0.1f );
279
280 if( flapflop != next )
281 {
282 flapflop = next;
283
284 audio_lock();
285 channel = audio_channel_crossfade( channel,
286 &audio_music[ next & 0x1 ], 0.05f,
287 AUDIO_FLAG_LOOP|AUDIO_FLAG_SPACIAL_3D
288 );
289 channel = audio_channel_set_spacial( channel,
290 (v3f){ -22.3f, 60.1f, -52.7f },
291 50.0f );
292
293 audio_channel_sidechain_lfo( channel, 0, 1.0f );
294 audio_unlock();
295 }
296 #endif
297 }
298
299 VG_STATIC void audio_free(void)
300 {
301 /* TODO! */
302 vg_warn( "UNIMPLEMENTED: audio_free()\n" );
303 }
304
305 VG_STATIC void audio_sample_occlusion( v3f origin )
306 {
307 float d = 0.0f,
308 sample_dist = 880.0f;
309
310 int sample_count = 8;
311
312 int lv = 0;
313 v3f last;
314 v3_zero(last);
315
316 world_instance *world = get_active_world();
317
318 for( int i=0; i<sample_count; i++ )
319 {
320 v3f dir;
321 vg_rand_dir( dir );
322
323 ray_hit contact;
324 contact.dist = 15.0f;
325
326 if( ray_world( world, origin, dir, &contact ) )
327 {
328 d += contact.dist;
329
330 #if 0
331 vg_line( origin, contact.pos, 0xff0000ff );
332 vg_line_pt3( contact.pos, 0.1f, 0xff0000ff );
333
334 if( lv )
335 vg_line( contact.pos, last, 0xffffffff );
336 #endif
337
338 v3_copy( contact.pos, last );
339 lv = 1;
340 }
341 else
342 {
343 v3f p1;
344 v3_muladds( origin, dir, sample_dist, p1 );
345
346 #if 0
347 vg_line( origin, p1, 0xffcccccc );
348 #endif
349
350 d += sample_dist;
351 lv = 0;
352 }
353
354 }
355
356 float occlusion = 1.0f - (d * (1.0f/(sample_dist*(float)sample_count))),
357 rate = VG_TIMESTEP_FIXED * k_audio_occlusion_rate,
358 target = powf( vg_maxf(occlusion,0.0f), 6.0f );
359 audio_occlusion_current = vg_lerpf( audio_occlusion_current, target, rate );
360 }
361
362 enum audio_sprite_type
363 {
364 k_audio_sprite_type_none,
365 k_audio_sprite_type_grass,
366 k_audio_sprite_type_water
367 };
368
369 /*
370 * Trace out a random point, near the player to try and determine water areas
371 */
372 VG_STATIC enum audio_sprite_type audio_sample_sprite_random( v3f origin,
373 v3f output )
374 {
375 v3f chance = { (vg_randf()-0.5f) * 30.0f,
376 8.0f,
377 (vg_randf()-0.5f) * 30.0f };
378
379 v3f pos;
380 v3_add( chance, origin, pos );
381
382 ray_hit contact;
383 contact.dist = vg_minf( 16.0f, pos[1] );
384
385 world_instance *world = get_active_world();
386
387 if( ray_world( world, pos, (v3f){0.0f,-1.0f,0.0f}, &contact ) )
388 {
389 struct world_material *mat = ray_hit_material( world, &contact );
390
391 if( mat->info.surface_prop == k_surface_prop_grass)
392 {
393 v3_copy( contact.pos, output );
394 return k_audio_sprite_type_grass;
395 }
396 else
397 {
398 #if 0
399 vg_line( pos, contact.pos, 0xff0000ff );
400 vg_line_pt3( contact.pos, 0.3f, 0xff0000ff );
401 #endif
402 return k_audio_sprite_type_none;
403 }
404 }
405
406 output[0] = pos[0];
407 output[1] = 0.0f;
408 output[2] = pos[2];
409
410 if( world->water.enabled )
411 return k_audio_sprite_type_water;
412 else
413 return k_audio_sprite_type_none;
414 }
415
416 VG_STATIC void audio_debug_soundscapes(void)
417 {
418 if( !k_audio_debug_soundscape ) return;
419
420 char buf[64];
421 snprintf( buf, 31, "occlusion: %.5f", audio_occlusion_current );
422
423 vg_uictx.cursor[0] = 450;
424 vg_uictx.cursor[1] = 10;
425 vg_uictx.cursor[2] = audio_occlusion_current * 200.0f;
426 vg_uictx.cursor[3] = 20;
427
428 ui_fill_rect( vg_uictx.cursor, 0x55cccccc );
429 ui_text( vg_uictx.cursor, buf, 1, 0 );
430 }
431
432 #endif /* AUDIO_H */