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 Lesser General Public License
7  * as published by the Free Software Foundation; either version 2.1 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  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser 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 *
new_fluid_channel(fluid_synth_t * synth,int num)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 
49     if(chan == NULL)
50     {
51         FLUID_LOG(FLUID_ERR, "Out of memory");
52         return NULL;
53     }
54 
55     chan->synth = synth;
56     chan->channum = num;
57     chan->preset = NULL;
58     chan->tuning = NULL;
59 
60     fluid_channel_init(chan);
61     fluid_channel_init_ctrl(chan, 0);
62 
63     return chan;
64 }
65 
66 static void
fluid_channel_init(fluid_channel_t * chan)67 fluid_channel_init(fluid_channel_t *chan)
68 {
69     fluid_preset_t *newpreset;
70     int i, prognum, banknum;
71 
72     chan->sostenuto_orderid = 0;
73     /*--- Init poly/mono modes variables --------------------------------------*/
74     chan->mode = 0;
75     chan->mode_val = 0;
76 
77     /* monophonic list initialization */
78     for(i = 0; i < FLUID_CHANNEL_SIZE_MONOLIST; i++)
79     {
80         chan->monolist[i].next = i + 1;
81     }
82 
83     chan->monolist[FLUID_CHANNEL_SIZE_MONOLIST - 1].next = 0; /* ending element chained to the 1st */
84     chan->i_last = chan->n_notes = 0; /* clears the list */
85     chan->i_first = chan->monolist[chan->i_last].next; /* first note index in the list */
86     fluid_channel_clear_prev_note(chan); /* Mark previous note invalid */
87     /*---*/
88     chan->key_mono_sustained = INVALID_NOTE; /* No previous mono note sustained */
89     chan->legatomode = FLUID_CHANNEL_LEGATO_MODE_MULTI_RETRIGGER;		/* Default mode */
90     chan->portamentomode = FLUID_CHANNEL_PORTAMENTO_MODE_LEGATO_ONLY;	/* Default mode */
91     /*--- End of poly/mono initialization --------------------------------------*/
92 
93     chan->channel_type = (chan->channum == 9) ? CHANNEL_TYPE_DRUM : CHANNEL_TYPE_MELODIC;
94     prognum = 0;
95     banknum = (chan->channel_type == CHANNEL_TYPE_DRUM) ? DRUM_INST_BANK : 0;
96 
97     chan->sfont_bank_prog = 0 << SFONT_SHIFTVAL | banknum << BANK_SHIFTVAL
98                             | prognum << PROG_SHIFTVAL;
99 
100     newpreset = fluid_synth_find_preset(chan->synth, banknum, prognum);
101     fluid_channel_set_preset(chan, newpreset);
102 
103     chan->interp_method = FLUID_INTERP_DEFAULT;
104     chan->tuning_bank = 0;
105     chan->tuning_prog = 0;
106     chan->nrpn_select = 0;
107     chan->nrpn_active = 0;
108 
109     if(chan->tuning)
110     {
111         fluid_tuning_unref(chan->tuning, 1);
112         chan->tuning = NULL;
113     }
114 }
115 
116 /*
117   @param is_all_ctrl_off if nonzero, only resets some controllers, according to
118   http://www.midi.org/techspecs/rp15.php
119 */
120 void
fluid_channel_init_ctrl(fluid_channel_t * chan,int is_all_ctrl_off)121 fluid_channel_init_ctrl(fluid_channel_t *chan, int is_all_ctrl_off)
122 {
123     int i;
124 
125     chan->channel_pressure = 0;
126     chan->pitch_bend = 0x2000; /* Range is 0x4000, pitch bend wheel starts in centered position */
127 
128     for(i = 0; i < GEN_LAST; i++)
129     {
130         chan->gen[i] = 0.0f;
131     }
132 
133     if(is_all_ctrl_off)
134     {
135         for(i = 0; i < ALL_SOUND_OFF; i++)
136         {
137             if(i >= EFFECTS_DEPTH1 && i <= EFFECTS_DEPTH5)
138             {
139                 continue;
140             }
141 
142             if(i >= SOUND_CTRL1 && i <= SOUND_CTRL10)
143             {
144                 continue;
145             }
146 
147             if(i == BANK_SELECT_MSB || i == BANK_SELECT_LSB || i == VOLUME_MSB ||
148                     i == VOLUME_LSB || i == PAN_MSB || i == PAN_LSB ||
149                     i == BALANCE_MSB || i == BALANCE_LSB
150               )
151             {
152                 continue;
153             }
154 
155             fluid_channel_set_cc(chan, i, 0);
156         }
157     }
158     else
159     {
160         for(i = 0; i < 128; i++)
161         {
162             fluid_channel_set_cc(chan, i, 0);
163         }
164 
165         fluid_channel_clear_portamento(chan); /* Clear PTC receive */
166         chan->previous_cc_breath = 0;/* Reset previous breath */
167     }
168 
169     /* Reset polyphonic key pressure on all voices */
170     for(i = 0; i < 128; i++)
171     {
172         fluid_channel_set_key_pressure(chan, i, 0);
173     }
174 
175     /* Set RPN controllers to NULL state */
176     fluid_channel_set_cc(chan, RPN_LSB, 127);
177     fluid_channel_set_cc(chan, RPN_MSB, 127);
178 
179     /* Set NRPN controllers to NULL state */
180     fluid_channel_set_cc(chan, NRPN_LSB, 127);
181     fluid_channel_set_cc(chan, NRPN_MSB, 127);
182 
183     /* Expression (MSB & LSB) */
184     fluid_channel_set_cc(chan, EXPRESSION_MSB, 127);
185     fluid_channel_set_cc(chan, EXPRESSION_LSB, 127);
186 
187     if(!is_all_ctrl_off)
188     {
189 
190         chan->pitch_wheel_sensitivity = 2; /* two semi-tones */
191 
192         /* Just like panning, a value of 64 indicates no change for sound ctrls */
193         for(i = SOUND_CTRL1; i <= SOUND_CTRL10; i++)
194         {
195             fluid_channel_set_cc(chan, i, 64);
196         }
197 
198         /* Volume / initial attenuation (MSB & LSB) */
199         fluid_channel_set_cc(chan, VOLUME_MSB, 100);
200         fluid_channel_set_cc(chan, VOLUME_LSB, 0);
201 
202         /* Pan (MSB & LSB) */
203         fluid_channel_set_cc(chan, PAN_MSB, 64);
204         fluid_channel_set_cc(chan, PAN_LSB, 0);
205 
206         /* Balance (MSB & LSB) */
207         fluid_channel_set_cc(chan, BALANCE_MSB, 64);
208         fluid_channel_set_cc(chan, BALANCE_LSB, 0);
209 
210         /* Reverb */
211         /* fluid_channel_set_cc (chan, EFFECTS_DEPTH1, 40); */
212         /* Note: although XG standard specifies the default amount of reverb to
213            be 40, most people preferred having it at zero.
214            See http://lists.gnu.org/archive/html/fluid-dev/2009-07/msg00016.html */
215     }
216 }
217 
218 /* Only called by delete_fluid_synth(), so no need to queue a preset free event */
219 void
delete_fluid_channel(fluid_channel_t * chan)220 delete_fluid_channel(fluid_channel_t *chan)
221 {
222     fluid_return_if_fail(chan != NULL);
223 
224     FLUID_FREE(chan);
225 }
226 
227 /* FIXME - Calls fluid_channel_init() potentially in synthesis context */
228 void
fluid_channel_reset(fluid_channel_t * chan)229 fluid_channel_reset(fluid_channel_t *chan)
230 {
231     fluid_channel_init(chan);
232     fluid_channel_init_ctrl(chan, 0);
233 }
234 
235 /* Should only be called from synthesis context */
236 int
fluid_channel_set_preset(fluid_channel_t * chan,fluid_preset_t * preset)237 fluid_channel_set_preset(fluid_channel_t *chan, fluid_preset_t *preset)
238 {
239     fluid_sfont_t *sfont;
240 
241     if(chan->preset == preset)
242     {
243         return FLUID_OK;
244     }
245 
246     if(chan->preset)
247     {
248         sfont = chan->preset->sfont;
249         sfont->refcount--;
250     }
251 
252     fluid_preset_notify(chan->preset, FLUID_PRESET_UNSELECTED, chan->channum);
253 
254     chan->preset = preset;
255 
256     if(preset)
257     {
258         sfont = preset->sfont;
259         sfont->refcount++;
260     }
261 
262     fluid_preset_notify(preset, FLUID_PRESET_SELECTED, chan->channum);
263 
264     return FLUID_OK;
265 }
266 
267 /* Set SoundFont ID, MIDI bank and/or program.  Use -1 to use current value. */
268 void
fluid_channel_set_sfont_bank_prog(fluid_channel_t * chan,int sfontnum,int banknum,int prognum)269 fluid_channel_set_sfont_bank_prog(fluid_channel_t *chan, int sfontnum,
270                                   int banknum, int prognum)
271 {
272     int oldval, newval, oldmask;
273 
274     newval = ((sfontnum != -1) ? sfontnum << SFONT_SHIFTVAL : 0)
275              | ((banknum != -1) ? banknum << BANK_SHIFTVAL : 0)
276              | ((prognum != -1) ? prognum << PROG_SHIFTVAL : 0);
277 
278     oldmask = ((sfontnum != -1) ? 0 : SFONT_MASKVAL)
279               | ((banknum != -1) ? 0 : BANK_MASKVAL)
280               | ((prognum != -1) ? 0 : PROG_MASKVAL);
281 
282     oldval = chan->sfont_bank_prog;
283     newval = (newval & ~oldmask) | (oldval & oldmask);
284     chan->sfont_bank_prog = newval;
285 }
286 
287 /* Set bank LSB 7 bits */
288 void
fluid_channel_set_bank_lsb(fluid_channel_t * chan,int banklsb)289 fluid_channel_set_bank_lsb(fluid_channel_t *chan, int banklsb)
290 {
291     int oldval, newval, style;
292 
293     style = chan->synth->bank_select;
294 
295     if(style == FLUID_BANK_STYLE_GM ||
296             style == FLUID_BANK_STYLE_GS)
297     {
298         return;    /* ignored */
299     }
300 
301     oldval = chan->sfont_bank_prog;
302 
303     if(style == FLUID_BANK_STYLE_XG)
304     {
305         newval = (oldval & ~BANK_MASKVAL) | (banklsb << BANK_SHIFTVAL);
306     }
307     else /* style == FLUID_BANK_STYLE_MMA */
308     {
309         newval = (oldval & ~BANKLSB_MASKVAL) | (banklsb << BANK_SHIFTVAL);
310     }
311 
312     chan->sfont_bank_prog = newval;
313 }
314 
315 /* Set bank MSB 7 bits */
316 void
fluid_channel_set_bank_msb(fluid_channel_t * chan,int bankmsb)317 fluid_channel_set_bank_msb(fluid_channel_t *chan, int bankmsb)
318 {
319     int oldval, newval, style;
320 
321     style = chan->synth->bank_select;
322 
323     if(style == FLUID_BANK_STYLE_XG)
324     {
325         /* XG bank, do drum-channel auto-switch */
326         /* The number "120" was based on several keyboards having drums at 120 - 127,
327            reference: http://lists.nongnu.org/archive/html/fluid-dev/2011-02/msg00003.html */
328         chan->channel_type = (120 <= bankmsb) ? CHANNEL_TYPE_DRUM : CHANNEL_TYPE_MELODIC;
329         return;
330     }
331 
332     if(style == FLUID_BANK_STYLE_GM ||
333             chan->channel_type == CHANNEL_TYPE_DRUM)
334     {
335         return;    /* ignored */
336     }
337 
338     oldval = chan->sfont_bank_prog;
339 
340     if(style == FLUID_BANK_STYLE_GS)
341     {
342         newval = (oldval & ~BANK_MASKVAL) | (bankmsb << BANK_SHIFTVAL);
343     }
344     else /* style == FLUID_BANK_STYLE_MMA */
345     {
346         newval = (oldval & ~BANKMSB_MASKVAL) | (bankmsb << (BANK_SHIFTVAL + 7));
347     }
348 
349     chan->sfont_bank_prog = newval;
350 
351 }
352 
353 /* Get SoundFont ID, MIDI bank and/or program.  Use NULL to ignore a value. */
354 void
fluid_channel_get_sfont_bank_prog(fluid_channel_t * chan,int * sfont,int * bank,int * prog)355 fluid_channel_get_sfont_bank_prog(fluid_channel_t *chan, int *sfont,
356                                   int *bank, int *prog)
357 {
358     int sfont_bank_prog;
359 
360     sfont_bank_prog = chan->sfont_bank_prog;
361 
362     if(sfont)
363     {
364         *sfont = (sfont_bank_prog & SFONT_MASKVAL) >> SFONT_SHIFTVAL;
365     }
366 
367     if(bank)
368     {
369         *bank = (sfont_bank_prog & BANK_MASKVAL) >> BANK_SHIFTVAL;
370     }
371 
372     if(prog)
373     {
374         *prog = (sfont_bank_prog & PROG_MASKVAL) >> PROG_SHIFTVAL;
375     }
376 }
377 
378 /**
379  * Updates legato/ staccato playing state
380  * The function is called:
381  * - on noteon before adding a note into the monolist.
382  * - on noteoff after removing a note out of the monolist.
383  * @param chan  fluid_channel_t.
384 */
385 static void
fluid_channel_update_legato_staccato_state(fluid_channel_t * chan)386 fluid_channel_update_legato_staccato_state(fluid_channel_t *chan)
387 {
388     /* Updates legato/ staccato playing state */
389     if(chan->n_notes)
390     {
391         chan->mode |= FLUID_CHANNEL_LEGATO_PLAYING; /* Legato state */
392     }
393     else
394     {
395         chan->mode &= ~ FLUID_CHANNEL_LEGATO_PLAYING; /* Staccato state */
396     }
397 }
398 
399 /**
400  * Adds a note into the monophonic list. The function is part of the legato
401  * detector. fluid_channel_add_monolist() is intended to be called by
402  * fluid_synth_noteon_mono_LOCAL().
403  *
404  * When a note is added at noteOn each element is use in the forward direction
405  * and indexed by i_last variable.
406  *
407  * @param chan  fluid_channel_t.
408  * @param key MIDI note number (0-127).
409  * @param vel MIDI velocity (0-127, 0=noteoff).
410  * @param onenote. When 1 the function adds the note but the monophonic list
411  *                 keeps only one note (used on noteOn poly).
412  * Note: i_last index keeps a trace of the most recent note added.
413  *       prev_note keeps a trace of the note prior i_last note.
414  *       FLUID_CHANNEL_LEGATO_PLAYING bit keeps trace of legato/staccato playing state.
415  *
416  * More informations in FluidPolyMono-0004.pdf chapter 4 (Appendices).
417 */
418 void
fluid_channel_add_monolist(fluid_channel_t * chan,unsigned char key,unsigned char vel,unsigned char onenote)419 fluid_channel_add_monolist(fluid_channel_t *chan, unsigned char key,
420                            unsigned char vel, unsigned char onenote)
421 {
422     unsigned char i_last = chan->i_last;
423     /* Updates legato/ staccato playing state */
424     fluid_channel_update_legato_staccato_state(chan);
425 
426     if(chan->n_notes)
427     {
428         /* keeps trace of the note prior last note */
429         chan->prev_note = chan->monolist[i_last].note;
430     }
431 
432     /* moves i_last forward before writing new note */
433     i_last = chan->monolist[i_last].next;
434     chan->i_last = i_last; 			/* now ilast indexes the last note */
435     chan->monolist[i_last].note = key; /* we save note and velocity */
436     chan->monolist[i_last].vel = vel;
437 
438     if(onenote)
439     {
440         /* clears monolist before one note addition */
441         chan->i_first = i_last;
442         chan->n_notes = 0;
443     }
444 
445     if(chan->n_notes < FLUID_CHANNEL_SIZE_MONOLIST)
446     {
447         chan->n_notes++; /* updates n_notes */
448     }
449     else
450     {
451         /* The end of buffer is reach. So circular motion for i_first */
452         /* i_first index is moved forward */
453         chan->i_first = chan->monolist[i_last].next;
454     }
455 }
456 
457 /**
458  * Searching a note in the monophonic list. The function is part of the legato
459  * detector. fluid_channel_search_monolist() is intended to be called by
460  * fluid_synth_noteoff_mono_LOCAL().
461  *
462  * The search starts from the first note in the list indexed by i_first
463 
464  * @param chan  fluid_channel_t.
465  * @param key MIDI note number (0-127) to search.
466  * @param i_prev pointer on returned index of the note prior the note to search.
467  * @return index of the note if find, FLUID_FAILED otherwise.
468  *
469  */
470 int
fluid_channel_search_monolist(fluid_channel_t * chan,unsigned char key,int * i_prev)471 fluid_channel_search_monolist(fluid_channel_t *chan, unsigned char key, int *i_prev)
472 {
473     short n = chan->n_notes; /* number of notes in monophonic list */
474     short j, i = chan->i_first; /* searching starts from i_first included */
475 
476     for(j = 0 ; j < n ; j++)
477     {
478         if(chan->monolist[i].note == key)
479         {
480             if(i == chan->i_first)
481             {
482                 /* tracking index of the previous note (i_prev) */
483                 for(j = chan->i_last ; n < FLUID_CHANNEL_SIZE_MONOLIST; n++)
484                 {
485                     j = chan->monolist[j].next;
486                 }
487 
488                 * i_prev = j; /* returns index of the previous note */
489             }
490 
491             return i; /* returns index of the note to search */
492         }
493 
494         * i_prev = i; /* tracking index of the previous note (i_prev) */
495         i = chan->monolist[i].next; /* next element */
496     }
497 
498     return FLUID_FAILED; /* not found */
499 }
500 
501 /**
502  * removes a note from the monophonic list. The function is part of
503  * the legato detector.
504  * fluid_channel_remove_monolist() is intended to be called by
505  * fluid_synth_noteoff_mono_LOCAL().
506  *
507  * When a note is removed at noteOff the element concerned is fast unlinked
508  * and relinked after the i_last element.
509  *
510  * @param chan  fluid_channel_t.
511  * @param
512  *   i, index of the note to remove. If i is invalid or the list is
513  *      empty, the function do nothing and returns FLUID_FAILED.
514  * @param
515  *   On input, i_prev is a pointer on index of the note previous i.
516  *   On output i_prev is a pointer on index of the note previous i if i is the last note
517  *   in the list,FLUID_FAILED otherwise. When the returned index is valid it means
518  *   a legato detection on noteoff.
519  *
520  * Note: the following variables in Channel keeps trace of the situation.
521  *       - i_last index keeps a trace of the most recent note played even if
522  *       the list is empty.
523  *       - prev_note keeps a trace of the note removed if it is i_last.
524  *       - FLUID_CHANNEL_LEGATO_PLAYING bit keeps a trace of legato/staccato playing state.
525  *
526  * More informations in FluidPolyMono-0004.pdf chapter 4 (Appendices).
527  */
528 void
fluid_channel_remove_monolist(fluid_channel_t * chan,int i,int * i_prev)529 fluid_channel_remove_monolist(fluid_channel_t *chan, int i, int *i_prev)
530 {
531     unsigned char i_last = chan->i_last;
532 
533     /* checks if index is valid */
534     if(i < 0 || i >= FLUID_CHANNEL_SIZE_MONOLIST || !chan->n_notes)
535     {
536         * i_prev =  FLUID_FAILED;
537     }
538 
539     /* The element is about to be removed and inserted between i_last and next */
540     /* Note: when i is egal to i_last or egal to i_first, removing/inserting
541        isn't necessary */
542     if(i == i_last)
543     {
544         /* Removing/Inserting isn't necessary */
545         /* keeps trace of the note prior last note */
546         chan->prev_note = chan->monolist[i_last].note;
547         /* moves i_last backward to the previous  */
548         chan->i_last = *i_prev; /* i_last index is moved backward */
549     }
550     else
551     {
552         /* i is before i_last */
553         if(i == chan->i_first)
554         {
555             /* Removing/inserting isn't necessary */
556             /* i_first index is moved forward to the next element*/
557             chan->i_first = chan->monolist[i].next;
558         }
559         else
560         {
561             /* i is between i_first and i_last */
562             /* Unlinks element i and inserts after i_last */
563             chan->monolist[* i_prev].next = chan->monolist[i].next; /* unlinks i */
564             /*inserts i after i_last */
565             chan->monolist[i].next = chan->monolist[i_last].next;
566             chan->monolist[i_last].next = i;
567         }
568 
569         * i_prev =  FLUID_FAILED;
570     }
571 
572     chan->n_notes--; /* updates the number of note in the list */
573     /* Updates legato/ staccato playing state */
574     fluid_channel_update_legato_staccato_state(chan);
575 }
576 
577 /**
578  * On noteOff on a polyphonic channel,the monophonic list is fully flushed.
579  *
580  * @param chan  fluid_channel_t.
581  * Note: i_last index keeps a trace of the most recent note played even if
582  *       the list is empty.
583  *       prev_note keeps a trace of the note i_last .
584  *       FLUID_CHANNEL_LEGATO_PLAYING bit keeps a trace of legato/staccato playing.
585  */
fluid_channel_clear_monolist(fluid_channel_t * chan)586 void fluid_channel_clear_monolist(fluid_channel_t *chan)
587 {
588     /* keeps trace off the most recent note played */
589     chan->prev_note = chan->monolist[chan->i_last].note;
590 
591     /* flushes the monolist */
592     chan->i_first = chan->monolist[chan->i_last].next;
593     chan->n_notes = 0;
594     /* Update legato/ sataccato playing state */
595     chan->mode &= ~ FLUID_CHANNEL_LEGATO_PLAYING; /* Staccato state */
596 }
597 
598 /**
599  * On noteOn on a polyphonic channel,adds the note into the monophonic list
600  * keeping only this note.
601  * @param
602  *   chan  fluid_channel_t.
603  *   key, vel, note and velocity added in the monolist
604  * Note: i_last index keeps a trace of the most recent note inserted.
605  *       prev_note keeps a trace of the note prior i_last note.
606  *       FLUID_CHANNEL_LEGATO_PLAYING bit keeps trace of legato/staccato playing.
607  */
fluid_channel_set_onenote_monolist(fluid_channel_t * chan,unsigned char key,unsigned char vel)608 void fluid_channel_set_onenote_monolist(fluid_channel_t *chan, unsigned char key,
609                                         unsigned char vel)
610 {
611     fluid_channel_add_monolist(chan, key, vel, 1);
612 }
613 
614 /**
615  * The function changes the state (Valid/Invalid) of the previous note played in
616  * a staccato manner (fluid_channel_prev_note()).
617  * When potamento mode 'each note' or 'staccato only' is selected, on next
618  * noteOn a portamento will be started from the most recent note played
619  * staccato.
620  * It will be possible that it isn't appropriate. To give the musician the
621  * possibility to choose a portamento from this note , prev_note will be forced
622  * to invalid state on noteOff if portamento pedal is Off.
623  *
624  * The function is intended to be called when the following event occurs:
625  * - On noteOff (in poly or mono mode), to mark prev_note invalid.
626  * - On Portamento Off(in poly or mono mode), to mark prev_note invalid.
627  * @param chan  fluid_channel_t.
628  */
fluid_channel_invalid_prev_note_staccato(fluid_channel_t * chan)629 void fluid_channel_invalid_prev_note_staccato(fluid_channel_t *chan)
630 {
631     /* checks if the playing is staccato */
632     if(!(chan->mode & FLUID_CHANNEL_LEGATO_PLAYING))
633     {
634 
635         /* checks if portamento pedal is off */
636         if(! fluid_channel_portamento(chan))
637         {
638             /* forces prev_note invalid */
639             fluid_channel_clear_prev_note(chan);
640         }
641     }
642 
643     /* else prev_note still remains valid for next fromkey portamento */
644 }
645 
646 /**
647  * The function handles poly/mono commutation on legato pedal On/Off.
648  * @param chan  fluid_channel_t.
649  * @param value, value of the CC legato.
650  */
fluid_channel_cc_legato(fluid_channel_t * chan,int value)651 void fluid_channel_cc_legato(fluid_channel_t *chan, int value)
652 {
653     /* Special handling of the monophonic list  */
654     if(!(chan->mode & FLUID_CHANNEL_POLY_OFF) && chan->n_notes)  /* The monophonic list have notes */
655     {
656         if(value < 64)   /* legato is released */
657         {
658             /* returns from monophonic to polyphonic with notes in the monophonic list */
659 
660             /* The monophonic list is flushed keeping last note only
661                Note: i_last index keeps a trace of the most recent note played.
662                prev_note keeps a trace of the note i_last.
663                FLUID_CHANNEL_LEGATO_PLAYING bit keeps trace of legato/staccato playing.
664             */
665             chan->i_first = chan->i_last;
666             chan->n_notes = 1;
667         }
668         else /* legato is depressed */
669         {
670             /* Inters in monophonic from polyphonic with note in monophonic list */
671             /* Stops the running note to remain coherent with Breath Sync mode */
672             if((chan->mode &  FLUID_CHANNEL_BREATH_SYNC) && !fluid_channel_breath_msb(chan))
673             {
674                 fluid_synth_noteoff_monopoly(chan->synth, chan->channum,
675                                              fluid_channel_last_note(chan), 1);
676             }
677         }
678     }
679 }
680 
681 /**
682  * The function handles CC Breath On/Off detection. When a channel is in
683  * Breath Sync mode and in monophonic playing, the breath controller allows
684  * to trigger noteon/noteoff note when the musician starts to breath (noteon) and
685  * stops to breath (noteoff).
686  * @param chan  fluid_channel_t.
687  * @param value, value of the CC Breath..
688  */
fluid_channel_cc_breath_note_on_off(fluid_channel_t * chan,int value)689 void fluid_channel_cc_breath_note_on_off(fluid_channel_t *chan, int value)
690 {
691     if((chan->mode &  FLUID_CHANNEL_BREATH_SYNC)  && fluid_channel_is_playing_mono(chan) &&
692             (chan->n_notes))
693     {
694         /* The monophonic list isn't empty */
695         if((value > 0) && (chan->previous_cc_breath == 0))
696         {
697             /* CC Breath On detection */
698             fluid_synth_noteon_mono_staccato(chan->synth, chan->channum,
699                                              fluid_channel_last_note(chan),
700                                              fluid_channel_last_vel(chan));
701         }
702         else if((value == 0) && (chan->previous_cc_breath > 0))
703         {
704             /* CC Breath Off detection */
705             fluid_synth_noteoff_monopoly(chan->synth, chan->channum,
706                                          fluid_channel_last_note(chan), 1);
707         }
708     }
709 
710     chan->previous_cc_breath = value;
711 }
712