more tweaks
[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_hits[] =
39 {
40 { .path="sound/hit0.ogg" },
41 { .path="sound/hit1.ogg" },
42 { .path="sound/hit2.ogg" },
43 { .path="sound/hit3.ogg" },
44 { .path="sound/hit4.ogg" }
45 };
46
47 audio_clip audio_splash =
48 { .path = "sound/splash.ogg" };
49
50 audio_clip audio_jumps[] = {
51 { .path = "sound/jump0.ogg" },
52 { .path = "sound/jump1.ogg" },
53 };
54
55 audio_clip audio_footsteps[] = {
56 {.path = "sound/step_concrete0.ogg" },
57 {.path = "sound/step_concrete1.ogg" },
58 {.path = "sound/step_concrete2.ogg" },
59 {.path = "sound/step_concrete3.ogg" }
60 };
61
62 audio_clip audio_footsteps_grass[] = {
63 {.path = "sound/step_bush0.ogg" },
64 {.path = "sound/step_bush1.ogg" },
65 {.path = "sound/step_bush2.ogg" },
66 {.path = "sound/step_bush3.ogg" },
67 {.path = "sound/step_bush4.ogg" },
68 {.path = "sound/step_bush5.ogg" }
69 };
70
71 audio_clip audio_footsteps_wood[] = {
72 {.path = "sound/step_wood0.ogg" },
73 {.path = "sound/step_wood1.ogg" },
74 {.path = "sound/step_wood2.ogg" },
75 {.path = "sound/step_wood3.ogg" },
76 {.path = "sound/step_wood4.ogg" },
77 {.path = "sound/step_wood5.ogg" }
78 };
79
80 audio_clip audio_lands[] = {
81 { .path = "sound/land0.ogg" },
82 { .path = "sound/land1.ogg" },
83 { .path = "sound/land2.ogg" },
84 { .path = "sound/landsk0.ogg" },
85 { .path = "sound/landsk1.ogg" },
86 { .path = "sound/onto.ogg" },
87 { .path = "sound/outo.ogg" },
88 };
89
90 audio_clip audio_water[] = {
91 { .path = "sound/wave0.ogg" },
92 { .path = "sound/wave1.ogg" },
93 { .path = "sound/wave2.ogg" },
94 { .path = "sound/wave3.ogg" },
95 { .path = "sound/wave4.ogg" },
96 { .path = "sound/wave5.ogg" }
97 };
98
99 audio_clip audio_grass[] = {
100 { .path = "sound/grass0.ogg" },
101 { .path = "sound/grass1.ogg" },
102 { .path = "sound/grass2.ogg" },
103 { .path = "sound/grass3.ogg" },
104 };
105
106 audio_clip audio_ambience[] =
107 {
108 { .path="sound/town_generic.ogg" }
109 };
110
111 audio_clip audio_gate_pass = {
112 .path = "sound/gate_pass.ogg"
113 };
114
115 audio_clip audio_gate_lap = {
116 .path = "sound/gate_lap.ogg"
117 };
118
119 audio_clip audio_gate_ambient = {
120 .path = "sound/gate_ambient.ogg"
121 };
122
123 audio_clip audio_rewind[] = {
124 { .path = "sound/rewind_start.ogg" },
125 { .path = "sound/rewind_end_1.5.ogg" },
126 { .path = "sound/rewind_end_2.5.ogg" },
127 { .path = "sound/rewind_end_6.5.ogg" },
128 { .path = "sound/rewind_clack.ogg" },
129 };
130
131 audio_clip audio_ui[] = {
132 { .path = "sound/ui_click.ogg" },
133 { .path = "sound/ui_ding.ogg" },
134 };
135
136 VG_STATIC void audio_init(void)
137 {
138 audio_clip_loadn( audio_board, vg_list_size(audio_board), NULL );
139 audio_clip_loadn( audio_taps, vg_list_size(audio_taps), NULL );
140 audio_clip_loadn( audio_hits, vg_list_size(audio_hits), NULL );
141 audio_clip_loadn( audio_ambience, vg_list_size(audio_ambience), NULL );
142 audio_clip_loadn( &audio_splash, 1, NULL );
143 audio_clip_loadn( &audio_gate_pass, 1, NULL );
144 audio_clip_loadn( &audio_gate_lap, 1, NULL );
145 audio_clip_loadn( &audio_gate_ambient, 1, NULL );
146
147 audio_clip_loadn( audio_jumps, vg_list_size(audio_jumps), NULL );
148 audio_clip_loadn( audio_lands, vg_list_size(audio_lands), NULL );
149 audio_clip_loadn( audio_water, vg_list_size(audio_water), NULL );
150 audio_clip_loadn( audio_grass, vg_list_size(audio_grass), NULL );
151 audio_clip_loadn( audio_footsteps, vg_list_size(audio_footsteps), NULL );
152 audio_clip_loadn( audio_footsteps_grass,
153 vg_list_size(audio_footsteps_grass), NULL );
154 audio_clip_loadn( audio_footsteps_wood,
155 vg_list_size(audio_footsteps_wood), NULL );
156 audio_clip_loadn( audio_rewind, vg_list_size(audio_rewind), NULL );
157 audio_clip_loadn( audio_ui, vg_list_size(audio_ui), NULL );
158
159 vg_var_push( (struct vg_var){
160 .name = "aud_debug_soundscape",
161 .data = &k_audio_debug_soundscape,
162 .data_type = k_var_dtype_i32,
163 .opt_i32 = { .min=0, .max=1, .clamp=0 },
164 .persistent = 1
165 });
166
167 vg_var_push( (struct vg_var){
168 .name = "aud_occlusion_rate",
169 .data = &k_audio_occlusion_rate,
170 .data_type = k_var_dtype_f32,
171 .opt_f32 = { .clamp = 0 },
172 .persistent = 1
173 });
174
175 audio_lock();
176 audio_set_lfo_wave( 0, k_lfo_polynomial_bipolar, 80.0f );
177 audio_set_lfo_frequency( 0, 20.0f );
178 audio_unlock();
179 }
180
181 VG_STATIC void audio_free(void)
182 {
183 /* TODO! */
184 vg_warn( "UNIMPLEMENTED: audio_free()\n" );
185 }
186
187 enum audio_sprite_type
188 {
189 k_audio_sprite_type_none,
190 k_audio_sprite_type_grass,
191 k_audio_sprite_type_water
192 };
193
194 /*
195 * Trace out a random point, near the player to try and determine water areas
196 */
197 VG_STATIC enum audio_sprite_type audio_sample_sprite_random( v3f origin,
198 v3f output )
199 {
200 v3f chance = { (vg_randf()-0.5f) * 30.0f,
201 8.0f,
202 (vg_randf()-0.5f) * 30.0f };
203
204 v3f pos;
205 v3_add( chance, origin, pos );
206
207 ray_hit contact;
208 contact.dist = vg_minf( 16.0f, pos[1] );
209
210 world_instance *world = get_active_world();
211
212 if( ray_world( world, pos, (v3f){0.0f,-1.0f,0.0f}, &contact ) ){
213 struct world_surface *mat = ray_hit_surface( world, &contact );
214
215 if( mat->info.surface_prop == k_surface_prop_grass){
216 v3_copy( contact.pos, output );
217 return k_audio_sprite_type_grass;
218 }
219 else{
220 #if 0
221 vg_line( pos, contact.pos, 0xff0000ff );
222 vg_line_pt3( contact.pos, 0.3f, 0xff0000ff );
223 #endif
224 return k_audio_sprite_type_none;
225 }
226 }
227
228 output[0] = pos[0];
229 output[1] = 0.0f;
230 output[2] = pos[2];
231
232 float dist = fabsf(output[1] - origin[1]);
233
234 if( world->water.enabled && dist<=40.0f )
235 return k_audio_sprite_type_water;
236 else
237 return k_audio_sprite_type_none;
238 }
239
240 VG_STATIC void audio_ambient_sprite_play( v3f co, audio_clip *clip )
241 {
242 audio_lock();
243 u32 channel_id = 31342352;
244 audio_channel *ch = audio_get_group_idle_channel( channel_id, 4 );
245
246 if( ch ){
247 audio_channel_init( ch, clip, AUDIO_FLAG_SPACIAL_3D );
248 audio_channel_group( ch, channel_id );
249 audio_channel_set_spacial( ch, co, 80.0f );
250 audio_channel_edit_volume( ch, 1.0f, 1 );
251 ch = audio_relinquish_channel( ch );
252 }
253 audio_unlock();
254 }
255
256 VG_STATIC void audio_ambient_sprites_update( world_instance *world, v3f co )
257 {
258 static float accum = 0.0f;
259 accum += vg.frame_delta;
260
261 if( accum > 0.1f )
262 accum -= 0.1f;
263 else return;
264
265 v3f sprite_pos;
266 enum audio_sprite_type sprite_type =
267 audio_sample_sprite_random( co, sprite_pos );
268
269 if( sprite_type != k_audio_sprite_type_none ){
270 if( sprite_type == k_audio_sprite_type_grass ){
271 audio_ambient_sprite_play( sprite_pos, &audio_grass[rand()%4] );
272 }
273 else if( sprite_type == k_audio_sprite_type_water ){
274 if( world->water.enabled )
275 audio_ambient_sprite_play( sprite_pos, &audio_water[rand()%6] );
276 }
277 }
278 }
279
280 #endif /* AUDIO_H */