unfinished work
[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 {
209 struct synth_bird_signature *sig = &bird->settings.pattern[i];
210
211 u32 l = sig->length * (float)BIRD_SAMPLE_RATE,
212 p = sig->pause * (float)BIRD_SAMPLE_RATE;
213
214 total += l+p;
215 }
216
217 return total;
218 }
219
220 static void synth_bird_reset( struct synth_bird *bird )
221 {
222 bird->rt.osc_main[0] = 0;
223 bird->rt.osc_main[1] = 0;
224 bird->rt.osc_main[2] = 0;
225 bird->rt.osc_main[3] = 0;
226 bird->rt.osc_lfo = 0;
227
228 bird->rt.volume[0] = 0.0f;
229 bird->rt.volume[1] = 0.0f;
230 bird->rt.volume[2] = 0.0f;
231 bird->rt.volume[3] = 0.0f;
232
233 bird->rt.fundamental = 0.0f;
234 bird->rt.x = 0;
235 bird->rt.length = bird->settings.pattern[0].length * (float)BIRD_SAMPLE_RATE;
236 bird->rt.gate = 1;
237 bird->rt.adsr = 0;
238 bird->rt.frame = 0;
239 bird->rt.lfo_hz = 0;
240 bird->rt.fm_depth = 0.0f;
241
242 bird->rt.adsr_rise = bird->settings.adsr_rise * (float)BIRD_SAMPLE_RATE;
243 bird->rt.adsr_fall = bird->settings.adsr_fall * (float)BIRD_SAMPLE_RATE;
244 }
245
246 static u32 synth_bird_save_size( struct synth_bird *bird )
247 {
248 return sizeof(struct synth_bird_signature) * bird->settings.pattern_length +
249 sizeof(struct synth_bird_settings);
250 }
251
252 static void synth_bird_save( struct synth_bird *bird, void *txt )
253 {
254 void *src = &bird->settings;
255 vg_bin_str( src, txt, synth_bird_save_size( bird ) );
256 }
257
258 #if 0
259 static void synth_bird_load( struct synth_bird *bird,
260 const char *txt, u32 length )
261 {
262 vg_str_bin( txt, &bird->settings, length );
263 synth_bird_reset( bird );
264 }
265
266 /* expects a null terminated string */
267 static u32 synth_bird_memory_requirement( u32 string_length )
268 {
269 return (string_length/2) +
270 sizeof(struct synth_bird) - sizeof(struct synth_bird_settings);
271 }
272 #endif
273
274 #ifdef SYNTH_BIRD_STDLIB
275 #include "stdlib.h"
276 #include "string.h"
277
278 static struct synth_bird *synth_bird_create(
279 struct synth_bird_settings *settings,
280 struct synth_bird_signature *pattern,
281 u32 pattern_length )
282 {
283 u32 pattern_size = sizeof( struct synth_bird_signature ) * pattern_length;
284 u32 total_size = sizeof( struct synth_bird ) + pattern_size;
285 struct synth_bird *bird = malloc( total_size );
286
287 bird->settings = *settings;
288
289 memcpy( bird->settings.pattern, pattern, pattern_size );
290 bird->settings.pattern_length = pattern_length;
291
292 synth_bird_reset( bird );
293 return bird;
294 }
295
296 #endif
297
298 static void synth_bird_think( struct synth_bird *bird )
299 {
300 struct synth_bird_signature *sig = &bird->settings.pattern[ bird->rt.frame ];
301
302 bird->rt.x ++;
303 if( bird->rt.x >= bird->rt.length )
304 {
305 if( bird->rt.gate && (sig->pause != 0.0f) )
306 {
307 bird->rt.gate = 0;
308 bird->rt.length = sig->pause * (float)BIRD_SAMPLE_RATE;
309 }
310 else
311 {
312 bird->rt.frame ++;
313
314 if( bird->rt.frame >= bird->settings.pattern_length )
315 bird->rt.frame = 0;
316
317 sig = &bird->settings.pattern[ bird->rt.frame ];
318
319 bird->rt.gate = 1;
320 bird->rt.length = sig->length * (float)BIRD_SAMPLE_RATE;
321 }
322
323 bird->rt.x = 0;
324 }
325
326 if( bird->rt.gate )
327 {
328 bird->rt.adsr += bird->rt.adsr_rise;
329 if( bird->rt.adsr > BIRD_SAMPLE_RATE )
330 bird->rt.adsr = BIRD_SAMPLE_RATE;
331 }
332 else
333 {
334 bird->rt.adsr -= bird->rt.adsr_fall;
335 if( bird->rt.adsr < 0 )
336 bird->rt.adsr = 0;
337 }
338
339 if( bird->rt.gate )
340 {
341 float l = (float)bird->rt.length,
342 t = ((float)bird->rt.x * (1.0f/l))*2.0f - 1.0f,
343 tt = t*t,
344 ttt = tt*t;
345
346 bird->rt.fundamental = sig->x0 + t*sig->x1 + tt*sig->x2 + ttt*sig->x3;
347 }
348
349 float vol = (float)bird->rt.adsr * (1.0f/(float)BIRD_SAMPLE_RATE);
350
351 bird->rt.fm_depth = sig->fm;
352 bird->rt.lfo_hz = sig->lfo_hz;
353 bird->rt.volume[0] = sig->v0 * vol;
354 bird->rt.volume[1] = sig->v1 * vol;
355 bird->rt.volume[2] = sig->v2 * vol;
356 bird->rt.volume[3] = sig->v3 * vol;
357 }
358
359 static inline void int_add_mod( int *a, int const b )
360 {
361 *a += b;
362
363 if( *a > BIRD_SAMPLE_RATE )
364 *a -= BIRD_SAMPLE_RATE;
365 }
366
367 static void synth_bird_generate_samples( struct synth_bird *bird,
368 float *stereo_buffer, int samples )
369 {
370 for( int _=0; _<samples; _++ )
371 {
372 synth_bird_think( bird );
373
374 int_add_mod( &bird->rt.osc_lfo, bird->rt.lfo_hz );
375 float fm = sine_1second_1( bird->rt.osc_lfo ) * bird->rt.fm_depth;
376
377 int freq = bird->rt.fundamental + fm;
378 int hz[4] =
379 {
380 (freq * bird->settings.tones[0][0]) / bird->settings.tones[0][1],
381 (freq * bird->settings.tones[1][0]) / bird->settings.tones[1][1],
382 (freq * bird->settings.tones[2][0]) / bird->settings.tones[2][1],
383 (freq * bird->settings.tones[3][0]) / bird->settings.tones[3][1],
384 };
385
386 int_add_mod( bird->rt.osc_main + 0, hz[0] );
387 int_add_mod( bird->rt.osc_main + 1, hz[1] );
388 int_add_mod( bird->rt.osc_main + 2, hz[2] );
389 int_add_mod( bird->rt.osc_main + 3, hz[3] );
390
391 float v[4];
392 sine_1second_4( bird->rt.osc_main, v );
393
394 float s = v[0] * bird->rt.volume[0] +
395 v[1] * bird->rt.volume[1] +
396 v[2] * bird->rt.volume[2] +
397 v[3] * bird->rt.volume[3] ;
398
399 stereo_buffer[ _*2+0 ] = s;
400 stereo_buffer[ _*2+1 ] = s;
401 }
402 }
403
404 #ifdef SYNTH_BIRD_STDLIB
405 #include "stdio.h"
406
407 #define KNRM "\x1B[00m"
408 #define KRED "\x1B[31m"
409 #define KGRN "\x1B[32m"
410 #define KYEL "\x1B[33m"
411 #define KBLU "\x1B[34m"
412 #define KMAG "\x1B[35m"
413 #define KCYN "\x1B[36m"
414 #define KWHT "\x1B[37m"
415
416 #define LOG_BAR0 " . |"
417 #define LOG_BAR1 "-------+-------+-------+-------+"
418 #define LOG_BAR2 " "
419
420 static void synth_bird_log_pattern( struct synth_bird *bird )
421 {
422 synth_bird_reset( bird );
423
424 char output[][400]= {
425 KNRM "9k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
426 KNRM " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
427 KNRM "8k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
428 KNRM " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
429 KNRM "7k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
430 KNRM " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
431 KMAG "6k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
432 KMAG " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
433 KCYN "5k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
434 KCYN " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
435 KBLU "4k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
436 KBLU " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
437 KGRN "3k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
438 KGRN " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
439 KYEL "2k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
440 KYEL " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
441 KRED "1k-" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
442 KRED " -" LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0 LOG_BAR0,
443 KWHT " +" LOG_BAR1 LOG_BAR1 LOG_BAR1 LOG_BAR1 LOG_BAR1
444 KWHT " 0.0s"LOG_BAR2"0.5s"LOG_BAR2"1.0s"LOG_BAR2"1.5s"LOG_BAR2"2.0s"LOG_BAR2
445 };
446
447 for( int i=0; i<190*8; i++ )
448 {
449 for( int j=0; j<BIRD_SAMPLE_RATE/(32*8*2); j++ )
450 synth_bird_think( bird );
451
452 float hz = bird->rt.fundamental / 500.0f;
453 int j = hz;
454
455 if( j < 0 ) j = 0;
456 if( j > 18 ) j = 18;
457 j = 18-j;
458
459 float level = bird->rt.adsr;
460 level *= (1.0f/(float)BIRD_SAMPLE_RATE);
461
462 int ch = level*3.0f;
463
464 if( ch )
465 output[j][(i/8)+7] = " *###"[ch];
466 }
467
468 for( int i=0; i<20; i++ )
469 {
470 puts( output[i] );
471 }
472 }
473 #endif
474
475 #endif /* VG_AUDIO_SYNTH_BIRD_H */