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