+ GUI: remove more unused control pointers
[calf.git] / src / fluidsynth.cpp
blob30d33605b6e41d54f852d96b7d3a0bedef063ce8
1 /* Calf DSP Library
2 * Example audio modules - monosynth
4 * Copyright (C) 2001-2007 Krzysztof Foltman
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 #include <assert.h>
22 #include <memory.h>
23 #include <config.h>
24 #include <complex>
25 #if USE_JACK
26 #include <jack/jack.h>
27 #endif
28 #include <calf/giface.h>
29 #include <calf/modules_synths.h>
30 #include <calf/modules_dev.h>
32 #if ENABLE_EXPERIMENTAL
34 using namespace dsp;
35 using namespace calf_plugins;
36 using namespace std;
38 fluidsynth_audio_module::fluidsynth_audio_module()
40 settings = NULL;
41 synth = NULL;
44 void fluidsynth_audio_module::post_instantiate()
46 settings = new_fluid_settings();
47 synth = create_synth(sfid);
50 void fluidsynth_audio_module::activate()
54 void fluidsynth_audio_module::deactivate()
58 fluid_synth_t *fluidsynth_audio_module::create_synth(int &new_sfid)
60 fluid_settings_t *new_settings = new_fluid_settings();
61 fluid_synth_t *s = new_fluid_synth(new_settings);
62 if (!soundfont.empty())
64 int sid = fluid_synth_sfload(s, soundfont.c_str(), 1);
65 if (sid == -1)
67 delete_fluid_synth(s);
68 return NULL;
70 assert(sid >= 0);
71 printf("sid=%d\n", sid);
72 fluid_synth_sfont_select(s, 0, sid);
73 fluid_synth_bank_select(s, 0, 0);
74 fluid_synth_program_change(s, 0, 0);
75 new_sfid = sid;
77 fluid_sfont_t* sfont = fluid_synth_get_sfont(s, 0);
78 soundfont_name = (*sfont->get_name)(sfont);
80 sfont->iteration_start(sfont);
82 string preset_list;
83 fluid_preset_t tmp;
84 while(sfont->iteration_next(sfont, &tmp))
86 string pname = tmp.get_name(&tmp);
87 int bank = tmp.get_banknum(&tmp);
88 int num = tmp.get_num(&tmp);
89 sf_preset_names[num + 128 * bank] = pname;
90 preset_list += calf_utils::i2s(num + 128 * bank) + "\t" + pname + "\n";
92 soundfont_preset_list = preset_list;
94 else
95 new_sfid = -1;
96 return s;
99 void fluidsynth_audio_module::note_on(int note, int vel)
101 fluid_synth_noteon(synth, 0, note, vel);
104 void fluidsynth_audio_module::note_off(int note, int vel)
106 fluid_synth_noteoff(synth, 0, note);
109 void fluidsynth_audio_module::control_change(int controller, int value)
111 fluid_synth_cc(synth, 0, controller, value);
113 if (controller == 0 || controller == 32)
114 update_preset_num();
117 void fluidsynth_audio_module::program_change(int program)
119 fluid_synth_program_change(synth, 0, program);
121 update_preset_num();
125 void fluidsynth_audio_module::update_preset_num()
127 fluid_preset_t *p = fluid_synth_get_channel_preset(synth, 0);
128 if (p)
129 last_selected_preset = p->get_num(p) + 128 * p->get_banknum(p);
130 else
131 last_selected_preset = 0;
134 uint32_t fluidsynth_audio_module::process(uint32_t offset, uint32_t nsamples, uint32_t inputs_mask, uint32_t outputs_mask)
136 fluid_synth_set_gain(synth, *params[par_master]);
137 fluid_synth_write_float(synth, nsamples, outs[0], offset, 1, outs[1], offset, 1);
138 return 3;
141 char *fluidsynth_audio_module::configure(const char *key, const char *value)
143 if (!strcmp(key, "soundfont"))
145 printf("Loading %s\n", value);
146 soundfont = value;
147 int newsfid = -1;
148 fluid_synth_t *new_synth = create_synth(newsfid);
150 if (new_synth)
152 synth = new_synth;
153 sfid = newsfid;
154 update_preset_num();
156 else
157 return strdup("Cannot load a soundfont");
158 // XXXKF memory leak
160 return NULL;
163 void fluidsynth_audio_module::send_configures(send_configure_iface *sci)
165 sci->send_configure("soundfont", soundfont.c_str());
168 int fluidsynth_audio_module::send_status_updates(send_updates_iface *sui, int last_serial)
170 sui->send_status("sf_name", soundfont_name.c_str());
171 sui->send_status("preset_list", soundfont_preset_list.c_str());
173 map<uint32_t, string>::const_iterator i = sf_preset_names.find(last_selected_preset);
174 if (i == sf_preset_names.end())
175 sui->send_status("preset_name", "");
176 else
177 sui->send_status("preset_name", i->second.c_str());
178 return last_serial + 1;
181 fluidsynth_audio_module::~fluidsynth_audio_module()
183 if (synth) {
184 delete_fluid_synth(synth);
185 synth = NULL;
187 if (settings) {
188 // delete_fluid_settings(settings);
189 settings = NULL;
193 #endif