build system revision
[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 #include "vg/vg_audio_dsp.h"
12
13 audio_clip audio_board[] =
14 {
15 { .path="sound/skate_hpf.ogg" },
16 { .path="sound/wheel.ogg" },
17 { .path="sound/slide.ogg" },
18 { .path="sound/grind_enter.ogg" },
19 { .path="sound/grind_exit.ogg" },
20 { .path="sound/grind_loop.ogg" },
21 { .path="sound/woodslide.ogg" },
22 { .path="sound/metalscrape.ogg" },
23 { .path="sound/slidetap.ogg" }
24 };
25
26 audio_clip audio_taps[] =
27 {
28 { .path="sound/tap0.ogg" },
29 { .path="sound/tap1.ogg" },
30 { .path="sound/tap2.ogg" },
31 { .path="sound/tap3.ogg" }
32 };
33
34 audio_clip audio_flips[] =
35 {
36 { .path="sound/lf0.ogg" },
37 { .path="sound/lf1.ogg" },
38 { .path="sound/lf2.ogg" },
39 { .path="sound/lf3.ogg" },
40 };
41
42 audio_clip audio_hits[] =
43 {
44 { .path="sound/hit0.ogg" },
45 { .path="sound/hit1.ogg" },
46 { .path="sound/hit2.ogg" },
47 { .path="sound/hit3.ogg" },
48 { .path="sound/hit4.ogg" }
49 };
50
51 audio_clip audio_splash =
52 { .path = "sound/splash.ogg" };
53
54 audio_clip audio_jumps[] = {
55 { .path = "sound/jump0.ogg" },
56 { .path = "sound/jump1.ogg" },
57 };
58
59 audio_clip audio_footsteps[] = {
60 {.path = "sound/step_concrete0.ogg" },
61 {.path = "sound/step_concrete1.ogg" },
62 {.path = "sound/step_concrete2.ogg" },
63 {.path = "sound/step_concrete3.ogg" }
64 };
65
66 audio_clip audio_footsteps_grass[] = {
67 {.path = "sound/step_bush0.ogg" },
68 {.path = "sound/step_bush1.ogg" },
69 {.path = "sound/step_bush2.ogg" },
70 {.path = "sound/step_bush3.ogg" },
71 {.path = "sound/step_bush4.ogg" },
72 {.path = "sound/step_bush5.ogg" }
73 };
74
75 audio_clip audio_footsteps_wood[] = {
76 {.path = "sound/step_wood0.ogg" },
77 {.path = "sound/step_wood1.ogg" },
78 {.path = "sound/step_wood2.ogg" },
79 {.path = "sound/step_wood3.ogg" },
80 {.path = "sound/step_wood4.ogg" },
81 {.path = "sound/step_wood5.ogg" }
82 };
83
84 audio_clip audio_lands[] = {
85 { .path = "sound/land0.ogg" },
86 { .path = "sound/land1.ogg" },
87 { .path = "sound/land2.ogg" },
88 { .path = "sound/landsk0.ogg" },
89 { .path = "sound/landsk1.ogg" },
90 { .path = "sound/onto.ogg" },
91 { .path = "sound/outo.ogg" },
92 };
93
94 audio_clip audio_water[] = {
95 { .path = "sound/wave0.ogg" },
96 { .path = "sound/wave1.ogg" },
97 { .path = "sound/wave2.ogg" },
98 { .path = "sound/wave3.ogg" },
99 { .path = "sound/wave4.ogg" },
100 { .path = "sound/wave5.ogg" }
101 };
102
103 audio_clip audio_grass[] = {
104 { .path = "sound/grass0.ogg" },
105 { .path = "sound/grass1.ogg" },
106 { .path = "sound/grass2.ogg" },
107 { .path = "sound/grass3.ogg" },
108 };
109
110 audio_clip audio_ambience[] =
111 {
112 { .path="sound/town_generic.ogg" }
113 };
114
115 audio_clip audio_gate_pass = {
116 .path = "sound/gate_pass.ogg"
117 };
118
119 audio_clip audio_gate_lap = {
120 .path = "sound/gate_lap.ogg"
121 };
122
123 audio_clip audio_gate_ambient = {
124 .path = "sound/gate_ambient.ogg"
125 };
126
127 audio_clip audio_rewind[] = {
128 { .path = "sound/rewind_start.ogg" },
129 { .path = "sound/rewind_end_1.5.ogg" },
130 { .path = "sound/rewind_end_2.5.ogg" },
131 { .path = "sound/rewind_end_6.5.ogg" },
132 { .path = "sound/rewind_clack.ogg" },
133 };
134
135 audio_clip audio_ui[] = {
136 { .path = "sound/ui_click.ogg" },
137 { .path = "sound/ui_ding.ogg" },
138 { .path = "sound/teleport.ogg" }
139 };
140
141 audio_clip audio_challenge[] = {
142 { .path = "sound/objective0.ogg" },
143 { .path = "sound/objective1.ogg" },
144 { .path = "sound/objective_win.ogg" },
145 { .path = "sound/ui_good.ogg" },
146 { .path = "sound/ui_inf.ogg" },
147 { .path = "sound/ui_ok.ogg" },
148 { .path = "sound/objective_fail.ogg" }
149 };
150
151 struct air_synth_data {
152 f32 speed;
153
154 /* internal */
155 f32 t;
156 struct dsp_biquad lpf;
157 SDL_SpinLock sl;
158 }
159 static air_data;
160
161 static void audio_air_synth_get_samples( void *_data, f32 *buf, u32 count ){
162 struct air_synth_data *data = _data;
163
164 SDL_AtomicLock( &data->sl );
165 f32 spd = data->speed;
166 SDL_AtomicUnlock( &data->sl );
167
168 f32 s0 = sinf(data->t*2.0f),
169 s1 = sinf(data->t*0.43f),
170 s2 = sinf(data->t*1.333f),
171 sm = vg_clampf( data->speed / 45.0f, 0, 1 ),
172 ft = (s0*s1*s2)*0.5f+0.5f,
173 f = vg_lerpf( 200.0f, 1200.0f, sm*0.7f + ft*0.3f ),
174 vol = 0.25f * sm;
175
176 dsp_init_biquad_butterworth_lpf( &data->lpf, f );
177
178 for( u32 i=0; i<count; i ++ ){
179 f32 v = (vg_randf64(&vg_dsp.rand) * 2.0f - 1.0f) * vol;
180 v = dsp_biquad_process( &data->lpf, v );
181
182 buf[i*2+0] = v;
183 buf[i*2+1] = v;
184 }
185
186 data->t += (f32)(count)/44100.0f;
187 };
188
189 static audio_clip air_synth = {
190 .flags = k_audio_format_gen,
191 .size = 0,
192 .func = audio_air_synth_get_samples,
193 .data = &air_data
194 };
195
196 static void audio_init(void)
197 {
198 audio_clip_loadn( audio_board, vg_list_size(audio_board), NULL );
199 audio_clip_loadn( audio_taps, vg_list_size(audio_taps), NULL );
200 audio_clip_loadn( audio_flips, vg_list_size(audio_flips), NULL );
201 audio_clip_loadn( audio_hits, vg_list_size(audio_hits), NULL );
202 audio_clip_loadn( audio_ambience, vg_list_size(audio_ambience), NULL );
203 audio_clip_loadn( &audio_splash, 1, NULL );
204 audio_clip_loadn( &audio_gate_pass, 1, NULL );
205 audio_clip_loadn( &audio_gate_lap, 1, NULL );
206 audio_clip_loadn( &audio_gate_ambient, 1, NULL );
207
208 audio_clip_loadn( audio_jumps, vg_list_size(audio_jumps), NULL );
209 audio_clip_loadn( audio_lands, vg_list_size(audio_lands), NULL );
210 audio_clip_loadn( audio_water, vg_list_size(audio_water), NULL );
211 audio_clip_loadn( audio_grass, vg_list_size(audio_grass), NULL );
212 audio_clip_loadn( audio_footsteps, vg_list_size(audio_footsteps), NULL );
213 audio_clip_loadn( audio_footsteps_grass,
214 vg_list_size(audio_footsteps_grass), NULL );
215 audio_clip_loadn( audio_footsteps_wood,
216 vg_list_size(audio_footsteps_wood), NULL );
217 audio_clip_loadn( audio_rewind, vg_list_size(audio_rewind), NULL );
218 audio_clip_loadn( audio_ui, vg_list_size(audio_ui), NULL );
219 audio_clip_loadn( audio_challenge, vg_list_size(audio_challenge), NULL );
220
221 audio_lock();
222 audio_set_lfo_wave( 0, k_lfo_polynomial_bipolar, 80.0f );
223 audio_set_lfo_frequency( 0, 20.0f );
224
225 skaterift.aud_air = audio_get_first_idle_channel();
226 if( skaterift.aud_air )
227 audio_channel_init( skaterift.aud_air, &air_synth, 0 );
228
229 audio_unlock();
230 }
231
232 static void audio_free(void)
233 {
234 /* TODO! */
235 vg_warn( "UNIMPLEMENTED: audio_free()\n" );
236 }
237
238 enum audio_sprite_type
239 {
240 k_audio_sprite_type_none,
241 k_audio_sprite_type_grass,
242 k_audio_sprite_type_water
243 };
244
245 static void audio_ambient_sprite_play( v3f co, audio_clip *clip )
246 {
247 audio_lock();
248 u16 group_id = 0xfff0;
249 audio_channel *ch = audio_get_group_idle_channel( group_id, 4 );
250
251 if( ch ){
252 audio_channel_init( ch, clip, AUDIO_FLAG_SPACIAL_3D );
253 audio_channel_group( ch, group_id );
254 audio_channel_set_spacial( ch, co, 80.0f );
255 audio_channel_edit_volume( ch, 1.0f, 1 );
256 ch = audio_relinquish_channel( ch );
257 }
258 audio_unlock();
259 }
260
261 static
262 enum audio_sprite_type world_audio_sample_sprite_random(v3f origin, v3f output);
263 static void audio_ambient_sprites_update( world_instance *world, v3f co )
264 {
265 static float accum = 0.0f;
266 accum += vg.time_delta;
267
268 if( accum > 0.1f )
269 accum -= 0.1f;
270 else return;
271
272 v3f sprite_pos;
273 enum audio_sprite_type sprite_type =
274 world_audio_sample_sprite_random( co, sprite_pos );
275
276 if( sprite_type != k_audio_sprite_type_none ){
277 if( sprite_type == k_audio_sprite_type_grass ){
278 audio_ambient_sprite_play( sprite_pos,
279 &audio_grass[vg_randu32(&vg.rand)%4] );
280 }
281 else if( sprite_type == k_audio_sprite_type_water ){
282 if( world->water.enabled ){
283 audio_ambient_sprite_play( sprite_pos,
284 &audio_water[vg_randu32(&vg.rand)%6] );
285 }
286 }
287 }
288 }
289
290 #endif /* AUDIO_H */