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