1 /* FluidSynth - A Software Synthesizer
3 * Copyright (C) 2003 Peter Hanappe and others.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public License
7 * as published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "fluid_chan.h"
22 #include "fluid_mod.h"
23 #include "fluid_synth.h"
24 #include "fluid_sfont.h"
26 /* Field shift amounts for sfont_bank_prog bit field integer */
27 #define PROG_SHIFTVAL 0
28 #define BANK_SHIFTVAL 8
29 #define SFONT_SHIFTVAL 22
31 /* Field mask values for sfont_bank_prog bit field integer */
32 #define PROG_MASKVAL 0x000000FF /* Bit 7 is used to indicate unset state */
33 #define BANK_MASKVAL 0x003FFF00
34 #define BANKLSB_MASKVAL 0x00007F00
35 #define BANKMSB_MASKVAL 0x003F8000
36 #define SFONT_MASKVAL 0xFFC00000
39 static void fluid_channel_init(fluid_channel_t* chan);
43 new_fluid_channel(fluid_synth_t* synth, int num)
45 fluid_channel_t* chan;
47 chan = FLUID_NEW(fluid_channel_t);
49 FLUID_LOG(FLUID_ERR, "Out of memory");
58 fluid_channel_init(chan);
59 fluid_channel_init_ctrl(chan, 0);
65 fluid_channel_init(fluid_channel_t* chan)
67 fluid_preset_t *newpreset;
70 chan->sostenuto_orderid = 0;
72 chan->channel_type = (chan->channum == 9) ? CHANNEL_TYPE_DRUM : CHANNEL_TYPE_MELODIC;
74 banknum = (chan->channel_type == CHANNEL_TYPE_DRUM) ? DRUM_INST_BANK : 0;
76 chan->sfont_bank_prog = 0 << SFONT_SHIFTVAL | banknum << BANK_SHIFTVAL
77 | prognum << PROG_SHIFTVAL;
79 newpreset = fluid_synth_find_preset(chan->synth, banknum, prognum);
80 fluid_channel_set_preset(chan, newpreset);
82 chan->interp_method = FLUID_INTERP_DEFAULT;
83 chan->tuning_bank = 0;
84 chan->tuning_prog = 0;
85 chan->nrpn_select = 0;
86 chan->nrpn_active = 0;
90 fluid_tuning_unref (chan->tuning, 1);
96 @param is_all_ctrl_off if nonzero, only resets some controllers, according to
97 http://www.midi.org/techspecs/rp15.php
100 fluid_channel_init_ctrl(fluid_channel_t* chan, int is_all_ctrl_off)
104 chan->key_pressure = 0;
105 chan->channel_pressure = 0;
106 chan->pitch_bend = 0x2000; /* Range is 0x4000, pitch bend wheel starts in centered position */
108 for (i = 0; i < GEN_LAST; i++) {
110 chan->gen_abs[i] = 0;
113 if (is_all_ctrl_off) {
114 for (i = 0; i < ALL_SOUND_OFF; i++) {
115 if (i >= EFFECTS_DEPTH1 && i <= EFFECTS_DEPTH5) {
118 if (i >= SOUND_CTRL1 && i <= SOUND_CTRL10) {
121 if (i == BANK_SELECT_MSB || i == BANK_SELECT_LSB || i == VOLUME_MSB ||
122 i == VOLUME_LSB || i == PAN_MSB || i == PAN_LSB) {
126 fluid_channel_set_cc (chan, i, 0);
130 for (i = 0; i < 128; i++) {
131 fluid_channel_set_cc (chan, i, 0);
135 /* Set RPN controllers to NULL state */
136 fluid_channel_set_cc (chan, RPN_LSB, 127);
137 fluid_channel_set_cc (chan, RPN_MSB, 127);
139 /* Set NRPN controllers to NULL state */
140 fluid_channel_set_cc (chan, NRPN_LSB, 127);
141 fluid_channel_set_cc (chan, NRPN_MSB, 127);
143 /* Expression (MSB & LSB) */
144 fluid_channel_set_cc (chan, EXPRESSION_MSB, 127);
145 fluid_channel_set_cc (chan, EXPRESSION_LSB, 127);
147 if (!is_all_ctrl_off) {
149 chan->pitch_wheel_sensitivity = 2; /* two semi-tones */
151 /* Just like panning, a value of 64 indicates no change for sound ctrls */
152 for (i = SOUND_CTRL1; i <= SOUND_CTRL10; i++) {
153 fluid_channel_set_cc (chan, i, 64);
156 /* Volume / initial attenuation (MSB & LSB) */
157 fluid_channel_set_cc (chan, VOLUME_MSB, 100);
158 fluid_channel_set_cc (chan, VOLUME_LSB, 0);
160 /* Pan (MSB & LSB) */
161 fluid_channel_set_cc (chan, PAN_MSB, 64);
162 fluid_channel_set_cc (chan, PAN_LSB, 0);
165 /* fluid_channel_set_cc (chan, EFFECTS_DEPTH1, 40); */
166 /* Note: although XG standard specifies the default amount of reverb to
167 be 40, most people preferred having it at zero.
168 See http://lists.gnu.org/archive/html/fluid-dev/2009-07/msg00016.html */
172 /* Only called by delete_fluid_synth(), so no need to queue a preset free event */
174 delete_fluid_channel(fluid_channel_t* chan)
176 if (chan->preset) delete_fluid_preset (chan->preset);
181 /* FIXME - Calls fluid_channel_init() potentially in synthesis context */
183 fluid_channel_reset(fluid_channel_t* chan)
185 fluid_channel_init(chan);
186 fluid_channel_init_ctrl(chan, 0);
189 /* Should only be called from synthesis context */
191 fluid_channel_set_preset(fluid_channel_t* chan, fluid_preset_t* preset)
194 fluid_preset_notify (chan->preset, FLUID_PRESET_UNSELECTED, chan->channum);
197 fluid_sfont_t *sfont;
198 sfont = chan->preset->sfont;
199 delete_fluid_preset (chan->preset);
200 fluid_synth_sfont_unref (chan->synth, sfont); /* -- unref preset's SoundFont */
203 chan->preset = preset;
205 fluid_preset_notify (preset, FLUID_PRESET_SELECTED, chan->channum);
210 /* Set SoundFont ID, MIDI bank and/or program. Use -1 to use current value. */
212 fluid_channel_set_sfont_bank_prog(fluid_channel_t* chan, int sfontnum,
213 int banknum, int prognum)
215 int oldval, newval, oldmask;
217 newval = ((sfontnum != -1) ? sfontnum << SFONT_SHIFTVAL : 0)
218 | ((banknum != -1) ? banknum << BANK_SHIFTVAL : 0)
219 | ((prognum != -1) ? prognum << PROG_SHIFTVAL : 0);
221 oldmask = ((sfontnum != -1) ? 0 : SFONT_MASKVAL)
222 | ((banknum != -1) ? 0 : BANK_MASKVAL)
223 | ((prognum != -1) ? 0 : PROG_MASKVAL);
225 oldval = chan->sfont_bank_prog;
226 newval = (newval & ~oldmask) | (oldval & oldmask);
227 chan->sfont_bank_prog = newval;
230 /* Set bank LSB 7 bits */
232 fluid_channel_set_bank_lsb(fluid_channel_t* chan, int banklsb)
234 int oldval, newval, style;
236 style = chan->synth->bank_select;
237 if (style == FLUID_BANK_STYLE_GM ||
238 style == FLUID_BANK_STYLE_GS)
239 return; /* ignored */
241 oldval = chan->sfont_bank_prog;
242 if (style == FLUID_BANK_STYLE_XG)
243 newval = (oldval & ~BANK_MASKVAL) | (banklsb << BANK_SHIFTVAL);
244 else /* style == FLUID_BANK_STYLE_MMA */
245 newval = (oldval & ~BANKLSB_MASKVAL) | (banklsb << BANK_SHIFTVAL);
246 chan->sfont_bank_prog = newval;
249 /* Set bank MSB 7 bits */
251 fluid_channel_set_bank_msb(fluid_channel_t* chan, int bankmsb)
253 int oldval, newval, style;
255 style = chan->synth->bank_select;
257 if (style == FLUID_BANK_STYLE_XG)
259 /* XG bank, do drum-channel auto-switch */
260 /* The number "120" was based on several keyboards having drums at 120 - 127,
261 reference: http://lists.nongnu.org/archive/html/fluid-dev/2011-02/msg00003.html */
262 chan->channel_type = (120 <= bankmsb) ? CHANNEL_TYPE_DRUM : CHANNEL_TYPE_MELODIC;
266 if (style == FLUID_BANK_STYLE_GM ||
267 chan->channel_type == CHANNEL_TYPE_DRUM)
268 return; /* ignored */
270 oldval = chan->sfont_bank_prog;
271 if (style == FLUID_BANK_STYLE_GS)
272 newval = (oldval & ~BANK_MASKVAL) | (bankmsb << BANK_SHIFTVAL);
273 else /* style == FLUID_BANK_STYLE_MMA */
274 newval = (oldval & ~BANKMSB_MASKVAL) | (bankmsb << (BANK_SHIFTVAL + 7));
275 chan->sfont_bank_prog = newval;
279 /* Get SoundFont ID, MIDI bank and/or program. Use NULL to ignore a value. */
281 fluid_channel_get_sfont_bank_prog(fluid_channel_t* chan, int *sfont,
282 int *bank, int *prog)
286 sfont_bank_prog = chan->sfont_bank_prog;
288 if (sfont) *sfont = (sfont_bank_prog & SFONT_MASKVAL) >> SFONT_SHIFTVAL;
289 if (bank) *bank = (sfont_bank_prog & BANK_MASKVAL) >> BANK_SHIFTVAL;
290 if (prog) *prog = (sfont_bank_prog & PROG_MASKVAL) >> PROG_SHIFTVAL;