fix uninitialized
[vg.git] / vg_audio_synth_bird.h
1 #ifndef VG_AUDIO_SYNTH_BIRD_H
2 #define VG_AUDIO_SYNTH_BIRD_H
3
4 #include "vg_binstr.h"
5 #include "vg_stdint.h"
6
7 #ifndef BIRD_SAMPLE_RATE
8 #define BIRD_SAMPLE_RATE 44100
9 #endif
10
11 struct synth_bird_signature{
12 float length, pause, /* timings in seconds */
13 x0,x1,x2,x3, /* polynomial coefficients for the fundemental */
14 v0,v1,v2,v3; /* volume of each oscillator */
15
16 int lfo_hz; /* LFO frequency (30-60hz) */
17 float fm; /* LFO modulation depth (+/- hz) */
18 };
19
20 struct synth_bird{
21 struct{
22 int osc_main[4],
23 osc_lfo;
24 float volume[4];
25
26 float fundamental;
27 int x, length, /* position/length of signature in samples */
28 gate, adsr, /* adsr ranges 0->44100 */
29 frame; /* current frame of the pattern */
30
31 int lfo_hz;
32 float fm_depth;
33
34 int adsr_rise,
35 adsr_fall;
36 }
37 rt;
38
39 struct synth_bird_settings{
40 int tones[4][2]; /* fraction of the fundemental tone
41 for each oscillator */
42 float adsr_rise, /* rise/fall in seconds */
43 adsr_fall;
44
45 enum bird_lfo_wave{
46 k_bird_lfo_sine_approx,
47 k_bird_lfo_bipolar_poly
48 }
49 type;
50
51 int pattern_length;
52 struct synth_bird_signature pattern[];
53 }
54 settings;
55 };
56
57 #define DEFAULT_VOL 1.0f,0.5f,0.2f,0.125f
58 #define DEFAULT_TONES { {1,1}, {6,5}, {8,7}, {13,12} }
59 #define DEFAULT_RISE 0.00090702947f
60 #define DEFAULT_FALL 0.00226757369f
61
62 static struct synth_bird_settings synth_bird__default_settings =
63 {
64 .tones = DEFAULT_TONES,
65 .type = k_bird_lfo_sine_approx,
66 .adsr_rise = DEFAULT_RISE,
67 .adsr_fall = DEFAULT_FALL
68 };
69
70 static struct synth_bird_signature synth_bird__warbling_vireo[] =
71 {
72 /* timing fundemental volumes lfo hz,depth */
73 {0.13,0.10, 4000,100,100,0, DEFAULT_VOL, 60,200 },
74 {0.10,0.05, 4200,-500,1700,0, DEFAULT_VOL, 60,96 },
75 {0.10,0.00, 2400,-1200,1000,1700, DEFAULT_VOL, 60,96 },
76 {0.06,0.04, 3100,200,-10,-1100, DEFAULT_VOL, 60,90 },
77 {0.13,0.07, 4600,-2000,0,1300, DEFAULT_VOL, 60,10 },
78 {0.05,0.00, 2700,-300,700,800, DEFAULT_VOL, 60,10 },
79 {0.09,0.07, 3600,-300,0,0, DEFAULT_VOL, 60,20 },
80 {0.05,0.07, 4800,1240,300,0, DEFAULT_VOL, 60,20 },
81 {0.08,0.02, 2700,-800,150,1000, DEFAULT_VOL, 60,160 },
82 {0.12,0.08, 2700,-800,150,1000, DEFAULT_VOL, 60,160 },
83 {0.10,0.04, 6300,-100,-3200,1000, DEFAULT_VOL, 60,100 },
84 {0.16,0.10, 4260,-200,300,1100, DEFAULT_VOL, 60,20 }
85 };
86
87 static struct synth_bird_signature synth_bird__pied_monarch[] =
88 {
89 /* timing fundemental volumes lfo hz,depth */
90 {0.18,0.13, 2200,700,-300,0, 0.6,0.05,0,0, 60,0 },
91 {0.17,0.12, 2200,700,-300,0, 0.8,0.05,0,0, 60,0 },
92 {0.16,0.11, 2200,700,-300,0, 0.9,0.05,0,0, 60,0 },
93 {0.14,0.09, 2200,700,-300,0, 1.0,0.05,0,0, 60,0 },
94 {0.12,0.07, 2200,700,-300,0, 1.0,0.05,0,0, 60,0 },
95 {0.11,0.06, 2200,700,-300,0, 1.0,0.05,0,0, 60,0 },
96 {0.10,0.05, 2200,700,-300,0, 1.0,0.05,0,0, 60,0 },
97 {0.10,0.05, 2200,700,-300,0, 1.0,0.05,0,0, 60,0 },
98 {0.10,0.05, 2200,700,-300,0, 1.0,0.05,0,0, 60,0 },
99 {0.10,0.05, 2200,700,-300,0, 1.0,0.05,0,0, 60,0 },
100 {0.10,0.05, 2200,700,-300,0, 1.0,0.05,0,0, 60,0 },
101 {0.10,0.10, 2200,700,-300,0, 1.0,0.05,0,0, 60,0 }
102 };
103
104 static struct synth_bird_signature synth_bird__bridled_honeyeater[] =
105 {
106 /* timing fundemental volumes lfo hz,depth */
107 {0.10,0.10, 2000,-1000,600,0, 1.00,0.00,0.00,0.00, 30,60},
108 {0.10,0.10, 4000,0,-200,-200, 0.80,0.25,0.25,0.25, 30,60},
109 {0.06,0.01, 4000,0,-700,-800, 0.90,0.10,0.00,0.00, 60,20},
110 {0.07,0.01, 3950,0,-700,-800, 0.90,0.10,0.00,0.00, 60,20},
111 {0.08,0.01, 3900,0,-700,-800, 0.90,0.10,0.00,0.00, 60,20},
112 {0.09,0.01, 3850,0,-700,-800, 0.90,0.10,0.00,0.00, 60,20},
113 {0.10,0.02, 3800,0,-700,-800, 0.90,0.20,0.10,0.00, 60,20},
114 {0.11,0.05, 3750,0,-700,-800, 0.90,0.40,0.20,0.00, 60,20},
115 {0.12,0.20, 3700,0,-700,-800, 0.30,0.10,0.00,0.00, 60,20},
116 {0.10,0.10, 2600,1300,600,0, 0.97,0.03,0.00,0.00, 60,20},
117 };
118
119 static struct synth_bird_signature synth_bird__cricket[] =
120 {
121 /* timing fundemental volumes lfo hz, depth */
122 {0.10,0.15, 5000,0,0,100, 0.25,0.25,0.25,0.25, 40,200},
123 {0.11,0.14, 5000,0,0,100, 0.25,0.25,0.25,0.25, 40,200},
124 {0.13,0.15, 5000,0,0,100, 0.25,0.25,0.25,0.25, 40,200},
125 {0.09,0.16, 5000,0,0,100, 0.25,0.25,0.25,0.25, 40,200},
126 {0.10,0.12, 5000,0,0,100, 0.25,0.25,0.25,0.25, 40,200},
127 {0.10,0.15, 5000,0,0,100, 0.25,0.25,0.25,0.25, 40,200},
128 {0.11,0.14, 5000,0,0,100, 0.25,0.25,0.25,0.25, 40,200},
129 {0.13,0.15, 5000,0,0,100, 0.25,0.25,0.25,0.25, 40,200},
130 {0.09,0.16, 5000,0,0,100, 0.25,0.25,0.25,0.25, 40,200},
131 {0.10,0.12, 5000,0,0,100, 0.25,0.25,0.25,0.25, 40,200}
132 };
133
134 static struct synth_bird_signature synth_bird__gray_shrikethrush[] =
135 {
136 /* timing fundemental volumes lfo hz, depth */
137 { 0.13,0.1, 2600,-200,-100,200, 0.9,0.1,0.05,0.001, 60,10 }
138 };
139
140 static struct synth_bird_signature synth_bird__boobook[] =
141 {
142 /* timing fundemental volumes lfo hz, depth */
143 {0.3,0.14, 700,0,-100,100, 0.9,0.14,0.0,0.2, 30,18},
144 {0.3,1.20, 630,0,-100,100, 0.9,0.00,0.3,0.0, 30,18}
145 };
146
147 static struct synth_bird_signature synth_bird__shrike_tit[] =
148 {
149 /* timing fundemental volumes lfo hz, depth */
150 {0.6,1.4, 2300,-300,-100,100, 1.0,0.14,0.0,0.1, 60,5 }
151 };
152
153 /* sine functions over the range [0, 44100] : [-pi, pi].
154 * Not accurate! */
155
156 static float sine_1second_1( int o )
157 {
158 float s = (o<(BIRD_SAMPLE_RATE/2))?-1.0f:1.0f;
159 float t = ((float)o*(1.0f/(float)(BIRD_SAMPLE_RATE/2)))-1.0f - s*0.5f;
160 float t2 = t*t;
161 float t4 = t2*t2;
162 return s*(5.0f*t2-4.0f*t4-1.0f);
163 }
164
165 static void sine_1second_4( int o[4], float v[4] )
166 {
167 float s[4],t[4],t2[4],t4[4];
168 s[0] = (o[0]<(BIRD_SAMPLE_RATE/2))?-1.0f:1.0f;
169 s[1] = (o[1]<(BIRD_SAMPLE_RATE/2))?-1.0f:1.0f;
170 s[2] = (o[2]<(BIRD_SAMPLE_RATE/2))?-1.0f:1.0f;
171 s[3] = (o[3]<(BIRD_SAMPLE_RATE/2))?-1.0f:1.0f;
172
173 t[0] = ((float)o[0]*(1.0f/(float)(BIRD_SAMPLE_RATE/2)))-1.0f - s[0]*0.5f;
174 t[1] = ((float)o[1]*(1.0f/(float)(BIRD_SAMPLE_RATE/2)))-1.0f - s[1]*0.5f;
175 t[2] = ((float)o[2]*(1.0f/(float)(BIRD_SAMPLE_RATE/2)))-1.0f - s[2]*0.5f;
176 t[3] = ((float)o[3]*(1.0f/(float)(BIRD_SAMPLE_RATE/2)))-1.0f - s[3]*0.5f;
177
178 t2[0] = t[0]*t[0];
179 t2[1] = t[1]*t[1];
180 t2[2] = t[2]*t[2];
181 t2[3] = t[3]*t[3];
182
183 t4[0] = t2[0]*t2[0];
184 t4[1] = t2[1]*t2[1];
185 t4[2] = t2[2]*t2[2];
186 t4[3] = t2[3]*t2[3];
187
188 v[0] = s[0]*(5.0f*t2[0]-4.0f*t4[0]-1.0f);
189 v[1] = s[1]*(5.0f*t2[1]-4.0f*t4[1]-1.0f);
190 v[2] = s[2]*(5.0f*t2[2]-4.0f*t4[2]-1.0f);
191 v[3] = s[3]*(5.0f*t2[3]-4.0f*t4[3]-1.0f);
192 }
193
194 static float saw_1second_1( int o )
195 {
196 float t = ((float)o*(1.0f/(float)(BIRD_SAMPLE_RATE/2)))-1.0f,
197 tt = t*t,
198 ttt = tt*t;
199
200 return -2.5f*ttt+2.5f*t;
201 }
202
203 static u32 synth_bird_get_length_in_samples( struct synth_bird *bird )
204 {
205 u32 total = 0;
206
207 for( int i=0; i<bird->settings.pattern_length; i ++ ){
208 struct synth_bird_signature *sig = &bird->settings.pattern[i];
209
210 u32 l = sig->length * (float)BIRD_SAMPLE_RATE,
211 p = sig->pause * (float)BIRD_SAMPLE_RATE;
212
213 total += l+p;
214 }
215
216 return total;
217 }
218
219 static void synth_bird_reset( struct synth_bird *bird )
220 {
221 bird->rt.osc_main[0] = 0;
222 bird->rt.osc_main[1] = 0;
223 bird->rt.osc_main[2] = 0;
224 bird->rt.osc_main[3] = 0;
225 bird->rt.osc_lfo = 0;
226
227 bird->rt.volume[0] = 0.0f;
228 bird->rt.volume[1] = 0.0f;
229 bird->rt.volume[2] = 0.0f;
230 bird->rt.volume[3] = 0.0f;
231
232 bird->rt.fundamental = 0.0f;
233 bird->rt.x = 0;
234 bird->rt.length = bird->settings.pattern[0].length * (float)BIRD_SAMPLE_RATE;
235 bird->rt.gate = 1;
236 bird->rt.adsr = 0;
237 bird->rt.frame = 0;
238 bird->rt.lfo_hz = 0;
239 bird->rt.fm_depth = 0.0f;
240
241 bird->rt.adsr_rise = bird->settings.adsr_rise * (float)BIRD_SAMPLE_RATE;
242 bird->rt.adsr_fall = bird->settings.adsr_fall * (float)BIRD_SAMPLE_RATE;
243 }
244
245 static u32 synth_bird_save_size( struct synth_bird *bird )
246 {
247 return sizeof(struct synth_bird_signature) * bird->settings.pattern_length +
248 sizeof(struct synth_bird_settings);
249 }
250
251 static void synth_bird_save( struct synth_bird *bird, void *txt )
252 {
253 void *src = &bird->settings;
254 vg_bin_str( src, txt, synth_bird_save_size( bird ) );
255 }
256
257 #if 0
258 static void synth_bird_load( struct synth_bird *bird,
259 const char *txt, u32 length )
260 {
261 vg_str_bin( txt, &bird->settings, length );
262 synth_bird_reset( bird );
263 }
264
265 /* expects a null terminated string */
266 static u32 synth_bird_memory_requirement( u32 string_length )
267 {
268 return (string_length/2) +
269 sizeof(struct synth_bird) - sizeof(struct synth_bird_settings);
270 }
271 #endif
272
273 #ifdef SYNTH_BIRD_STDLIB
274 #include "stdlib.h"
275 #include "string.h"
276
277 static struct synth_bird *synth_bird_create(
278 struct synth_bird_settings *settings,
279 struct synth_bird_signature *pattern,
280 u32 pattern_length )
281 {
282 u32 pattern_size = sizeof( struct synth_bird_signature ) * pattern_length;
283 u32 total_size = sizeof( struct synth_bird ) + pattern_size;
284 struct synth_bird *bird = malloc( total_size );
285
286 bird->settings = *settings;
287
288 memcpy( bird->settings.pattern, pattern, pattern_size );
289 bird->settings.pattern_length = pattern_length;
290
291 synth_bird_reset( bird );
292 return bird;
293 }
294
295 #endif
296
297 static void synth_bird_think( struct synth_bird *bird )
298 {
299 struct synth_bird_signature *sig = &bird->settings.pattern[ bird->rt.frame ];
300
301 bird->rt.x ++;
302 if( bird->rt.x >= bird->rt.length )
303 {
304 if( bird->rt.gate && (sig->pause != 0.0f) )
305 {
306 bird->rt.gate = 0;
307 bird->rt.length = sig->pause * (float)BIRD_SAMPLE_RATE;
308 }
309 else
310 {
311 bird->rt.frame ++;
312
313 if( bird->rt.frame >= bird->settings.pattern_length )
314 bird->rt.frame = 0;
315
316 sig = &bird->settings.pattern[ bird->rt.frame ];
317
318 bird->rt.gate = 1;
319 bird->rt.length = sig->length * (float)BIRD_SAMPLE_RATE;
320 }
321
322 bird->rt.x = 0;
323 }
324
325 if( bird->rt.gate )
326 {
327 bird->rt.adsr += bird->rt.adsr_rise;
328 if( bird->rt.adsr > BIRD_SAMPLE_RATE )
329 bird->rt.adsr = BIRD_SAMPLE_RATE;
330 }
331 else
332 {
333 bird->rt.adsr -= bird->rt.adsr_fall;
334 if( bird->rt.adsr < 0 )
335 bird->rt.adsr = 0;
336 }
337
338 if( bird->rt.gate )
339 {
340 float l = (float)bird->rt.length,
341 t = ((float)bird->rt.x * (1.0f/l))*2.0f - 1.0f,
342 tt = t*t,
343 ttt = tt*t;
344
345 bird->rt.fundamental = sig->x0 + t*sig->x1 + tt*sig->x2 + ttt*sig->x3;
346 }
347
348 float vol = (float)bird->rt.adsr * (1.0f/(float)BIRD_SAMPLE_RATE);
349
350 bird->rt.fm_depth = sig->fm;
351 bird->rt.lfo_hz = sig->lfo_hz;
352 bird->rt.volume[0] = sig->v0 * vol;
353 bird->rt.volume[1] = sig->v1 * vol;
354 bird->rt.volume[2] = sig->v2 * vol;
355 bird->rt.volume[3] = sig->v3 * vol;
356 }
357
358 static inline void int_add_mod( int *a, int const b )
359 {
360 *a += b;
361
362 if( *a > BIRD_SAMPLE_RATE )
363 *a -= BIRD_SAMPLE_RATE;
364 }
365
366 static void synth_bird_generate_samples( struct synth_bird *bird,
367 float *stereo_buffer, int samples )
368 {
369 for( int _=0; _<samples; _++ )
370 {
371 synth_bird_think( bird );
372
373 int_add_mod( &bird->rt.osc_lfo, bird->rt.lfo_hz );
374 float fm = sine_1second_1( bird->rt.osc_lfo ) * bird->rt.fm_depth;
375
376 int freq = bird->rt.fundamental + fm;
377 int hz[4] =
378 {
379 (freq * bird->settings.tones[0][0]) / bird->settings.tones[0][1],
380 (freq * bird->settings.tones[1][0]) / bird->settings.tones[1][1],
381 (freq * bird->settings.tones[2][0]) / bird->settings.tones[2][1],
382 (freq * bird->settings.tones[3][0]) / bird->settings.tones[3][1],
383 };
384
385 int_add_mod( bird->rt.osc_main + 0, hz[0] );
386 int_add_mod( bird->rt.osc_main + 1, hz[1] );
387 int_add_mod( bird->rt.osc_main + 2, hz[2] );
388 int_add_mod( bird->rt.osc_main + 3, hz[3] );
389
390 float v[4];
391 sine_1second_4( bird->rt.osc_main, v );
392
393 float s = v[0] * bird->rt.volume[0] +
394 v[1] * bird->rt.volume[1] +
395 v[2] * bird->rt.volume[2] +
396 v[3] * bird->rt.volume[3] ;
397
398 stereo_buffer[ _*2+0 ] = s;
399 stereo_buffer[ _*2+1 ] = s;
400 }
401 }
402
403 #ifdef SYNTH_BIRD_STDLIB
404 #include "stdio.h"
405
406 #define KNRM "\x1B[00m"
407 #define KRED "\x1B[31m"
408 #define KGRN "\x1B[32m"
409 #define KYEL "\x1B[33m"
410 #define KBLU "\x1B[34m"
411 #define KMAG "\x1B[35m"
412 #define KCYN "\x1B[36m"
413 #define KWHT "\x1B[37m"
414
415 #define LOG_BAR0 " . |"
416 #define LOG_BAR1 "-------+-------+-------+-------+"
417 #define LOG_BAR2 " "
418
419 static void synth_bird_log_pattern( struct synth_bird *bird )
420 {
421 synth_bird_reset( bird );
422
423 char output[][400]= {
424 KNRM "9k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
425 KNRM " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
426 KNRM "8k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
427 KNRM " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
428 KNRM "7k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
429 KNRM " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
430 KMAG "6k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
431 KMAG " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
432 KCYN "5k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
433 KCYN " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
434 KBLU "4k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
435 KBLU " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
436 KGRN "3k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
437 KGRN " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
438 KYEL "2k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
439 KYEL " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
440 KRED "1k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
441 KRED " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
442 KWHT " +" LOG_BAR1 LOG_BAR1 LOG_BAR1 LOG_BAR1 LOG_BAR1
443 KWHT " 0.0s"LOG_BAR2"0.5s"LOG_BAR2"1.0s"LOG_BAR2"1.5s"LOG_BAR2"2.0s"LOG_BAR2
444 };
445
446 for( int i=0; i<190*8; i++ )
447 {
448 for( int j=0; j<BIRD_SAMPLE_RATE/(32*8*2); j++ )
449 synth_bird_think( bird );
450
451 float hz = bird->rt.fundamental / 500.0f;
452 int j = hz;
453
454 if( j < 0 ) j = 0;
455 if( j > 18 ) j = 18;
456 j = 18-j;
457
458 float level = bird->rt.adsr;
459 level *= (1.0f/(float)BIRD_SAMPLE_RATE);
460
461 int ch = level*3.0f;
462
463 if( ch )
464 output[j][(i/8)+7] = " *###"[ch];
465 }
466
467 for( int i=0; i<20; i++ )
468 {
469 puts( output[i] );
470 }
471 }
472 #endif
473
474 #endif /* VG_AUDIO_SYNTH_BIRD_H */