Path: blob/master/libs/fluidsynth/src/synth/fluid_synth_monopoly.c
4396 views
/* FluidSynth - A Software Synthesizer1*2* Copyright (C) 2003 Peter Hanappe and others.3*4* This library is free software; you can redistribute it and/or5* modify it under the terms of the GNU Lesser General Public License6* as published by the Free Software Foundation; either version 2.1 of7* the License, or (at your option) any later version.8*9* This library is distributed in the hope that it will be useful, but10* WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12* Lesser General Public License for more details.13*14* You should have received a copy of the GNU Lesser General Public15* License along with this library; if not, write to the Free16* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA17* 02110-1301, USA18*/1920#include "fluid_synth.h"21#include "fluid_chan.h"22#include "fluid_defsfont.h"232425/******************************************************************************26The legato detector is composed as this,27variables:28- monolist: monophonic list variable.29- prev_note: to store the most recent note before adding on noteon or before30removing on noteoff.31- FLUID_CHANNEL_LEGATO_PLAYING: legato/staccato state bit that informs on32legato or staccato playing.33functions:34- fluid_channel_add_monolist(), for inserting a new note.35- fluid_channel_search_monolist(), for searching the position of a note36into the list.37- fluid_channel_remove_monolist(), for removing a note from the list.3839The monophonic list40+------------------------------------------------+41| +----+ +----+ +----+ +----+ |42| |note| |note| |note| |note| |43+--->|vel |-->|vel |-->....-->|vel |-->|vel |----+44+----+ +----+ +----+ +----+45/|\ /|\46| |47i_first i_last4849The list allows an easy automatic detection of a legato passage when it is50played on a MIDI keyboard input device.51It is useful also when the input device is an ewi (electronic wind instrument)52or evi (electronic valve instrument) and these instruments are unable to send53MIDI CC legato on/off.5455The list memorizes the notes in playing order.56- (a) On noteOn n2, if a previous note n1 exists, there is a legato57detection with n1 (with or without portamento from n1 to n2 See note below).58- (b) On noteOff of the running note n2, if a previous note n1 exists,59there is a legato detection from n2 to n1, allowing fast trills playing60(with or without portamento from n2 to n1. See note below).6162Notes in the list are inserted to the end of the list that works like a63circular buffer.The features are:64651) It is always possible to play an infinite legato passage in66direct order (n1_On,n2_On,n3_On,....).67682) Playing legato in the reverse order (n10_Off, n9_Off,,...) helps in69fast trills playing as the list memorizes 10 most recent notes.70713) Playing an infinite lagato passage in ascendant or descendant order,72without playing trills is always possible using the usual way like this:73First we begin with an ascendant passage,74n1On, (n2On,n1Off), (n3On,n2Off) , (n4On,n3Off), then75we continue with a descendant passage76(n3On,n4off), (n2On,n3off), (n1On,n2off), n1Off...and so on7778Each MIDI channel have a legato detector.7980Note:81Portamento is a feature independent of the legato detector. So82portamento isn't part of the lagato detector. However portamento83(when enabled) is triggered at noteOn (like legato). Like in legato84situation it is usual to have a portamento from a note 'fromkey' to another85note 'tokey'. Portamento fromkey note choice is determined at noteOn by86fluid_synth_get_fromkey_portamento_legato() (see below).8788More information in FluidPolyMono-0004.pdf chapter 4 (Appendices).89******************************************************************************/909192/*****************************************************************************93Portamento related functions in Poly or Mono mode94******************************************************************************/9596/**97* fluid_synth_get_fromkey_portamento_legato returns two information:98* - fromkey note for portamento.99* - fromkey note for legato.100* +-----> fromkey_portamento101* ______|________102* portamento modes >------->| |103* | get_fromkey |104* Porta.on/off >------------------------->|_______________|105* (PTC) |106* +-----> fromkey_legato107*108* The functions is intended to be call on noteOn mono109* see fluid_synth_noteon_mono_staccato(), fluid_synth_noteon_monopoly_legato()110* -------111* 1)The function determines if a portamento must occur on next noteOn.112* The value returned is 'fromkey portamento' which is the pitchstart key113* of a portamento, as function of PTC or (default_fromkey, prev_note) both114* if Portamento On. By order of precedence the result is:115* 1.1) PTC have precedence over Portamento On.116* If CC PTC has been received, its value supersedes and any117* portamento pedal On, default_fromkey,prev_note or portamento mode.118* 1.2) Otherwise ,when Portamento On the function takes the following value:119* - default_fromkey if valid120* - otherwise prev_note(prev_note is the note prior the most recent121* note played).122* Then portamento mode is applied to validate the value chosen.123* Where portamento mode is:124* - each note, a portamento occurs on each note.125* - legato only, portamento only on notes played legato.126* - staccato only, portamento only on notes played staccato.127* 1.3) Otherwise, portamento is off,INVALID_NOTE is returned (portamento is disabled).128* ------129* 2)The function determines if a legato playing must occur on next noteOn.130* 'fromkey legato note' is returned as a function of default_fromkey, PTC,131* current mono/poly mode,actual 'staccato/legato' playing state and prev_note.132* By order of precedence the result is:133* 2.1) If valid, default_fromkey have precedence over any others values.134* 2.2) Otherwise if CC PTC has been received its value is returned.135* 2.3) Otherwise fromkey legato is determined from the mono/poly mode,136* the actual 'staccato/legato' playing state (FLUID_CHANNEL_LEGATO_PLAYING) and prev_note137* as this:138* - in (poly/Mono) staccato , INVALID_NOTE is returned.139* - in poly legato , actually we don't want playing legato. So140* INVALID_NOTE is returned.141* - in mono legato , prev_note is returned.142*143* On input144* @param chan fluid_channel_t.145* @param defaultFromkey, the default 'fromkey portamento' note or 'fromkey legato'146* note (see description above).147*148* @return149* 1)'fromkey portamento' is returned in fluid_synth_t.fromkey_portamento.150* If valid,it means that portamento is enabled .151*152* 2)The 'fromkey legato' note is returned.153*154* Notes about usage:155* The function is intended to be called when the following event occurs:156* - On noteOn (Poly or Mono) after insertion in the monophonic list.157* - On noteOff(mono legato playing). In this case, default_fromkey must be valid.158*159* Typical calling usage:160* - In poly, default_fromkey must be INVALID_NOTE.161* - In mono staccato playing,default_fromkey must be INVALID_NOTE.162* - In mono legato playing,default_fromkey must be valid.163*/164static char fluid_synth_get_fromkey_portamento_legato(fluid_channel_t *chan,165int default_fromkey)166{167unsigned char ptc = fluid_channel_get_cc(chan, PORTAMENTO_CTRL);168169if(fluid_channel_is_valid_note(ptc))170{171/* CC PTC has been received */172fluid_channel_clear_portamento(chan); /* clears the CC PTC receive */173chan->synth->fromkey_portamento = ptc;/* returns fromkey portamento */174175/* returns fromkey legato */176if(!fluid_channel_is_valid_note(default_fromkey))177{178default_fromkey = ptc;179}180}181else182{183/* determines and returns fromkey portamento */184unsigned char fromkey_portamento = INVALID_NOTE;185186if(fluid_channel_portamento(chan))187{188/* Portamento when Portamento pedal is On */189/* 'fromkey portamento'is determined from the portamento mode190and the most recent note played (prev_note)*/191enum fluid_channel_portamento_mode portamentomode = chan->portamentomode;192193if(fluid_channel_is_valid_note(default_fromkey))194{195fromkey_portamento = default_fromkey; /* on each note */196}197else198{199fromkey_portamento = fluid_channel_prev_note(chan); /* on each note */200}201202if(portamentomode == FLUID_CHANNEL_PORTAMENTO_MODE_LEGATO_ONLY)203{204/* Mode portamento:legato only */205if(!(chan->mode & FLUID_CHANNEL_LEGATO_PLAYING))206{207fromkey_portamento = INVALID_NOTE;208}209}210else if(portamentomode == FLUID_CHANNEL_PORTAMENTO_MODE_STACCATO_ONLY)211{212/* Mode portamento:staccato only */213if(chan->mode & FLUID_CHANNEL_LEGATO_PLAYING)214{215fromkey_portamento = INVALID_NOTE;216}217}218219/* else Mode portamento: on each note (staccato/legato) */220}221222/* Returns fromkey portamento */223chan->synth->fromkey_portamento = fromkey_portamento;224225/* Determines and returns fromkey legato */226if(!fluid_channel_is_valid_note(default_fromkey))227{228/* in staccato (poly/Mono) returns INVALID_NOTE */229/* In mono mode legato playing returns the note prior most230recent note played */231if(fluid_channel_is_playing_mono(chan) && (chan->mode & FLUID_CHANNEL_LEGATO_PLAYING))232{233default_fromkey = fluid_channel_prev_note(chan); /* note prior last note */234}235236/* In poly mode legato playing, actually we don't want playing legato.237So returns INVALID_NOTE */238}239}240241return default_fromkey; /* Returns legato fromkey */242}243244/*****************************************************************************245noteon - noteoff functions in Mono mode246******************************************************************************/247/*248* noteon - noteoff on a channel in "monophonic playing".249*250* A channel needs to be played monophonic if this channel has been set in251* monophonic mode by basic channel API.(see fluid_synth_polymono.c).252* A channel needs also to be played monophonic if it has been set in253* polyphonic mode and legato pedal is On during the playing.254* When a channel is in "monophonic playing" state, only one note at a time can be255* played in a staccato or legato manner (with or without portamento).256* More information in FluidPolyMono-0004.pdf chapter 4 (Appendices).257* _______________258* ________________ | noteon |259* | legato detector| O-->| mono_staccato |--*-> preset_noteon260* noteon_mono ->| (add_monolist) |--O-- |_______________| | (with or without)261* LOCAL |________________| O /|\ | (portamento)262* /|\ set_onenote | | fromkey |263* | | | portamento|264* noteOn poly >---*------------------* | |265* | | |266* | _____ |________ |267* portamento modes >--- | ->| | |268* | | get_fromkey | |269* Porta.on/off >--------------------- | ->|_______________| |270* (PTC) | | |271* | fromkey | fromkey |272* | legato | portamento|273* | _____\|/_______ |274* *-->| noteon |--/275* | | monopoly |276* | | legato |----> voices277* legato modes >------- | ->|_______________| triggering278* | (with or without)279* | (portamento)280* |281* |282* noteOff poly >---*----------------- | ---------+283* | clear | |284* _\|/_____________ | |285* | legato detector | O |286* noteoff_mono->|(search_monolist)|-O-- _____\|/_______287* LOCAL |(remove_monolist)| O-->| noteoff |288* |_________________| | monopoly |----> noteoff289* Sust.on/off >------------------------->|_______________|290* Sost.on/off291------------------------------------------------------------------------------*/292293/**294* Plays a noteon event for a Synth instance in "monophonic playing" state.295* Please see the description above about "monophonic playing".296* _______________297* ________________ | noteon |298* | legato detector| O-->| mono_staccato |--->preset_noteon299* noteon_mono ->| (add_monolist) |--O-- |_______________|300* LOCAL |________________| O301* |302* |303* |304* |305* |306* |307* |308* |309* |310* | _______________311* | | noteon |312* +-->| monopoly |313* | legato |---> voices314* |_______________| triggering315*316* The function uses the legato detector (see above) to determine if the note must317* be played staccato or legato.318*319* @param synth instance.320* @param chan MIDI channel number (0 to MIDI channel count - 1).321* @param key MIDI note number (0-127).322* @param vel MIDI velocity (0-127).323* @return FLUID_OK on success, FLUID_FAILED otherwise.324*/325int fluid_synth_noteon_mono_LOCAL(fluid_synth_t *synth, int chan,326int key, int vel)327{328fluid_channel_t *channel = synth->channel[chan];329330/* Adds the note into the monophonic list */331fluid_channel_add_monolist(channel, key, vel, 0);332333/* in Breath Sync mode, the noteon triggering is postponed334until the musician starts blowing in the breath controller */335if(!(channel->mode & FLUID_CHANNEL_BREATH_SYNC) ||336fluid_channel_breath_msb(channel))337{338/* legato/staccato playing detection */339if(channel->mode & FLUID_CHANNEL_LEGATO_PLAYING)340{341/* legato playing */342/* legato from prev_note to key */343/* the voices from prev_note key number are to be used to play key number */344/* fromkey must be valid */345return fluid_synth_noteon_monopoly_legato(synth, chan,346fluid_channel_prev_note(channel), key, vel);347}348else349{350/* staccato playing */351return fluid_synth_noteon_mono_staccato(synth, chan, key, vel);352}353}354else355{356return FLUID_OK;357}358}359360/**361* Plays a noteoff event for a Synth instance in "monophonic playing" state.362* Please see the description above about "monophonic playing"363*364* _______________365* | noteon |366* +-->| monopoly |367* | | legato |----> voices368* | |_______________| triggering369* | (with or without)370* | (portamento)371* |372* |373* |374* |375* |376* |377* _________________ |378* | legato detector | O379* noteoff_mono->|(search_monolist)|-O-- _______________380* LOCAL |(remove_monolist)| O-->| noteoff |381* |_________________| | monopoly |----> noteoff382* |_______________|383*384* The function uses the legato detector (see above) to determine if the noteoff must385* be played staccato or legato.386*387* @param synth instance.388* @param chan MIDI channel number (0 to MIDI channel count - 1).389* @param key MIDI note number (0-127).390* @return FLUID_OK on success, FLUID_FAILED otherwise.391*/392int fluid_synth_noteoff_mono_LOCAL(fluid_synth_t *synth, int chan, int key)393{394int status;395int i, i_prev;396fluid_channel_t *channel = synth->channel[chan];397/* searching the note in the monophonic list */398i = fluid_channel_search_monolist(channel, key, &i_prev);399400if(i >= 0)401{402/* the note is in the monophonic list */403/* Removes the note from the monophonic list */404fluid_channel_remove_monolist(channel, i, &i_prev);405406/* in Breath Sync mode, the noteoff triggering is done407if the musician is blowing in the breath controller */408if(!(channel->mode & FLUID_CHANNEL_BREATH_SYNC) ||409fluid_channel_breath_msb(channel))410{411/* legato playing detection */412if(channel->mode & FLUID_CHANNEL_LEGATO_PLAYING)413{414/* the list contains others notes */415if(i_prev >= 0)416{417/* legato playing detection on noteoff */418/* legato from key to i_prev key */419/* the voices from key number are to be used to420play i_prev key number. */421status = fluid_synth_noteon_monopoly_legato(synth, chan,422key, channel->monolist[i_prev].note,423channel->monolist[i_prev].vel);424}425/* else the note doesn't need to be played off */426else427{428status = FLUID_OK;429}430}431else432{433/* the monophonic list is empty */434/* plays the monophonic note noteoff and eventually held435by sustain/sostenuto */436status = fluid_synth_noteoff_monopoly(synth, chan, key, 1);437}438}439else440{441status = FLUID_OK;442}443}444else445{446/* the note is not found in the list so the note was447played On when the channel was in polyphonic playing */448/* plays the noteoff as for polyphonic */449status = fluid_synth_noteoff_monopoly(synth, chan, key, 0);450}451452return status;453}454455/*----------------------------------------------------------------------------456staccato playing457-----------------------------------------------------------------------------*/458/**459* Plays noteon for a monophonic note in staccato manner.460* Please see the description above about "monophonic playing".461* _______________462* | noteon |463* noteon_mono >------------------------>| mono_staccato |----> preset_noteon464* |_______________| (with or without)465* LOCAL /|\ (portamento)466* | fromkey467* | portamento468* |469* |470* ______|________471* portamento modes >----->| |472* | get_fromkey |473* Porta.on/off >----------------------->|_______________|474* Portamento475* (PTC)476*477* We are in staccato situation (where no previous note have been depressed).478* Before the note been passed to fluid_preset_noteon(), the function must determine479* the from_key_portamento parameter used by fluid_preset_noteon().480*481* from_key_portamento is returned by fluid_synth_get_fromkey_portamento_legato() function.482* fromkey_portamento is set to valid/invalid key value depending of the portamento483* modes (see portamento mode API) , CC portamento On/Off , and CC portamento control484* (PTC).485*486* @param synth instance.487* @param chan MIDI channel number (0 to MIDI channel count - 1).488* @param key MIDI note number (0-127).489* @param vel MIDI velocity (0-127).490* @return FLUID_OK on success, FLUID_FAILED otherwise.491*/492int493fluid_synth_noteon_mono_staccato(fluid_synth_t *synth, int chan, int key, int vel)494{495fluid_channel_t *channel = synth->channel[chan];496497/* Before playing a new note, if a previous monophonic note is currently498sustained it needs to be released */499fluid_synth_release_voice_on_same_note_LOCAL(synth, chan, channel->key_mono_sustained);500/* Get possible 'fromkey portamento' */501fluid_synth_get_fromkey_portamento_legato(channel, INVALID_NOTE);502/* The note needs to be played by voices allocation */503return fluid_preset_noteon(channel->preset, synth, chan, key, vel);504}505506/**507* Plays noteoff for a polyphonic or monophonic note508* Please see the description above about "monophonic playing".509*510*511* noteOff poly >---------------------------------+512* |513* |514* |515* noteoff_mono _____\|/_______516* LOCAL >------------------------->| noteoff |517* | monopoly |----> noteoff518* Sust.on/off >------------------------->|_______________|519* Sost.on/off520*521* The function has the same behaviour when the noteoff is poly of mono, except522* that for mono noteoff, if any pedal (sustain or sostenuto ) is depressed, the523* key is memorized. This is necessary when the next mono note will be played524* staccato, as any current mono note currently sustained will need to be released525* (see fluid_synth_noteon_mono_staccato()).526* Note also that for a monophonic legato passage, the function is called only when527* the last noteoff of the passage occurs. That means that if sustain or sostenuto528* is depressed, only the last note of a legato passage will be sustained.529*530* @param synth instance.531* @param chan MIDI channel number (0 to MIDI channel count - 1).532* @param key MIDI note number (0-127).533* @param Mono, 1 noteoff on monophonic note.534* 0 noteoff on polyphonic note.535* @return FLUID_OK on success, FLUID_FAILED otherwise.536*537* Note: On return, on monophonic, possible sustained note is memorized in538* key_mono_sustained. Memorization is done here on noteOff.539*/540int fluid_synth_noteoff_monopoly(fluid_synth_t *synth, int chan, int key,541char Mono)542{543int status = FLUID_FAILED;544fluid_voice_t *voice;545int i;546fluid_channel_t *channel = synth->channel[chan];547548/* Key_sustained is prepared to return no note sustained (INVALID_NOTE) */549if(Mono)550{551channel->key_mono_sustained = INVALID_NOTE; /* no mono note sustained */552}553554/* noteoff for all voices with same chan and same key */555for(i = 0; i < synth->polyphony; i++)556{557voice = synth->voice[i];558559if(fluid_voice_is_on(voice) &&560fluid_voice_get_channel(voice) == chan &&561fluid_voice_get_key(voice) == key)562{563if(synth->verbose)564{565int used_voices = 0;566int k;567568for(k = 0; k < synth->polyphony; k++)569{570if(!_AVAILABLE(synth->voice[k]))571{572used_voices++;573}574}575576FLUID_LOG(FLUID_INFO, "noteoff\t%d\t%d\t%d\t%05d\t%.3f\t%d",577fluid_voice_get_channel(voice), fluid_voice_get_key(voice), 0,578fluid_voice_get_id(voice),579(fluid_curtime() - synth->start) / 1000.0f,580used_voices);581} /* if verbose */582583fluid_voice_noteoff(voice);584585/* noteoff on monophonic note */586/* Key memorization if the note is sustained */587if(Mono &&588(fluid_voice_is_sustained(voice) || fluid_voice_is_sostenuto(voice)))589{590channel->key_mono_sustained = key;591}592593status = FLUID_OK;594} /* if voice on */595} /* for all voices */596597return status;598}599600/*----------------------------------------------------------------------------601legato playing602-----------------------------------------------------------------------------*/603/**604* Plays noteon for a monophonic note played legato.605* Please see the description above about "monophonic playing".606*607*608* _______________609* portamento modes >----->| |610* | get_fromkey |611* Porta.on/off >----------------------->|_______________|612* Portamento |613* (PTC) | +-->preset_noteon614* fromkey | fromkey | (with or without)615* legato | portamento| (portamento)616* _____\|/_______ |617* | noteon |--+618* noteon_mono >------------------------>| monopoly |619* LOCAL | legato |----->voices620* |_______________| triggering621* /|\ (with or without)622* | (portamento)623* legato modes >-----------------+624*625* We are in legato situation (where a previous note has been depressed).626* The function must determine the from_key_portamento and from_key_legato parameters627* used respectively by fluid_preset_noteon() function and voices triggering functions.628*629* from_key_portamento and from_key_legato are returned by630* fluid_synth_get_fromkey_portamento_legato() function.631* fromkey_portamento is set to valid/invalid key value depending of the portamento632* modes (see portamento mode API), CC portamento On/Off, and CC portamento control633* (PTC).634* Then, depending of the legato modes (see legato mode API), the function will call635* the appropriate triggering functions.636* @param synth instance.637* @param chan MIDI channel number (0 to MIDI channel count - 1).638* @param fromkey MIDI note number (0-127).639* @param tokey MIDI note number (0-127).640* @param vel MIDI velocity (0-127).641* @return FLUID_OK on success, FLUID_FAILED otherwise.642*643* Note: The voices with key 'fromkey' are to be used to play key 'tokey'.644* The function is able to play legato through Preset Zone(s) (PZ) and645* Instrument Zone(s) (IZ) as far as possible.646* When key tokey is outside the current Instrument Zone, Preset Zone,647* current 'fromkey' voices are released. If necessary new voices648* are restarted when tokey enters inside new Instrument(s) Zones,Preset Zone(s).649* More information in FluidPolyMono-0004.pdf chapter 4.7 (Appendices).650*/651int fluid_synth_noteon_monopoly_legato(fluid_synth_t *synth, int chan,652int fromkey, int tokey, int vel)653{654fluid_channel_t *channel = synth->channel[chan];655enum fluid_channel_legato_mode legatomode = channel->legatomode;656fluid_voice_t *voice;657int i ;658/* Gets possible 'fromkey portamento' and possible 'fromkey legato' note */659fromkey = fluid_synth_get_fromkey_portamento_legato(channel, fromkey);660661if(fluid_channel_is_valid_note(fromkey))662{663for(i = 0; i < synth->polyphony; i++)664{665/* searching fromkey voices: only those who don't have 'note off' */666voice = synth->voice[i];667668if(fluid_voice_is_on(voice) &&669fluid_voice_get_channel(voice) == chan &&670fluid_voice_get_key(voice) == fromkey)671{672fluid_zone_range_t *zone_range = voice->zone_range;673674/* Ignores voice when there is no instrument zone (i.e no zone_range). Otherwise675checks if tokey is inside the range of the running voice */676if(zone_range && fluid_zone_inside_range(zone_range, tokey, vel))677{678switch(legatomode)679{680case FLUID_CHANNEL_LEGATO_MODE_RETRIGGER: /* mode 0 */681fluid_voice_release(voice); /* normal release */682break;683684case FLUID_CHANNEL_LEGATO_MODE_MULTI_RETRIGGER: /* mode 1 */685/* Skip in attack section */686fluid_voice_update_multi_retrigger_attack(voice, tokey, vel);687688/* Starts portamento if enabled */689if(fluid_channel_is_valid_note(synth->fromkey_portamento))690{691/* Sends portamento parameters to the voice dsp */692fluid_voice_update_portamento(voice,693synth->fromkey_portamento,694tokey);695}696697/* The voice is now used to play tokey in legato manner */698/* Marks this Instrument Zone to be ignored during next699fluid_preset_noteon() */700zone_range->ignore = TRUE;701break;702703default: /* Invalid mode: this should never happen */704FLUID_LOG(FLUID_WARN, "Failed to execute legato mode: %d",705legatomode);706return FLUID_FAILED;707}708}709else710{711/* tokey note is outside the voice range, so the voice is released */712fluid_voice_release(voice);713}714}715}716}717718/* May be,tokey will enter in new others Insrument Zone(s),Preset Zone(s), in719this case it needs to be played by voices allocation */720return fluid_preset_noteon(channel->preset, synth, chan, tokey, vel);721}722723724