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