4 * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "bcdisplayinfo.h"
28 #include "synthesizer.h"
30 #include "transportque.inc"
38 REGISTER_PLUGIN(Synth)
43 Synth::Synth(PluginServer *server)
44 : PluginAClient(server)
58 NEW_WINDOW_MACRO(Synth, SynthWindow);
60 const char* Synth::plugin_title() { return N_("Synthesizer"); }
61 int Synth::is_realtime() { return 1; }
62 int Synth::is_synthesis() { return 1; }
73 LOAD_CONFIGURATION_MACRO(Synth, SynthConfig)
80 void Synth::read_data(KeyFrame *keyframe)
83 char string[BCTEXTLEN];
84 // cause htal file to read directly from text
85 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
87 //printf("Synth::read_data %s\n", keyframe->get_data());
88 int result = 0, current_osc = 0;
89 //int total_oscillators = 0;
92 result = input.read_tag();
96 if(input.tag.title_is("SYNTH"))
98 config.wetness = input.tag.get_property("WETNESS", config.wetness);
102 window_w = input.tag.get_property("WINDOW_W", window_w);
103 window_h = input.tag.get_property("WINDOW_H", window_h);
105 config.momentary_notes = input.tag.get_property("MOMENTARY_NOTES", config.momentary_notes);
107 //printf("Synth::read_data %d %d %d\n", __LINE__, window_w, window_h);
108 for(int i = 0; i < MAX_FREQS; i++)
110 sprintf(string, "BASEFREQ_%d", i);
111 config.base_freq[i] = input.tag.get_property(string, (double)0);
114 config.wavefunction = input.tag.get_property("WAVEFUNCTION", config.wavefunction);
115 //total_oscillators = input.tag.get_property("OSCILLATORS", 0);
118 if(input.tag.title_is("OSCILLATOR"))
120 if(current_osc >= config.oscillator_config.total)
121 config.oscillator_config.append(new SynthOscillatorConfig(current_osc));
123 config.oscillator_config.values[current_osc]->read_data(&input);
129 while(config.oscillator_config.total > current_osc)
130 config.oscillator_config.remove_object();
133 void Synth::save_data(KeyFrame *keyframe)
136 char string[BCTEXTLEN];
138 // cause htal file to store data directly in text
139 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
141 output.tag.set_title("SYNTH");
142 output.tag.set_property("WETNESS", config.wetness);
143 output.tag.set_property("WINDOW_W", window_w);
144 output.tag.set_property("WINDOW_H", window_h);
145 output.tag.set_property("MOMENTARY_NOTES", config.momentary_notes);
147 for(int i = 0; i < MAX_FREQS; i++)
149 // if(!EQUIV(config.base_freq[i], 0))
151 sprintf(string, "BASEFREQ_%d", i);
152 output.tag.set_property(string, config.base_freq[i]);
153 //printf("Synth::save_data %d %s\n", __LINE__, string);
157 output.tag.set_property("WAVEFUNCTION", config.wavefunction);
158 output.tag.set_property("OSCILLATORS", config.oscillator_config.total);
160 output.append_newline();
162 for(int i = 0; i < config.oscillator_config.total; i++)
164 config.oscillator_config.values[i]->save_data(&output);
167 output.terminate_string();
168 //printf("Synth::save_data %d %s\n", __LINE__, output.string);
169 // data is now in *text
174 void Synth::update_gui()
178 if(load_configuration())
180 thread->window->lock_window();
181 ((SynthWindow*)thread->window)->update_gui();
182 thread->window->unlock_window();
188 void Synth::add_oscillator()
190 if(config.oscillator_config.total > 20) return;
192 config.oscillator_config.append(new SynthOscillatorConfig(config.oscillator_config.total - 1));
195 void Synth::delete_oscillator()
197 if(config.oscillator_config.total)
199 config.oscillator_config.remove_object();
204 double Synth::get_total_power()
208 if(config.wavefunction == DC) return 1.0;
210 for(int i = 0; i < config.oscillator_config.total; i++)
212 result += db.fromdb(config.oscillator_config.values[i]->level);
215 if(result == 0) result = 1; // prevent division by 0
220 double Synth::solve_eqn(double *output,
223 double normalize_constant,
226 SynthOscillatorConfig *config =
227 this->config.oscillator_config.values[oscillator];
228 if(config->level <= INFINITYGAIN) return 0;
230 double power = this->db.fromdb(config->level) * normalize_constant;
231 // Period of fundamental frequency in samples
232 double orig_period = (double)get_samplerate() /
234 // Starting sample in waveform
235 double x = waveform_sample;
236 double phase_offset = config->phase * orig_period;
237 //printf("Synth::solve_eqn %d %f\n", __LINE__, config->phase);
238 // Period of current oscillator
239 double period = orig_period / config->freq_factor;
242 if(get_direction() == PLAY_REVERSE) step = -1;
244 switch(this->config.wavefunction)
247 for(sample = 0; sample < length; sample++)
249 output[sample] += power;
254 for(sample = 0; sample < length; sample++)
256 output[sample] += sin((x + phase_offset) /
265 for(sample = 0; sample < length; sample++)
267 output[sample] += function_sawtooth((x + phase_offset) /
274 for(sample = 0; sample < length; sample++)
276 output[sample] += function_square((x + phase_offset) /
283 for(sample = 0; sample < length; sample++)
285 output[sample] += function_triangle((x + phase_offset) /
292 for(sample = 0; sample < length; sample++)
294 output[sample] += function_pulse((x + phase_offset) /
301 for(sample = 0; sample < length; sample++)
303 output[sample] += function_noise() * power;
310 double Synth::get_point(float x, double normalize_constant)
313 for(int i = 0; i < config.oscillator_config.total; i++)
314 result += get_oscillator_point(x, normalize_constant, i);
319 double Synth::get_oscillator_point(float x,
320 double normalize_constant,
323 SynthOscillatorConfig *config = this->config.oscillator_config.values[oscillator];
324 double power = db.fromdb(config->level) * normalize_constant;
325 switch(this->config.wavefunction)
331 return sin((x + config->phase) * config->freq_factor * 2 * M_PI) * power;
334 return function_sawtooth((x + config->phase) * config->freq_factor) * power;
337 return function_square((x + config->phase) * config->freq_factor) * power;
340 return function_triangle((x + config->phase) * config->freq_factor) * power;
343 return function_pulse((x + config->phase) * config->freq_factor) * power;
346 return function_noise() * power;
352 double Synth::function_square(double x)
354 x -= (int)x; // only fraction counts
355 return (x < .5) ? -1 : 1;
358 double Synth::function_pulse(double x)
360 x -= (int)x; // only fraction counts
361 return (x < .5) ? 0 : 1;
364 double Synth::function_noise()
366 return (double)(rand() % 65536 - 32768) / 32768;
369 double Synth::function_sawtooth(double x)
375 double Synth::function_triangle(double x)
378 return (x < .5) ? 1 - x * 4 : -3 + x * 4;
381 int Synth::process_realtime(int64_t size,
385 // sample relative to start of plugin
386 waveform_sample = get_source_position();
388 need_reconfigure |= load_configuration();
389 if(need_reconfigure) reconfigure();
391 double wetness = DB::fromdb(config.wetness);
392 if(EQUIV(config.wetness, INFINITYGAIN)) wetness = 0;
395 double *output_samples = output_ptr->get_data();
396 double *input_samples = input_ptr->get_data();
397 for(int j = 0; j < size; j++)
398 output_samples[j] = input_samples[j] * wetness;
400 // Overlay each frequency
401 for(int j = 0; j < MAX_FREQS; j++)
403 if(!EQUIV(config.base_freq[j], 0))
410 input_ptr->get_data(),
411 output_ptr->get_data());
412 //printf("Synth::process_realtime 2\n");
416 // waveform_sample += size;
420 int Synth::overlay_synth(double freq,
425 double normalize_constant = 1.0 / get_total_power();
426 for(int i = 0; i < config.oscillator_config.total; i++)
435 void Synth::reconfigure()
437 need_reconfigure = 0;
438 // waveform_sample = 0;
441 int Synth::freq_exists(double freq)
443 for(int i = 0; i < MAX_FREQS; i++)
446 // printf("Synth::freq_exists %d %d %f %f\n",
450 // config.base_freq[i]);
451 if(fabs(freq - config.base_freq[i]) < 1.0)
458 void Synth::new_freq(double freq)
461 for(int i = 0; i < MAX_FREQS; i++)
463 if(EQUIV(config.base_freq[i], freq)) return;
466 for(int i = 0; i < MAX_FREQS; i++)
468 if(EQUIV(config.base_freq[i], 0))
470 config.base_freq[i] = freq;
471 //printf("Synth::new_freq %d\n", __LINE__);
477 void Synth::delete_freq(double freq)
479 for(int i = 0; i < MAX_FREQS; i++)
481 if(EQUIV(config.base_freq[i], freq))
483 //printf("Synth::delete_freq %d\n", __LINE__);
484 // Shift frequencies back
485 for(int j = i; j < MAX_FREQS - 1; j++)
487 config.base_freq[j] = config.base_freq[j + 1];
489 config.base_freq[MAX_FREQS - 1] = 0;
496 void Synth::delete_freqs()
498 for(int i = 0; i < MAX_FREQS; i++)
499 config.base_freq[i] = 0;
529 SynthWindow::SynthWindow(Synth *synth)
530 : PluginClientWindow(synth,
544 bzero(notes, sizeof(SynthNote*) * TOTALNOTES);
548 SynthWindow::~SynthWindow()
558 static const char *keyboard_map[] =
560 "q", "2", "w", "3", "e", "r", "5", "t", "6", "y", "7", "u",
561 "z", "s", "x", "d", "c", "v", "g", "b", "h", "n", "j", "m"
564 void SynthWindow::create_objects()
567 add_subwindow(menu = new BC_MenuBar(0, 0, get_w()));
569 BC_Menu *levelmenu, *phasemenu, *harmonicmenu;
570 menu->add_menu(levelmenu = new BC_Menu(_("Level")));
571 menu->add_menu(phasemenu = new BC_Menu(_("Phase")));
572 menu->add_menu(harmonicmenu = new BC_Menu(_("Harmonic")));
574 levelmenu->add_item(new SynthLevelInvert(synth));
575 levelmenu->add_item(new SynthLevelMax(synth));
576 levelmenu->add_item(new SynthLevelRandom(synth));
577 levelmenu->add_item(new SynthLevelSine(synth));
578 levelmenu->add_item(new SynthLevelSlope(synth));
579 levelmenu->add_item(new SynthLevelZero(synth));
581 phasemenu->add_item(new SynthPhaseInvert(synth));
582 phasemenu->add_item(new SynthPhaseRandom(synth));
583 phasemenu->add_item(new SynthPhaseSine(synth));
584 phasemenu->add_item(new SynthPhaseZero(synth));
586 harmonicmenu->add_item(new SynthFreqEnum(synth));
587 harmonicmenu->add_item(new SynthFreqEven(synth));
588 harmonicmenu->add_item(new SynthFreqFibonacci(synth));
589 harmonicmenu->add_item(new SynthFreqOdd(synth));
590 harmonicmenu->add_item(new SynthFreqPrime(synth));
594 add_subwindow(new BC_Title(x, y, _("Waveform")));
596 add_subwindow(new BC_Title(x, y, _("Wave Function")));
599 add_subwindow(canvas = new SynthCanvas(synth, this, x, y, 230, 160));
603 char string[BCTEXTLEN];
604 waveform_to_text(string, synth->config.wavefunction);
606 add_subwindow(waveform = new SynthWaveForm(synth, x, y, string));
607 waveform->create_objects();
609 int x1 = x + waveform->get_w() + 10;
612 add_subwindow(new BC_Title(x, y, _("Base Frequency:")));
614 add_subwindow(base_freq = new SynthBaseFreq(synth, this, x, y));
615 base_freq->update((float)synth->config.base_freq[0]);
616 x += base_freq->get_w() + synth->get_theme()->widget_border;
617 add_subwindow(freqpot = new SynthFreqPot(synth, this, x, y - 10));
618 base_freq->freq_pot = freqpot;
619 freqpot->freq_text = base_freq;
620 x -= base_freq->get_w() + synth->get_theme()->widget_border;
622 add_subwindow(new BC_Title(x, y, _("Wetness:")));
623 add_subwindow(wetness = new SynthWetness(synth, x + 70, y - 10));
626 add_subwindow(new SynthClear(synth, x, y));
631 add_subwindow(new BC_Title(x, y, _("Level")));
633 add_subwindow(new BC_Title(x, y, _("Phase")));
635 add_subwindow(new BC_Title(x, y, _("Harmonic")));
640 add_subwindow(osc_subwindow = new BC_SubWindow(x, y, 265, get_h() - y));
642 add_subwindow(osc_scroll = new OscScroll(synth, this, x, y, get_h() - y));
646 add_subwindow(new SynthAddOsc(synth, this, x, y));
648 add_subwindow(new SynthDelOsc(synth, this, x, y));
653 #include "white_up_png.h"
654 #include "white_hi_png.h"
655 #include "white_dn_png.h"
656 #include "white_checked_png.h"
657 #include "white_checkedhi_png.h"
658 #include "black_up_png.h"
659 #include "black_hi_png.h"
660 #include "black_dn_png.h"
661 #include "black_checked_png.h"
662 #include "black_checkedhi_png.h"
663 white_key[0] = new VFrame(white_up_png);
664 white_key[1] = new VFrame(white_hi_png);
665 white_key[2] = new VFrame(white_checked_png);
666 white_key[3] = new VFrame(white_dn_png);
667 white_key[4] = new VFrame(white_checkedhi_png);
668 black_key[0] = new VFrame(black_up_png);
669 black_key[1] = new VFrame(black_hi_png);
670 black_key[2] = new VFrame(black_checked_png);
671 black_key[3] = new VFrame(black_dn_png);
672 black_key[4] = new VFrame(black_checkedhi_png);
675 add_subwindow(note_subwindow = new BC_SubWindow(x1,
678 white_key[0]->get_h() + MARGIN +
679 get_text_height(MEDIUMFONT) + MARGIN +
680 get_text_height(MEDIUMFONT) + MARGIN));
681 add_subwindow(note_scroll = new NoteScroll(synth,
684 note_subwindow->get_y() + note_subwindow->get_h(),
685 note_subwindow->get_w()));
687 add_subwindow(momentary = new SynthMomentary(this,
689 note_scroll->get_y() + note_scroll->get_h() + MARGIN,
690 _("Momentary notes")));
693 add_subwindow(note_instructions = new BC_Title(
695 momentary->get_y() + momentary->get_h() + MARGIN,
696 _("Ctrl or Shift to select multiple notes.")));
699 update_oscillators();
705 int SynthWindow::keypress_event()
707 if(ctrl_down() && get_keypress() == 'w')
715 int SynthWindow::resize_event(int w, int h)
717 clear_box(0, 0, w, h);
718 osc_subwindow->reposition_window(osc_subwindow->get_x(),
719 osc_subwindow->get_y(),
720 osc_subwindow->get_w(),
721 h - osc_subwindow->get_y());
722 osc_subwindow->clear_box(0, 0, osc_subwindow->get_w(), osc_subwindow->get_h());
723 osc_scroll->reposition_window(osc_scroll->get_x(),
725 h - osc_scroll->get_y());
726 note_subwindow->reposition_window(note_subwindow->get_x(),
727 note_subwindow->get_y(),
728 w - note_subwindow->get_x(),
729 note_subwindow->get_h());
730 note_scroll->reposition_window(note_scroll->get_x(),
731 note_scroll->get_y(),
732 w - note_scroll->get_x());
733 note_scroll->update_length(white_key[0]->get_w() * TOTALNOTES * 7 / 12 +
734 white_key[0]->get_w(),
735 note_scroll->get_position(),
736 note_subwindow->get_w(),
741 update_oscillators();
747 void SynthWindow::update_gui()
749 char string[BCTEXTLEN];
750 freqpot->update((int)synth->config.base_freq[0]);
751 base_freq->update((float)synth->config.base_freq[0]);
752 wetness->update(synth->config.wetness);
753 waveform_to_text(string, synth->config.wavefunction);
754 waveform->set_text(string);
755 momentary->update(synth->config.momentary_notes);
758 update_oscillators();
760 update_note_selection();
764 void SynthWindow::update_scrollbar()
766 osc_scroll->update_length(synth->config.oscillator_config.total * OSCILLATORHEIGHT,
767 osc_scroll->get_position(),
768 osc_subwindow->get_h(),
773 void SynthWindow::update_whitekey(int number,
780 note_subwindow->add_subwindow(notes[number] = new SynthNote(this,
781 white_key, number, x, y));
782 if(number >= FIRST_TITLE && number < LAST_TITLE)
783 note_subwindow->add_subwindow(
784 note_titles[(*current_title)++] = new BC_Title(
785 x + text_white_margin,
787 keyboard_map[number - FIRST_TITLE]));
788 //printf("SynthWindow::update_whitekey %d\n", __LINE__);
792 notes[number]->reposition_window(x, y);
793 if(number >= FIRST_TITLE && number < LAST_TITLE)
794 note_titles[(*current_title)++]->reposition_window(x + text_white_margin,
800 void SynthWindow::update_blackkey(int number,
807 note_subwindow->add_subwindow(notes[number] = new SynthNote(this,
808 black_key, number, x, y));
809 if(number >= FIRST_TITLE && number < LAST_TITLE)
810 note_subwindow->add_subwindow(
811 note_titles[(*current_title)++] = new BC_Title(x + text_black_margin,
813 keyboard_map[number - FIRST_TITLE]));
817 notes[number]->reposition_window(x, y);
818 if(number >= FIRST_TITLE && number < LAST_TITLE)
819 note_titles[(*current_title)++]->reposition_window(x + text_black_margin,
824 void SynthWindow::update_notes()
826 //int octave_w = white_key[0]->get_w() * 7;
827 int white_w = white_key[0]->get_w();
828 int black_w = black_key[0]->get_w();
829 int white_w1 = white_w - black_w / 2 - 2;
830 int white_w2 = white_w / 2;
831 int white_w3 = white_w * 2 / 3;
834 y1 = y + white_key[0]->get_h() + 10;
835 y2 = y1 + get_text_height(MEDIUMFONT) + 10;
836 y3 = y2 + get_text_height(MEDIUMFONT) + 10;
837 text_black_margin = black_w / 2 - get_text_width(MEDIUMFONT, "O") / 2;
838 text_white_margin = white_w / 2 - get_text_width(MEDIUMFONT, "O") / 2;
841 //printf("SynthWindow::update_notes %d\n", __LINE__);
842 note_subwindow->clear_box(0, 0, get_w(), get_h());
844 note_subwindow->set_color(get_resources()->default_text_color);
847 // To get the stacking order:
848 // pass 0 is white keys
849 // pass 1 is black keys
850 int current_title = 0;
851 for(int pass = 0; pass < 2; pass++)
853 x = -note_scroll->get_position();
855 for(int i = 0; i < TOTALNOTES; i++)
857 int octave_note = i % 12;
864 update_whitekey(i, ¤t_title, x, y);
870 update_whitekey(i, ¤t_title, x, y);
876 update_whitekey(i, ¤t_title, x, y);
880 update_whitekey(i, ¤t_title, x, y);
886 update_whitekey(i, ¤t_title, x, y);
892 update_whitekey(i, ¤t_title, x, y);
898 update_whitekey(i, ¤t_title, x, y);
909 update_blackkey(i, ¤t_title, x + white_w2, y);
913 update_blackkey(i, ¤t_title, x + white_w3, y);
920 update_blackkey(i, ¤t_title, x + white_w2, y);
924 update_blackkey(i, ¤t_title, x + white_w1, y);
928 update_blackkey(i, ¤t_title, x + white_w3, y);
940 void SynthWindow::update_note_selection()
942 for(int i = 0; i < TOTALNOTES; i++)
945 for(int j = 0; j < MAX_FREQS; j++)
947 if(synth->freq_exists(keyboard_freqs[notes[i]->number]))
956 notes[i]->set_value(1);
959 notes[i]->set_value(0);
964 void SynthWindow::update_oscillators()
966 int i, y = -osc_scroll->get_position();
970 // Add new oscillators
972 i < synth->config.oscillator_config.total;
976 SynthOscillatorConfig *config = synth->config.oscillator_config.values[i];
978 if(oscillators.total <= i)
980 oscillators.append(gui = new SynthOscGUI(this, i));
981 gui->create_objects(y);
985 gui = oscillators.values[i];
987 gui->title->reposition_window(gui->title->get_x(), y + 15);
989 gui->level->reposition_window(gui->level->get_x(), y);
990 gui->level->update(config->level);
992 gui->phase->reposition_window(gui->phase->get_x(), y);
993 gui->phase->update((int64_t)(config->phase * 360));
995 gui->freq->reposition_window(gui->freq->get_x(), y);
996 gui->freq->update((int64_t)(config->freq_factor));
998 y += OSCILLATORHEIGHT;
1001 // Delete old oscillators
1003 i < oscillators.total;
1005 oscillators.remove_object();
1009 int SynthWindow::waveform_to_text(char *text, int waveform)
1013 case DC: sprintf(text, _("DC")); break;
1014 case SINE: sprintf(text, _("Sine")); break;
1015 case SAWTOOTH: sprintf(text, _("Sawtooth")); break;
1016 case SQUARE: sprintf(text, _("Square")); break;
1017 case TRIANGLE: sprintf(text, _("Triangle")); break;
1018 case PULSE: sprintf(text, _("Pulse")); break;
1019 case NOISE: sprintf(text, _("Noise")); break;
1025 SynthMomentary::SynthMomentary(SynthWindow *window, int x, int y, char *text)
1028 window->synth->config.momentary_notes,
1031 this->window = window;
1034 int SynthMomentary::handle_event()
1036 window->synth->config.momentary_notes = get_value();
1037 window->synth->send_configure_change();
1044 SynthNote::SynthNote(SynthWindow *window,
1052 window->synth->freq_exists(keyboard_freqs[number]))
1054 this->window = window;
1055 this->number = number;
1061 void SynthNote::start_note()
1063 if(window->synth->config.momentary_notes) note_on = 1;
1065 //printf("SynthNote::start_note %d %d\n", __LINE__, ctrl_down());
1066 if(window->synth->config.momentary_notes || (!ctrl_down() && !shift_down()))
1068 // Kill all frequencies
1069 window->synth->delete_freqs();
1072 window->synth->new_freq(keyboard_freqs[number]);
1073 window->base_freq->update((float)window->synth->config.base_freq[0]);
1074 //printf("SynthNote::start_note %d %f\n", __LINE__, window->synth->config.base_freq[0]);
1075 window->freqpot->update(window->synth->config.base_freq[0]);
1076 window->synth->send_configure_change();
1077 window->update_note_selection();
1080 void SynthNote::stop_note()
1083 window->synth->delete_freq(keyboard_freqs[number]);
1084 window->base_freq->update((float)window->synth->config.base_freq[0]);
1085 window->freqpot->update(window->synth->config.base_freq[0]);
1086 window->synth->send_configure_change();
1087 window->update_note_selection();
1090 int SynthNote::keypress_event()
1092 if(number >= FIRST_TITLE && number < LAST_TITLE)
1094 if(get_keypress() == keyboard_map[number - FIRST_TITLE][0])
1096 if((ctrl_down() || shift_down()) &&
1097 window->synth->freq_exists(keyboard_freqs[number]))
1107 // Key releases are repeated, so momentary notes may not work
1114 int SynthNote::keyrelease_event()
1116 if(note_on && window->synth->config.momentary_notes)
1125 int SynthNote::cursor_motion_event()
1128 if(window->current_note > -1)
1130 int cursor_x = get_relative_cursor_x();
1131 int cursor_y = get_relative_cursor_y();
1132 if(cursor_x >= 0 && cursor_x < get_w() &&
1133 cursor_y >= 0 && cursor_y < get_h())
1135 if(window->starting_notes)
1144 window->current_note = number;
1151 int SynthNote::button_press_event()
1153 if(BC_Toggle::button_press_event())
1155 // printf("SynthNote::button_press_event %d %d %d\n",
1158 // window->synth->freq_exists(keyboard_freqs[number]));
1159 window->starting_notes = 1;
1160 if((ctrl_down() || shift_down()) &&
1161 window->synth->freq_exists(keyboard_freqs[number]))
1163 //printf("SynthNote::button_press_event %d\n", __LINE__);
1165 window->starting_notes = 0;
1169 //printf("SynthNote::button_press_event %d\n", __LINE__);
1171 window->starting_notes = 1;
1173 window->current_note = number;
1179 int SynthNote::button_release_event()
1181 // Change frequency permanently
1182 if(window->current_note == number)
1184 if(window->synth->config.momentary_notes)
1186 // Mute on button release
1190 window->current_note = -1;
1193 return BC_Toggle::button_release_event();
1196 int SynthNote::draw_face(int flash, int flush)
1198 BC_Toggle::draw_face(0, 0);
1199 static const char *titles[] =
1216 const char *text = titles[number % (sizeof(titles) / sizeof(char*))];
1217 char string[BCTEXTLEN];
1218 sprintf(string, "%s%d", text, number / 12);
1219 //printf("SynthNote::draw_face %d %d %d %d %s\n", __LINE__, number, get_w(), get_h(), text);
1226 draw_text(get_w() / 2 - get_text_width(MEDIUMFONT, string) / 2,
1227 get_h() - get_text_height(MEDIUMFONT, string) - window->synth->get_theme()->widget_border,
1231 if(flash) this->flash(0);
1232 if(flush) this->flush();
1242 SynthOscGUI::SynthOscGUI(SynthWindow *window, int number)
1244 this->window = window;
1245 this->number = number;
1248 SynthOscGUI::~SynthOscGUI()
1256 void SynthOscGUI::create_objects(int y)
1258 char text[BCTEXTLEN];
1259 sprintf(text, "%d:", number + 1);
1260 window->osc_subwindow->add_subwindow(title = new BC_Title(10, y + 15, text));
1262 window->osc_subwindow->add_subwindow(level = new SynthOscGUILevel(window->synth, this, y));
1263 window->osc_subwindow->add_subwindow(phase = new SynthOscGUIPhase(window->synth, this, y));
1264 window->osc_subwindow->add_subwindow(freq = new SynthOscGUIFreq(window->synth, this, y));
1270 SynthOscGUILevel::SynthOscGUILevel(Synth *synth, SynthOscGUI *gui, int y)
1273 synth->config.oscillator_config.values[gui->number]->level,
1277 this->synth = synth;
1281 SynthOscGUILevel::~SynthOscGUILevel()
1285 int SynthOscGUILevel::handle_event()
1287 SynthOscillatorConfig *config = synth->config.oscillator_config.values[gui->number];
1288 config->level = get_value();
1289 gui->window->canvas->update();
1290 synth->send_configure_change();
1296 SynthOscGUIPhase::SynthOscGUIPhase(Synth *synth, SynthOscGUI *gui, int y)
1299 (int64_t)(synth->config.oscillator_config.values[gui->number]->phase * 360),
1303 this->synth = synth;
1307 SynthOscGUIPhase::~SynthOscGUIPhase()
1311 int SynthOscGUIPhase::handle_event()
1313 SynthOscillatorConfig *config = synth->config.oscillator_config.values[gui->number];
1314 config->phase = (float)get_value() / 360;
1315 gui->window->canvas->update();
1316 synth->send_configure_change();
1322 SynthOscGUIFreq::SynthOscGUIFreq(Synth *synth, SynthOscGUI *gui, int y)
1325 (int64_t)(synth->config.oscillator_config.values[gui->number]->freq_factor),
1329 this->synth = synth;
1333 SynthOscGUIFreq::~SynthOscGUIFreq()
1337 int SynthOscGUIFreq::handle_event()
1339 SynthOscillatorConfig *config = synth->config.oscillator_config.values[gui->number];
1340 config->freq_factor = get_value();
1341 gui->window->canvas->update();
1342 synth->send_configure_change();
1352 SynthAddOsc::SynthAddOsc(Synth *synth, SynthWindow *window, int x, int y)
1353 : BC_GenericButton(x, y, _("Add"))
1355 this->synth = synth;
1356 this->window = window;
1359 SynthAddOsc::~SynthAddOsc()
1363 int SynthAddOsc::handle_event()
1365 synth->add_oscillator();
1366 synth->send_configure_change();
1367 window->update_gui();
1373 SynthDelOsc::SynthDelOsc(Synth *synth, SynthWindow *window, int x, int y)
1374 : BC_GenericButton(x, y, _("Delete"))
1376 this->synth = synth;
1377 this->window = window;
1380 SynthDelOsc::~SynthDelOsc()
1384 int SynthDelOsc::handle_event()
1386 synth->delete_oscillator();
1387 synth->send_configure_change();
1388 window->update_gui();
1393 OscScroll::OscScroll(Synth *synth,
1394 SynthWindow *window,
1402 synth->config.oscillator_config.total * OSCILLATORHEIGHT,
1404 window->osc_subwindow->get_h())
1406 this->synth = synth;
1407 this->window = window;
1410 OscScroll::~OscScroll()
1414 int OscScroll::handle_event()
1416 window->update_oscillators();
1422 NoteScroll::NoteScroll(Synth *synth,
1423 SynthWindow *window,
1431 window->white_key[0]->get_w() * TOTALNOTES * 7 / 12 + window->white_key[0]->get_w(),
1433 window->note_subwindow->get_w())
1435 this->synth = synth;
1436 this->window = window;
1439 NoteScroll::~NoteScroll()
1443 int NoteScroll::handle_event()
1445 window->update_notes();
1462 SynthClear::SynthClear(Synth *synth, int x, int y)
1463 : BC_GenericButton(x, y, _("Clear"))
1465 this->synth = synth;
1467 SynthClear::~SynthClear()
1470 int SynthClear::handle_event()
1472 synth->config.reset();
1473 synth->send_configure_change();
1474 synth->update_gui();
1483 SynthWaveForm::SynthWaveForm(Synth *synth, int x, int y, char *text)
1484 : BC_PopupMenu(x, y, 120, text)
1486 this->synth = synth;
1489 SynthWaveForm::~SynthWaveForm()
1493 void SynthWaveForm::create_objects()
1495 // add_item(new SynthWaveFormItem(synth, _("DC"), DC));
1496 add_item(new SynthWaveFormItem(synth, _("Sine"), SINE));
1497 add_item(new SynthWaveFormItem(synth, _("Sawtooth"), SAWTOOTH));
1498 add_item(new SynthWaveFormItem(synth, _("Square"), SQUARE));
1499 add_item(new SynthWaveFormItem(synth, _("Triangle"), TRIANGLE));
1500 add_item(new SynthWaveFormItem(synth, _("Pulse"), PULSE));
1501 add_item(new SynthWaveFormItem(synth, _("Noise"), NOISE));
1504 SynthWaveFormItem::SynthWaveFormItem(Synth *synth, char *text, int value)
1507 this->synth = synth;
1508 this->value = value;
1511 SynthWaveFormItem::~SynthWaveFormItem()
1515 int SynthWaveFormItem::handle_event()
1517 synth->config.wavefunction = value;
1518 ((SynthWindow*)synth->thread->window)->canvas->update();
1519 get_popup_menu()->set_text(get_text());
1520 synth->send_configure_change();
1525 SynthWetness::SynthWetness(Synth *synth, int x, int y)
1528 synth->config.wetness,
1532 this->synth = synth;
1535 int SynthWetness::handle_event()
1537 synth->config.wetness = get_value();
1538 synth->send_configure_change();
1544 SynthFreqPot::SynthFreqPot(Synth *synth, SynthWindow *window, int x, int y)
1545 : BC_QPot(x, y, synth->config.base_freq[0])
1547 this->synth = synth;
1548 this->window = window;
1550 SynthFreqPot::~SynthFreqPot()
1553 int SynthFreqPot::handle_event()
1555 if(get_value() > 0 && get_value() < 30000)
1557 synth->config.base_freq[0] = get_value();
1558 freq_text->update(get_value());
1559 synth->send_configure_change();
1560 window->update_note_selection();
1567 SynthBaseFreq::SynthBaseFreq(Synth *synth, SynthWindow *window, int x, int y)
1568 : BC_TextBox(x, y, 100, 1, (float)0)
1570 this->synth = synth;
1571 this->window = window;
1574 SynthBaseFreq::~SynthBaseFreq()
1577 int SynthBaseFreq::handle_event()
1579 double new_value = atof(get_text());
1581 if(new_value < 30000)
1583 synth->config.base_freq[0] = new_value;
1584 freq_pot->update(synth->config.base_freq[0]);
1585 synth->send_configure_change();
1586 window->update_note_selection();
1595 SynthCanvas::SynthCanvas(Synth *synth,
1596 SynthWindow *window,
1607 this->synth = synth;
1608 this->window = window;
1611 SynthCanvas::~SynthCanvas()
1615 int SynthCanvas::update()
1619 clear_box(0, 0, get_w(), get_h());
1622 draw_line(0, get_h() / 2 + y, get_w(), get_h() / 2 + y);
1626 double normalize_constant = (double)1 / synth->get_total_power();
1627 y1 = (int)(synth->get_point((float)0, normalize_constant) * get_h() / 2);
1629 for(int i = 1; i < get_w(); i++)
1631 y2 = (int)(synth->get_point((float)i / get_w(), normalize_constant) * get_h() / 2);
1632 draw_line(i - 1, get_h() / 2 - y1, i, get_h() / 2 - y2);
1646 // ======================= level calculations
1647 SynthLevelZero::SynthLevelZero(Synth *synth)
1648 : BC_MenuItem(_("Zero"))
1650 this->synth = synth;
1653 SynthLevelZero::~SynthLevelZero()
1657 int SynthLevelZero::handle_event()
1659 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1661 synth->config.oscillator_config.values[i]->level = INFINITYGAIN;
1664 ((SynthWindow*)synth->thread->window)->update_gui();
1665 synth->send_configure_change();
1669 SynthLevelMax::SynthLevelMax(Synth *synth)
1670 : BC_MenuItem(_("Maximum"))
1672 this->synth = synth;
1675 SynthLevelMax::~SynthLevelMax()
1679 int SynthLevelMax::handle_event()
1681 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1683 synth->config.oscillator_config.values[i]->level = 0;
1685 ((SynthWindow*)synth->thread->window)->update_gui();
1686 synth->send_configure_change();
1690 SynthLevelNormalize::SynthLevelNormalize(Synth *synth)
1691 : BC_MenuItem(_("Normalize"))
1693 this->synth = synth;
1696 SynthLevelNormalize::~SynthLevelNormalize()
1700 int SynthLevelNormalize::handle_event()
1705 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1707 total += synth->db.fromdb(synth->config.oscillator_config.values[i]->level);
1710 float scale = 1 / total;
1713 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1715 new_value = synth->db.fromdb(synth->config.oscillator_config.values[i]->level);
1717 new_value = synth->db.todb(new_value);
1719 synth->config.oscillator_config.values[i]->level = new_value;
1722 ((SynthWindow*)synth->thread->window)->update_gui();
1723 synth->send_configure_change();
1727 SynthLevelSlope::SynthLevelSlope(Synth *synth)
1728 : BC_MenuItem(_("Slope"))
1730 this->synth = synth;
1733 SynthLevelSlope::~SynthLevelSlope()
1737 int SynthLevelSlope::handle_event()
1739 float slope = (float)INFINITYGAIN / synth->config.oscillator_config.total;
1741 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1743 synth->config.oscillator_config.values[i]->level = i * slope;
1746 ((SynthWindow*)synth->thread->window)->update_gui();
1747 synth->send_configure_change();
1751 SynthLevelRandom::SynthLevelRandom(Synth *synth)
1752 : BC_MenuItem(_("Random"))
1754 this->synth = synth;
1756 SynthLevelRandom::~SynthLevelRandom()
1760 int SynthLevelRandom::handle_event()
1763 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1765 synth->config.oscillator_config.values[i]->level = -(rand() % -INFINITYGAIN);
1768 ((SynthWindow*)synth->thread->window)->update_gui();
1769 synth->send_configure_change();
1773 SynthLevelInvert::SynthLevelInvert(Synth *synth)
1774 : BC_MenuItem(_("Invert"))
1776 this->synth = synth;
1778 SynthLevelInvert::~SynthLevelInvert()
1782 int SynthLevelInvert::handle_event()
1784 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1786 synth->config.oscillator_config.values[i]->level =
1787 INFINITYGAIN - synth->config.oscillator_config.values[i]->level;
1790 ((SynthWindow*)synth->thread->window)->update_gui();
1791 synth->send_configure_change();
1795 SynthLevelSine::SynthLevelSine(Synth *synth)
1796 : BC_MenuItem(_("Sine"))
1798 this->synth = synth;
1800 SynthLevelSine::~SynthLevelSine()
1804 int SynthLevelSine::handle_event()
1808 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1810 new_value = (float)i / synth->config.oscillator_config.total * 2 * M_PI;
1811 new_value = sin(new_value) * INFINITYGAIN / 2 + INFINITYGAIN / 2;
1812 synth->config.oscillator_config.values[i]->level = new_value;
1815 ((SynthWindow*)synth->thread->window)->update_gui();
1816 synth->send_configure_change();
1820 // ============================ phase calculations
1822 SynthPhaseInvert::SynthPhaseInvert(Synth *synth)
1823 : BC_MenuItem(_("Invert"))
1825 this->synth = synth;
1827 SynthPhaseInvert::~SynthPhaseInvert()
1831 int SynthPhaseInvert::handle_event()
1833 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1835 synth->config.oscillator_config.values[i]->phase =
1836 1 - synth->config.oscillator_config.values[i]->phase;
1839 ((SynthWindow*)synth->thread->window)->update_gui();
1840 synth->send_configure_change();
1844 SynthPhaseZero::SynthPhaseZero(Synth *synth)
1845 : BC_MenuItem(_("Zero"))
1847 this->synth = synth;
1849 SynthPhaseZero::~SynthPhaseZero()
1853 int SynthPhaseZero::handle_event()
1855 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1857 synth->config.oscillator_config.values[i]->phase = 0;
1860 ((SynthWindow*)synth->thread->window)->update_gui();
1861 synth->send_configure_change();
1865 SynthPhaseSine::SynthPhaseSine(Synth *synth)
1866 : BC_MenuItem(_("Sine"))
1868 this->synth = synth;
1870 SynthPhaseSine::~SynthPhaseSine()
1874 int SynthPhaseSine::handle_event()
1877 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1879 new_value = (float)i / synth->config.oscillator_config.total * 2 * M_PI;
1880 new_value = sin(new_value) / 2 + .5;
1881 synth->config.oscillator_config.values[i]->phase = new_value;
1884 ((SynthWindow*)synth->thread->window)->update_gui();
1885 synth->send_configure_change();
1889 SynthPhaseRandom::SynthPhaseRandom(Synth *synth)
1890 : BC_MenuItem(_("Random"))
1892 this->synth = synth;
1894 SynthPhaseRandom::~SynthPhaseRandom()
1898 int SynthPhaseRandom::handle_event()
1901 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1903 synth->config.oscillator_config.values[i]->phase =
1904 (float)(rand() % 360) / 360;
1907 ((SynthWindow*)synth->thread->window)->update_gui();
1908 synth->send_configure_change();
1913 // ============================ freq calculations
1915 SynthFreqRandom::SynthFreqRandom(Synth *synth)
1916 : BC_MenuItem(_("Random"))
1918 this->synth = synth;
1920 SynthFreqRandom::~SynthFreqRandom()
1924 int SynthFreqRandom::handle_event()
1927 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1929 synth->config.oscillator_config.values[i]->freq_factor = rand() % 100;
1932 ((SynthWindow*)synth->thread->window)->update_gui();
1933 synth->send_configure_change();
1937 SynthFreqEnum::SynthFreqEnum(Synth *synth)
1938 : BC_MenuItem(_("Enumerate"))
1940 this->synth = synth;
1942 SynthFreqEnum::~SynthFreqEnum()
1946 int SynthFreqEnum::handle_event()
1948 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1950 synth->config.oscillator_config.values[i]->freq_factor = (float)i + 1;
1953 ((SynthWindow*)synth->thread->window)->update_gui();
1954 synth->send_configure_change();
1958 SynthFreqEven::SynthFreqEven(Synth *synth)
1959 : BC_MenuItem(_("Even"))
1961 this->synth = synth;
1963 SynthFreqEven::~SynthFreqEven()
1967 int SynthFreqEven::handle_event()
1969 if(synth->config.oscillator_config.total)
1970 synth->config.oscillator_config.values[0]->freq_factor = (float)1;
1972 for(int i = 1; i < synth->config.oscillator_config.total; i++)
1974 synth->config.oscillator_config.values[i]->freq_factor = (float)i * 2;
1977 ((SynthWindow*)synth->thread->window)->update_gui();
1978 synth->send_configure_change();
1982 SynthFreqOdd::SynthFreqOdd(Synth *synth)
1983 : BC_MenuItem(_("Odd"))
1984 { this->synth = synth; }
1985 SynthFreqOdd::~SynthFreqOdd()
1989 int SynthFreqOdd::handle_event()
1991 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1993 synth->config.oscillator_config.values[i]->freq_factor = (float)1 + i * 2;
1996 ((SynthWindow*)synth->thread->window)->update_gui();
1997 synth->send_configure_change();
2001 SynthFreqFibonacci::SynthFreqFibonacci(Synth *synth)
2002 : BC_MenuItem(_("Fibonnacci"))
2004 this->synth = synth;
2006 SynthFreqFibonacci::~SynthFreqFibonacci()
2010 int SynthFreqFibonacci::handle_event()
2012 float last_value1 = 0, last_value2 = 1;
2013 for(int i = 0; i < synth->config.oscillator_config.total; i++)
2015 synth->config.oscillator_config.values[i]->freq_factor = last_value1 + last_value2;
2016 if(synth->config.oscillator_config.values[i]->freq_factor > 100) synth->config.oscillator_config.values[i]->freq_factor = 100;
2017 last_value1 = last_value2;
2018 last_value2 = synth->config.oscillator_config.values[i]->freq_factor;
2021 ((SynthWindow*)synth->thread->window)->update_gui();
2022 synth->send_configure_change();
2026 SynthFreqPrime::SynthFreqPrime(Synth *synth)
2027 : BC_MenuItem(_("Prime"))
2029 this->synth = synth;
2031 SynthFreqPrime::~SynthFreqPrime()
2035 int SynthFreqPrime::handle_event()
2038 for(int i = 0; i < synth->config.oscillator_config.total; i++)
2040 synth->config.oscillator_config.values[i]->freq_factor = number;
2041 number = get_next_prime(number);
2044 ((SynthWindow*)synth->thread->window)->update_gui();
2045 synth->send_configure_change();
2049 float SynthFreqPrime::get_next_prime(float number)
2058 for(float i = number - 1; i > 1 && !result; i--)
2060 if((number / i) - (int)(number / i) == 0) result = 1;
2074 SynthOscillatorConfig::SynthOscillatorConfig(int number)
2077 this->number = number;
2080 SynthOscillatorConfig::~SynthOscillatorConfig()
2084 void SynthOscillatorConfig::reset()
2092 void SynthOscillatorConfig::read_data(FileXML *file)
2094 level = file->tag.get_property("LEVEL", (float)level);
2095 phase = file->tag.get_property("PHASE", (float)phase);
2096 freq_factor = file->tag.get_property("FREQFACTOR", (float)freq_factor);
2099 void SynthOscillatorConfig::save_data(FileXML *file)
2101 file->tag.set_title("OSCILLATOR");
2102 file->tag.set_property("LEVEL", (float)level);
2103 file->tag.set_property("PHASE", (float)phase);
2104 file->tag.set_property("FREQFACTOR", (float)freq_factor);
2106 file->append_newline();
2109 int SynthOscillatorConfig::equivalent(SynthOscillatorConfig &that)
2111 if(EQUIV(level, that.level) &&
2112 EQUIV(phase, that.phase) &&
2113 EQUIV(freq_factor, that.freq_factor))
2119 void SynthOscillatorConfig::copy_from(SynthOscillatorConfig& that)
2123 freq_factor = that.freq_factor;
2127 SynthConfig::SynthConfig()
2132 SynthConfig::~SynthConfig()
2134 oscillator_config.remove_all_objects();
2137 void SynthConfig::reset()
2141 for(int i = 1; i < MAX_FREQS; i++)
2143 wavefunction = SINE;
2144 for(int i = 0; i < oscillator_config.total; i++)
2146 oscillator_config.values[i]->reset();
2149 momentary_notes = 0;
2152 int SynthConfig::equivalent(SynthConfig &that)
2154 //printf("SynthConfig::equivalent %d %d\n", base_freq, that.base_freq);
2155 for(int i = 0; i < MAX_FREQS; i++)
2156 if(base_freq[i] != that.base_freq[i]) return 0;
2158 if(wavefunction != that.wavefunction ||
2159 oscillator_config.total != that.oscillator_config.total ||
2160 momentary_notes != that.momentary_notes) return 0;
2162 for(int i = 0; i < oscillator_config.total; i++)
2164 if(!oscillator_config.values[i]->equivalent(*that.oscillator_config.values[i]))
2171 void SynthConfig::copy_from(SynthConfig& that)
2173 wetness = that.wetness;
2174 for(int i = 0; i < MAX_FREQS; i++)
2175 base_freq[i] = that.base_freq[i];
2176 wavefunction = that.wavefunction;
2177 momentary_notes = that.momentary_notes;
2181 i < oscillator_config.total && i < that.oscillator_config.total;
2184 oscillator_config.values[i]->copy_from(*that.oscillator_config.values[i]);
2188 i < that.oscillator_config.total;
2191 oscillator_config.append(new SynthOscillatorConfig(i));
2192 oscillator_config.values[i]->copy_from(*that.oscillator_config.values[i]);
2196 i < oscillator_config.total;
2199 oscillator_config.remove_object();
2204 void SynthConfig::interpolate(SynthConfig &prev,
2208 int64_t current_frame)
2210 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
2211 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
2214 wetness = (int)(prev.wetness * prev_scale + next.wetness * next_scale);
2215 // base_freq = (int)(prev.base_freq * prev_scale + next.base_freq * next_scale);
2217 momentary_notes = prev.momentary_notes;