settings menu & texsheet
[vg.git] / vg_audio_dsp.h
1 #ifndef VG_AUDIO_DSP_H
2 #define VG_AUDIO_DSP_H
3
4 #define VG_GAME
5 #include "vg/vg.h"
6
7 //#define VG_ECHO_LPF_BUTTERWORTH
8
9 static struct vg_dsp{
10 float *buffer;
11 u32 allocations;
12
13 u8 *view_texture_buffer;
14 GLuint view_texture;
15
16 float echo_distances[14],
17 echo_tunings[8],
18 reverb_wet_mix,
19 reverb_dry_mix;
20
21 vg_rand rand;
22 }
23 vg_dsp;
24
25 static float *dsp_allocate( u32 samples )
26 {
27 samples = vg_align4( samples );
28
29 if( vg_dsp.allocations + samples > (1024*1024)/4 )
30 vg_fatal_error( "too much dsp" );
31
32 float *buf = &vg_dsp.buffer[ vg_dsp.allocations ];
33 vg_dsp.allocations += samples;
34
35 return buf;
36 }
37
38
39 /*
40 * filters
41 * ----------------------------------------------
42 */
43
44 struct dsp_delay
45 {
46 u32 length, cur;
47 float *buffer;
48 };
49
50 struct dsp_lpf
51 {
52 float exponent;
53 float *buffer;
54 };
55
56 struct dsp_schroeder
57 {
58 struct dsp_delay M;
59 float gain;
60 };
61
62 struct dsp_biquad {
63 f32 a0, a1, a2, b1, b2, c0, d0,
64 xnz1, xnz2, ynz1, ynz2, offset;
65 };
66
67 static f32 dsp_biquad_process( struct dsp_biquad *bq, f32 xn ){
68 f32 yn = + bq->a0*xn + bq->a1*bq->xnz1 + bq->a2*bq->xnz2
69 - bq->b1*bq->ynz1 - bq->b2*bq->ynz2;
70 bq->xnz2 = bq->xnz1;
71 bq->xnz1 = xn;
72 bq->ynz2 = bq->ynz1;
73 bq->ynz1 = yn;
74 return yn + bq->offset;
75 }
76
77 static void dsp_init_biquad_butterworth_lpf( struct dsp_biquad *bq, f32 fc ){
78 f32 c = 1.0f/tanf(VG_PIf*fc / 44100.0f);
79 bq->a0 = 1.0f / (1.0f + sqrtf(2.0f)*c + powf(c, 2.0f) );
80 bq->a1 = 2.0f * bq->a0;
81 bq->a2 = bq->a0;
82 bq->b1 = 2.0f * bq->a0*(1.0f - powf(c, 2.0f));
83 bq->b2 = bq->a0 * (1.0f - sqrtf(2.0f)*c + powf(c, 2.0f) );
84 }
85
86 static inline void dsp_read_delay( struct dsp_delay *delay, float *s, u32 t ){
87 u32 index = delay->cur+t;
88
89 if( index >= delay->length )
90 index -= delay->length;
91
92 *s = delay->buffer[ index ];
93 }
94
95 static inline void dsp_write_delay( struct dsp_delay *delay, float *s )
96 {
97 u32 index = delay->cur;
98 delay->buffer[ index ] = *s;
99
100 delay->cur ++;
101
102 if( delay->cur >= delay->length )
103 delay->cur = 0;
104 }
105
106 static void dsp_init_delay( struct dsp_delay *delay, float length )
107 {
108 delay->length = 44100.0f * length;
109 delay->cur = 0;
110 delay->buffer = dsp_allocate( delay->length );
111
112 for( int i=0; i<delay->length; i++ )
113 delay->buffer[i] = 0.0f;
114 }
115
116 static void dsp_update_lpf( struct dsp_lpf *lpf, float freq )
117 {
118 lpf->exponent = 1.0f-expf( -(1.0f/44100.0f) * 2.0f * VG_PIf * freq );
119 }
120
121 static void dsp_init_lpf( struct dsp_lpf *lpf, float freq )
122 {
123 lpf->buffer = dsp_allocate( 4 );
124 lpf->buffer[0] = 0.0f;
125 dsp_update_lpf( lpf, freq );
126 }
127
128 static inline void dsp_write_lpf( struct dsp_lpf *lpf, float *s )
129 {
130 float diff = *s - lpf->buffer[0];
131 lpf->buffer[0] += diff * lpf->exponent;
132 }
133
134 static inline void dsp_read_lpf( struct dsp_lpf *lpf, float *s )
135 {
136 *s = lpf->buffer[0];
137 }
138
139 static void dsp_init_schroeder( struct dsp_schroeder *sch, float length,
140 float gain )
141 {
142 dsp_init_delay( &sch->M, length );
143 sch->gain = gain;
144 }
145
146 static inline void dsp_process_schroeder( struct dsp_schroeder *sch,
147 float *input, float *output )
148 {
149 float dry = *input;
150
151 float delay_output;
152 dsp_read_delay( &sch->M, &delay_output, 1 );
153
154 float feedback_attenuated = delay_output * sch->gain,
155 input_feedback_sum = dry + feedback_attenuated;
156
157 dsp_write_delay( &sch->M, &input_feedback_sum );
158
159 *output = delay_output - input_feedback_sum*sch->gain;
160 }
161
162 /* temporary global design */
163 static struct dsp_lpf __lpf_mud_free;
164 static struct dsp_delay __echos[8];
165
166 #ifdef VG_ECHO_LPF_BUTTERWORTH
167 static struct dsp_biquad __echos_lpf[8];
168 #else
169 static struct dsp_lpf __echos_lpf[8];
170 #endif
171 static struct dsp_schroeder __diffusion_chain[8];
172
173 static void async_vg_dsp_alloc_texture( void *payload, u32 size )
174 {
175 glGenTextures( 1, &vg_dsp.view_texture );
176 glBindTexture( GL_TEXTURE_2D, vg_dsp.view_texture );
177 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0,
178 GL_RGBA, GL_UNSIGNED_BYTE, vg_dsp.view_texture_buffer );
179 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
180 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
181 }
182
183 static void vg_dsp_init( void ){
184 vg_rand_seed( &vg_dsp.rand, 461 );
185 vg_dsp.buffer = vg_linear_alloc( vg_mem.rtmemory, 1024*1024*1 );
186 vg_dsp.view_texture_buffer = vg_linear_alloc( vg_mem.rtmemory, 512*512 );
187
188 vg_async_call( async_vg_dsp_alloc_texture, NULL, 0 );
189
190 /* temporary global design */
191 dsp_init_lpf( &__lpf_mud_free, 125.0f );
192
193 float sizes[] =
194 { 2.0f, 4.0f, 8.0f, 16.0f, 32.0f, 64.0f, 128.0f, 256.0f };
195
196 float variance = 0.1f;
197
198 for( int i=0; i<8; i++ ){
199 float reflection_time = ((sizes[i])/343.0f) * 1000.0f;
200
201 float var = 1.0f + (vg_randf64(&vg_dsp.rand)*2.0f - 1.0f) * variance,
202 total = reflection_time * var;
203
204 dsp_init_delay( &__echos[i], total / 1000.0f );
205
206 float freq = vg_lerpf( 800.0f, 350.0f, sizes[i] / 256.0f );
207
208 #ifdef VG_ECHO_LPF_BUTTERWORTH
209 dsp_init_biquad_butterworth_lpf( &__echos_lpf[i], freq );
210 #else
211 dsp_init_lpf( &__echos_lpf[i], freq );
212 #endif
213 }
214
215 float diffusions[] = { 187.0f, 159.0f, 143.0f, 121.0f,
216 79.0f, 57.0f, 27.0f, 11.0f };
217
218 for( int i=0; i<8; i++ ){
219 dsp_init_schroeder( __diffusion_chain+i, diffusions[i]/1000.0f, 0.7f );
220 }
221 }
222
223 static void vg_dsp_process( float *stereo_in, float *stereo_out )
224 {
225 float in_total = (stereo_in[0]+stereo_in[1])*0.5f;
226 float recieved = 0.0f;
227
228 for( int i=0; i<8; i++ ){
229 f32 echo;
230 dsp_read_delay( __echos+i, &echo, 1 );
231
232 #ifdef VG_ECHO_LPF_BUTTERWORTH
233 echo = dsp_biquad_process( __echos_lpf+i, echo );
234 #else
235 dsp_write_lpf( __echos_lpf+i, &echo );
236 dsp_read_lpf( __echos_lpf+i, &echo );
237 #endif
238
239 recieved += echo * vg_dsp.echo_tunings[i]*0.98;
240 }
241
242 float diffused = recieved;
243
244 for( int i=0; i<8; i++ ){
245 dsp_process_schroeder( __diffusion_chain+i, &diffused, &diffused );
246 }
247
248 float diffuse_mix = vg_dsp.reverb_wet_mix;
249 diffuse_mix = vg_lerpf( recieved, diffused, diffuse_mix );
250 float total = in_total + diffuse_mix;
251
252 float low_mud;
253 dsp_write_lpf( &__lpf_mud_free, &total );
254 dsp_read_lpf( &__lpf_mud_free, &low_mud );
255
256 total -= low_mud;
257
258 for( int i=0; i<8; i++ )
259 dsp_write_delay( __echos+i, &total );
260
261 stereo_out[0] = stereo_in[0]*vg_dsp.reverb_dry_mix;
262 stereo_out[1] = stereo_in[1]*vg_dsp.reverb_dry_mix;
263 stereo_out[0] += diffuse_mix*2.0f*vg_dsp.reverb_wet_mix;
264 stereo_out[1] += diffuse_mix*2.0f*vg_dsp.reverb_wet_mix;
265 }
266
267 static void dsp_update_tunings(void)
268 {
269 float sizes[] =
270 { 2.0f, 4.0f, 8.0f, 16.0f, 32.0f, 64.0f, 128.0f, 256.0f };
271 float volumes[] =
272 { 0.2f, 0.3f, 0.5f, 0.7f, 0.8f, 0.9f, 1.0f, 1.0f };
273
274 float avg_distance = 0.0f;
275
276 for( int i=0; i<8; i++ )
277 vg_dsp.echo_tunings[i] = 0.5f;
278
279 for( int j=0; j<14; j++ ){
280 float d = vg_dsp.echo_distances[j];
281
282 for( int i=0; i<7; i++ ){
283 if( d < sizes[i+1] ){
284 float range = sizes[i+1]-sizes[i];
285 float t = vg_clampf( (d - sizes[i])/range, 0.0f, 1.0f );
286
287 vg_dsp.echo_tunings[i ] += 1.0f-t;
288 vg_dsp.echo_tunings[i+1] += t;
289
290 break;
291 }
292 }
293
294 avg_distance += d;
295 }
296 avg_distance /= 14.0f;
297
298
299 vg_dsp.reverb_wet_mix =1.0f-vg_clampf((avg_distance-30.0f)/200.0f,0.0f,1.0f);
300 vg_dsp.reverb_dry_mix =1.0f-vg_dsp.reverb_wet_mix*0.4f;
301
302 float total = 0.0f;
303 for( int i=0; i<8; i++ )
304 total += vg_dsp.echo_tunings[i];
305
306 if( total > 0.0f ){
307 float inverse = 1.0f/total;
308
309 for( int i=0;i<8; i++ ){
310 vg_dsp.echo_tunings[i] *= inverse;
311 }
312 }
313
314 for( int i=0; i<8; i++ ){
315 float freq = vg_lerpf( 200.0f, 500.0f, vg_dsp.echo_tunings[i] );
316
317 #ifdef VG_ECHO_LPF_BUTTERWORTH
318 dsp_init_biquad_butterworth_lpf( &__echos_lpf[i], freq );
319 #else
320 dsp_update_lpf( &__echos_lpf[i], freq );
321 #endif
322 }
323
324 for( int i=0;i<8; i++ ){
325 vg_dsp.echo_tunings[i] *= volumes[i];
326 }
327 }
328
329 static void vg_dsp_free( void )
330 {
331 glDeleteTextures( 1, &vg_dsp.view_texture );
332 }
333
334 static void vg_dsp_update_texture( void )
335 {
336 for( int i=0; i<512*512; i++ ){
337 float v = vg_clampf( vg_dsp.buffer[i] * 0.5f + 0.5f, 0.0f, 1.0f );
338 vg_dsp.view_texture_buffer[i] = v * 255.0f;
339 }
340 }
341
342 #endif /* VG_AUDIO_DSP_H */