c6eb723146f5c3218b99b7d08335f4ce8ad6f3d7
[ardour.git] / libs / fluidsynth / src / fluid_chan.c
1 /* FluidSynth - A Software Synthesizer
2  *
3  * Copyright (C) 2003  Peter Hanappe and others.
4  *
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.
9  *
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.
14  *
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
18  * 02110-1301, USA
19  */
20
21 #include "fluid_chan.h"
22 #include "fluid_mod.h"
23 #include "fluid_synth.h"
24 #include "fluid_sfont.h"
25
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
30
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
37
38
39 static void fluid_channel_init(fluid_channel_t* chan);
40
41
42 fluid_channel_t*
43 new_fluid_channel(fluid_synth_t* synth, int num)
44 {
45   fluid_channel_t* chan;
46
47   chan = FLUID_NEW(fluid_channel_t);
48   if (chan == NULL) {
49     FLUID_LOG(FLUID_ERR, "Out of memory");
50     return NULL;
51   }
52
53   chan->synth = synth;
54   chan->channum = num;
55   chan->preset = NULL;
56   chan->tuning = NULL;
57
58   fluid_channel_init(chan);
59   fluid_channel_init_ctrl(chan, 0);
60
61   return chan;
62 }
63
64 static void
65 fluid_channel_init(fluid_channel_t* chan)
66 {
67   fluid_preset_t *newpreset;
68   int prognum, banknum;
69
70   chan->sostenuto_orderid = 0;
71
72   chan->channel_type = (chan->channum == 9) ? CHANNEL_TYPE_DRUM : CHANNEL_TYPE_MELODIC;
73   prognum = 0;
74   banknum = (chan->channel_type == CHANNEL_TYPE_DRUM) ? DRUM_INST_BANK : 0;
75
76   chan->sfont_bank_prog = 0 << SFONT_SHIFTVAL | banknum << BANK_SHIFTVAL
77     | prognum << PROG_SHIFTVAL;
78
79   newpreset = fluid_synth_find_preset(chan->synth, banknum, prognum);
80   fluid_channel_set_preset(chan, newpreset);
81
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;
87
88   if (chan->tuning)
89   {
90     fluid_tuning_unref (chan->tuning, 1);
91     chan->tuning = NULL;
92   }
93 }
94
95 /*
96   @param is_all_ctrl_off if nonzero, only resets some controllers, according to 
97   http://www.midi.org/techspecs/rp15.php 
98 */
99 void
100 fluid_channel_init_ctrl(fluid_channel_t* chan, int is_all_ctrl_off)
101 {
102   int i;
103
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 */
107
108   for (i = 0; i < GEN_LAST; i++) {
109     chan->gen[i] = 0.0f;
110     chan->gen_abs[i] = 0;
111   }
112
113   if (is_all_ctrl_off) {
114     for (i = 0; i < ALL_SOUND_OFF; i++) {
115       if (i >= EFFECTS_DEPTH1 && i <= EFFECTS_DEPTH5) {
116         continue;
117       }
118       if (i >= SOUND_CTRL1 && i <= SOUND_CTRL10) {
119         continue;
120       }
121       if (i == BANK_SELECT_MSB || i == BANK_SELECT_LSB || i == VOLUME_MSB || 
122           i == VOLUME_LSB || i == PAN_MSB || i == PAN_LSB) {
123         continue;
124       }
125
126       fluid_channel_set_cc (chan, i, 0);
127     }
128   }
129   else {
130     for (i = 0; i < 128; i++) {
131       fluid_channel_set_cc (chan, i, 0);
132     }
133   }
134
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);
138
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);
142
143   /* Expression (MSB & LSB) */
144   fluid_channel_set_cc (chan, EXPRESSION_MSB, 127);
145   fluid_channel_set_cc (chan, EXPRESSION_LSB, 127);
146
147   if (!is_all_ctrl_off) {
148
149     chan->pitch_wheel_sensitivity = 2; /* two semi-tones */
150
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);
154     }
155
156     /* Volume / initial attenuation (MSB & LSB) */
157     fluid_channel_set_cc (chan, VOLUME_MSB, 100);
158     fluid_channel_set_cc (chan, VOLUME_LSB, 0);
159
160     /* Pan (MSB & LSB) */
161     fluid_channel_set_cc (chan, PAN_MSB, 64);
162     fluid_channel_set_cc (chan, PAN_LSB, 0);
163
164     /* Reverb */
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 */
169   }
170 }
171
172 /* Only called by delete_fluid_synth(), so no need to queue a preset free event */
173 int
174 delete_fluid_channel(fluid_channel_t* chan)
175 {
176   if (chan->preset) delete_fluid_preset (chan->preset);
177   FLUID_FREE(chan);
178   return FLUID_OK;
179 }
180
181 /* FIXME - Calls fluid_channel_init() potentially in synthesis context */
182 void
183 fluid_channel_reset(fluid_channel_t* chan)
184 {
185   fluid_channel_init(chan);
186   fluid_channel_init_ctrl(chan, 0);
187 }
188
189 /* Should only be called from synthesis context */
190 int
191 fluid_channel_set_preset(fluid_channel_t* chan, fluid_preset_t* preset)
192 {
193
194   fluid_preset_notify (chan->preset, FLUID_PRESET_UNSELECTED, chan->channum);
195
196   if (chan->preset) {
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 */
201   }
202   
203   chan->preset = preset;
204
205   fluid_preset_notify (preset, FLUID_PRESET_SELECTED, chan->channum);
206
207   return FLUID_OK;
208 }
209
210 /* Set SoundFont ID, MIDI bank and/or program.  Use -1 to use current value. */
211 void
212 fluid_channel_set_sfont_bank_prog(fluid_channel_t* chan, int sfontnum,
213                                   int banknum, int prognum)
214 {
215   int oldval, newval, oldmask;
216
217   newval = ((sfontnum != -1) ? sfontnum << SFONT_SHIFTVAL : 0)
218     | ((banknum != -1) ? banknum << BANK_SHIFTVAL : 0)
219     | ((prognum != -1) ? prognum << PROG_SHIFTVAL : 0);
220
221   oldmask = ((sfontnum != -1) ? 0 : SFONT_MASKVAL)
222     | ((banknum != -1) ? 0 : BANK_MASKVAL)
223     | ((prognum != -1) ? 0 : PROG_MASKVAL);
224
225   oldval = chan->sfont_bank_prog;
226   newval = (newval & ~oldmask) | (oldval & oldmask);
227   chan->sfont_bank_prog = newval;
228 }
229
230 /* Set bank LSB 7 bits */
231 void
232 fluid_channel_set_bank_lsb(fluid_channel_t* chan, int banklsb)
233 {
234   int oldval, newval, style;
235
236   style = chan->synth->bank_select;
237   if (style == FLUID_BANK_STYLE_GM ||
238       style == FLUID_BANK_STYLE_GS)
239       return; /* ignored */
240
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;
247 }
248
249 /* Set bank MSB 7 bits */
250 void
251 fluid_channel_set_bank_msb(fluid_channel_t* chan, int bankmsb)
252 {
253   int oldval, newval, style;
254
255   style = chan->synth->bank_select;
256
257   if (style == FLUID_BANK_STYLE_XG)
258   {
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;
263     return;
264   }
265
266   if (style == FLUID_BANK_STYLE_GM ||
267       chan->channel_type == CHANNEL_TYPE_DRUM)
268       return; /* ignored */
269
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;
276
277 }
278
279 /* Get SoundFont ID, MIDI bank and/or program.  Use NULL to ignore a value. */
280 void
281 fluid_channel_get_sfont_bank_prog(fluid_channel_t* chan, int *sfont,
282                                   int *bank, int *prog)
283 {
284   int sfont_bank_prog;
285
286   sfont_bank_prog = chan->sfont_bank_prog;
287
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;
291 }