4 * Copyright (C) 2010 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
27 #include "filexml.inc"
30 #include "pluginaclient.h"
31 #include "synthesizer.inc"
35 // Frequency table for piano
36 float keyboard_freqs[] =
109 #define TOTALOSCILLATORS 1
110 #define TOTALNOTES ((int)(sizeof(keyboard_freqs) / sizeof(float)))
112 #define FIRST_TITLE (MIDDLE_C - 12)
113 #define LAST_TITLE (MIDDLE_C + 12)
114 #define MARGIN yS(10)
134 class SynthMomentary;
136 class SynthWindow : public PluginClientWindow
139 SynthWindow(Synth *synth);
142 void create_objects();
143 int resize_event(int w, int h);
145 int waveform_to_text(char *text, int waveform);
146 void update_scrollbar();
147 void update_oscillators();
149 void update_note_selection();
150 int keypress_event();
151 void update_blackkey(int number, int *current_title, int x, int y);
152 void update_whitekey(int number, int *current_title, int x, int y);
157 SynthWetness *wetness;
158 SynthWaveForm *waveform;
159 SynthBaseFreq *base_freq;
160 SynthFreqPot *freqpot;
161 BC_SubWindow *osc_subwindow;
162 OscScroll *osc_scroll;
163 BC_SubWindow *note_subwindow;
164 NoteScroll *note_scroll;
165 ArrayList<SynthOscGUI*> oscillators;
166 SynthNote *notes[TOTALNOTES];
167 BC_Title *note_titles[TOTALNOTES];
168 BC_Title *note_instructions;
169 SynthMomentary *momentary;
170 VFrame *white_key[5];
171 VFrame *black_key[5];
175 int text_white_margin;
176 int text_black_margin;
177 // Button press currently happening if > -1
179 // If we are stopping or starting notes in a drag
183 class SynthMomentary : public BC_CheckBox
186 SynthMomentary(SynthWindow *window, int x, int y, char *text);
191 class SynthNote : public BC_Toggle
194 SynthNote(SynthWindow *window, VFrame **images, int number, int x, int y);
197 int keypress_event();
198 int keyrelease_event();
199 int button_press_event();
200 int button_release_event();
201 int cursor_motion_event();
202 int draw_face(int flash, int flush);
209 class SynthOscGUILevel;
210 class SynthOscGUIPhase;
211 class SynthOscGUIFreq;
216 SynthOscGUI(SynthWindow *window, int number);
219 void create_objects(int view_y);
221 SynthOscGUILevel *level;
222 SynthOscGUIPhase *phase;
223 SynthOscGUIFreq *freq;
230 class SynthOscGUILevel : public BC_FPot
233 SynthOscGUILevel(Synth *synth, SynthOscGUI *gui, int y);
242 class SynthOscGUIPhase : public BC_IPot
245 SynthOscGUIPhase(Synth *synth, SynthOscGUI *gui, int y);
254 class SynthOscGUIFreq : public BC_IPot
257 SynthOscGUIFreq(Synth *synth, SynthOscGUI *gui, int y);
266 class OscScroll : public BC_ScrollBar
269 OscScroll(Synth *synth, SynthWindow *window, int x, int y, int h);
278 class NoteScroll : public BC_ScrollBar
281 NoteScroll(Synth *synth, SynthWindow *window, int x, int y, int w);
290 class SynthAddOsc : public BC_GenericButton
293 SynthAddOsc(Synth *synth, SynthWindow *window, int x, int y);
303 class SynthDelOsc : public BC_GenericButton
306 SynthDelOsc(Synth *synth, SynthWindow *window, int x, int y);
315 class SynthClear : public BC_GenericButton
318 SynthClear(Synth *synth, int x, int y);
324 class SynthWaveForm : public BC_PopupMenu
327 SynthWaveForm(Synth *synth, int x, int y, char *text);
330 void create_objects();
334 class SynthWaveFormItem : public BC_MenuItem
337 SynthWaveFormItem(Synth *synth, char *text, int value);
338 ~SynthWaveFormItem();
346 class SynthBaseFreq : public BC_TextBox
349 SynthBaseFreq(Synth *synth, SynthWindow *window, int x, int y);
353 SynthFreqPot *freq_pot;
357 class SynthFreqPot : public BC_QPot
360 SynthFreqPot(Synth *synth, SynthWindow *window, int x, int y);
365 SynthBaseFreq *freq_text;
368 class SynthWetness : public BC_FPot
371 SynthWetness(Synth *synth, int x, int y);
377 class SynthCanvas : public BC_SubWindow
380 SynthCanvas(Synth *synth,
396 // ======================= level calculations
397 class SynthLevelZero : public BC_MenuItem
400 SynthLevelZero(Synth *synth);
406 class SynthLevelMax : public BC_MenuItem
409 SynthLevelMax(Synth *synth);
415 class SynthLevelNormalize : public BC_MenuItem
418 SynthLevelNormalize(Synth *synth);
419 ~SynthLevelNormalize();
424 class SynthLevelSlope : public BC_MenuItem
427 SynthLevelSlope(Synth *synth);
433 class SynthLevelRandom : public BC_MenuItem
436 SynthLevelRandom(Synth *synth);
442 class SynthLevelInvert : public BC_MenuItem
445 SynthLevelInvert(Synth *synth);
451 class SynthLevelSine : public BC_MenuItem
454 SynthLevelSine(Synth *synth);
460 // ============================ phase calculations
462 class SynthPhaseInvert : public BC_MenuItem
465 SynthPhaseInvert(Synth *synth);
471 class SynthPhaseZero : public BC_MenuItem
474 SynthPhaseZero(Synth *synth);
480 class SynthPhaseSine : public BC_MenuItem
483 SynthPhaseSine(Synth *synth);
489 class SynthPhaseRandom : public BC_MenuItem
492 SynthPhaseRandom(Synth *synth);
499 // ============================ freq calculations
501 class SynthFreqRandom : public BC_MenuItem
504 SynthFreqRandom(Synth *synth);
510 class SynthFreqEnum : public BC_MenuItem
513 SynthFreqEnum(Synth *synth);
519 class SynthFreqEven : public BC_MenuItem
522 SynthFreqEven(Synth *synth);
528 class SynthFreqOdd : public BC_MenuItem
531 SynthFreqOdd(Synth *synth);
537 class SynthFreqFibonacci : public BC_MenuItem
540 SynthFreqFibonacci(Synth *synth);
541 ~SynthFreqFibonacci();
546 class SynthFreqPrime : public BC_MenuItem
549 SynthFreqPrime(Synth *synth);
554 float get_next_prime(float number);
559 class SynthOscillatorConfig
562 SynthOscillatorConfig(int number);
563 ~SynthOscillatorConfig();
565 int equivalent(SynthOscillatorConfig &that);
566 void copy_from(SynthOscillatorConfig& that);
568 void read_data(FileXML *file);
569 void save_data(FileXML *file);
586 int equivalent(SynthConfig &that);
587 void copy_from(SynthConfig &that);
588 void interpolate(SynthConfig &prev,
592 int64_t current_frame);
596 // base frequency for oscillators
597 // Freqs of 0 are unused.
598 double base_freq[MAX_FREQS];
599 int wavefunction; // SINE, SAWTOOTH, etc
600 ArrayList<SynthOscillatorConfig*> oscillator_config;
605 class Synth : public PluginAClient
608 Synth(PluginServer *server);
612 PLUGIN_CLASS_MEMBERS(SynthConfig)
615 void read_data(KeyFrame *keyframe);
616 void save_data(KeyFrame *keyframe);
617 int process_realtime(int64_t size, Samples *input_ptr, Samples *output_ptr);
622 // Frequency is in the table of base_freqs
623 int freq_exists(double freq);
624 // Manage frequency table
625 void new_freq(double freq);
626 void delete_freq(double freq);
629 void add_oscillator();
630 void delete_oscillator();
631 double get_total_power();
632 double get_oscillator_point(float x,
633 double normalize_constant,
635 double solve_eqn(double *output,
638 double normalize_constant,
640 double get_point(float x, double normalize_constant);
641 double function_square(double x);
642 double function_pulse(double x);
643 double function_noise();
644 double function_sawtooth(double x);
645 double function_triangle(double x);
647 int overlay_synth(double freq,
656 int window_w, window_h;
657 int need_reconfigure;
659 // Samples since last reconfiguration
660 int64_t waveform_sample;