compressors: added mkup_gain reset, fixed smooth_only
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / synthesizer / synthesizer.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2017 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcdisplayinfo.h"
23 #include "clip.h"
24 #include "bchash.h"
25 #include "filexml.h"
26 #include "language.h"
27 #include "samples.h"
28 #include "synthesizer.h"
29 #include "theme.h"
30 #include "transportque.inc"
31 #include "vframe.h"
32
33 #include <string.h>
34
35
36
37
38 REGISTER_PLUGIN(Synth)
39
40
41
42
43 Synth::Synth(PluginServer *server)
44  : PluginAClient(server)
45 {
46         reset();
47         window_w = xS(640);
48         window_h = yS(480);
49 }
50
51
52
53 Synth::~Synth()
54 {
55
56 }
57
58 NEW_WINDOW_MACRO(Synth, SynthWindow);
59
60 const char* Synth::plugin_title() { return N_("Synthesizer"); }
61 int Synth::is_realtime() { return 1; }
62 int Synth::is_synthesis() { return 1; }
63
64
65 void Synth::reset()
66 {
67         need_reconfigure = 1;
68 }
69
70
71
72
73 LOAD_CONFIGURATION_MACRO(Synth, SynthConfig)
74
75
76
77
78
79
80 void Synth::read_data(KeyFrame *keyframe)
81 {
82         FileXML input;
83         char string[BCTEXTLEN];
84 // cause htal file to read directly from text
85         input.set_shared_input(keyframe->xbuf);
86
87 //printf("Synth::read_data %s\n", keyframe->get_data());
88         int result = 0, current_osc = 0;
89         while(!result)
90         {
91                 result = input.read_tag();
92
93                 if(!result)
94                 {
95                         if(input.tag.title_is("SYNTH"))
96                         {
97                                 config.wetness = input.tag.get_property("WETNESS", config.wetness);
98
99                                 if(is_defaults())
100                                 {
101                                         window_w = input.tag.get_property("WINDOW_W", window_w);
102                                         window_h = input.tag.get_property("WINDOW_H", window_h);
103                                 }
104                                 config.momentary_notes = input.tag.get_property("MOMENTARY_NOTES", config.momentary_notes);
105
106 //printf("Synth::read_data %d %d %d\n", __LINE__, window_w, window_h);
107                                 for(int i = 0; i < MAX_FREQS; i++)
108                                 {
109                                         sprintf(string, "BASEFREQ_%d", i);
110                                         config.base_freq[i] = input.tag.get_property(string, (double)0);
111                                 }
112
113                                 config.wavefunction = input.tag.get_property("WAVEFUNCTION", config.wavefunction);
114                                 // int total_oscillators = input.tag.get_property("OSCILLATORS", 0);
115                         }
116                         else
117                         if(input.tag.title_is("OSCILLATOR"))
118                         {
119                                 if(current_osc >= config.oscillator_config.total)
120                                         config.oscillator_config.append(new SynthOscillatorConfig(current_osc));
121
122                                 config.oscillator_config.values[current_osc]->read_data(&input);
123                                 current_osc++;
124                         }
125                 }
126         }
127
128         while(config.oscillator_config.total > current_osc)
129                 config.oscillator_config.remove_object();
130 }
131
132 void Synth::save_data(KeyFrame *keyframe)
133 {
134         FileXML output;
135         char string[BCTEXTLEN];
136
137 // cause htal file to store data directly in text
138         output.set_shared_output(keyframe->xbuf);
139
140         output.tag.set_title("SYNTH");
141         output.tag.set_property("WETNESS", config.wetness);
142         output.tag.set_property("WINDOW_W", window_w);
143         output.tag.set_property("WINDOW_H", window_h);
144         output.tag.set_property("MOMENTARY_NOTES", config.momentary_notes);
145
146         for(int i = 0; i < MAX_FREQS; i++)
147         {
148 //              if(!EQUIV(config.base_freq[i], 0))
149                 {
150                         sprintf(string, "BASEFREQ_%d", i);
151                         output.tag.set_property(string, config.base_freq[i]);
152 //printf("Synth::save_data %d %s\n", __LINE__, string);
153                 }
154         }
155
156         output.tag.set_property("WAVEFUNCTION", config.wavefunction);
157         output.tag.set_property("OSCILLATORS", config.oscillator_config.total);
158         output.append_tag();
159         output.append_newline();
160
161         for(int i = 0; i < config.oscillator_config.total; i++)
162         {
163                 config.oscillator_config.values[i]->save_data(&output);
164         }
165
166         output.terminate_string();
167 //printf("Synth::save_data %d %s\n", __LINE__, output.string);
168 // data is now in *text
169 }
170
171
172
173 void Synth::update_gui()
174 {
175         if(thread)
176         {
177                 if(load_configuration())
178                 {
179                         thread->window->lock_window();
180                         ((SynthWindow*)thread->window)->update_gui();
181                         thread->window->unlock_window();
182                 }
183         }
184 }
185
186
187 void Synth::add_oscillator()
188 {
189         if(config.oscillator_config.total > MAX_OSCILLATORS) return;
190
191         config.oscillator_config.append(new SynthOscillatorConfig(config.oscillator_config.total - 1));
192 }
193
194 void Synth::delete_oscillator()
195 {
196         if(config.oscillator_config.total)
197         {
198                 config.oscillator_config.remove_object();
199         }
200 }
201
202
203 double Synth::get_total_power()
204 {
205         double result = 0;
206
207         if(config.wavefunction == DC) return 1.0;
208
209         for(int i = 0; i < config.oscillator_config.total; i++)
210         {
211                 result += db.fromdb(config.oscillator_config.values[i]->level);
212         }
213
214         if(result == 0) result = 1;  // prevent division by 0
215         return result;
216 }
217
218
219 double Synth::solve_eqn(double *output,
220         int length,
221         double freq,
222         double normalize_constant,
223         int oscillator)
224 {
225         SynthOscillatorConfig *config =
226                 this->config.oscillator_config.values[oscillator];
227         if(config->level <= INFINITYGAIN) return 0;
228
229         double power = this->db.fromdb(config->level) * normalize_constant;
230 // Period of fundamental frequency in samples
231         double orig_period = (double)get_samplerate() /
232                 freq;
233 // Starting sample in waveform
234         double x = waveform_sample;
235 //printf("Synth::solve_eqn %d %f\n", __LINE__, config->phase);
236         double phase_offset = config->phase * orig_period;
237 // Period of current oscillator
238         double period = orig_period / config->freq_factor;
239         int sample;
240         double step = 1;
241         if(get_direction() == PLAY_REVERSE) step = -1;
242
243         switch(this->config.wavefunction)
244         {
245                 case DC:
246                         for(sample = 0; sample < length; sample++)
247                         {
248                                 output[sample] += power;
249                         }
250                         break;
251
252                 case SINE:
253                         for(sample = 0; sample < length; sample++)
254                         {
255                                 output[sample] += sin((x + phase_offset) /
256                                         period * 2 * M_PI) * power;
257                                 x += step;
258                         }
259                         break;
260
261                 case SAWTOOTH:
262                         for(sample = 0; sample < length; sample++)
263                         {
264                                 output[sample] += function_sawtooth((x + phase_offset) /
265                                         period) * power;
266                                 x += step;
267                         }
268                         break;
269
270                 case SQUARE:
271                         for(sample = 0; sample < length; sample++)
272                         {
273                                 output[sample] += function_square((x + phase_offset) /
274                                         period) * power;
275                                 x += step;
276                         }
277                         break;
278
279                 case TRIANGLE:
280                         for(sample = 0; sample < length; sample++)
281                         {
282                                 output[sample] += function_triangle((x + phase_offset) /
283                                         period) * power;
284                                 x += step;
285                         }
286                         break;
287
288                 case PULSE:
289                         for(sample = 0; sample < length; sample++)
290                         {
291                                 output[sample] += function_pulse((x + phase_offset) /
292                                         period) * power;
293                                 x += step;
294                         }
295                         break;
296
297                 case NOISE:
298                         for(sample = 0; sample < length; sample++)
299                         {
300                                 output[sample] += function_noise() * power;
301                         }
302                         break;
303         }
304         return 0;
305 }
306
307 double Synth::get_point(float x, double normalize_constant)
308 {
309         double result = 0;
310         for(int i = 0; i < config.oscillator_config.total; i++)
311                 result += get_oscillator_point(x, normalize_constant, i);
312
313         return result;
314 }
315
316 double Synth::get_oscillator_point(float x,
317                 double normalize_constant,
318                 int oscillator)
319 {
320         SynthOscillatorConfig *config = this->config.oscillator_config.values[oscillator];
321         double power = db.fromdb(config->level) * normalize_constant;
322         switch(this->config.wavefunction)
323         {
324                 case DC:
325                         return power;
326                         break;
327                 case SINE:
328                         return sin((x + config->phase) * config->freq_factor * 2 * M_PI) * power;
329                         break;
330                 case SAWTOOTH:
331                         return function_sawtooth((x + config->phase) * config->freq_factor) * power;
332                         break;
333                 case SQUARE:
334                         return function_square((x + config->phase) * config->freq_factor) * power;
335                         break;
336                 case TRIANGLE:
337                         return function_triangle((x + config->phase) * config->freq_factor) * power;
338                         break;
339                 case PULSE:
340                         return function_pulse((x + config->phase) * config->freq_factor) * power;
341                         break;
342                 case NOISE:
343                         return function_noise() * power;
344                         break;
345         }
346         return 0;
347 }
348
349 double Synth::function_square(double x)
350 {
351         x -= (int)x; // only fraction counts
352         return (x < .5) ? -1 : 1;
353 }
354
355 double Synth::function_pulse(double x)
356 {
357         x -= (int)x; // only fraction counts
358         return (x < .5) ? 0 : 1;
359 }
360
361 double Synth::function_noise()
362 {
363         return (double)(rand() % 65536 - 32768) / 32768;
364 }
365
366 double Synth::function_sawtooth(double x)
367 {
368         x -= (int)x;
369         return 1 - x * 2;
370 }
371
372 double Synth::function_triangle(double x)
373 {
374         x -= (int)x;
375         return (x < .5) ? 1 - x * 4 : -3 + x * 4;
376 }
377
378 int Synth::process_realtime(int64_t size,
379         Samples *input_ptr,
380         Samples *output_ptr)
381 {
382 // sample relative to start of plugin
383         waveform_sample = get_source_position();
384
385         need_reconfigure |= load_configuration();
386         if(need_reconfigure) reconfigure();
387
388         double wetness = DB::fromdb(config.wetness);
389         if(EQUIV(config.wetness, INFINITYGAIN)) wetness = 0;
390
391 // Apply wetness
392         double *output_samples = output_ptr->get_data();
393         double *input_samples = input_ptr->get_data();
394         for(int j = 0; j < size; j++)
395                 output_samples[j] = input_samples[j] * wetness;
396
397 // Overlay each frequency
398         for(int j = 0; j < MAX_FREQS; j++)
399         {
400                 if(!EQUIV(config.base_freq[j], 0))
401                 {
402
403 // Compute fragment
404                         overlay_synth(
405                                 config.base_freq[j],
406                                 size,
407                                 input_ptr->get_data(),
408                                 output_ptr->get_data());
409         //printf("Synth::process_realtime 2\n");
410                 }
411         }
412
413 //      waveform_sample += size;
414         return 0;
415 }
416
417 int Synth::overlay_synth(double freq,
418         int64_t length,
419         double *input,
420         double *output)
421 {
422         double normalize_constant = 1.0 / get_total_power();
423         for(int i = 0; i < config.oscillator_config.total; i++)
424                 solve_eqn(output, length, freq, normalize_constant, i);
425         return length;
426 }
427
428 void Synth::reconfigure()
429 {
430         need_reconfigure = 0;
431 //      waveform_sample = 0;
432 }
433
434 int Synth::freq_exists(double freq)
435 {
436         for(int i = 0; i < MAX_FREQS; i++)
437         {
438
439 // printf("Synth::freq_exists %d %d %f %f\n",
440 // __LINE__,
441 // i,
442 // freq,
443 // config.base_freq[i]);
444                 if(fabs(freq - config.base_freq[i]) < 1.0)
445                         return 1;
446         }
447
448         return 0;
449 }
450
451 void Synth::new_freq(double freq)
452 {
453 // Check for dupes
454         for(int i = 0; i < MAX_FREQS; i++)
455         {
456                 if(EQUIV(config.base_freq[i], freq)) return;
457         }
458
459         for(int i = 0; i < MAX_FREQS; i++)
460         {
461                 if(EQUIV(config.base_freq[i], 0))
462                 {
463                         config.base_freq[i] = freq;
464 //printf("Synth::new_freq %d\n", __LINE__);
465                         break;
466                 }
467         }
468 }
469
470 void Synth::delete_freq(double freq)
471 {
472         for(int i = 0; i < MAX_FREQS; i++)
473         {
474                 if(EQUIV(config.base_freq[i], freq))
475                 {
476 //printf("Synth::delete_freq %d\n", __LINE__);
477 // Shift frequencies back
478                         for(int j = i; j < MAX_FREQS - 1; j++)
479                         {
480                                 config.base_freq[j] = config.base_freq[j + 1];
481                         }
482                         config.base_freq[MAX_FREQS - 1] = 0;
483
484                         i--;
485                 }
486         }
487 }
488
489 void Synth::delete_freqs()
490 {
491         for(int i = 0; i < MAX_FREQS; i++)
492                 config.base_freq[i] = 0;
493 }
494
495
496 SynthWindow::SynthWindow(Synth *synth)
497  : PluginClientWindow(synth, synth->window_w, synth->window_h,
498                 xS(400), yS(350), 1)
499 {
500         this->synth = synth;
501         white_key[0] = 0;
502         white_key[1] = 0;
503         white_key[2] = 0;
504         black_key[0] = 0;
505         black_key[1] = 0;
506         black_key[2] = 0;
507         bzero(notes, sizeof(SynthNote*) * TOTALNOTES);
508         current_note = -1;
509 }
510
511 SynthWindow::~SynthWindow()
512 {
513         delete white_key[0];
514         delete white_key[1];
515         delete white_key[2];
516         delete black_key[0];
517         delete black_key[1];
518         delete black_key[2];
519 }
520
521 static const char *keyboard_map[] =
522 {
523         "q", "2", "w", "3", "e", "r", "5", "t", "6", "y", "7", "u",
524         "z", "s", "x", "d", "c", "v", "g", "b", "h", "n", "j", "m"
525 };
526
527 void SynthWindow::create_objects()
528 {
529         int margin = client->get_theme()->widget_border;
530
531         BC_MenuBar *menu;
532         add_subwindow(menu = new BC_MenuBar(0, 0, get_w()));
533
534         BC_Menu *levelmenu, *phasemenu, *harmonicmenu;
535         menu->add_menu(levelmenu = new BC_Menu(_("Level")));
536         menu->add_menu(phasemenu = new BC_Menu(_("Phase")));
537         menu->add_menu(harmonicmenu = new BC_Menu(_("Harmonic")));
538
539         levelmenu->add_item(new SynthLevelInvert(synth));
540         levelmenu->add_item(new SynthLevelMax(synth));
541         levelmenu->add_item(new SynthLevelRandom(synth));
542         levelmenu->add_item(new SynthLevelSine(synth));
543         levelmenu->add_item(new SynthLevelSlope(synth));
544         levelmenu->add_item(new SynthLevelZero(synth));
545
546         phasemenu->add_item(new SynthPhaseInvert(synth));
547         phasemenu->add_item(new SynthPhaseRandom(synth));
548         phasemenu->add_item(new SynthPhaseSine(synth));
549         phasemenu->add_item(new SynthPhaseZero(synth));
550
551         harmonicmenu->add_item(new SynthFreqMin(synth));
552         harmonicmenu->add_item(new SynthFreqEnum(synth));
553         harmonicmenu->add_item(new SynthFreqEven(synth));
554         harmonicmenu->add_item(new SynthFreqFibonacci(synth));
555         harmonicmenu->add_item(new SynthFreqOdd(synth));
556         harmonicmenu->add_item(new SynthFreqPrime(synth));
557
558         int xs10 = xS(10), xs20 = xS(20), xs50 = xS(50), xs70 = xS(70), xs75 = xS(75), xs240 = xS(240), xs265 = xS(265);
559         int ys10 = yS(10), ys20 = yS(20), ys30 = yS(30), ys40 = yS(40), ys220 = yS(220);
560         int x = xs10, y = ys30;
561
562         add_subwindow(new BC_Title(x, y, _("Waveform")));
563         x += xs240;
564         add_subwindow(new BC_Title(x, y, _("Wave Function")));
565         y += ys20;
566         x = xs10;
567         add_subwindow(canvas = new SynthCanvas(synth, this, x, y, xS(230), yS(160)));
568         canvas->update();
569
570         x += xs240;
571         char string[BCTEXTLEN];
572         waveform_to_text(string, synth->config.wavefunction);
573
574         add_subwindow(waveform = new SynthWaveForm(synth, x, y, string));
575         waveform->create_objects();
576         y += ys30;
577         int x1 = x + waveform->get_w() + xs50;
578
579
580         add_subwindow(new BC_Title(x, y, _("Base Frequency:")));
581         y += ys30;
582         add_subwindow(base_freq = new SynthBaseFreq(synth, this, x, y));
583         base_freq->update((float)synth->config.base_freq[0]);
584         x += base_freq->get_w() + synth->get_theme()->widget_border;
585         add_subwindow(freqpot = new SynthFreqPot(synth, this, x, y - ys10));
586         base_freq->freq_pot = freqpot;
587         freqpot->freq_text = base_freq;
588         x -= base_freq->get_w() + synth->get_theme()->widget_border;
589         y += ys40;
590         add_subwindow(new BC_Title(x, y, _("Wetness:")));
591         add_subwindow(wetness = new SynthWetness(synth, x + xs70, y - ys10));
592
593         y += ys40;
594         add_subwindow(new SynthClear(synth, x, y));
595
596
597         x = xs50;
598         y = ys220;
599         add_subwindow(new BC_Title(x, y, _("Level")));
600         x += xs75;
601         add_subwindow(new BC_Title(x, y, _("Phase")));
602         x += xs75;
603         add_subwindow(new BC_Title(x, y, _("Harmonic")));
604
605
606
607         y += ys20; x = xs10;
608         add_subwindow(osc_subwindow = new BC_SubWindow(x, y, xs265, get_h() - y));
609         x += xs265;
610         add_subwindow(osc_scroll = new OscScroll(synth, this, x, y, get_h() - y));
611
612
613         x += xs20;
614         add_subwindow(new SynthAddOsc(synth, this, x, y));
615         y += ys30;
616         add_subwindow(new SynthDelOsc(synth, this, x, y));
617
618 // Create keyboard
619         y = ys30;
620
621 #include "white_up_png.h"
622 #include "white_hi_png.h"
623 #include "white_dn_png.h"
624 #include "white_checked_png.h"
625 #include "white_checkedhi_png.h"
626 #include "black_up_png.h"
627 #include "black_hi_png.h"
628 #include "black_dn_png.h"
629 #include "black_checked_png.h"
630 #include "black_checkedhi_png.h"
631         white_key[0] = new VFramePng(white_up_png);
632         white_key[1] = new VFramePng(white_hi_png);
633         white_key[2] = new VFramePng(white_checked_png);
634         white_key[3] = new VFramePng(white_dn_png);
635         white_key[4] = new VFramePng(white_checkedhi_png);
636         black_key[0] = new VFramePng(black_up_png);
637         black_key[1] = new VFramePng(black_hi_png);
638         black_key[2] = new VFramePng(black_checked_png);
639         black_key[3] = new VFramePng(black_dn_png);
640         black_key[4] = new VFramePng(black_checkedhi_png);
641
642
643         add_subwindow(note_subwindow = new BC_SubWindow(x1+xS(20),
644                 y, get_w() - (x1+xS(20)),
645                 white_key[0]->get_h() + margin +
646                 get_text_height(MEDIUMFONT) + margin +
647                 get_text_height(MEDIUMFONT) + margin));
648         add_subwindow(note_scroll = new NoteScroll(synth, this, x1,
649                 note_subwindow->get_y() + note_subwindow->get_h(),
650                 note_subwindow->get_w()));
651
652         add_subwindow(momentary = new SynthMomentary(this, x1,
653                 note_scroll->get_y() + note_scroll->get_h() + margin,
654                 _("Momentary notes")));
655
656
657         add_subwindow(note_instructions = new BC_Title( x1,
658                 momentary->get_y() + momentary->get_h() + margin,
659                 _("Ctrl or Shift to select multiple notes.")));
660
661         update_scrollbar();
662         update_oscillators();
663         update_notes();
664
665         show_window();
666 }
667
668 int SynthWindow::keypress_event()
669 {
670         if(ctrl_down() && get_keypress() == 'w')
671         {
672                 set_done(1);
673                 return 1;
674         }
675         return 0;
676 }
677
678 int SynthWindow::resize_event(int w, int h)
679 {
680         clear_box(0, 0, w, h);
681         osc_subwindow->reposition_window(osc_subwindow->get_x(),
682                 osc_subwindow->get_y(),
683                 osc_subwindow->get_w(),
684                 h - osc_subwindow->get_y());
685         osc_subwindow->clear_box(0, 0, osc_subwindow->get_w(), osc_subwindow->get_h());
686         osc_scroll->reposition_window(osc_scroll->get_x(),
687                 osc_scroll->get_y(),
688                 h - osc_scroll->get_y());
689         note_subwindow->reposition_window(note_subwindow->get_x(),
690                 note_subwindow->get_y(),
691                 w - note_subwindow->get_x(),
692                 note_subwindow->get_h());
693         note_scroll->reposition_window(note_scroll->get_x(),
694                 note_scroll->get_y(),
695                 w - note_scroll->get_x());
696         note_scroll->update_length(white_key[0]->get_w() * TOTALNOTES * 7 / 12 +
697                         white_key[0]->get_w(),
698                 note_scroll->get_position(),
699                 note_subwindow->get_w(),
700                 0);
701
702         update_scrollbar();
703         update_notes();
704         update_oscillators();
705         synth->window_w = w;
706         synth->window_h = h;
707         return 1;
708 }
709
710 void SynthWindow::update_gui()
711 {
712         char string[BCTEXTLEN];
713         freqpot->update((int)synth->config.base_freq[0]);
714         base_freq->update((float)synth->config.base_freq[0]);
715         wetness->update(synth->config.wetness);
716         waveform_to_text(string, synth->config.wavefunction);
717         waveform->set_text(string);
718         momentary->update(synth->config.momentary_notes);
719
720         update_scrollbar();
721         update_oscillators();
722         canvas->update();
723         update_note_selection();
724         show_window();
725 }
726
727 void SynthWindow::update_scrollbar()
728 {
729         osc_scroll->update_length(synth->config.oscillator_config.total * OSCILLATORHEIGHT,
730                 osc_scroll->get_position(),
731                 osc_subwindow->get_h(),
732                 0);
733 }
734
735
736 void SynthWindow::update_whitekey(int number,
737         int *current_title,
738         int x,
739         int y)
740 {
741         if(!notes[number])
742         {
743                 note_subwindow->add_subwindow(notes[number] = new SynthNote(this,
744                         white_key, number, x, y));
745                 if(number >= FIRST_TITLE && number < LAST_TITLE)
746                         note_subwindow->add_subwindow(
747                                 note_titles[(*current_title)++] = new BC_Title(
748                                         x + text_white_margin, y2,
749                                         keyboard_map[number - FIRST_TITLE]));
750 //printf("SynthWindow::update_whitekey %d\n", __LINE__);
751         }
752         else
753         {
754                 notes[number]->reposition_window(x, y);
755                 if(number >= FIRST_TITLE && number < LAST_TITLE)
756                         note_titles[(*current_title)++]->reposition_window(x + text_white_margin,
757                                         y2);
758         }
759 }
760
761
762 void SynthWindow::update_blackkey(int number,
763         int *current_title,
764         int x,
765         int y)
766 {
767         if(!notes[number])
768         {
769                 note_subwindow->add_subwindow(notes[number] = new SynthNote(this,
770                         black_key, number, x, y));
771                 if(number >= FIRST_TITLE && number < LAST_TITLE)
772                         note_subwindow->add_subwindow(
773                                 note_titles[(*current_title)++] = new BC_Title(x + text_black_margin,
774                                         y1, keyboard_map[number - FIRST_TITLE]));
775         }
776         else
777         {
778                 notes[number]->reposition_window(x, y);
779                 if(number >= FIRST_TITLE && number < LAST_TITLE)
780                         note_titles[(*current_title)++]->reposition_window(x + text_black_margin, y1);
781         }
782 }
783
784 void SynthWindow::update_notes()
785 {
786         //int octave_w = white_key[0]->get_w() * 7;
787         int white_w = white_key[0]->get_w();
788         int black_w = black_key[0]->get_w();
789         int white_w1 = white_w - black_w / 2 - 2;
790         int white_w2 = white_w / 2;
791         int white_w3 = white_w * 2 / 3;
792         int x = 0, y = 0, ys5 = yS(5);
793         y1 = y + white_key[0]->get_h() + ys5;
794         y2 = y1 + get_text_height(MEDIUMFONT) + ys5;
795         y3 = y2 + get_text_height(MEDIUMFONT) + ys5;
796         text_black_margin = black_w / 2 - get_text_width(MEDIUMFONT, "O") / 2;
797         text_white_margin = white_w / 2 - get_text_width(MEDIUMFONT, "O") / 2;
798
799
800 //printf("SynthWindow::update_notes %d\n", __LINE__);
801         note_subwindow->clear_box(0, 0, get_w(), get_h());
802
803         note_subwindow->set_color(get_resources()->default_text_color);
804
805 // Add new notes
806 // To get the stacking order:
807 // pass 0 is white keys
808 // pass 1 is black keys
809         int current_title = 0;
810         for(int pass = 0; pass < 2; pass++)
811         {
812                 x = -note_scroll->get_position();
813
814                 for(int i = 0; i < TOTALNOTES; i++)
815                 {
816                         int octave_note = i % 12;
817                         if(!pass)
818                         {
819 // White keys
820                                 switch(octave_note)
821                                 {
822                                         case 0:
823                                                 update_whitekey(i, &current_title, x, y);
824                                                 break;
825                                         case 1:
826                                                 x += white_w;
827                                                 break;
828                                         case 2:
829                                                 update_whitekey(i, &current_title, x, y);
830                                                 break;
831                                         case 3:
832                                                 x += white_w;
833                                                 break;
834                                         case 4:
835                                                 update_whitekey(i, &current_title, x, y);
836                                                 x += white_w;
837                                                 break;
838                                         case 5:
839                                                 update_whitekey(i, &current_title, x, y);
840                                                 break;
841                                         case 6:
842                                                 x += white_w;
843                                                 break;
844                                         case 7:
845                                                 update_whitekey(i, &current_title, x, y);
846                                                 break;
847                                         case 8:
848                                                 x += white_w;
849                                                 break;
850                                         case 9:
851                                                 update_whitekey(i, &current_title, x, y);
852                                                 break;
853                                         case 10:
854                                                 x += white_w;
855                                                 break;
856                                         case 11:
857                                                 update_whitekey(i, &current_title, x, y);
858                                                 x += white_w;
859                                                 break;
860                                 }
861                         }
862                         else
863                         {
864 // Black keys
865                                 switch(octave_note)
866                                 {
867                                         case 1:
868                                                 update_blackkey(i, &current_title, x + white_w2, y);
869                                                 x += white_w;
870                                                 break;
871                                         case 3:
872                                                 update_blackkey(i, &current_title, x + white_w3, y);
873                                                 x += white_w;
874                                                 break;
875                                         case 4:
876                                                 x += white_w;
877                                                 break;
878                                         case 6:
879                                                 update_blackkey(i, &current_title, x + white_w2, y);
880                                                 x += white_w;
881                                                 break;
882                                         case 8:
883                                                 update_blackkey(i, &current_title, x + white_w1, y);
884                                                 x += white_w;
885                                                 break;
886                                         case 10:
887                                                 update_blackkey(i, &current_title, x + white_w3, y);
888                                                 x += white_w;
889                                                 break;
890                                         case 11:
891                                                 x += white_w;
892                                                 break;
893                                 }
894                         }
895                 }
896         }
897 }
898
899 void SynthWindow::update_note_selection()
900 {
901         for(int i = 0; i < TOTALNOTES; i++)
902         {
903                 int got_it = 0;
904                 for(int j = 0; j < MAX_FREQS; j++)
905                 {
906                         if(synth->freq_exists(keyboard_freqs[notes[i]->number]))
907                         {
908                                 got_it = 1;
909                                 break;
910                         }
911                 }
912
913                 if(got_it)
914                 {
915                         notes[i]->set_value(1);
916                 }
917                 else
918                         notes[i]->set_value(0);
919         }
920 }
921
922
923 void SynthWindow::update_oscillators()
924 {
925         int i, y = -osc_scroll->get_position();
926
927
928
929 // Add new oscillators
930         for(i = 0;
931                 i < synth->config.oscillator_config.total;
932                 i++)
933         {
934                 SynthOscGUI *gui;
935                 SynthOscillatorConfig *config = synth->config.oscillator_config.values[i];
936
937                 if(oscillators.total <= i)
938                 {
939                         oscillators.append(gui = new SynthOscGUI(this, i));
940                         gui->create_objects(y);
941                 }
942                 else
943                 {
944                         gui = oscillators.values[i];
945
946                         gui->title->reposition_window(gui->title->get_x(), y + yS(15));
947
948                         gui->level->reposition_window(gui->level->get_x(), y);
949                         gui->level->update(config->level);
950
951                         gui->phase->reposition_window(gui->phase->get_x(), y);
952                         gui->phase->update((int64_t)(config->phase * 360));
953
954                         gui->freq->reposition_window(gui->freq->get_x(), y);
955                         gui->freq->update(config->freq_factor);
956                 }
957                 y += OSCILLATORHEIGHT;
958         }
959
960 // Delete old oscillators
961         for( ;
962                 i < oscillators.total;
963                 i++)
964                 oscillators.remove_object();
965 }
966
967
968 int SynthWindow::waveform_to_text(char *text, int waveform)
969 {
970         switch(waveform)
971         {
972                 case DC:              sprintf(text, _("DC"));           break;
973                 case SINE:            sprintf(text, _("Sine"));           break;
974                 case SAWTOOTH:        sprintf(text, _("Sawtooth"));       break;
975                 case SQUARE:          sprintf(text, _("Square"));         break;
976                 case TRIANGLE:        sprintf(text, _("Triangle"));       break;
977                 case PULSE:           sprintf(text, _("Pulse"));       break;
978                 case NOISE:           sprintf(text, _("Noise"));       break;
979         }
980         return 0;
981 }
982
983
984 SynthMomentary::SynthMomentary(SynthWindow *window, int x, int y, char *text)
985  : BC_CheckBox(x, y, window->synth->config.momentary_notes, text)
986 {
987         this->window = window;
988 }
989
990 int SynthMomentary::handle_event()
991 {
992         window->synth->config.momentary_notes = get_value();
993         window->synth->send_configure_change();
994         return 1;
995 }
996
997
998
999
1000 SynthNote::SynthNote(SynthWindow *window, VFrame **images, int number, int x, int y)
1001  : BC_Toggle(x, y, images, window->synth->freq_exists(keyboard_freqs[number]))
1002 {
1003         this->window = window;
1004         this->number = number;
1005         note_on = 0;
1006         set_select_drag(1);
1007         set_radial(1);
1008 }
1009
1010 void SynthNote::start_note()
1011 {
1012         if(window->synth->config.momentary_notes) note_on = 1;
1013
1014 //printf("SynthNote::start_note %d %d\n", __LINE__, ctrl_down());
1015         if(window->synth->config.momentary_notes || (!ctrl_down() && !shift_down()))
1016         {
1017 // Kill all frequencies
1018                 window->synth->delete_freqs();
1019         }
1020
1021         window->synth->new_freq(keyboard_freqs[number]);
1022         window->base_freq->update((float)window->synth->config.base_freq[0]);
1023 //printf("SynthNote::start_note %d %f\n", __LINE__, window->synth->config.base_freq[0]);
1024         window->freqpot->update(window->synth->config.base_freq[0]);
1025         window->synth->send_configure_change();
1026         window->update_note_selection();
1027 }
1028
1029 void SynthNote::stop_note()
1030 {
1031         note_on = 0;
1032         window->synth->delete_freq(keyboard_freqs[number]);
1033         window->base_freq->update((float)window->synth->config.base_freq[0]);
1034         window->freqpot->update(window->synth->config.base_freq[0]);
1035         window->synth->send_configure_change();
1036         window->update_note_selection();
1037 }
1038
1039 int SynthNote::keypress_event()
1040 {
1041         if(number >= FIRST_TITLE && number < LAST_TITLE)
1042         {
1043                 if(get_keypress() == keyboard_map[number - FIRST_TITLE][0])
1044                 {
1045                         if((ctrl_down() || shift_down()) &&
1046                                 window->synth->freq_exists(keyboard_freqs[number]))
1047                         {
1048                                 stop_note();
1049                         }
1050                         else
1051                         {
1052                                 start_note();
1053                                 set_value(1);
1054                         }
1055
1056 // Key releases are repeated, so momentary notes may not work
1057                         return 1;
1058                 }
1059         }
1060         return 0;
1061 }
1062
1063 int SynthNote::keyrelease_event()
1064 {
1065         if(note_on && window->synth->config.momentary_notes)
1066         {
1067                 stop_note();
1068                 set_value(0);
1069                 return 1;
1070         }
1071         return 0;
1072 }
1073
1074 int SynthNote::cursor_motion_event()
1075 {
1076         int result = 0;
1077         if(window->current_note > -1)
1078         {
1079                 int cursor_x = get_relative_cursor_x();
1080                 int cursor_y = get_relative_cursor_y();
1081                 if(cursor_x >= 0 && cursor_x < get_w() &&
1082                         cursor_y >= 0 && cursor_y < get_h())
1083                 {
1084                         if(window->starting_notes)
1085                         {
1086                                 start_note();
1087                         }
1088                         else
1089                         {
1090                                 stop_note();
1091                         }
1092
1093                         window->current_note = number;
1094                         result = 1;
1095                 }
1096         }
1097         return result;
1098 }
1099
1100 int SynthNote::button_press_event()
1101 {
1102         if(BC_Toggle::button_press_event())
1103         {
1104 // printf("SynthNote::button_press_event %d %d %d\n",
1105 // __LINE__,
1106 // ctrl_down(),
1107 // window->synth->freq_exists(keyboard_freqs[number]));
1108                 window->starting_notes = 1;
1109                 if((ctrl_down() || shift_down()) &&
1110                         window->synth->freq_exists(keyboard_freqs[number]))
1111                 {
1112 //printf("SynthNote::button_press_event %d\n", __LINE__);
1113                         stop_note();
1114                         window->starting_notes = 0;
1115                 }
1116                 else
1117                 {
1118 //printf("SynthNote::button_press_event %d\n", __LINE__);
1119                         start_note();
1120                         window->starting_notes = 1;
1121                 }
1122                 window->current_note = number;
1123                 return 1;
1124         }
1125         return 0;
1126 }
1127
1128 int SynthNote::button_release_event()
1129 {
1130 // Change frequency permanently
1131         if(window->current_note == number)
1132         {
1133                 if(window->synth->config.momentary_notes)
1134                 {
1135 // Mute on button release
1136                         stop_note();
1137                         set_value(0);
1138                 }
1139                 window->current_note = -1;
1140         }
1141
1142         return BC_Toggle::button_release_event();
1143 }
1144
1145 int SynthNote::draw_face(int flash, int flush)
1146 {
1147         BC_Toggle::draw_face(0, 0);
1148         static const char *titles[] =
1149         {
1150                 "C",
1151                 "C#",
1152                 "D",
1153                 "D#",
1154                 "E",
1155                 "F",
1156                 "F#",
1157                 "G",
1158                 "G#",
1159                 "A",
1160                 "A#",
1161                 "B"
1162         };
1163
1164
1165         const char *text = titles[number % (sizeof(titles) / sizeof(char*))];
1166         char string[BCTEXTLEN];
1167         sprintf(string, "%s%d", text, number / 12);
1168 //printf("SynthNote::draw_face %d %d %d %d %s\n", __LINE__, number, get_w(), get_h(), text);
1169         if(text[1] == '#')
1170         {
1171         }
1172         else
1173         {
1174                 set_color(BLACK);
1175                 draw_text(get_w() / 2 - get_text_width(MEDIUMFONT, string) / 2,
1176                         get_h() - get_text_height(MEDIUMFONT, string) - window->synth->get_theme()->widget_border,
1177                         string);
1178         }
1179
1180         if(flash) this->flash(0);
1181         if(flush) this->flush();
1182         return 0;
1183 }
1184
1185
1186
1187
1188
1189
1190
1191 SynthOscGUI::SynthOscGUI(SynthWindow *window, int number)
1192 {
1193         this->window = window;
1194         this->number = number;
1195 }
1196
1197 SynthOscGUI::~SynthOscGUI()
1198 {
1199         delete title;
1200         delete level;
1201         delete phase;
1202         delete freq;
1203 }
1204
1205 void SynthOscGUI::create_objects(int y)
1206 {
1207         char text[BCTEXTLEN];
1208         sprintf(text, "%d:", number + 1);
1209         window->osc_subwindow->add_subwindow(title = new BC_Title(xS(10), y+yS(15), text));
1210
1211         window->osc_subwindow->add_subwindow(level = new SynthOscGUILevel(window->synth, this, y));
1212         window->osc_subwindow->add_subwindow(phase = new SynthOscGUIPhase(window->synth, this, y));
1213         window->osc_subwindow->add_subwindow(freq = new SynthOscGUIFreq(window->synth, this, y));
1214 }
1215
1216
1217
1218
1219 SynthOscGUILevel::SynthOscGUILevel(Synth *synth, SynthOscGUI *gui, int y)
1220  : BC_FPot(xS(50), y,
1221         synth->config.oscillator_config.values[gui->number]->level,
1222         INFINITYGAIN, 0)
1223 {
1224         this->synth = synth;
1225         this->gui = gui;
1226 }
1227
1228 SynthOscGUILevel::~SynthOscGUILevel()
1229 {
1230 }
1231
1232 int SynthOscGUILevel::handle_event()
1233 {
1234         SynthOscillatorConfig *config = synth->config.oscillator_config.values[gui->number];
1235         config->level = get_value();
1236         gui->window->canvas->update();
1237         synth->send_configure_change();
1238         return 1;
1239 }
1240
1241
1242
1243 SynthOscGUIPhase::SynthOscGUIPhase(Synth *synth, SynthOscGUI *gui, int y)
1244  : BC_IPot(xS(125), y,
1245         (int64_t)(synth->config.oscillator_config.values[gui->number]->phase * 360),
1246         0, 360)
1247 {
1248         this->synth = synth;
1249         this->gui = gui;
1250 }
1251
1252 SynthOscGUIPhase::~SynthOscGUIPhase()
1253 {
1254 }
1255
1256 int SynthOscGUIPhase::handle_event()
1257 {
1258         SynthOscillatorConfig *config = synth->config.oscillator_config.values[gui->number];
1259         config->phase = (float)get_value() / 360;
1260         gui->window->canvas->update();
1261         synth->send_configure_change();
1262         return 1;
1263 }
1264
1265
1266
1267 SynthOscGUIFreq::SynthOscGUIFreq(Synth *synth, SynthOscGUI *gui, int y)
1268  : BC_FPot(xS(200), y,
1269         (int64_t)(synth->config.oscillator_config.values[gui->number]->freq_factor),
1270         1, 100)
1271 {
1272         this->synth = synth;
1273         this->gui = gui;
1274 }
1275
1276 SynthOscGUIFreq::~SynthOscGUIFreq()
1277 {
1278 }
1279
1280 int SynthOscGUIFreq::handle_event()
1281 {
1282         SynthOscillatorConfig *config = synth->config.oscillator_config.values[gui->number];
1283         config->freq_factor = get_value();
1284         gui->window->canvas->update();
1285         synth->send_configure_change();
1286         return 1;
1287 }
1288
1289
1290 SynthAddOsc::SynthAddOsc(Synth *synth, SynthWindow *window, int x, int y)
1291  : BC_GenericButton(x, y, _("Add"))
1292 {
1293         this->synth = synth;
1294         this->window = window;
1295 }
1296
1297 SynthAddOsc::~SynthAddOsc()
1298 {
1299 }
1300
1301 int SynthAddOsc::handle_event()
1302 {
1303         synth->add_oscillator();
1304         synth->send_configure_change();
1305         window->update_gui();
1306         return 1;
1307 }
1308
1309
1310 SynthDelOsc::SynthDelOsc(Synth *synth, SynthWindow *window, int x, int y)
1311  : BC_GenericButton(x, y, _("Delete"))
1312 {
1313         this->synth = synth;
1314         this->window = window;
1315 }
1316
1317 SynthDelOsc::~SynthDelOsc()
1318 {
1319 }
1320
1321 int SynthDelOsc::handle_event()
1322 {
1323         synth->delete_oscillator();
1324         synth->send_configure_change();
1325         window->update_gui();
1326         return 1;
1327 }
1328
1329
1330 OscScroll::OscScroll(Synth *synth, SynthWindow *window,
1331                 int x, int y, int h)
1332  : BC_ScrollBar(x, y, SCROLL_VERT, h,
1333         synth->config.oscillator_config.total * OSCILLATORHEIGHT,
1334         0, window->osc_subwindow->get_h())
1335 {
1336         this->synth = synth;
1337         this->window = window;
1338 }
1339
1340 OscScroll::~OscScroll()
1341 {
1342 }
1343
1344 int OscScroll::handle_event()
1345 {
1346         window->update_oscillators();
1347         return 1;
1348 }
1349
1350
1351
1352 NoteScroll::NoteScroll(Synth *synth, SynthWindow *window,
1353                 int x, int y, int w)
1354  : BC_ScrollBar(x, y, SCROLL_HORIZ, w,
1355         window->white_key[0]->get_w() * TOTALNOTES * 7 / 12 + window->white_key[0]->get_w(),
1356         0, window->note_subwindow->get_w())
1357 {
1358         this->synth = synth;
1359         this->window = window;
1360 }
1361
1362 NoteScroll::~NoteScroll()
1363 {
1364 }
1365
1366 int NoteScroll::handle_event()
1367 {
1368         window->update_notes();
1369         return 1;
1370 }
1371
1372
1373 SynthClear::SynthClear(Synth *synth, int x, int y)
1374  : BC_GenericButton(x, y, _("Clear"))
1375 {
1376         this->synth = synth;
1377 }
1378 SynthClear::~SynthClear()
1379 {
1380 }
1381 int SynthClear::handle_event()
1382 {
1383         synth->config.reset();
1384         synth->send_configure_change();
1385         synth->update_gui();
1386         return 1;
1387 }
1388
1389
1390
1391
1392
1393
1394 SynthWaveForm::SynthWaveForm(Synth *synth, int x, int y, char *text)
1395  : BC_PopupMenu(x, y, xS(120), text)
1396 {
1397         this->synth = synth;
1398 }
1399
1400 SynthWaveForm::~SynthWaveForm()
1401 {
1402 }
1403
1404 void SynthWaveForm::create_objects()
1405 {
1406 //      add_item(new SynthWaveFormItem(synth, _("DC"), DC));
1407         add_item(new SynthWaveFormItem(synth, _("Sine"), SINE));
1408         add_item(new SynthWaveFormItem(synth, _("Sawtooth"), SAWTOOTH));
1409         add_item(new SynthWaveFormItem(synth, _("Square"), SQUARE));
1410         add_item(new SynthWaveFormItem(synth, _("Triangle"), TRIANGLE));
1411         add_item(new SynthWaveFormItem(synth, _("Pulse"), PULSE));
1412         add_item(new SynthWaveFormItem(synth, _("Noise"), NOISE));
1413 }
1414
1415 SynthWaveFormItem::SynthWaveFormItem(Synth *synth, char *text, int value)
1416  : BC_MenuItem(text)
1417 {
1418         this->synth = synth;
1419         this->value = value;
1420 }
1421
1422 SynthWaveFormItem::~SynthWaveFormItem()
1423 {
1424 }
1425
1426 int SynthWaveFormItem::handle_event()
1427 {
1428         synth->config.wavefunction = value;
1429         ((SynthWindow*)synth->thread->window)->canvas->update();
1430         get_popup_menu()->set_text(get_text());
1431         synth->send_configure_change();
1432         return 1;
1433 }
1434
1435
1436 SynthWetness::SynthWetness(Synth *synth, int x, int y)
1437  : BC_FPot(x, y, synth->config.wetness, INFINITYGAIN, 0)
1438 {
1439         this->synth = synth;
1440 }
1441
1442 int SynthWetness::handle_event()
1443 {
1444         synth->config.wetness = get_value();
1445         synth->send_configure_change();
1446         return 1;
1447 }
1448
1449
1450
1451 SynthFreqPot::SynthFreqPot(Synth *synth, SynthWindow *window, int x, int y)
1452  : BC_QPot(x, y, synth->config.base_freq[0])
1453 {
1454         this->synth = synth;
1455         this->window = window;
1456 }
1457 SynthFreqPot::~SynthFreqPot()
1458 {
1459 }
1460 int SynthFreqPot::handle_event()
1461 {
1462         if(get_value() > 0 && get_value() < 30000)
1463         {
1464                 synth->config.base_freq[0] = get_value();
1465                 freq_text->update(get_value());
1466                 synth->send_configure_change();
1467                 window->update_note_selection();
1468         }
1469         return 1;
1470 }
1471
1472
1473
1474 SynthBaseFreq::SynthBaseFreq(Synth *synth, SynthWindow *window, int x, int y)
1475  : BC_TextBox(x, y, xS(100), 1, (float)0)
1476 {
1477         this->synth = synth;
1478         this->window = window;
1479         set_precision(2);
1480 }
1481 SynthBaseFreq::~SynthBaseFreq()
1482 {
1483 }
1484 int SynthBaseFreq::handle_event()
1485 {
1486         double new_value = atof(get_text());
1487 // 0 is mute
1488         if(new_value < 30000)
1489         {
1490                 synth->config.base_freq[0] = new_value;
1491                 freq_pot->update(synth->config.base_freq[0]);
1492                 synth->send_configure_change();
1493                 window->update_note_selection();
1494         }
1495         return 1;
1496 }
1497
1498
1499
1500
1501
1502 SynthCanvas::SynthCanvas(Synth *synth, SynthWindow *window,
1503                 int x, int y, int w, int h)
1504  : BC_SubWindow(x, y, w, h, BLACK)
1505 {
1506         this->synth = synth;
1507         this->window = window;
1508 }
1509
1510 SynthCanvas::~SynthCanvas()
1511 {
1512 }
1513
1514 int SynthCanvas::update()
1515 {
1516         int y1, y2, y = 0;
1517
1518         clear_box(0, 0, get_w(), get_h());
1519         set_color(RED);
1520
1521         draw_line(0, get_h() / 2 + y, get_w(), get_h() / 2 + y);
1522
1523         set_color(GREEN);
1524
1525         double normalize_constant = (double)1 / synth->get_total_power();
1526         y1 = (int)(synth->get_point((float)0, normalize_constant) * get_h() / 2);
1527
1528         for(int i = 1; i < get_w(); i++)
1529         {
1530                 y2 = (int)(synth->get_point((float)i / get_w(), normalize_constant) * get_h() / 2);
1531                 draw_line(i - 1, get_h() / 2 - y1, i, get_h() / 2 - y2);
1532                 y1 = y2;
1533         }
1534         flash();
1535         return 0;
1536 }
1537
1538
1539 // ======================= level calculations
1540 SynthLevelZero::SynthLevelZero(Synth *synth)
1541  : BC_MenuItem(_("Zero"))
1542 {
1543         this->synth = synth;
1544 }
1545
1546 SynthLevelZero::~SynthLevelZero()
1547 {
1548 }
1549
1550 int SynthLevelZero::handle_event()
1551 {
1552         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1553         {
1554                 synth->config.oscillator_config.values[i]->level = INFINITYGAIN;
1555         }
1556
1557         ((SynthWindow*)synth->thread->window)->update_gui();
1558         synth->send_configure_change();
1559         return 1;
1560 }
1561
1562 SynthLevelMax::SynthLevelMax(Synth *synth)
1563  : BC_MenuItem(_("Maximum"))
1564 {
1565         this->synth = synth;
1566 }
1567
1568 SynthLevelMax::~SynthLevelMax()
1569 {
1570 }
1571
1572 int SynthLevelMax::handle_event()
1573 {
1574         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1575         {
1576                 synth->config.oscillator_config.values[i]->level = 0;
1577         }
1578         ((SynthWindow*)synth->thread->window)->update_gui();
1579         synth->send_configure_change();
1580         return 1;
1581 }
1582
1583 SynthLevelNormalize::SynthLevelNormalize(Synth *synth)
1584  : BC_MenuItem(_("Normalize"))
1585 {
1586         this->synth = synth;
1587 }
1588
1589 SynthLevelNormalize::~SynthLevelNormalize()
1590 {
1591 }
1592
1593 int SynthLevelNormalize::handle_event()
1594 {
1595 // get total power
1596         float total = 0;
1597
1598         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1599         {
1600                 total += synth->db.fromdb(synth->config.oscillator_config.values[i]->level);
1601         }
1602
1603         float scale = 1 / total;
1604         float new_value;
1605
1606         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1607         {
1608                 new_value = synth->db.fromdb(synth->config.oscillator_config.values[i]->level);
1609                 new_value *= scale;
1610                 new_value = synth->db.todb(new_value);
1611
1612                 synth->config.oscillator_config.values[i]->level = new_value;
1613         }
1614
1615         ((SynthWindow*)synth->thread->window)->update_gui();
1616         synth->send_configure_change();
1617         return 1;
1618 }
1619
1620 SynthLevelSlope::SynthLevelSlope(Synth *synth)
1621  : BC_MenuItem(_("Slope"))
1622 {
1623         this->synth = synth;
1624 }
1625
1626 SynthLevelSlope::~SynthLevelSlope()
1627 {
1628 }
1629
1630 int SynthLevelSlope::handle_event()
1631 {
1632         float slope = (float)INFINITYGAIN / synth->config.oscillator_config.total;
1633
1634         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1635         {
1636                 synth->config.oscillator_config.values[i]->level = i * slope;
1637         }
1638
1639         ((SynthWindow*)synth->thread->window)->update_gui();
1640         synth->send_configure_change();
1641         return 1;
1642 }
1643
1644 SynthLevelRandom::SynthLevelRandom(Synth *synth)
1645  : BC_MenuItem(_("Random"))
1646 {
1647         this->synth = synth;
1648 }
1649 SynthLevelRandom::~SynthLevelRandom()
1650 {
1651 }
1652
1653 int SynthLevelRandom::handle_event()
1654 {
1655         srand(time(0));
1656         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1657         {
1658                 synth->config.oscillator_config.values[i]->level = -(rand() % -INFINITYGAIN);
1659         }
1660
1661         ((SynthWindow*)synth->thread->window)->update_gui();
1662         synth->send_configure_change();
1663         return 1;
1664 }
1665
1666 SynthLevelInvert::SynthLevelInvert(Synth *synth)
1667  : BC_MenuItem(_("Invert"))
1668 {
1669         this->synth = synth;
1670 }
1671 SynthLevelInvert::~SynthLevelInvert()
1672 {
1673 }
1674
1675 int SynthLevelInvert::handle_event()
1676 {
1677         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1678         {
1679                 synth->config.oscillator_config.values[i]->level =
1680                         INFINITYGAIN - synth->config.oscillator_config.values[i]->level;
1681         }
1682
1683         ((SynthWindow*)synth->thread->window)->update_gui();
1684         synth->send_configure_change();
1685         return 1;
1686 }
1687
1688 SynthLevelSine::SynthLevelSine(Synth *synth)
1689  : BC_MenuItem(_("Sine"))
1690 {
1691         this->synth = synth;
1692 }
1693 SynthLevelSine::~SynthLevelSine()
1694 {
1695 }
1696
1697 int SynthLevelSine::handle_event()
1698 {
1699         float new_value;
1700
1701         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1702         {
1703                 new_value = (float)i / synth->config.oscillator_config.total * 2 * M_PI;
1704                 new_value = sin(new_value) * INFINITYGAIN / 2 + INFINITYGAIN / 2;
1705                 synth->config.oscillator_config.values[i]->level = new_value;
1706         }
1707
1708         ((SynthWindow*)synth->thread->window)->update_gui();
1709         synth->send_configure_change();
1710         return 1;
1711 }
1712
1713 // ============================ phase calculations
1714
1715 SynthPhaseInvert::SynthPhaseInvert(Synth *synth)
1716  : BC_MenuItem(_("Invert"))
1717 {
1718         this->synth = synth;
1719 }
1720 SynthPhaseInvert::~SynthPhaseInvert()
1721 {
1722 }
1723
1724 int SynthPhaseInvert::handle_event()
1725 {
1726         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1727         {
1728                 synth->config.oscillator_config.values[i]->phase =
1729                         1 - synth->config.oscillator_config.values[i]->phase;
1730         }
1731
1732         ((SynthWindow*)synth->thread->window)->update_gui();
1733         synth->send_configure_change();
1734         return 1;
1735 }
1736
1737 SynthPhaseZero::SynthPhaseZero(Synth *synth)
1738  : BC_MenuItem(_("Zero"))
1739 {
1740         this->synth = synth;
1741 }
1742 SynthPhaseZero::~SynthPhaseZero()
1743 {
1744 }
1745
1746 int SynthPhaseZero::handle_event()
1747 {
1748         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1749         {
1750                 synth->config.oscillator_config.values[i]->phase = 0;
1751         }
1752
1753         ((SynthWindow*)synth->thread->window)->update_gui();
1754         synth->send_configure_change();
1755         return 1;
1756 }
1757
1758 SynthPhaseSine::SynthPhaseSine(Synth *synth)
1759  : BC_MenuItem(_("Sine"))
1760 {
1761         this->synth = synth;
1762 }
1763 SynthPhaseSine::~SynthPhaseSine()
1764 {
1765 }
1766
1767 int SynthPhaseSine::handle_event()
1768 {
1769         float new_value;
1770         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1771         {
1772                 new_value = (float)i / synth->config.oscillator_config.total * 2 * M_PI;
1773                 new_value = sin(new_value) / 2 + .5;
1774                 synth->config.oscillator_config.values[i]->phase = new_value;
1775         }
1776
1777         ((SynthWindow*)synth->thread->window)->update_gui();
1778         synth->send_configure_change();
1779         return 1;
1780 }
1781
1782 SynthPhaseRandom::SynthPhaseRandom(Synth *synth)
1783  : BC_MenuItem(_("Random"))
1784 {
1785         this->synth = synth;
1786 }
1787 SynthPhaseRandom::~SynthPhaseRandom()
1788 {
1789 }
1790
1791 int SynthPhaseRandom::handle_event()
1792 {
1793         srand(time(0));
1794         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1795         {
1796                 synth->config.oscillator_config.values[i]->phase =
1797                         (float)(rand() % 360) / 360;
1798         }
1799
1800         ((SynthWindow*)synth->thread->window)->update_gui();
1801         synth->send_configure_change();
1802         return 1;
1803 }
1804
1805
1806 // ============================ freq calculations
1807
1808 SynthFreqRandom::SynthFreqRandom(Synth *synth)
1809  : BC_MenuItem(_("Random"))
1810 {
1811         this->synth = synth;
1812 }
1813 SynthFreqRandom::~SynthFreqRandom()
1814 {
1815 }
1816
1817 int SynthFreqRandom::handle_event()
1818 {
1819         srand(time(0));
1820         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1821         {
1822                 synth->config.oscillator_config.values[i]->freq_factor = rand() % 100;
1823         }
1824
1825         ((SynthWindow*)synth->thread->window)->update_gui();
1826         synth->send_configure_change();
1827         return 1;
1828 }
1829
1830 SynthFreqPow1::SynthFreqPow1(Synth *synth)
1831  : BC_MenuItem(_("Powers of 1.4"))
1832 {
1833         this->synth = synth;
1834 }
1835 SynthFreqPow1::~SynthFreqPow1()
1836 {
1837 }
1838
1839 int SynthFreqPow1::handle_event()
1840 {
1841         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1842         {
1843                 synth->config.oscillator_config.values[i]->freq_factor = pow(sqrt(2), i);
1844         }
1845
1846         ((SynthWindow*)synth->thread->window)->update_gui();
1847         synth->send_configure_change();
1848         return 1;
1849 }
1850
1851
1852 SynthFreqPow2::SynthFreqPow2(Synth *synth)
1853  : BC_MenuItem(_("Powers of 2"))
1854 {
1855         this->synth = synth;
1856 }
1857 SynthFreqPow2::~SynthFreqPow2()
1858 {
1859 }
1860
1861 int SynthFreqPow2::handle_event()
1862 {
1863         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1864         {
1865                 synth->config.oscillator_config.values[i]->freq_factor = pow(2, i);
1866         }
1867
1868         ((SynthWindow*)synth->thread->window)->update_gui();
1869         synth->send_configure_change();
1870         return 1;
1871 }
1872
1873
1874
1875
1876 SynthFreqMin::SynthFreqMin(Synth *synth)
1877  : BC_MenuItem(_("Minimum"))
1878 {
1879         this->synth = synth;
1880 }
1881 SynthFreqMin::~SynthFreqMin()
1882 {
1883 }
1884
1885 int SynthFreqMin::handle_event()
1886 {
1887         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1888         {
1889                 synth->config.oscillator_config.values[i]->freq_factor = 1;
1890         }
1891
1892         ((SynthWindow*)synth->thread->window)->update_gui();
1893         synth->send_configure_change();
1894         return 1;
1895 }
1896
1897
1898 SynthFreqEnum::SynthFreqEnum(Synth *synth)
1899  : BC_MenuItem(_("Enumerate"))
1900 {
1901         this->synth = synth;
1902 }
1903 SynthFreqEnum::~SynthFreqEnum()
1904 {
1905 }
1906
1907 int SynthFreqEnum::handle_event()
1908 {
1909         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1910         {
1911                 synth->config.oscillator_config.values[i]->freq_factor = (float)i + 1;
1912         }
1913
1914         ((SynthWindow*)synth->thread->window)->update_gui();
1915         synth->send_configure_change();
1916         return 1;
1917 }
1918
1919 SynthFreqEven::SynthFreqEven(Synth *synth)
1920  : BC_MenuItem(_("Even"))
1921 {
1922         this->synth = synth;
1923 }
1924 SynthFreqEven::~SynthFreqEven()
1925 {
1926 }
1927
1928 int SynthFreqEven::handle_event()
1929 {
1930         if(synth->config.oscillator_config.total)
1931                 synth->config.oscillator_config.values[0]->freq_factor = (float)1;
1932
1933         for(int i = 1; i < synth->config.oscillator_config.total; i++)
1934         {
1935                 synth->config.oscillator_config.values[i]->freq_factor = (float)i * 2;
1936         }
1937
1938         ((SynthWindow*)synth->thread->window)->update_gui();
1939         synth->send_configure_change();
1940         return 1;
1941 }
1942
1943 SynthFreqOdd::SynthFreqOdd(Synth *synth)
1944  : BC_MenuItem(_("Odd"))
1945 { this->synth = synth; }
1946 SynthFreqOdd::~SynthFreqOdd()
1947 {
1948 }
1949
1950 int SynthFreqOdd::handle_event()
1951 {
1952         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1953         {
1954                 synth->config.oscillator_config.values[i]->freq_factor = (float)1 + i * 2;
1955         }
1956
1957         ((SynthWindow*)synth->thread->window)->update_gui();
1958         synth->send_configure_change();
1959         return 1;
1960 }
1961
1962 SynthFreqFibonacci::SynthFreqFibonacci(Synth *synth)
1963  : BC_MenuItem(_("Fibonnacci"))
1964 {
1965         this->synth = synth;
1966 }
1967 SynthFreqFibonacci::~SynthFreqFibonacci()
1968 {
1969 }
1970
1971 int SynthFreqFibonacci::handle_event()
1972 {
1973         float last_value1 = 0, last_value2 = 1;
1974         for(int i = 0; i < synth->config.oscillator_config.total; i++)
1975         {
1976                 synth->config.oscillator_config.values[i]->freq_factor = last_value1 + last_value2;
1977                 if(synth->config.oscillator_config.values[i]->freq_factor > 100)
1978                         synth->config.oscillator_config.values[i]->freq_factor = 100;
1979                 last_value1 = last_value2;
1980                 last_value2 = synth->config.oscillator_config.values[i]->freq_factor;
1981         }
1982
1983         ((SynthWindow*)synth->thread->window)->update_gui();
1984         synth->send_configure_change();
1985         return 1;
1986 }
1987
1988 SynthFreqPrime::SynthFreqPrime(Synth *synth)
1989  : BC_MenuItem(_("Prime"))
1990 {
1991         this->synth = synth;
1992 }
1993 SynthFreqPrime::~SynthFreqPrime()
1994 {
1995 }
1996
1997 int SynthFreqPrime::handle_event()
1998 {
1999         float number = 1;
2000         for(int i = 0; i < synth->config.oscillator_config.total; i++)
2001         {
2002                 synth->config.oscillator_config.values[i]->freq_factor = number;
2003                 number = get_next_prime(number);
2004         }
2005
2006         ((SynthWindow*)synth->thread->window)->update_gui();
2007         synth->send_configure_change();
2008         return 1;
2009 }
2010
2011 float SynthFreqPrime::get_next_prime(float number)
2012 {
2013         int result = 1;
2014
2015         while(result)
2016         {
2017                 result = 0;
2018                 number++;
2019
2020                 for(float i = number - 1; i > 1 && !result; i--)
2021                 {
2022                         if((number / i) - (int)(number / i) == 0) result = 1;
2023                 }
2024         }
2025
2026         return number;
2027 }
2028
2029
2030
2031
2032
2033
2034
2035
2036 SynthOscillatorConfig::SynthOscillatorConfig(int number)
2037 {
2038         reset();
2039         this->number = number;
2040 }
2041
2042 SynthOscillatorConfig::~SynthOscillatorConfig()
2043 {
2044 }
2045
2046 void SynthOscillatorConfig::reset()
2047 {
2048         level = 0;
2049         phase = 0;
2050         freq_factor = 1;
2051 }
2052
2053
2054 void SynthOscillatorConfig::read_data(FileXML *file)
2055 {
2056         level = file->tag.get_property("LEVEL", (float)level);
2057         phase = file->tag.get_property("PHASE", (float)phase);
2058         freq_factor = file->tag.get_property("FREQFACTOR", (float)freq_factor);
2059 }
2060
2061 void SynthOscillatorConfig::save_data(FileXML *file)
2062 {
2063         file->tag.set_title("OSCILLATOR");
2064         file->tag.set_property("LEVEL", (float)level);
2065         file->tag.set_property("PHASE", (float)phase);
2066         file->tag.set_property("FREQFACTOR", (float)freq_factor);
2067         file->append_tag();
2068         file->tag.set_title("/OSCILLATOR");
2069         file->append_tag();
2070         file->append_newline();
2071 }
2072
2073 int SynthOscillatorConfig::equivalent(SynthOscillatorConfig &that)
2074 {
2075         if(EQUIV(level, that.level) &&
2076                 EQUIV(phase, that.phase) &&
2077                 EQUIV(freq_factor, that.freq_factor))
2078                 return 1;
2079         else
2080                 return 0;
2081 }
2082
2083 void SynthOscillatorConfig::copy_from(SynthOscillatorConfig& that)
2084 {
2085         level = that.level;
2086         phase = that.phase;
2087         freq_factor = that.freq_factor;
2088 }
2089
2090
2091 SynthConfig::SynthConfig()
2092 {
2093         reset();
2094 }
2095
2096 SynthConfig::~SynthConfig()
2097 {
2098         oscillator_config.remove_all_objects();
2099 }
2100
2101 void SynthConfig::reset()
2102 {
2103         wetness = 0;
2104         base_freq[0] = 440;
2105         for(int i = 1; i < MAX_FREQS; i++)
2106                 base_freq[i] = 0;
2107         wavefunction = SINE;
2108         for(int i = 0; i < oscillator_config.total; i++)
2109         {
2110                 oscillator_config.values[i]->reset();
2111         }
2112
2113         momentary_notes = 0;
2114 }
2115
2116 int SynthConfig::equivalent(SynthConfig &that)
2117 {
2118 //printf("SynthConfig::equivalent %d %d\n", base_freq, that.base_freq);
2119         for(int i = 0; i < MAX_FREQS; i++)
2120                 if(base_freq[i] != that.base_freq[i]) return 0;
2121
2122         if(wavefunction != that.wavefunction ||
2123                 oscillator_config.total != that.oscillator_config.total ||
2124                 momentary_notes != that.momentary_notes) return 0;
2125
2126         for(int i = 0; i < oscillator_config.total; i++)
2127         {
2128                 if(!oscillator_config.values[i]->equivalent(*that.oscillator_config.values[i]))
2129                         return 0;
2130         }
2131
2132         return 1;
2133 }
2134
2135 void SynthConfig::copy_from(SynthConfig& that)
2136 {
2137         wetness = that.wetness;
2138         for(int i = 0; i < MAX_FREQS; i++)
2139                 base_freq[i] = that.base_freq[i];
2140         wavefunction = that.wavefunction;
2141         momentary_notes = that.momentary_notes;
2142
2143         int i;
2144         for(i = 0;
2145                 i < oscillator_config.total && i < that.oscillator_config.total;
2146                 i++)
2147         {
2148                 oscillator_config.values[i]->copy_from(*that.oscillator_config.values[i]);
2149         }
2150
2151         for( ;
2152                 i < that.oscillator_config.total;
2153                 i++)
2154         {
2155                 oscillator_config.append(new SynthOscillatorConfig(i));
2156                 oscillator_config.values[i]->copy_from(*that.oscillator_config.values[i]);
2157         }
2158
2159         for( ;
2160                 i < oscillator_config.total;
2161                 i++)
2162         {
2163                 oscillator_config.remove_object();
2164         }
2165
2166 }
2167
2168 void SynthConfig::interpolate(SynthConfig &prev,
2169         SynthConfig &next,
2170         int64_t prev_frame,
2171         int64_t next_frame,
2172         int64_t current_frame)
2173 {
2174         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
2175         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
2176
2177         copy_from(prev);
2178         wetness = (int)(prev.wetness * prev_scale + next.wetness * next_scale);
2179 //      base_freq = (int)(prev.base_freq * prev_scale + next.base_freq * next_scale);
2180
2181         momentary_notes = prev.momentary_notes;
2182 }
2183