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.tag.set_title("/SYNTH");
169 output.append_newline();
170 output.terminate_string();
171 //printf("Synth::save_data %d %s\n", __LINE__, output.string);
172 // data is now in *text
177 void Synth::update_gui()
179 if( !thread ) return;
180 SynthWindow *window = (SynthWindow*)thread->window;
181 // load_configuration,read_data deletes oscillator_config
182 window->lock_window("Synth::update_gui");
183 if( load_configuration() )
184 window->update_gui();
185 window->unlock_window();
189 void Synth::add_oscillator()
191 if(config.oscillator_config.total > 20) return;
193 config.oscillator_config.append(new SynthOscillatorConfig(config.oscillator_config.total - 1));
196 void Synth::delete_oscillator()
198 if(config.oscillator_config.total)
200 config.oscillator_config.remove_object();
205 double Synth::get_total_power()
209 if(config.wavefunction == DC) return 1.0;
211 for(int i = 0; i < config.oscillator_config.total; i++)
213 result += db.fromdb(config.oscillator_config.values[i]->level);
216 if(result == 0) result = 1; // prevent division by 0
221 double Synth::solve_eqn(double *output,
224 double normalize_constant,
227 SynthOscillatorConfig *config =
228 this->config.oscillator_config.values[oscillator];
229 if(config->level <= INFINITYGAIN) return 0;
231 double power = this->db.fromdb(config->level) * normalize_constant;
232 // Period of fundamental frequency in samples
233 double orig_period = (double)get_samplerate() /
235 // Starting sample in waveform
236 double x = waveform_sample;
237 double phase_offset = config->phase * orig_period;
238 //printf("Synth::solve_eqn %d %f\n", __LINE__, config->phase);
239 // Period of current oscillator
240 double period = orig_period / config->freq_factor;
243 if(get_direction() == PLAY_REVERSE) step = -1;
245 switch(this->config.wavefunction)
248 for(sample = 0; sample < length; sample++)
250 output[sample] += power;
255 for(sample = 0; sample < length; sample++)
257 output[sample] += sin((x + phase_offset) /
266 for(sample = 0; sample < length; sample++)
268 output[sample] += function_sawtooth((x + phase_offset) /
275 for(sample = 0; sample < length; sample++)
277 output[sample] += function_square((x + phase_offset) /
284 for(sample = 0; sample < length; sample++)
286 output[sample] += function_triangle((x + phase_offset) /
293 for(sample = 0; sample < length; sample++)
295 output[sample] += function_pulse((x + phase_offset) /
302 for(sample = 0; sample < length; sample++)
304 output[sample] += function_noise() * power;
311 double Synth::get_point(float x, double normalize_constant)
314 for(int i = 0; i < config.oscillator_config.total; i++)
315 result += get_oscillator_point(x, normalize_constant, i);
320 double Synth::get_oscillator_point(float x,
321 double normalize_constant,
324 SynthOscillatorConfig *config = this->config.oscillator_config.values[oscillator];
325 double power = db.fromdb(config->level) * normalize_constant;
326 switch(this->config.wavefunction)
332 return sin((x + config->phase) * config->freq_factor * 2 * M_PI) * power;
335 return function_sawtooth((x + config->phase) * config->freq_factor) * power;
338 return function_square((x + config->phase) * config->freq_factor) * power;
341 return function_triangle((x + config->phase) * config->freq_factor) * power;
344 return function_pulse((x + config->phase) * config->freq_factor) * power;
347 return function_noise() * power;
353 double Synth::function_square(double x)
355 x -= (int)x; // only fraction counts
356 return (x < .5) ? -1 : 1;
359 double Synth::function_pulse(double x)
361 x -= (int)x; // only fraction counts
362 return (x < .5) ? 0 : 1;
365 double Synth::function_noise()
367 return (double)(rand() % 65536 - 32768) / 32768;
370 double Synth::function_sawtooth(double x)
376 double Synth::function_triangle(double x)
379 return (x < .5) ? 1 - x * 4 : -3 + x * 4;
382 int Synth::process_realtime(int64_t size,
386 // sample relative to start of plugin
387 waveform_sample = get_source_position();
389 need_reconfigure |= load_configuration();
390 if(need_reconfigure) reconfigure();
392 double wetness = DB::fromdb(config.wetness);
393 if(EQUIV(config.wetness, INFINITYGAIN)) wetness = 0;
396 double *output_samples = output_ptr->get_data();
397 double *input_samples = input_ptr->get_data();
398 for(int j = 0; j < size; j++)
399 output_samples[j] = input_samples[j] * wetness;
401 // Overlay each frequency
402 for(int j = 0; j < MAX_FREQS; j++)
404 if(!EQUIV(config.base_freq[j], 0))
411 input_ptr->get_data(),
412 output_ptr->get_data());
413 //printf("Synth::process_realtime 2\n");
417 // waveform_sample += size;
421 int Synth::overlay_synth(double freq,
426 double normalize_constant = 1.0 / get_total_power();
427 for(int i = 0; i < config.oscillator_config.total; i++)
436 void Synth::reconfigure()
438 need_reconfigure = 0;
439 // waveform_sample = 0;
442 int Synth::freq_exists(double freq)
444 for(int i = 0; i < MAX_FREQS; i++)
447 // printf("Synth::freq_exists %d %d %f %f\n",
451 // config.base_freq[i]);
452 if(fabs(freq - config.base_freq[i]) < 1.0)
459 void Synth::new_freq(double freq)
462 for(int i = 0; i < MAX_FREQS; i++)
464 if(EQUIV(config.base_freq[i], freq)) return;
467 for(int i = 0; i < MAX_FREQS; i++)
469 if(EQUIV(config.base_freq[i], 0))
471 config.base_freq[i] = freq;
472 //printf("Synth::new_freq %d\n", __LINE__);
478 void Synth::delete_freq(double freq)
480 for(int i = 0; i < MAX_FREQS; i++)
482 if(EQUIV(config.base_freq[i], freq))
484 //printf("Synth::delete_freq %d\n", __LINE__);
485 // Shift frequencies back
486 for(int j = i; j < MAX_FREQS - 1; j++)
488 config.base_freq[j] = config.base_freq[j + 1];
490 config.base_freq[MAX_FREQS - 1] = 0;
497 void Synth::delete_freqs()
499 for(int i = 0; i < MAX_FREQS; i++)
500 config.base_freq[i] = 0;
530 SynthWindow::SynthWindow(Synth *synth)
531 : PluginClientWindow(synth,
545 bzero(notes, sizeof(SynthNote*) * TOTALNOTES);
549 SynthWindow::~SynthWindow()
559 static const char *keyboard_map[] =
561 "q", "2", "w", "3", "e", "r", "5", "t", "6", "y", "7", "u",
562 "z", "s", "x", "d", "c", "v", "g", "b", "h", "n", "j", "m"
565 void SynthWindow::create_objects()
568 add_subwindow(menu = new BC_MenuBar(0, 0, get_w()));
570 BC_Menu *levelmenu, *phasemenu, *harmonicmenu;
571 menu->add_menu(levelmenu = new BC_Menu(_("Level")));
572 menu->add_menu(phasemenu = new BC_Menu(_("Phase")));
573 menu->add_menu(harmonicmenu = new BC_Menu(_("Harmonic")));
575 levelmenu->add_item(new SynthLevelInvert(synth));
576 levelmenu->add_item(new SynthLevelMax(synth));
577 levelmenu->add_item(new SynthLevelRandom(synth));
578 levelmenu->add_item(new SynthLevelSine(synth));
579 levelmenu->add_item(new SynthLevelSlope(synth));
580 levelmenu->add_item(new SynthLevelZero(synth));
582 phasemenu->add_item(new SynthPhaseInvert(synth));
583 phasemenu->add_item(new SynthPhaseRandom(synth));
584 phasemenu->add_item(new SynthPhaseSine(synth));
585 phasemenu->add_item(new SynthPhaseZero(synth));
587 harmonicmenu->add_item(new SynthFreqEnum(synth));
588 harmonicmenu->add_item(new SynthFreqEven(synth));
589 harmonicmenu->add_item(new SynthFreqFibonacci(synth));
590 harmonicmenu->add_item(new SynthFreqOdd(synth));
591 harmonicmenu->add_item(new SynthFreqPrime(synth));
595 add_subwindow(new BC_Title(x, y, _("Waveform")));
597 add_subwindow(new BC_Title(x, y, _("Wave Function")));
600 add_subwindow(canvas = new SynthCanvas(synth, this, x, y, 230, 160));
604 char string[BCTEXTLEN];
605 waveform_to_text(string, synth->config.wavefunction);
607 add_subwindow(waveform = new SynthWaveForm(synth, x, y, string));
608 waveform->create_objects();
610 int x1 = x + waveform->get_w() + 10;
613 add_subwindow(new BC_Title(x, y, _("Base Frequency:")));
615 add_subwindow(base_freq = new SynthBaseFreq(synth, this, x, y));
616 base_freq->update((float)synth->config.base_freq[0]);
617 x += base_freq->get_w() + synth->get_theme()->widget_border;
618 add_subwindow(freqpot = new SynthFreqPot(synth, this, x, y - 10));
619 base_freq->freq_pot = freqpot;
620 freqpot->freq_text = base_freq;
621 x -= base_freq->get_w() + synth->get_theme()->widget_border;
623 add_subwindow(new BC_Title(x, y, _("Wetness:")));
624 add_subwindow(wetness = new SynthWetness(synth, x + 70, y - 10));
627 add_subwindow(new SynthClear(synth, x, y));
632 add_subwindow(new BC_Title(x, y, _("Level")));
634 add_subwindow(new BC_Title(x, y, _("Phase")));
636 add_subwindow(new BC_Title(x, y, _("Harmonic")));
641 add_subwindow(osc_subwindow = new BC_SubWindow(x, y, 265, get_h() - y));
643 add_subwindow(osc_scroll = new OscScroll(synth, this, x, y, get_h() - y));
647 add_subwindow(new SynthAddOsc(synth, this, x, y));
649 add_subwindow(new SynthDelOsc(synth, this, x, y));
654 #include "white_up_png.h"
655 #include "white_hi_png.h"
656 #include "white_dn_png.h"
657 #include "white_checked_png.h"
658 #include "white_checkedhi_png.h"
659 #include "black_up_png.h"
660 #include "black_hi_png.h"
661 #include "black_dn_png.h"
662 #include "black_checked_png.h"
663 #include "black_checkedhi_png.h"
664 white_key[0] = new VFramePng(white_up_png);
665 white_key[1] = new VFramePng(white_hi_png);
666 white_key[2] = new VFramePng(white_checked_png);
667 white_key[3] = new VFramePng(white_dn_png);
668 white_key[4] = new VFramePng(white_checkedhi_png);
669 black_key[0] = new VFramePng(black_up_png);
670 black_key[1] = new VFramePng(black_hi_png);
671 black_key[2] = new VFramePng(black_checked_png);
672 black_key[3] = new VFramePng(black_dn_png);
673 black_key[4] = new VFramePng(black_checkedhi_png);
676 add_subwindow(note_subwindow = new BC_SubWindow(x1,
679 white_key[0]->get_h() + MARGIN +
680 get_text_height(MEDIUMFONT) + MARGIN +
681 get_text_height(MEDIUMFONT) + MARGIN));
682 add_subwindow(note_scroll = new NoteScroll(synth,
685 note_subwindow->get_y() + note_subwindow->get_h(),
686 note_subwindow->get_w()));
688 add_subwindow(momentary = new SynthMomentary(this,
690 note_scroll->get_y() + note_scroll->get_h() + MARGIN,
691 _("Momentary notes")));
694 add_subwindow(note_instructions = new BC_Title(
696 momentary->get_y() + momentary->get_h() + MARGIN,
697 _("Ctrl or Shift to select multiple notes.")));
700 update_oscillators();
706 int SynthWindow::keypress_event()
708 if(ctrl_down() && get_keypress() == 'w')
716 int SynthWindow::resize_event(int w, int h)
718 clear_box(0, 0, w, h);
719 osc_subwindow->reposition_window(osc_subwindow->get_x(),
720 osc_subwindow->get_y(),
721 osc_subwindow->get_w(),
722 h - osc_subwindow->get_y());
723 osc_subwindow->clear_box(0, 0, osc_subwindow->get_w(), osc_subwindow->get_h());
724 osc_scroll->reposition_window(osc_scroll->get_x(),
726 h - osc_scroll->get_y());
727 note_subwindow->reposition_window(note_subwindow->get_x(),
728 note_subwindow->get_y(),
729 w - note_subwindow->get_x(),
730 note_subwindow->get_h());
731 note_scroll->reposition_window(note_scroll->get_x(),
732 note_scroll->get_y(),
733 w - note_scroll->get_x());
734 note_scroll->update_length(white_key[0]->get_w() * TOTALNOTES * 7 / 12 +
735 white_key[0]->get_w(),
736 note_scroll->get_position(),
737 note_subwindow->get_w(),
742 update_oscillators();
748 void SynthWindow::update_gui()
750 char string[BCTEXTLEN];
751 freqpot->update((int)synth->config.base_freq[0]);
752 base_freq->update((float)synth->config.base_freq[0]);
753 wetness->update(synth->config.wetness);
754 waveform_to_text(string, synth->config.wavefunction);
755 waveform->set_text(string);
756 momentary->update(synth->config.momentary_notes);
759 update_oscillators();
761 update_note_selection();
765 void SynthWindow::update_scrollbar()
767 osc_scroll->update_length(synth->config.oscillator_config.total * OSCILLATORHEIGHT,
768 osc_scroll->get_position(),
769 osc_subwindow->get_h(),
774 void SynthWindow::update_whitekey(int number,
781 note_subwindow->add_subwindow(notes[number] = new SynthNote(this,
782 white_key, number, x, y));
783 if(number >= FIRST_TITLE && number < LAST_TITLE)
784 note_subwindow->add_subwindow(
785 note_titles[(*current_title)++] = new BC_Title(
786 x + text_white_margin,
788 keyboard_map[number - FIRST_TITLE]));
789 //printf("SynthWindow::update_whitekey %d\n", __LINE__);
793 notes[number]->reposition_window(x, y);
794 if(number >= FIRST_TITLE && number < LAST_TITLE)
795 note_titles[(*current_title)++]->reposition_window(x + text_white_margin,
801 void SynthWindow::update_blackkey(int number,
808 note_subwindow->add_subwindow(notes[number] = new SynthNote(this,
809 black_key, number, x, y));
810 if(number >= FIRST_TITLE && number < LAST_TITLE)
811 note_subwindow->add_subwindow(
812 note_titles[(*current_title)++] = new BC_Title(x + text_black_margin,
814 keyboard_map[number - FIRST_TITLE]));
818 notes[number]->reposition_window(x, y);
819 if(number >= FIRST_TITLE && number < LAST_TITLE)
820 note_titles[(*current_title)++]->reposition_window(x + text_black_margin,
825 void SynthWindow::update_notes()
827 //int octave_w = white_key[0]->get_w() * 7;
828 int white_w = white_key[0]->get_w();
829 int black_w = black_key[0]->get_w();
830 int white_w1 = white_w - black_w / 2 - 2;
831 int white_w2 = white_w / 2;
832 int white_w3 = white_w * 2 / 3;
835 y1 = y + white_key[0]->get_h() + 10;
836 y2 = y1 + get_text_height(MEDIUMFONT) + 10;
837 y3 = y2 + get_text_height(MEDIUMFONT) + 10;
838 text_black_margin = black_w / 2 - get_text_width(MEDIUMFONT, "O") / 2;
839 text_white_margin = white_w / 2 - get_text_width(MEDIUMFONT, "O") / 2;
842 //printf("SynthWindow::update_notes %d\n", __LINE__);
843 note_subwindow->clear_box(0, 0, get_w(), get_h());
845 note_subwindow->set_color(get_resources()->default_text_color);
848 // To get the stacking order:
849 // pass 0 is white keys
850 // pass 1 is black keys
851 int current_title = 0;
852 for(int pass = 0; pass < 2; pass++)
854 x = -note_scroll->get_position();
856 for(int i = 0; i < TOTALNOTES; i++)
858 int octave_note = i % 12;
865 update_whitekey(i, ¤t_title, x, y);
871 update_whitekey(i, ¤t_title, x, y);
877 update_whitekey(i, ¤t_title, x, y);
881 update_whitekey(i, ¤t_title, x, y);
887 update_whitekey(i, ¤t_title, x, y);
893 update_whitekey(i, ¤t_title, x, y);
899 update_whitekey(i, ¤t_title, x, y);
910 update_blackkey(i, ¤t_title, x + white_w2, y);
914 update_blackkey(i, ¤t_title, x + white_w3, y);
921 update_blackkey(i, ¤t_title, x + white_w2, y);
925 update_blackkey(i, ¤t_title, x + white_w1, y);
929 update_blackkey(i, ¤t_title, x + white_w3, y);
941 void SynthWindow::update_note_selection()
943 for(int i = 0; i < TOTALNOTES; i++)
946 for(int j = 0; j < MAX_FREQS; j++)
948 if(synth->freq_exists(keyboard_freqs[notes[i]->number]))
957 notes[i]->set_value(1);
960 notes[i]->set_value(0);
965 void SynthWindow::update_oscillators()
967 int i, y = -osc_scroll->get_position();
971 // Add new oscillators
973 i < synth->config.oscillator_config.total;
977 SynthOscillatorConfig *config = synth->config.oscillator_config.values[i];
979 if(oscillators.total <= i)
981 oscillators.append(gui = new SynthOscGUI(this, i));
982 gui->create_objects(y);
986 gui = oscillators.values[i];
988 gui->title->reposition_window(gui->title->get_x(), y + 15);
990 gui->level->reposition_window(gui->level->get_x(), y);
991 gui->level->update(config->level);
993 gui->phase->reposition_window(gui->phase->get_x(), y);
994 gui->phase->update((int64_t)(config->phase * 360));
996 gui->freq->reposition_window(gui->freq->get_x(), y);
997 gui->freq->update((int64_t)(config->freq_factor));
999 y += OSCILLATORHEIGHT;
1002 // Delete old oscillators
1004 i < oscillators.total;
1006 oscillators.remove_object();
1010 int SynthWindow::waveform_to_text(char *text, int waveform)
1014 case DC: sprintf(text, _("DC")); break;
1015 case SINE: sprintf(text, _("Sine")); break;
1016 case SAWTOOTH: sprintf(text, _("Sawtooth")); break;
1017 case SQUARE: sprintf(text, _("Square")); break;
1018 case TRIANGLE: sprintf(text, _("Triangle")); break;
1019 case PULSE: sprintf(text, _("Pulse")); break;
1020 case NOISE: sprintf(text, _("Noise")); break;
1026 SynthMomentary::SynthMomentary(SynthWindow *window, int x, int y, char *text)
1029 window->synth->config.momentary_notes,
1032 this->window = window;
1035 int SynthMomentary::handle_event()
1037 window->synth->config.momentary_notes = get_value();
1038 window->synth->send_configure_change();
1045 SynthNote::SynthNote(SynthWindow *window,
1053 window->synth->freq_exists(keyboard_freqs[number]))
1055 this->window = window;
1056 this->number = number;
1062 void SynthNote::start_note()
1064 if(window->synth->config.momentary_notes) note_on = 1;
1066 //printf("SynthNote::start_note %d %d\n", __LINE__, ctrl_down());
1067 if(window->synth->config.momentary_notes || (!ctrl_down() && !shift_down()))
1069 // Kill all frequencies
1070 window->synth->delete_freqs();
1073 window->synth->new_freq(keyboard_freqs[number]);
1074 window->base_freq->update((float)window->synth->config.base_freq[0]);
1075 //printf("SynthNote::start_note %d %f\n", __LINE__, window->synth->config.base_freq[0]);
1076 window->freqpot->update(window->synth->config.base_freq[0]);
1077 window->synth->send_configure_change();
1078 window->update_note_selection();
1081 void SynthNote::stop_note()
1084 window->synth->delete_freq(keyboard_freqs[number]);
1085 window->base_freq->update((float)window->synth->config.base_freq[0]);
1086 window->freqpot->update(window->synth->config.base_freq[0]);
1087 window->synth->send_configure_change();
1088 window->update_note_selection();
1091 int SynthNote::keypress_event()
1093 if(number >= FIRST_TITLE && number < LAST_TITLE)
1095 if(get_keypress() == keyboard_map[number - FIRST_TITLE][0])
1097 if((ctrl_down() || shift_down()) &&
1098 window->synth->freq_exists(keyboard_freqs[number]))
1108 // Key releases are repeated, so momentary notes may not work
1115 int SynthNote::keyrelease_event()
1117 if(note_on && window->synth->config.momentary_notes)
1126 int SynthNote::cursor_motion_event()
1129 if(window->current_note > -1)
1131 int cursor_x = get_relative_cursor_x();
1132 int cursor_y = get_relative_cursor_y();
1133 if(cursor_x >= 0 && cursor_x < get_w() &&
1134 cursor_y >= 0 && cursor_y < get_h())
1136 if(window->starting_notes)
1145 window->current_note = number;
1152 int SynthNote::button_press_event()
1154 if(BC_Toggle::button_press_event())
1156 // printf("SynthNote::button_press_event %d %d %d\n",
1159 // window->synth->freq_exists(keyboard_freqs[number]));
1160 window->starting_notes = 1;
1161 if((ctrl_down() || shift_down()) &&
1162 window->synth->freq_exists(keyboard_freqs[number]))
1164 //printf("SynthNote::button_press_event %d\n", __LINE__);
1166 window->starting_notes = 0;
1170 //printf("SynthNote::button_press_event %d\n", __LINE__);
1172 window->starting_notes = 1;
1174 window->current_note = number;
1180 int SynthNote::button_release_event()
1182 // Change frequency permanently
1183 if(window->current_note == number)
1185 if(window->synth->config.momentary_notes)
1187 // Mute on button release
1191 window->current_note = -1;
1194 return BC_Toggle::button_release_event();
1197 int SynthNote::draw_face(int flash, int flush)
1199 BC_Toggle::draw_face(0, 0);
1200 static const char *titles[] =
1217 const char *text = titles[number % (sizeof(titles) / sizeof(char*))];
1218 char string[BCTEXTLEN];
1219 sprintf(string, "%s%d", text, number / 12);
1220 //printf("SynthNote::draw_face %d %d %d %d %s\n", __LINE__, number, get_w(), get_h(), text);
1227 draw_text(get_w() / 2 - get_text_width(MEDIUMFONT, string) / 2,
1228 get_h() - get_text_height(MEDIUMFONT, string) - window->synth->get_theme()->widget_border,
1232 if(flash) this->flash(0);
1233 if(flush) this->flush();
1243 SynthOscGUI::SynthOscGUI(SynthWindow *window, int number)
1245 this->window = window;
1246 this->number = number;
1249 SynthOscGUI::~SynthOscGUI()
1257 void SynthOscGUI::create_objects(int y)
1259 char text[BCTEXTLEN];
1260 sprintf(text, "%d:", number + 1);
1261 window->osc_subwindow->add_subwindow(title = new BC_Title(10, y + 15, text));
1263 window->osc_subwindow->add_subwindow(level = new SynthOscGUILevel(window->synth, this, y));
1264 window->osc_subwindow->add_subwindow(phase = new SynthOscGUIPhase(window->synth, this, y));
1265 window->osc_subwindow->add_subwindow(freq = new SynthOscGUIFreq(window->synth, this, y));
1271 SynthOscGUILevel::SynthOscGUILevel(Synth *synth, SynthOscGUI *gui, int y)
1274 synth->config.oscillator_config.values[gui->number]->level,
1278 this->synth = synth;
1282 SynthOscGUILevel::~SynthOscGUILevel()
1286 int SynthOscGUILevel::handle_event()
1288 SynthOscillatorConfig *config = synth->config.oscillator_config.values[gui->number];
1289 config->level = get_value();
1290 gui->window->canvas->update();
1291 synth->send_configure_change();
1297 SynthOscGUIPhase::SynthOscGUIPhase(Synth *synth, SynthOscGUI *gui, int y)
1300 (int64_t)(synth->config.oscillator_config.values[gui->number]->phase * 360),
1304 this->synth = synth;
1308 SynthOscGUIPhase::~SynthOscGUIPhase()
1312 int SynthOscGUIPhase::handle_event()
1314 SynthOscillatorConfig *config = synth->config.oscillator_config.values[gui->number];
1315 config->phase = (float)get_value() / 360;
1316 gui->window->canvas->update();
1317 synth->send_configure_change();
1323 SynthOscGUIFreq::SynthOscGUIFreq(Synth *synth, SynthOscGUI *gui, int y)
1326 (int64_t)(synth->config.oscillator_config.values[gui->number]->freq_factor),
1330 this->synth = synth;
1334 SynthOscGUIFreq::~SynthOscGUIFreq()
1338 int SynthOscGUIFreq::handle_event()
1340 SynthOscillatorConfig *config = synth->config.oscillator_config.values[gui->number];
1341 config->freq_factor = get_value();
1342 gui->window->canvas->update();
1343 synth->send_configure_change();
1353 SynthAddOsc::SynthAddOsc(Synth *synth, SynthWindow *window, int x, int y)
1354 : BC_GenericButton(x, y, _("Add"))
1356 this->synth = synth;
1357 this->window = window;
1360 SynthAddOsc::~SynthAddOsc()
1364 int SynthAddOsc::handle_event()
1366 synth->add_oscillator();
1367 synth->send_configure_change();
1368 window->update_gui();
1374 SynthDelOsc::SynthDelOsc(Synth *synth, SynthWindow *window, int x, int y)
1375 : BC_GenericButton(x, y, _("Delete"))
1377 this->synth = synth;
1378 this->window = window;
1381 SynthDelOsc::~SynthDelOsc()
1385 int SynthDelOsc::handle_event()
1387 synth->delete_oscillator();
1388 synth->send_configure_change();
1389 window->update_gui();
1394 OscScroll::OscScroll(Synth *synth,
1395 SynthWindow *window,
1403 synth->config.oscillator_config.total * OSCILLATORHEIGHT,
1405 window->osc_subwindow->get_h())
1407 this->synth = synth;
1408 this->window = window;
1411 OscScroll::~OscScroll()
1415 int OscScroll::handle_event()
1417 window->update_oscillators();
1423 NoteScroll::NoteScroll(Synth *synth,
1424 SynthWindow *window,
1432 window->white_key[0]->get_w() * TOTALNOTES * 7 / 12 + window->white_key[0]->get_w(),
1434 window->note_subwindow->get_w())
1436 this->synth = synth;
1437 this->window = window;
1440 NoteScroll::~NoteScroll()
1444 int NoteScroll::handle_event()
1446 window->update_notes();
1463 SynthClear::SynthClear(Synth *synth, int x, int y)
1464 : BC_GenericButton(x, y, _("Clear"))
1466 this->synth = synth;
1468 SynthClear::~SynthClear()
1471 int SynthClear::handle_event()
1473 synth->config.reset();
1474 synth->send_configure_change();
1475 synth->update_gui();
1484 SynthWaveForm::SynthWaveForm(Synth *synth, int x, int y, char *text)
1485 : BC_PopupMenu(x, y, 120, text)
1487 this->synth = synth;
1490 SynthWaveForm::~SynthWaveForm()
1494 void SynthWaveForm::create_objects()
1496 // add_item(new SynthWaveFormItem(synth, _("DC"), DC));
1497 add_item(new SynthWaveFormItem(synth, _("Sine"), SINE));
1498 add_item(new SynthWaveFormItem(synth, _("Sawtooth"), SAWTOOTH));
1499 add_item(new SynthWaveFormItem(synth, _("Square"), SQUARE));
1500 add_item(new SynthWaveFormItem(synth, _("Triangle"), TRIANGLE));
1501 add_item(new SynthWaveFormItem(synth, _("Pulse"), PULSE));
1502 add_item(new SynthWaveFormItem(synth, _("Noise"), NOISE));
1505 SynthWaveFormItem::SynthWaveFormItem(Synth *synth, char *text, int value)
1508 this->synth = synth;
1509 this->value = value;
1512 SynthWaveFormItem::~SynthWaveFormItem()
1516 int SynthWaveFormItem::handle_event()
1518 synth->config.wavefunction = value;
1519 ((SynthWindow*)synth->thread->window)->canvas->update();
1520 get_popup_menu()->set_text(get_text());
1521 synth->send_configure_change();
1526 SynthWetness::SynthWetness(Synth *synth, int x, int y)
1529 synth->config.wetness,
1533 this->synth = synth;
1536 int SynthWetness::handle_event()
1538 synth->config.wetness = get_value();
1539 synth->send_configure_change();
1545 SynthFreqPot::SynthFreqPot(Synth *synth, SynthWindow *window, int x, int y)
1546 : BC_QPot(x, y, synth->config.base_freq[0])
1548 this->synth = synth;
1549 this->window = window;
1551 SynthFreqPot::~SynthFreqPot()
1554 int SynthFreqPot::handle_event()
1556 if(get_value() > 0 && get_value() < 30000)
1558 synth->config.base_freq[0] = get_value();
1559 freq_text->update(get_value());
1560 synth->send_configure_change();
1561 window->update_note_selection();
1568 SynthBaseFreq::SynthBaseFreq(Synth *synth, SynthWindow *window, int x, int y)
1569 : BC_TextBox(x, y, 100, 1, (float)0)
1571 this->synth = synth;
1572 this->window = window;
1575 SynthBaseFreq::~SynthBaseFreq()
1578 int SynthBaseFreq::handle_event()
1580 double new_value = atof(get_text());
1582 if(new_value < 30000)
1584 synth->config.base_freq[0] = new_value;
1585 freq_pot->update(synth->config.base_freq[0]);
1586 synth->send_configure_change();
1587 window->update_note_selection();
1596 SynthCanvas::SynthCanvas(Synth *synth,
1597 SynthWindow *window,
1608 this->synth = synth;
1609 this->window = window;
1612 SynthCanvas::~SynthCanvas()
1616 int SynthCanvas::update()
1620 clear_box(0, 0, get_w(), get_h());
1623 draw_line(0, get_h() / 2 + y, get_w(), get_h() / 2 + y);
1627 double normalize_constant = (double)1 / synth->get_total_power();
1628 y1 = (int)(synth->get_point((float)0, normalize_constant) * get_h() / 2);
1630 for(int i = 1; i < get_w(); i++)
1632 y2 = (int)(synth->get_point((float)i / get_w(), normalize_constant) * get_h() / 2);
1633 draw_line(i - 1, get_h() / 2 - y1, i, get_h() / 2 - y2);
1647 // ======================= level calculations
1648 SynthLevelZero::SynthLevelZero(Synth *synth)
1649 : BC_MenuItem(_("Zero"))
1651 this->synth = synth;
1654 SynthLevelZero::~SynthLevelZero()
1658 int SynthLevelZero::handle_event()
1660 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1662 synth->config.oscillator_config.values[i]->level = INFINITYGAIN;
1665 ((SynthWindow*)synth->thread->window)->update_gui();
1666 synth->send_configure_change();
1670 SynthLevelMax::SynthLevelMax(Synth *synth)
1671 : BC_MenuItem(_("Maximum"))
1673 this->synth = synth;
1676 SynthLevelMax::~SynthLevelMax()
1680 int SynthLevelMax::handle_event()
1682 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1684 synth->config.oscillator_config.values[i]->level = 0;
1686 ((SynthWindow*)synth->thread->window)->update_gui();
1687 synth->send_configure_change();
1691 SynthLevelNormalize::SynthLevelNormalize(Synth *synth)
1692 : BC_MenuItem(_("Normalize"))
1694 this->synth = synth;
1697 SynthLevelNormalize::~SynthLevelNormalize()
1701 int SynthLevelNormalize::handle_event()
1706 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1708 total += synth->db.fromdb(synth->config.oscillator_config.values[i]->level);
1711 float scale = 1 / total;
1714 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1716 new_value = synth->db.fromdb(synth->config.oscillator_config.values[i]->level);
1718 new_value = synth->db.todb(new_value);
1720 synth->config.oscillator_config.values[i]->level = new_value;
1723 ((SynthWindow*)synth->thread->window)->update_gui();
1724 synth->send_configure_change();
1728 SynthLevelSlope::SynthLevelSlope(Synth *synth)
1729 : BC_MenuItem(_("Slope"))
1731 this->synth = synth;
1734 SynthLevelSlope::~SynthLevelSlope()
1738 int SynthLevelSlope::handle_event()
1740 float slope = (float)INFINITYGAIN / synth->config.oscillator_config.total;
1742 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1744 synth->config.oscillator_config.values[i]->level = i * slope;
1747 ((SynthWindow*)synth->thread->window)->update_gui();
1748 synth->send_configure_change();
1752 SynthLevelRandom::SynthLevelRandom(Synth *synth)
1753 : BC_MenuItem(_("Random"))
1755 this->synth = synth;
1757 SynthLevelRandom::~SynthLevelRandom()
1761 int SynthLevelRandom::handle_event()
1764 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1766 synth->config.oscillator_config.values[i]->level = -(rand() % -INFINITYGAIN);
1769 ((SynthWindow*)synth->thread->window)->update_gui();
1770 synth->send_configure_change();
1774 SynthLevelInvert::SynthLevelInvert(Synth *synth)
1775 : BC_MenuItem(_("Invert"))
1777 this->synth = synth;
1779 SynthLevelInvert::~SynthLevelInvert()
1783 int SynthLevelInvert::handle_event()
1785 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1787 synth->config.oscillator_config.values[i]->level =
1788 INFINITYGAIN - synth->config.oscillator_config.values[i]->level;
1791 ((SynthWindow*)synth->thread->window)->update_gui();
1792 synth->send_configure_change();
1796 SynthLevelSine::SynthLevelSine(Synth *synth)
1797 : BC_MenuItem(_("Sine"))
1799 this->synth = synth;
1801 SynthLevelSine::~SynthLevelSine()
1805 int SynthLevelSine::handle_event()
1809 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1811 new_value = (float)i / synth->config.oscillator_config.total * 2 * M_PI;
1812 new_value = sin(new_value) * INFINITYGAIN / 2 + INFINITYGAIN / 2;
1813 synth->config.oscillator_config.values[i]->level = new_value;
1816 ((SynthWindow*)synth->thread->window)->update_gui();
1817 synth->send_configure_change();
1821 // ============================ phase calculations
1823 SynthPhaseInvert::SynthPhaseInvert(Synth *synth)
1824 : BC_MenuItem(_("Invert"))
1826 this->synth = synth;
1828 SynthPhaseInvert::~SynthPhaseInvert()
1832 int SynthPhaseInvert::handle_event()
1834 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1836 synth->config.oscillator_config.values[i]->phase =
1837 1 - synth->config.oscillator_config.values[i]->phase;
1840 ((SynthWindow*)synth->thread->window)->update_gui();
1841 synth->send_configure_change();
1845 SynthPhaseZero::SynthPhaseZero(Synth *synth)
1846 : BC_MenuItem(_("Zero"))
1848 this->synth = synth;
1850 SynthPhaseZero::~SynthPhaseZero()
1854 int SynthPhaseZero::handle_event()
1856 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1858 synth->config.oscillator_config.values[i]->phase = 0;
1861 ((SynthWindow*)synth->thread->window)->update_gui();
1862 synth->send_configure_change();
1866 SynthPhaseSine::SynthPhaseSine(Synth *synth)
1867 : BC_MenuItem(_("Sine"))
1869 this->synth = synth;
1871 SynthPhaseSine::~SynthPhaseSine()
1875 int SynthPhaseSine::handle_event()
1878 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1880 new_value = (float)i / synth->config.oscillator_config.total * 2 * M_PI;
1881 new_value = sin(new_value) / 2 + .5;
1882 synth->config.oscillator_config.values[i]->phase = new_value;
1885 ((SynthWindow*)synth->thread->window)->update_gui();
1886 synth->send_configure_change();
1890 SynthPhaseRandom::SynthPhaseRandom(Synth *synth)
1891 : BC_MenuItem(_("Random"))
1893 this->synth = synth;
1895 SynthPhaseRandom::~SynthPhaseRandom()
1899 int SynthPhaseRandom::handle_event()
1902 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1904 synth->config.oscillator_config.values[i]->phase =
1905 (float)(rand() % 360) / 360;
1908 ((SynthWindow*)synth->thread->window)->update_gui();
1909 synth->send_configure_change();
1914 // ============================ freq calculations
1916 SynthFreqRandom::SynthFreqRandom(Synth *synth)
1917 : BC_MenuItem(_("Random"))
1919 this->synth = synth;
1921 SynthFreqRandom::~SynthFreqRandom()
1925 int SynthFreqRandom::handle_event()
1928 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1930 synth->config.oscillator_config.values[i]->freq_factor = rand() % 100;
1933 ((SynthWindow*)synth->thread->window)->update_gui();
1934 synth->send_configure_change();
1938 SynthFreqEnum::SynthFreqEnum(Synth *synth)
1939 : BC_MenuItem(_("Enumerate"))
1941 this->synth = synth;
1943 SynthFreqEnum::~SynthFreqEnum()
1947 int SynthFreqEnum::handle_event()
1949 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1951 synth->config.oscillator_config.values[i]->freq_factor = (float)i + 1;
1954 ((SynthWindow*)synth->thread->window)->update_gui();
1955 synth->send_configure_change();
1959 SynthFreqEven::SynthFreqEven(Synth *synth)
1960 : BC_MenuItem(_("Even"))
1962 this->synth = synth;
1964 SynthFreqEven::~SynthFreqEven()
1968 int SynthFreqEven::handle_event()
1970 if(synth->config.oscillator_config.total)
1971 synth->config.oscillator_config.values[0]->freq_factor = (float)1;
1973 for(int i = 1; i < synth->config.oscillator_config.total; i++)
1975 synth->config.oscillator_config.values[i]->freq_factor = (float)i * 2;
1978 ((SynthWindow*)synth->thread->window)->update_gui();
1979 synth->send_configure_change();
1983 SynthFreqOdd::SynthFreqOdd(Synth *synth)
1984 : BC_MenuItem(_("Odd"))
1985 { this->synth = synth; }
1986 SynthFreqOdd::~SynthFreqOdd()
1990 int SynthFreqOdd::handle_event()
1992 for(int i = 0; i < synth->config.oscillator_config.total; i++)
1994 synth->config.oscillator_config.values[i]->freq_factor = (float)1 + i * 2;
1997 ((SynthWindow*)synth->thread->window)->update_gui();
1998 synth->send_configure_change();
2002 SynthFreqFibonacci::SynthFreqFibonacci(Synth *synth)
2003 : BC_MenuItem(_("Fibonnacci"))
2005 this->synth = synth;
2007 SynthFreqFibonacci::~SynthFreqFibonacci()
2011 int SynthFreqFibonacci::handle_event()
2013 float last_value1 = 0, last_value2 = 1;
2014 for(int i = 0; i < synth->config.oscillator_config.total; i++)
2016 synth->config.oscillator_config.values[i]->freq_factor = last_value1 + last_value2;
2017 if(synth->config.oscillator_config.values[i]->freq_factor > 100) synth->config.oscillator_config.values[i]->freq_factor = 100;
2018 last_value1 = last_value2;
2019 last_value2 = synth->config.oscillator_config.values[i]->freq_factor;
2022 ((SynthWindow*)synth->thread->window)->update_gui();
2023 synth->send_configure_change();
2027 SynthFreqPrime::SynthFreqPrime(Synth *synth)
2028 : BC_MenuItem(_("Prime"))
2030 this->synth = synth;
2032 SynthFreqPrime::~SynthFreqPrime()
2036 int SynthFreqPrime::handle_event()
2039 for(int i = 0; i < synth->config.oscillator_config.total; i++)
2041 synth->config.oscillator_config.values[i]->freq_factor = number;
2042 number = get_next_prime(number);
2045 ((SynthWindow*)synth->thread->window)->update_gui();
2046 synth->send_configure_change();
2050 float SynthFreqPrime::get_next_prime(float number)
2059 for(float i = number - 1; i > 1 && !result; i--)
2061 if((number / i) - (int)(number / i) == 0) result = 1;
2075 SynthOscillatorConfig::SynthOscillatorConfig(int number)
2078 this->number = number;
2081 SynthOscillatorConfig::~SynthOscillatorConfig()
2085 void SynthOscillatorConfig::reset()
2093 void SynthOscillatorConfig::read_data(FileXML *file)
2095 level = file->tag.get_property("LEVEL", (float)level);
2096 phase = file->tag.get_property("PHASE", (float)phase);
2097 freq_factor = file->tag.get_property("FREQFACTOR", (float)freq_factor);
2100 void SynthOscillatorConfig::save_data(FileXML *file)
2102 file->tag.set_title("OSCILLATOR");
2103 file->tag.set_property("LEVEL", (float)level);
2104 file->tag.set_property("PHASE", (float)phase);
2105 file->tag.set_property("FREQFACTOR", (float)freq_factor);
2107 file->tag.set_title("/OSCILLATOR");
2109 file->append_newline();
2112 int SynthOscillatorConfig::equivalent(SynthOscillatorConfig &that)
2114 if(EQUIV(level, that.level) &&
2115 EQUIV(phase, that.phase) &&
2116 EQUIV(freq_factor, that.freq_factor))
2122 void SynthOscillatorConfig::copy_from(SynthOscillatorConfig& that)
2126 freq_factor = that.freq_factor;
2130 SynthConfig::SynthConfig()
2135 SynthConfig::~SynthConfig()
2137 oscillator_config.remove_all_objects();
2140 void SynthConfig::reset()
2144 for(int i = 1; i < MAX_FREQS; i++)
2146 wavefunction = SINE;
2147 for(int i = 0; i < oscillator_config.total; i++)
2149 oscillator_config.values[i]->reset();
2152 momentary_notes = 0;
2155 int SynthConfig::equivalent(SynthConfig &that)
2157 //printf("SynthConfig::equivalent %d %d\n", base_freq, that.base_freq);
2158 for(int i = 0; i < MAX_FREQS; i++)
2159 if(base_freq[i] != that.base_freq[i]) return 0;
2161 if(wavefunction != that.wavefunction ||
2162 oscillator_config.total != that.oscillator_config.total ||
2163 momentary_notes != that.momentary_notes) return 0;
2165 for(int i = 0; i < oscillator_config.total; i++)
2167 if(!oscillator_config.values[i]->equivalent(*that.oscillator_config.values[i]))
2174 void SynthConfig::copy_from(SynthConfig& that)
2176 wetness = that.wetness;
2177 for(int i = 0; i < MAX_FREQS; i++)
2178 base_freq[i] = that.base_freq[i];
2179 wavefunction = that.wavefunction;
2180 momentary_notes = that.momentary_notes;
2184 i < oscillator_config.total && i < that.oscillator_config.total;
2187 oscillator_config.values[i]->copy_from(*that.oscillator_config.values[i]);
2191 i < that.oscillator_config.total;
2194 oscillator_config.append(new SynthOscillatorConfig(i));
2195 oscillator_config.values[i]->copy_from(*that.oscillator_config.values[i]);
2199 i < oscillator_config.total;
2202 oscillator_config.remove_object();
2207 void SynthConfig::interpolate(SynthConfig &prev,
2211 int64_t current_frame)
2213 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
2214 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
2217 wetness = (int)(prev.wetness * prev_scale + next.wetness * next_scale);
2218 // base_freq = (int)(prev.base_freq * prev_scale + next.base_freq * next_scale);
2220 momentary_notes = prev.momentary_notes;