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