|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2022 - Raw Material Software Limited
-
- JUCE is an open source library subject to commercial or open-source
- licensing.
-
- The code included in this file is provided under the terms of the ISC license
- http://www.isc.org/downloads/software-support-policy/isc-license. Permission
- To use, copy, modify, and/or distribute this software for any purpose with or
- without fee is hereby granted provided that the above copyright notice and
- this permission notice appear in all copies.
-
- JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
- EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
- DISCLAIMED.
-
- ==============================================================================
- */
-
- namespace juce
- {
-
- namespace
- {
- const uint8 noLSBValueReceived = 0xff;
- const Range<int> allChannels { 1, 17 };
-
- template <typename Range, typename Value>
- void mpeInstrumentFill (Range& range, const Value& value)
- {
- std::fill (std::begin (range), std::end (range), value);
- }
- }
-
- //==============================================================================
- MPEInstrument::MPEInstrument() noexcept
- {
- mpeInstrumentFill (lastPressureLowerBitReceivedOnChannel, noLSBValueReceived);
- mpeInstrumentFill (lastTimbreLowerBitReceivedOnChannel, noLSBValueReceived);
- mpeInstrumentFill (isMemberChannelSustained, false);
-
- pitchbendDimension.value = &MPENote::pitchbend;
- pressureDimension.value = &MPENote::pressure;
- timbreDimension.value = &MPENote::timbre;
-
- resetLastReceivedValues();
-
- legacyMode.channelRange = allChannels;
- }
-
- MPEInstrument::MPEInstrument (MPEZoneLayout layout)
- : MPEInstrument()
- {
- setZoneLayout (layout);
- }
-
- MPEInstrument::~MPEInstrument() = default;
-
- //==============================================================================
- MPEZoneLayout MPEInstrument::getZoneLayout() const noexcept
- {
- return zoneLayout;
- }
-
- void MPEInstrument::resetLastReceivedValues()
- {
- struct Defaults
- {
- MPEDimension& dimension;
- MPEValue defaultValue;
- };
-
- // The default value for pressure is 0, for all other dimensions it is centre
- for (const auto& pair : { Defaults { pressureDimension, MPEValue::minValue() },
- Defaults { pitchbendDimension, MPEValue::centreValue() },
- Defaults { timbreDimension, MPEValue::centreValue() } })
- {
- mpeInstrumentFill (pair.dimension.lastValueReceivedOnChannel, pair.defaultValue);
- }
- }
-
- void MPEInstrument::setZoneLayout (MPEZoneLayout newLayout)
- {
- releaseAllNotes();
-
- const ScopedLock sl (lock);
- legacyMode.isEnabled = false;
-
- if (zoneLayout != newLayout)
- {
- zoneLayout = newLayout;
- listeners.call ([=] (Listener& l) { l.zoneLayoutChanged(); });
- }
- }
-
- //==============================================================================
- void MPEInstrument::enableLegacyMode (int pitchbendRange, Range<int> channelRange)
- {
- if (legacyMode.isEnabled)
- return;
-
- releaseAllNotes();
-
- const ScopedLock sl (lock);
-
- legacyMode.isEnabled = true;
- legacyMode.pitchbendRange = pitchbendRange;
- legacyMode.channelRange = channelRange;
-
- zoneLayout.clearAllZones();
- listeners.call ([=] (Listener& l) { l.zoneLayoutChanged(); });
- }
-
- bool MPEInstrument::isLegacyModeEnabled() const noexcept
- {
- return legacyMode.isEnabled;
- }
-
- Range<int> MPEInstrument::getLegacyModeChannelRange() const noexcept
- {
- return legacyMode.channelRange;
- }
-
- void MPEInstrument::setLegacyModeChannelRange (Range<int> channelRange)
- {
- jassert (allChannels.contains (channelRange));
-
- releaseAllNotes();
- const ScopedLock sl (lock);
-
- if (legacyMode.channelRange != channelRange)
- {
- legacyMode.channelRange = channelRange;
- listeners.call ([=] (Listener& l) { l.zoneLayoutChanged(); });
- }
- }
-
- int MPEInstrument::getLegacyModePitchbendRange() const noexcept
- {
- return legacyMode.pitchbendRange;
- }
-
- void MPEInstrument::setLegacyModePitchbendRange (int pitchbendRange)
- {
- jassert (pitchbendRange >= 0 && pitchbendRange <= 96);
-
- releaseAllNotes();
- const ScopedLock sl (lock);
-
- if (legacyMode.pitchbendRange != pitchbendRange)
- {
- legacyMode.pitchbendRange = pitchbendRange;
- listeners.call ([=] (Listener& l) { l.zoneLayoutChanged(); });
- }
- }
-
- //==============================================================================
- void MPEInstrument::setPressureTrackingMode (TrackingMode modeToUse)
- {
- pressureDimension.trackingMode = modeToUse;
- }
-
- void MPEInstrument::setPitchbendTrackingMode (TrackingMode modeToUse)
- {
- pitchbendDimension.trackingMode = modeToUse;
- }
-
- void MPEInstrument::setTimbreTrackingMode (TrackingMode modeToUse)
- {
- timbreDimension.trackingMode = modeToUse;
- }
-
- //==============================================================================
- void MPEInstrument::addListener (Listener* listenerToAdd)
- {
- listeners.add (listenerToAdd);
- }
-
- void MPEInstrument::removeListener (Listener* listenerToRemove)
- {
- listeners.remove (listenerToRemove);
- }
-
- //==============================================================================
- void MPEInstrument::processNextMidiEvent (const MidiMessage& message)
- {
- zoneLayout.processNextMidiEvent (message);
-
- if (message.isNoteOn (true)) processMidiNoteOnMessage (message);
- else if (message.isNoteOff (false)) processMidiNoteOffMessage (message);
- else if (message.isResetAllControllers()
- || message.isAllNotesOff()) processMidiResetAllControllersMessage (message);
- else if (message.isPitchWheel()) processMidiPitchWheelMessage (message);
- else if (message.isChannelPressure()) processMidiChannelPressureMessage (message);
- else if (message.isController()) processMidiControllerMessage (message);
- else if (message.isAftertouch()) processMidiAfterTouchMessage (message);
- }
-
- //==============================================================================
- void MPEInstrument::processMidiNoteOnMessage (const MidiMessage& message)
- {
- // Note: If a note-on with velocity = 0 is used to convey a note-off,
- // then the actual note-off velocity is not known. In this case,
- // the MPE convention is to use note-off velocity = 64.
-
- if (message.getVelocity() == 0)
- {
- noteOff (message.getChannel(),
- message.getNoteNumber(),
- MPEValue::from7BitInt (64));
- }
- else
- {
- noteOn (message.getChannel(),
- message.getNoteNumber(),
- MPEValue::from7BitInt (message.getVelocity()));
- }
- }
-
- //==============================================================================
- void MPEInstrument::processMidiNoteOffMessage (const MidiMessage& message)
- {
- noteOff (message.getChannel(),
- message.getNoteNumber(),
- MPEValue::from7BitInt (message.getVelocity()));
- }
-
- //==============================================================================
- void MPEInstrument::processMidiPitchWheelMessage (const MidiMessage& message)
- {
- pitchbend (message.getChannel(),
- MPEValue::from14BitInt (message.getPitchWheelValue()));
- }
-
- //==============================================================================
- void MPEInstrument::processMidiChannelPressureMessage (const MidiMessage& message)
- {
- pressure (message.getChannel(),
- MPEValue::from7BitInt (message.getChannelPressureValue()));
- }
-
- //==============================================================================
- void MPEInstrument::processMidiControllerMessage (const MidiMessage& message)
- {
- switch (message.getControllerNumber())
- {
- case 64: sustainPedal (message.getChannel(), message.isSustainPedalOn()); break;
- case 66: sostenutoPedal (message.getChannel(), message.isSostenutoPedalOn()); break;
- case 70: handlePressureMSB (message.getChannel(), message.getControllerValue()); break;
- case 74: handleTimbreMSB (message.getChannel(), message.getControllerValue()); break;
- case 102: handlePressureLSB (message.getChannel(), message.getControllerValue()); break;
- case 106: handleTimbreLSB (message.getChannel(), message.getControllerValue()); break;
- default: break;
- }
- }
-
- //==============================================================================
- void MPEInstrument::processMidiResetAllControllersMessage (const MidiMessage& message)
- {
- // in MPE mode, "reset all controllers" is per-zone and expected on the master channel;
- // in legacy mode, it is per MIDI channel (within the channel range used).
-
- if (legacyMode.isEnabled && legacyMode.channelRange.contains (message.getChannel()))
- {
- for (int i = notes.size(); --i >= 0;)
- {
- auto& note = notes.getReference (i);
-
- if (note.midiChannel == message.getChannel())
- {
- note.keyState = MPENote::off;
- note.noteOffVelocity = MPEValue::from7BitInt (64); // some reasonable number
- listeners.call ([&] (Listener& l) { l.noteReleased (note); });
- notes.remove (i);
- }
- }
- }
- else if (isMasterChannel (message.getChannel()))
- {
- auto zone = (message.getChannel() == 1 ? zoneLayout.getLowerZone()
- : zoneLayout.getUpperZone());
-
- for (int i = notes.size(); --i >= 0;)
- {
- auto& note = notes.getReference (i);
-
- if (zone.isUsing (note.midiChannel))
- {
- note.keyState = MPENote::off;
- note.noteOffVelocity = MPEValue::from7BitInt (64); // some reasonable number
- listeners.call ([&] (Listener& l) { l.noteReleased (note); });
- notes.remove (i);
- }
- }
- }
- }
-
- void MPEInstrument::processMidiAfterTouchMessage (const MidiMessage& message)
- {
- if (! isMasterChannel (message.getChannel()))
- return;
-
- polyAftertouch (message.getChannel(), message.getNoteNumber(),
- MPEValue::from7BitInt (message.getAfterTouchValue()));
- }
-
- //==============================================================================
- void MPEInstrument::handlePressureMSB (int midiChannel, int value) noexcept
- {
- auto lsb = lastPressureLowerBitReceivedOnChannel[midiChannel - 1];
-
- pressure (midiChannel, lsb == noLSBValueReceived ? MPEValue::from7BitInt (value)
- : MPEValue::from14BitInt (lsb + (value << 7)));
- }
-
- void MPEInstrument::handlePressureLSB (int midiChannel, int value) noexcept
- {
- lastPressureLowerBitReceivedOnChannel[midiChannel - 1] = uint8 (value);
- }
-
- void MPEInstrument::handleTimbreMSB (int midiChannel, int value) noexcept
- {
- auto lsb = lastTimbreLowerBitReceivedOnChannel[midiChannel - 1];
-
- timbre (midiChannel, lsb == noLSBValueReceived ? MPEValue::from7BitInt (value)
- : MPEValue::from14BitInt (lsb + (value << 7)));
- }
-
- void MPEInstrument::handleTimbreLSB (int midiChannel, int value) noexcept
- {
- lastTimbreLowerBitReceivedOnChannel[midiChannel - 1] = uint8 (value);
- }
-
- //==============================================================================
- void MPEInstrument::noteOn (int midiChannel,
- int midiNoteNumber,
- MPEValue midiNoteOnVelocity)
- {
- if (! isUsingChannel (midiChannel))
- return;
-
- MPENote newNote (midiChannel,
- midiNoteNumber,
- midiNoteOnVelocity,
- getInitialValueForNewNote (midiChannel, pitchbendDimension),
- getInitialValueForNewNote (midiChannel, pressureDimension),
- getInitialValueForNewNote (midiChannel, timbreDimension),
- isMemberChannelSustained[midiChannel - 1] ? MPENote::keyDownAndSustained : MPENote::keyDown);
-
- const ScopedLock sl (lock);
- updateNoteTotalPitchbend (newNote);
-
- if (auto* alreadyPlayingNote = getNotePtr (midiChannel, midiNoteNumber))
- {
- // pathological case: second note-on received for same note -> retrigger it
- alreadyPlayingNote->keyState = MPENote::off;
- alreadyPlayingNote->noteOffVelocity = MPEValue::from7BitInt (64); // some reasonable number
- listeners.call ([=] (Listener& l) { l.noteReleased (*alreadyPlayingNote); });
- notes.remove (alreadyPlayingNote);
- }
-
- notes.add (newNote);
- listeners.call ([&] (Listener& l) { l.noteAdded (newNote); });
- }
-
- //==============================================================================
- void MPEInstrument::noteOff (int midiChannel,
- int midiNoteNumber,
- MPEValue midiNoteOffVelocity)
- {
- const ScopedLock sl (lock);
-
- if (notes.isEmpty() || ! isUsingChannel (midiChannel))
- return;
-
- if (auto* note = getNotePtr (midiChannel, midiNoteNumber))
- {
- note->keyState = (note->keyState == MPENote::keyDownAndSustained) ? MPENote::sustained : MPENote::off;
- note->noteOffVelocity = midiNoteOffVelocity;
-
- // If no more notes are playing on this channel in mpe mode, reset the dimension values
- if (! legacyMode.isEnabled && getLastNotePlayedPtr (midiChannel) == nullptr)
- {
- pressureDimension.lastValueReceivedOnChannel[midiChannel - 1] = MPEValue::minValue();
- pitchbendDimension.lastValueReceivedOnChannel[midiChannel - 1] = MPEValue::centreValue();
- timbreDimension.lastValueReceivedOnChannel[midiChannel - 1] = MPEValue::centreValue();
- }
-
- if (note->keyState == MPENote::off)
- {
- listeners.call ([=] (Listener& l) { l.noteReleased (*note); });
- notes.remove (note);
- }
- else
- {
- listeners.call ([=] (Listener& l) { l.noteKeyStateChanged (*note); });
- }
- }
- }
-
- //==============================================================================
- void MPEInstrument::pitchbend (int midiChannel, MPEValue value)
- {
- const ScopedLock sl (lock);
- updateDimension (midiChannel, pitchbendDimension, value);
- }
-
- void MPEInstrument::pressure (int midiChannel, MPEValue value)
- {
- const ScopedLock sl (lock);
- updateDimension (midiChannel, pressureDimension, value);
- }
-
- void MPEInstrument::timbre (int midiChannel, MPEValue value)
- {
- const ScopedLock sl (lock);
- updateDimension (midiChannel, timbreDimension, value);
- }
-
- void MPEInstrument::polyAftertouch (int midiChannel, int midiNoteNumber, MPEValue value)
- {
- const ScopedLock sl (lock);
-
- for (int i = notes.size(); --i >= 0;)
- {
- auto& note = notes.getReference (i);
-
- if (note.midiChannel == midiChannel
- && note.initialNote == midiNoteNumber
- && pressureDimension.getValue (note) != value)
- {
- pressureDimension.getValue (note) = value;
- callListenersDimensionChanged (note, pressureDimension);
- }
- }
- }
-
- MPEValue MPEInstrument::getInitialValueForNewNote (int midiChannel, MPEDimension& dimension) const
- {
- if (! legacyMode.isEnabled && getLastNotePlayedPtr (midiChannel) != nullptr)
- return &dimension == &pressureDimension ? MPEValue::minValue() : MPEValue::centreValue();
-
- return dimension.lastValueReceivedOnChannel[midiChannel - 1];
- }
-
- //==============================================================================
- void MPEInstrument::updateDimension (int midiChannel, MPEDimension& dimension, MPEValue value)
- {
- dimension.lastValueReceivedOnChannel[midiChannel - 1] = value;
-
- if (notes.isEmpty())
- return;
-
- if (isMemberChannel (midiChannel))
- {
- if (dimension.trackingMode == allNotesOnChannel)
- {
- for (int i = notes.size(); --i >= 0;)
- {
- auto& note = notes.getReference (i);
-
- if (note.midiChannel == midiChannel)
- updateDimensionForNote (note, dimension, value);
- }
- }
- else
- {
- if (auto* note = getNotePtr (midiChannel, dimension.trackingMode))
- updateDimensionForNote (*note, dimension, value);
- }
- }
- else if (isMasterChannel (midiChannel))
- {
- updateDimensionMaster (midiChannel == 1, dimension, value);
- }
- }
-
- //==============================================================================
- void MPEInstrument::updateDimensionMaster (bool isLowerZone, MPEDimension& dimension, MPEValue value)
- {
- auto zone = (isLowerZone ? zoneLayout.getLowerZone()
- : zoneLayout.getUpperZone());
-
- if (! zone.isActive())
- return;
-
- for (int i = notes.size(); --i >= 0;)
- {
- auto& note = notes.getReference (i);
-
- if (! zone.isUsing (note.midiChannel))
- continue;
-
- if (&dimension == &pitchbendDimension)
- {
- // master pitchbend is a special case: we don't change the note's own pitchbend,
- // instead we have to update its total (master + note) pitchbend.
- updateNoteTotalPitchbend (note);
- listeners.call ([&] (Listener& l) { l.notePitchbendChanged (note); });
- }
- else if (dimension.getValue (note) != value)
- {
- dimension.getValue (note) = value;
- callListenersDimensionChanged (note, dimension);
- }
- }
- }
-
- //==============================================================================
- void MPEInstrument::updateDimensionForNote (MPENote& note, MPEDimension& dimension, MPEValue value)
- {
- if (dimension.getValue (note) != value)
- {
- dimension.getValue (note) = value;
-
- if (&dimension == &pitchbendDimension)
- updateNoteTotalPitchbend (note);
-
- callListenersDimensionChanged (note, dimension);
- }
- }
-
- //==============================================================================
- void MPEInstrument::callListenersDimensionChanged (const MPENote& note, const MPEDimension& dimension)
- {
- if (&dimension == &pressureDimension) { listeners.call ([&] (Listener& l) { l.notePressureChanged (note); }); return; }
- if (&dimension == &timbreDimension) { listeners.call ([&] (Listener& l) { l.noteTimbreChanged (note); }); return; }
- if (&dimension == &pitchbendDimension) { listeners.call ([&] (Listener& l) { l.notePitchbendChanged (note); }); return; }
- }
-
- //==============================================================================
- void MPEInstrument::updateNoteTotalPitchbend (MPENote& note)
- {
- if (legacyMode.isEnabled)
- {
- note.totalPitchbendInSemitones = note.pitchbend.asSignedFloat() * (float) legacyMode.pitchbendRange;
- }
- else
- {
- auto zone = zoneLayout.getLowerZone();
-
- if (! zone.isActive() || ! zone.isUsing (note.midiChannel))
- {
- auto upperZone = zoneLayout.getUpperZone();
-
- if (upperZone.isActive() && upperZone.isUsing (note.midiChannel))
- {
- zone = upperZone;
- }
- else
- {
- // this note doesn't belong to any zone!
- jassertfalse;
- return;
- }
- }
-
- auto notePitchbendInSemitones = 0.0f;
-
- if (zone.isUsingChannelAsMemberChannel (note.midiChannel))
- notePitchbendInSemitones = note.pitchbend.asSignedFloat() * (float) zone.perNotePitchbendRange;
-
- auto masterPitchbendInSemitones = pitchbendDimension.lastValueReceivedOnChannel[zone.getMasterChannel() - 1]
- .asSignedFloat()
- * (float) zone.masterPitchbendRange;
-
- note.totalPitchbendInSemitones = notePitchbendInSemitones + masterPitchbendInSemitones;
- }
- }
-
- //==============================================================================
- void MPEInstrument::sustainPedal (int midiChannel, bool isDown)
- {
- const ScopedLock sl (lock);
- handleSustainOrSostenuto (midiChannel, isDown, false);
- }
-
- void MPEInstrument::sostenutoPedal (int midiChannel, bool isDown)
- {
- const ScopedLock sl (lock);
- handleSustainOrSostenuto (midiChannel, isDown, true);
- }
-
- //==============================================================================
- void MPEInstrument::handleSustainOrSostenuto (int midiChannel, bool isDown, bool isSostenuto)
- {
- // in MPE mode, sustain/sostenuto is per-zone and expected on the master channel;
- // in legacy mode, sustain/sostenuto is per MIDI channel (within the channel range used).
-
- if (legacyMode.isEnabled ? (! legacyMode.channelRange.contains (midiChannel)) : (! isMasterChannel (midiChannel)))
- return;
-
- auto zone = (midiChannel == 1 ? zoneLayout.getLowerZone()
- : zoneLayout.getUpperZone());
-
- for (int i = notes.size(); --i >= 0;)
- {
- auto& note = notes.getReference (i);
-
- if (legacyMode.isEnabled ? (note.midiChannel == midiChannel) : zone.isUsing (note.midiChannel))
- {
- if (note.keyState == MPENote::keyDown && isDown)
- note.keyState = MPENote::keyDownAndSustained;
- else if (note.keyState == MPENote::sustained && ! isDown)
- note.keyState = MPENote::off;
- else if (note.keyState == MPENote::keyDownAndSustained && ! isDown)
- note.keyState = MPENote::keyDown;
-
- if (note.keyState == MPENote::off)
- {
- listeners.call ([&] (Listener& l) { l.noteReleased (note); });
- notes.remove (i);
- }
- else
- {
- listeners.call ([&] (Listener& l) { l.noteKeyStateChanged (note); });
- }
- }
- }
-
- if (! isSostenuto)
- {
- isMemberChannelSustained[midiChannel - 1] = isDown;
-
- if (! legacyMode.isEnabled)
- {
- if (zone.isLowerZone())
- {
- for (int i = zone.getFirstMemberChannel(); i <= zone.getLastMemberChannel(); ++i)
- isMemberChannelSustained[i - 1] = isDown;
- }
- else
- {
- for (int i = zone.getFirstMemberChannel(); i >= zone.getLastMemberChannel(); --i)
- isMemberChannelSustained[i - 1] = isDown;
- }
- }
- }
- }
-
- //==============================================================================
- bool MPEInstrument::isMemberChannel (int midiChannel) const noexcept
- {
- if (legacyMode.isEnabled)
- return legacyMode.channelRange.contains (midiChannel);
-
- return zoneLayout.getLowerZone().isUsingChannelAsMemberChannel (midiChannel)
- || zoneLayout.getUpperZone().isUsingChannelAsMemberChannel (midiChannel);
- }
-
- bool MPEInstrument::isMasterChannel (int midiChannel) const noexcept
- {
- if (legacyMode.isEnabled)
- return false;
-
- const auto lowerZone = zoneLayout.getLowerZone();
- const auto upperZone = zoneLayout.getUpperZone();
-
- return (lowerZone.isActive() && midiChannel == lowerZone.getMasterChannel())
- || (upperZone.isActive() && midiChannel == upperZone.getMasterChannel());
- }
-
- bool MPEInstrument::isUsingChannel (int midiChannel) const noexcept
- {
- if (legacyMode.isEnabled)
- return legacyMode.channelRange.contains (midiChannel);
-
- return zoneLayout.getLowerZone().isUsing (midiChannel)
- || zoneLayout.getUpperZone().isUsing (midiChannel);
- }
-
- //==============================================================================
- int MPEInstrument::getNumPlayingNotes() const noexcept
- {
- return notes.size();
- }
-
- MPENote MPEInstrument::getNote (int midiChannel, int midiNoteNumber) const noexcept
- {
- if (auto* note = getNotePtr (midiChannel, midiNoteNumber))
- return *note;
-
- return {};
- }
-
- MPENote MPEInstrument::getNote (int index) const noexcept
- {
- return notes[index];
- }
-
- MPENote MPEInstrument::getNoteWithID (uint16 noteID) const noexcept
- {
- const ScopedLock sl (lock);
-
- for (auto& note : notes)
- if (note.noteID == noteID)
- return note;
-
- return {};
- }
-
- //==============================================================================
- MPENote MPEInstrument::getMostRecentNote (int midiChannel) const noexcept
- {
- if (auto* note = getLastNotePlayedPtr (midiChannel))
- return *note;
-
- return {};
- }
-
- MPENote MPEInstrument::getMostRecentNoteOtherThan (MPENote otherThanThisNote) const noexcept
- {
- for (auto i = notes.size(); --i >= 0;)
- {
- auto& note = notes.getReference (i);
-
- if (note != otherThanThisNote)
- return note;
- }
-
- return {};
- }
-
- //==============================================================================
- const MPENote* MPEInstrument::getNotePtr (int midiChannel, int midiNoteNumber) const noexcept
- {
- for (int i = 0; i < notes.size(); ++i)
- {
- auto& note = notes.getReference (i);
-
- if (note.midiChannel == midiChannel && note.initialNote == midiNoteNumber)
- return ¬e;
- }
-
- return nullptr;
- }
-
- MPENote* MPEInstrument::getNotePtr (int midiChannel, int midiNoteNumber) noexcept
- {
- return const_cast<MPENote*> (static_cast<const MPEInstrument&> (*this).getNotePtr (midiChannel, midiNoteNumber));
- }
-
- //==============================================================================
- const MPENote* MPEInstrument::getNotePtr (int midiChannel, TrackingMode mode) const noexcept
- {
- // for the "all notes" tracking mode, this method can never possibly
- // work because it returns 0 or 1 note but there might be more than one!
- jassert (mode != allNotesOnChannel);
-
- if (mode == lastNotePlayedOnChannel) return getLastNotePlayedPtr (midiChannel);
- if (mode == lowestNoteOnChannel) return getLowestNotePtr (midiChannel);
- if (mode == highestNoteOnChannel) return getHighestNotePtr (midiChannel);
-
- return nullptr;
- }
-
- MPENote* MPEInstrument::getNotePtr (int midiChannel, TrackingMode mode) noexcept
- {
- return const_cast<MPENote*> (static_cast<const MPEInstrument&> (*this).getNotePtr (midiChannel, mode));
- }
-
- //==============================================================================
- const MPENote* MPEInstrument::getLastNotePlayedPtr (int midiChannel) const noexcept
- {
- const ScopedLock sl (lock);
-
- for (auto i = notes.size(); --i >= 0;)
- {
- auto& note = notes.getReference (i);
-
- if (note.midiChannel == midiChannel
- && (note.keyState == MPENote::keyDown || note.keyState == MPENote::keyDownAndSustained))
- return ¬e;
- }
-
- return nullptr;
- }
-
- MPENote* MPEInstrument::getLastNotePlayedPtr (int midiChannel) noexcept
- {
- return const_cast<MPENote*> (static_cast<const MPEInstrument&> (*this).getLastNotePlayedPtr (midiChannel));
- }
-
- //==============================================================================
- const MPENote* MPEInstrument::getHighestNotePtr (int midiChannel) const noexcept
- {
- int initialNoteMax = -1;
- const MPENote* result = nullptr;
-
- for (auto i = notes.size(); --i >= 0;)
- {
- auto& note = notes.getReference (i);
-
- if (note.midiChannel == midiChannel
- && (note.keyState == MPENote::keyDown || note.keyState == MPENote::keyDownAndSustained)
- && note.initialNote > initialNoteMax)
- {
- result = ¬e;
- initialNoteMax = note.initialNote;
- }
- }
-
- return result;
- }
-
- MPENote* MPEInstrument::getHighestNotePtr (int midiChannel) noexcept
- {
- return const_cast<MPENote*> (static_cast<const MPEInstrument&> (*this).getHighestNotePtr (midiChannel));
- }
-
- const MPENote* MPEInstrument::getLowestNotePtr (int midiChannel) const noexcept
- {
- int initialNoteMin = 128;
- const MPENote* result = nullptr;
-
- for (auto i = notes.size(); --i >= 0;)
- {
- auto& note = notes.getReference (i);
-
- if (note.midiChannel == midiChannel
- && (note.keyState == MPENote::keyDown || note.keyState == MPENote::keyDownAndSustained)
- && note.initialNote < initialNoteMin)
- {
- result = ¬e;
- initialNoteMin = note.initialNote;
- }
- }
-
- return result;
- }
-
- MPENote* MPEInstrument::getLowestNotePtr (int midiChannel) noexcept
- {
- return const_cast<MPENote*> (static_cast<const MPEInstrument&> (*this).getLowestNotePtr (midiChannel));
- }
-
- //==============================================================================
- void MPEInstrument::releaseAllNotes()
- {
- const ScopedLock sl (lock);
-
- for (auto i = notes.size(); --i >= 0;)
- {
- auto& note = notes.getReference (i);
- note.keyState = MPENote::off;
- note.noteOffVelocity = MPEValue::from7BitInt (64); // some reasonable number
- listeners.call ([&] (Listener& l) { l.noteReleased (note); });
- }
-
- notes.clear();
- }
-
-
- //==============================================================================
- //==============================================================================
- #if JUCE_UNIT_TESTS
-
- class MPEInstrumentTests : public UnitTest
- {
- public:
- MPEInstrumentTests()
- : UnitTest ("MPEInstrument class", UnitTestCategories::midi)
- {
- // using lower and upper MPE zones with the following layout for testing
- //
- // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
- // * ...................| |........................ *
-
- testLayout.setLowerZone (5);
- testLayout.setUpperZone (6);
- }
-
- JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6262)
- void runTest() override
- {
- beginTest ("initial zone layout");
- {
- MPEInstrument test;
- expect (! test.getZoneLayout().getLowerZone().isActive());
- expect (! test.getZoneLayout().getUpperZone().isActive());
- }
-
- beginTest ("get/setZoneLayout");
- {
- MPEInstrument test;
- test.setZoneLayout (testLayout);
-
- auto newLayout = test.getZoneLayout();
-
- expect (test.getZoneLayout().getLowerZone().isActive());
- expect (test.getZoneLayout().getUpperZone().isActive());
- expectEquals (newLayout.getLowerZone().getMasterChannel(), 1);
- expectEquals (newLayout.getLowerZone().numMemberChannels, 5);
- expectEquals (newLayout.getUpperZone().getMasterChannel(), 16);
- expectEquals (newLayout.getUpperZone().numMemberChannels, 6);
- }
-
- beginTest ("noteOn / noteOff");
- {
- {
- MPEInstrument test;
- test.setZoneLayout (testLayout);
- expectEquals (test.getNumPlayingNotes(), 0);
- }
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- // note-on on unused channel - ignore
- test.noteOn (7, 60, MPEValue::from7BitInt (100));
- expectEquals (test.getNumPlayingNotes(), 0);
- expectEquals (test.noteAddedCallCounter, 0);
-
- // note-on on member channel - create new note
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- expectEquals (test.getNumPlayingNotes(), 1);
- expectEquals (test.noteAddedCallCounter, 1);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
-
- // note-off
- test.noteOff (3, 60, MPEValue::from7BitInt (33));
- expectEquals (test.getNumPlayingNotes(), 0);
- expectEquals (test.noteReleasedCallCounter, 1);
- expectHasFinishedNote (test, 3, 60, 33);
-
-
- // note-on on master channel - create new note
- test.noteOn (1, 62, MPEValue::from7BitInt (100));
- expectEquals (test.getNumPlayingNotes(), 1);
- expectEquals (test.noteAddedCallCounter, 2);
- expectNote (test.getNote (1, 62), 100, 0, 8192, 64, MPENote::keyDown);
-
- // note-off
- test.noteOff (1, 62, MPEValue::from7BitInt (33));
- expectEquals (test.getNumPlayingNotes(), 0);
- expectEquals (test.noteReleasedCallCounter, 2);
- expectHasFinishedNote (test, 1, 62, 33);
- }
-
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
-
- // note off with non-matching note number shouldn't do anything
- test.noteOff (3, 61, MPEValue::from7BitInt (33));
- expectEquals (test.getNumPlayingNotes(), 1);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteReleasedCallCounter, 0);
-
- // note off with non-matching midi channel shouldn't do anything
- test.noteOff (2, 60, MPEValue::from7BitInt (33));
- expectEquals (test.getNumPlayingNotes(), 1);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteReleasedCallCounter, 0);
- }
-
- {
- // can have multiple notes on the same channel
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
- test.noteOn (3, 0, MPEValue::from7BitInt (100));
- test.noteOn (3, 1, MPEValue::from7BitInt (100));
- test.noteOn (3, 2, MPEValue::from7BitInt (100));
- expectEquals (test.getNumPlayingNotes(), 3);
- expectNote (test.getNote (3, 0), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 1), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 2), 100, 0, 8192, 64, MPENote::keyDown);
- }
- {
- // pathological case: second note-on for same note should retrigger it.
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
- test.noteOn (3, 0, MPEValue::from7BitInt (100));
- test.noteOn (3, 0, MPEValue::from7BitInt (60));
- expectEquals (test.getNumPlayingNotes(), 1);
- expectNote (test.getNote (3, 0), 60, 0, 8192, 64, MPENote::keyDown);
- }
- }
-
- beginTest ("noteReleased after setZoneLayout");
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.noteOn (4, 61, MPEValue::from7BitInt (100));
- expectEquals (test.getNumPlayingNotes(), 3);
- expectEquals (test.noteReleasedCallCounter, 0);
-
- test.setZoneLayout (testLayout);
- expectEquals (test.getNumPlayingNotes(), 0);
- expectEquals (test.noteReleasedCallCounter, 3);
- }
-
- beginTest ("releaseAllNotes");
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (4, 61, MPEValue::from7BitInt (100));
- test.noteOn (15, 62, MPEValue::from7BitInt (100));
- expectEquals (test.getNumPlayingNotes(), 3);
-
- test.releaseAllNotes();
- expectEquals (test.getNumPlayingNotes(), 0);
- }
-
- beginTest ("sustainPedal");
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
- test.noteOn (3, 60, MPEValue::from7BitInt (100)); // note in lower zone
- test.noteOn (10, 60, MPEValue::from7BitInt (100)); // note in upper zone
-
- // sustain pedal on per-note channel shouldn't do anything.
- test.sustainPedal (3, true);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
-
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteKeyStateChangedCallCounter, 0);
-
- // sustain pedal on non-zone channel shouldn't do anything either.
- test.sustainPedal (7, true);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteKeyStateChangedCallCounter, 0);
-
- // sustain pedal on master channel should sustain notes on _that_ zone.
- test.sustainPedal (1, true);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDownAndSustained);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteKeyStateChangedCallCounter, 1);
-
- // release
- test.sustainPedal (1, false);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteKeyStateChangedCallCounter, 2);
-
- // should also sustain new notes added after the press
- test.sustainPedal (1, true);
- expectEquals (test.noteKeyStateChangedCallCounter, 3);
- test.noteOn (4, 51, MPEValue::from7BitInt (100));
- expectNote (test.getNote (4, 51), 100, 0, 8192, 64, MPENote::keyDownAndSustained);
- expectEquals (test.noteKeyStateChangedCallCounter, 3);
-
- // ...but only if that sustain came on the master channel of that zone!
- test.sustainPedal (11, true);
- test.noteOn (11, 52, MPEValue::from7BitInt (100));
- expectNote (test.getNote (11, 52), 100, 0, 8192, 64, MPENote::keyDown);
- test.noteOff (11, 52, MPEValue::from7BitInt (100));
- expectEquals (test.noteReleasedCallCounter, 1);
-
- // note-off should not turn off sustained notes inside the same zone
- test.noteOff (3, 60, MPEValue::from7BitInt (100));
- test.noteOff (4, 51, MPEValue::from7BitInt (100));
- test.noteOff (10, 60, MPEValue::from7BitInt (100)); // not affected!
- expectEquals (test.getNumPlayingNotes(), 2);
- expectEquals (test.noteReleasedCallCounter, 2);
- expectEquals (test.noteKeyStateChangedCallCounter, 5);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::sustained);
- expectNote (test.getNote (4, 51), 100, 0, 8192, 64, MPENote::sustained);
-
- // notes should be turned off when pedal is released
- test.sustainPedal (1, false);
- expectEquals (test.getNumPlayingNotes(), 0);
- expectEquals (test.noteReleasedCallCounter, 4);
- }
-
- beginTest ("sostenutoPedal");
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
- test.noteOn (3, 60, MPEValue::from7BitInt (100)); // note in lower zone
- test.noteOn (10, 60, MPEValue::from7BitInt (100)); // note in upper zone
-
- // sostenuto pedal on per-note channel shouldn't do anything.
- test.sostenutoPedal (3, true);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteKeyStateChangedCallCounter, 0);
-
- // sostenuto pedal on non-zone channel shouldn't do anything either.
- test.sostenutoPedal (9, true);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteKeyStateChangedCallCounter, 0);
-
- // sostenuto pedal on master channel should sustain notes on *that* zone.
- test.sostenutoPedal (1, true);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDownAndSustained);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteKeyStateChangedCallCounter, 1);
-
- // release
- test.sostenutoPedal (1, false);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteKeyStateChangedCallCounter, 2);
-
- // should only sustain notes turned on *before* the press (difference to sustain pedal)
- test.sostenutoPedal (1, true);
- expectEquals (test.noteKeyStateChangedCallCounter, 3);
- test.noteOn (4, 51, MPEValue::from7BitInt (100));
- expectEquals (test.getNumPlayingNotes(), 3);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDownAndSustained);
- expectNote (test.getNote (4, 51), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteKeyStateChangedCallCounter, 3);
-
- // note-off should not turn off sustained notes inside the same zone,
- // but only if they were turned on *before* the sostenuto pedal (difference to sustain pedal)
- test.noteOff (3, 60, MPEValue::from7BitInt (100));
- test.noteOff (4, 51, MPEValue::from7BitInt (100));
- test.noteOff (10, 60, MPEValue::from7BitInt (100)); // not affected!
- expectEquals (test.getNumPlayingNotes(), 1);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::sustained);
- expectEquals (test.noteReleasedCallCounter, 2);
- expectEquals (test.noteKeyStateChangedCallCounter, 4);
-
- // notes should be turned off when pedal is released
- test.sustainPedal (1, false);
- expectEquals (test.getNumPlayingNotes(), 0);
- expectEquals (test.noteReleasedCallCounter, 3);
- }
-
- beginTest ("getMostRecentNote");
- {
- MPEInstrument test;
- test.setZoneLayout (testLayout);
-
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
-
- {
- auto note = test.getMostRecentNote (2);
- expect (! note.isValid());
- }
- {
- auto note = test.getMostRecentNote (3);
- expect (note.isValid());
- expectEquals (int (note.midiChannel), 3);
- expectEquals (int (note.initialNote), 61);
- }
-
- test.sustainPedal (1, true);
- test.noteOff (3, 61, MPEValue::from7BitInt (100));
-
- {
- auto note = test.getMostRecentNote (3);
- expect (note.isValid());
- expectEquals (int (note.midiChannel), 3);
- expectEquals (int (note.initialNote), 60);
- }
-
- test.sustainPedal (1, false);
- test.noteOff (3, 60, MPEValue::from7BitInt (100));
-
- {
- auto note = test.getMostRecentNote (3);
- expect (! note.isValid());
- }
- }
-
- beginTest ("getMostRecentNoteOtherThan");
- {
- MPENote testNote (3, 60,
- MPEValue::centreValue(), MPEValue::centreValue(),
- MPEValue::centreValue(), MPEValue::centreValue());
-
- {
- // case 1: the note to exclude is not the most recent one.
-
- MPEInstrument test;
- test.setZoneLayout (testLayout);
- expect (! test.getMostRecentNoteOtherThan (testNote).isValid());
-
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- expect (! test.getMostRecentNoteOtherThan (testNote).isValid());
-
- test.noteOn (4, 61, MPEValue::from7BitInt (100));
- expect (test.getMostRecentNoteOtherThan (testNote).isValid());
- expect (test.getMostRecentNoteOtherThan (testNote).midiChannel == 4);
- expect (test.getMostRecentNoteOtherThan (testNote).initialNote == 61);
- }
- {
- // case 2: the note to exclude is the most recent one.
-
- MPEInstrument test;
- test.setZoneLayout (testLayout);
- expect (! test.getMostRecentNoteOtherThan (testNote).isValid());
-
- test.noteOn (4, 61, MPEValue::from7BitInt (100));
- expect (test.getMostRecentNoteOtherThan (testNote).isValid());
- expect (test.getMostRecentNoteOtherThan (testNote).midiChannel == 4);
- expect (test.getMostRecentNoteOtherThan (testNote).initialNote == 61);
-
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- expect (test.getMostRecentNoteOtherThan (testNote).isValid());
- expect (test.getMostRecentNoteOtherThan (testNote).midiChannel == 4);
- expect (test.getMostRecentNoteOtherThan (testNote).initialNote == 61);
- }
- }
-
- beginTest ("pressure");
- {
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (4, 60, MPEValue::from7BitInt (100));
- test.noteOn (10, 60, MPEValue::from7BitInt (100));
-
- // applying pressure on a per-note channel should modulate one note
- test.pressure (3, MPEValue::from7BitInt (33));
- expectNote (test.getNote (3, 60), 100, 33, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (4, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.notePressureChangedCallCounter, 1);
-
- // applying pressure on a master channel should modulate all notes in this zone
- test.pressure (1, MPEValue::from7BitInt (44));
- expectNote (test.getNote (3, 60), 100, 44, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (4, 60), 100, 44, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.notePressureChangedCallCounter, 3);
-
- // applying pressure on an unrelated channel should be ignored
- test.pressure (8, MPEValue::from7BitInt (55));
- expectNote (test.getNote (3, 60), 100, 44, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (4, 60), 100, 44, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.notePressureChangedCallCounter, 3);
- }
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- // two notes on same channel - only last added should be modulated
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.pressure (3, MPEValue::from7BitInt (66));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 66, 8192, 64, MPENote::keyDown);
- expectEquals (test.notePressureChangedCallCounter, 1);
- }
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- // edge case: two notes on same channel, one gets released,
- // then the other should be modulated
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.noteOff (3, 61, MPEValue::from7BitInt (100));
- test.pressure (3, MPEValue::from7BitInt (77));
- expectEquals (test.getNumPlayingNotes(), 1);
- expectNote (test.getNote (3, 60), 100, 77, 8192, 64, MPENote::keyDown);
- expectEquals (test.notePressureChangedCallCounter, 1);
- }
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- // if no pressure is sent before note-on, default = 0 should be used
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- }
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- // if pressure is sent before note-on, use that
- test.pressure (3, MPEValue::from7BitInt (77));
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- expectNote (test.getNote (3, 60), 100, 77, 8192, 64, MPENote::keyDown);
- }
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- // if pressure is sent before note-on, but it belonged to another note
- // on the same channel that has since been turned off, use default = 0
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.pressure (3, MPEValue::from7BitInt (77));
- test.noteOff (3, 61, MPEValue::from7BitInt (100));
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- }
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- // edge case: two notes on the same channel simultaneously. the second one should use
- // pressure = 0 initially but then react to additional pressure messages
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.pressure (3, MPEValue::from7BitInt (77));
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- test.pressure (3, MPEValue::from7BitInt (78));
- expectNote (test.getNote (3, 60), 100, 78, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 77, 8192, 64, MPENote::keyDown);
- }
-
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- // master channel will use poly-aftertouch for pressure
- test.noteOn (16, 60, MPEValue::from7BitInt (100));
- expectNote (test.getNote (16, 60), 100, 0, 8192, 64, MPENote::keyDown);
- test.aftertouch (16, 60, MPEValue::from7BitInt (27));
- expectNote (test.getNote (16, 60), 100, 27, 8192, 64, MPENote::keyDown);
-
- // member channels will not respond to poly-aftertouch
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.aftertouch (3, 60, MPEValue::from7BitInt (50));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- }
- }
-
- beginTest ("pitchbend");
- {
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (4, 60, MPEValue::from7BitInt (100));
- test.noteOn (10, 60, MPEValue::from7BitInt (100));
-
- // applying pitchbend on a per-note channel should modulate one note
- test.pitchbend (3, MPEValue::from14BitInt (1111));
- expectNote (test.getNote (3, 60), 100, 0, 1111, 64, MPENote::keyDown);
- expectNote (test.getNote (4, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.notePitchbendChangedCallCounter, 1);
-
- // applying pitchbend on a master channel should be ignored for the
- // value of per-note pitchbend. Tests covering master pitchbend below.
- // Note: noteChanged will be called anyway for notes in that zone
- // because the total pitchbend for those notes has changed
- test.pitchbend (1, MPEValue::from14BitInt (2222));
- expectNote (test.getNote (3, 60), 100, 0, 1111, 64, MPENote::keyDown);
- expectNote (test.getNote (4, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.notePitchbendChangedCallCounter, 3);
-
- // applying pitchbend on an unrelated channel should do nothing.
- test.pitchbend (8, MPEValue::from14BitInt (3333));
- expectNote (test.getNote (3, 60), 100, 0, 1111, 64, MPENote::keyDown);
- expectNote (test.getNote (4, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.notePitchbendChangedCallCounter, 3);
- }
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- // two notes on same channel - only last added should be bent
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.pitchbend (3, MPEValue::from14BitInt (4444));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 0, 4444, 64, MPENote::keyDown);
- expectEquals (test.notePitchbendChangedCallCounter, 1);
- }
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- // edge case: two notes on same channel, one gets released,
- // then the other should be bent
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.noteOff (3, 61, MPEValue::from7BitInt (100));
- test.pitchbend (3, MPEValue::from14BitInt (5555));
- expectEquals (test.getNumPlayingNotes(), 1);
- expectNote (test.getNote (3, 60), 100, 0, 5555, 64, MPENote::keyDown);
- expectEquals (test.notePitchbendChangedCallCounter, 1);
- }
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- // Richard's edge case:
- // - press one note
- // - press sustain (careful: must be sent on master channel)
- // - release first note (is still sustained!)
- // - press another note (happens to be on the same MIDI channel!)
- // - pitchbend that other note
- // - the first note should not be bent, only the second one.
-
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.sustainPedal (1, true);
- test.noteOff (3, 60, MPEValue::from7BitInt (64));
- expectEquals (test.getNumPlayingNotes(), 1);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::sustained);
- expectEquals (test.noteKeyStateChangedCallCounter, 2);
-
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.pitchbend (3, MPEValue::from14BitInt (6666));
- expectEquals (test.getNumPlayingNotes(), 2);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::sustained);
- expectNote (test.getNote (3, 61), 100, 0, 6666, 64, MPENote::keyDownAndSustained);
- expectEquals (test.notePitchbendChangedCallCounter, 1);
- }
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- // Zsolt's edge case:
- // - press one note
- // - modulate pitchbend or timbre
- // - release the note
- // - press same note again without sending a pitchbend or timbre message before the note-on
- // - the note should be turned on with a default value for pitchbend/timbre,
- // and *not* the last value received on channel.
-
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.pitchbend (3, MPEValue::from14BitInt (5555));
- expectNote (test.getNote (3, 60), 100, 0, 5555, 64, MPENote::keyDown);
-
- test.noteOff (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- }
- {
- // applying per-note pitchbend should set the note's totalPitchbendInSemitones
- // correctly depending on the per-note pitchbend range of the zone.
- UnitTestInstrument test;
-
- MPEZoneLayout layout = testLayout;
- test.setZoneLayout (layout); // default should be +/- 48 semitones
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.pitchbend (3, MPEValue::from14BitInt (4096));
- expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, -24.0, 0.01);
-
- layout.setLowerZone (5, 96);
- test.setZoneLayout (layout);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.pitchbend (3, MPEValue::from14BitInt (0)); // -max
- expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, -96.0, 0.01);
-
- layout.setLowerZone (5, 1);
- test.setZoneLayout (layout);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.pitchbend (3, MPEValue::from14BitInt (16383)); // +max
- expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, 1.0, 0.01);
-
- layout.setLowerZone (5, 0); // pitchbendrange = 0 --> no pitchbend at all
- test.setZoneLayout (layout);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.pitchbend (3, MPEValue::from14BitInt (12345));
- expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, 0.0, 0.01);
- }
- {
- // applying master pitchbend should set the note's totalPitchbendInSemitones
- // correctly depending on the master pitchbend range of the zone.
- UnitTestInstrument test;
-
- MPEZoneLayout layout = testLayout;
- test.setZoneLayout (layout); // default should be +/- 2 semitones
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.pitchbend (1, MPEValue::from14BitInt (4096)); //halfway between -max and centre
- expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, -1.0, 0.01);
-
- layout.setLowerZone (5, 48, 96);
- test.setZoneLayout (layout);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.pitchbend (1, MPEValue::from14BitInt (0)); // -max
- expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, -96.0, 0.01);
-
- layout.setLowerZone (5, 48, 1);
- test.setZoneLayout (layout);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.pitchbend (1, MPEValue::from14BitInt (16383)); // +max
- expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, 1.0, 0.01);
-
- layout.setLowerZone (5, 48, 0); // pitchbendrange = 0 --> no pitchbend at all
- test.setZoneLayout (layout);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.pitchbend (1, MPEValue::from14BitInt (12345));
- expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, 0.0, 0.01);
- }
- {
- // applying both per-note and master pitchbend simultaneously should set
- // the note's totalPitchbendInSemitones to the sum of both, correctly
- // weighted with the per-note and master pitchbend range, respectively.
- UnitTestInstrument test;
-
- MPEZoneLayout layout = testLayout;
- layout.setLowerZone (5, 12, 1);
- test.setZoneLayout (layout);
-
- test.pitchbend (1, MPEValue::from14BitInt (4096)); // master pitchbend 0.5 semitones down
- test.pitchbend (3, MPEValue::from14BitInt (0)); // per-note pitchbend 12 semitones down
- // additionally, note should react to both pitchbend messages
- // correctly even if they arrived before the note-on.
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, -12.5, 0.01);
- }
- }
-
- beginTest ("timbre");
- {
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (4, 60, MPEValue::from7BitInt (100));
- test.noteOn (10, 60, MPEValue::from7BitInt (100));
-
- // modulating timbre on a per-note channel should modulate one note
- test.timbre (3, MPEValue::from7BitInt (33));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 33, MPENote::keyDown);
- expectNote (test.getNote (4, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteTimbreChangedCallCounter, 1);
-
- // modulating timbre on a master channel should modulate all notes in this zone
- test.timbre (1, MPEValue::from7BitInt (44));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 44, MPENote::keyDown);
- expectNote (test.getNote (4, 60), 100, 0, 8192, 44, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteTimbreChangedCallCounter, 3);
-
- // modulating timbre on an unrelated channel should be ignored
- test.timbre (9, MPEValue::from7BitInt (55));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 44, MPENote::keyDown);
- expectNote (test.getNote (4, 60), 100, 0, 8192, 44, MPENote::keyDown);
- expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteTimbreChangedCallCounter, 3);
- }
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- // two notes on same channel - only last added should be modulated
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.timbre (3, MPEValue::from7BitInt (66));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 0, 8192, 66, MPENote::keyDown);
- expectEquals (test.noteTimbreChangedCallCounter, 1);
- }
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- // edge case: two notes on same channel, one gets released,
- // then the other should be modulated
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.noteOff (3, 61, MPEValue::from7BitInt (100));
- test.timbre (3, MPEValue::from7BitInt (77));
- expectEquals (test.getNumPlayingNotes(), 1);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 77, MPENote::keyDown);
- expectEquals (test.noteTimbreChangedCallCounter, 1);
- }
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- // Zsolt's edge case for timbre
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.timbre (3, MPEValue::from7BitInt (42));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 42, MPENote::keyDown);
-
- test.noteOff (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- }
- }
-
- beginTest ("setPressureTrackingMode");
- {
- {
- // last note played (= default)
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.setPressureTrackingMode (MPEInstrument::lastNotePlayedOnChannel);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 62, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.pressure (3, MPEValue::from7BitInt (99));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 62), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 99, 8192, 64, MPENote::keyDown);
- expectEquals (test.notePressureChangedCallCounter, 1);
- }
- {
- // lowest note
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.setPressureTrackingMode (MPEInstrument::lowestNoteOnChannel);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 62, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.pressure (3, MPEValue::from7BitInt (99));
- expectNote (test.getNote (3, 60), 100, 99, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 62), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.notePressureChangedCallCounter, 1);
- }
- {
- // highest note
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.setPressureTrackingMode (MPEInstrument::highestNoteOnChannel);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 62, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.pressure (3, MPEValue::from7BitInt (99));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 62), 100, 99, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.notePressureChangedCallCounter, 1);
- }
- {
- // all notes
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.setPressureTrackingMode (MPEInstrument::allNotesOnChannel);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 62, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.pressure (3, MPEValue::from7BitInt (99));
- expectNote (test.getNote (3, 60), 100, 99, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 62), 100, 99, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 99, 8192, 64, MPENote::keyDown);
- expectEquals (test.notePressureChangedCallCounter, 3);
- }
- }
-
- beginTest ("setPitchbendTrackingMode");
- {
- {
- // last note played (= default)
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.setPitchbendTrackingMode (MPEInstrument::lastNotePlayedOnChannel);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 62, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.pitchbend (3, MPEValue::from14BitInt (9999));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 62), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 0, 9999, 64, MPENote::keyDown);
- expectEquals (test.notePitchbendChangedCallCounter, 1);
- }
- {
- // lowest note
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.setPitchbendTrackingMode (MPEInstrument::lowestNoteOnChannel);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 62, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.pitchbend (3, MPEValue::from14BitInt (9999));
- expectNote (test.getNote (3, 60), 100, 0, 9999, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 62), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.notePitchbendChangedCallCounter, 1);
- }
- {
- // highest note
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.setPitchbendTrackingMode (MPEInstrument::highestNoteOnChannel);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 62, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.pitchbend (3, MPEValue::from14BitInt (9999));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 62), 100, 0, 9999, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.notePitchbendChangedCallCounter, 1);
- }
- {
- // all notes
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.setPitchbendTrackingMode (MPEInstrument::allNotesOnChannel);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 62, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.pitchbend (3, MPEValue::from14BitInt (9999));
- expectNote (test.getNote (3, 60), 100, 0, 9999, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 62), 100, 0, 9999, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 0, 9999, 64, MPENote::keyDown);
- expectEquals (test.notePitchbendChangedCallCounter, 3);
- }
- }
-
- beginTest ("setTimbreTrackingMode");
- {
- {
- // last note played (= default)
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.setTimbreTrackingMode (MPEInstrument::lastNotePlayedOnChannel);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 62, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.timbre (3, MPEValue::from7BitInt (99));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 62), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 0, 8192, 99, MPENote::keyDown);
- expectEquals (test.noteTimbreChangedCallCounter, 1);
- }
- {
- // lowest note
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.setTimbreTrackingMode (MPEInstrument::lowestNoteOnChannel);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 62, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.timbre (3, MPEValue::from7BitInt (99));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 99, MPENote::keyDown);
- expectNote (test.getNote (3, 62), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteTimbreChangedCallCounter, 1);
- }
- {
- // highest note
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.setTimbreTrackingMode (MPEInstrument::highestNoteOnChannel);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 62, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.timbre (3, MPEValue::from7BitInt (99));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (3, 62), 100, 0, 8192, 99, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 0, 8192, 64, MPENote::keyDown);
- expectEquals (test.noteTimbreChangedCallCounter, 1);
- }
- {
- // all notes
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
-
- test.setTimbreTrackingMode (MPEInstrument::allNotesOnChannel);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 62, MPEValue::from7BitInt (100));
- test.noteOn (3, 61, MPEValue::from7BitInt (100));
- test.timbre (3, MPEValue::from7BitInt (99));
- expectNote (test.getNote (3, 60), 100, 0, 8192, 99, MPENote::keyDown);
- expectNote (test.getNote (3, 62), 100, 0, 8192, 99, MPENote::keyDown);
- expectNote (test.getNote (3, 61), 100, 0, 8192, 99, MPENote::keyDown);
- expectEquals (test.noteTimbreChangedCallCounter, 3);
- }
- }
-
- beginTest ("processNextMidiEvent");
- {
- UnitTestInstrument test;
-
- // note on should trigger noteOn method call
-
- test.processNextMidiEvent (MidiMessage::noteOn (3, 42, uint8 (92)));
- expectEquals (test.noteOnCallCounter, 1);
- expectEquals (test.lastMidiChannelReceived, 3);
- expectEquals (test.lastMidiNoteNumberReceived, 42);
- expectEquals (test.lastMPEValueReceived.as7BitInt(), 92);
-
- // note off should trigger noteOff method call
-
- test.processNextMidiEvent (MidiMessage::noteOff (4, 12, uint8 (33)));
- expectEquals (test.noteOffCallCounter, 1);
- expectEquals (test.lastMidiChannelReceived, 4);
- expectEquals (test.lastMidiNoteNumberReceived, 12);
- expectEquals (test.lastMPEValueReceived.as7BitInt(), 33);
-
- // note on with velocity = 0 should trigger noteOff method call
- // with a note off velocity of 64 (centre value)
-
- test.processNextMidiEvent (MidiMessage::noteOn (5, 11, uint8 (0)));
- expectEquals (test.noteOffCallCounter, 2);
- expectEquals (test.lastMidiChannelReceived, 5);
- expectEquals (test.lastMidiNoteNumberReceived, 11);
- expectEquals (test.lastMPEValueReceived.as7BitInt(), 64);
-
- // pitchwheel message should trigger pitchbend method call
-
- test.processNextMidiEvent (MidiMessage::pitchWheel (1, 3333));
- expectEquals (test.pitchbendCallCounter, 1);
- expectEquals (test.lastMidiChannelReceived, 1);
- expectEquals (test.lastMPEValueReceived.as14BitInt(), 3333);
-
- // pressure using channel pressure message (7-bit value) should
- // trigger pressure method call
-
- test.processNextMidiEvent (MidiMessage::channelPressureChange (10, 35));
- expectEquals (test.pressureCallCounter, 1);
- expectEquals (test.lastMidiChannelReceived, 10);
- expectEquals (test.lastMPEValueReceived.as7BitInt(), 35);
-
- // pressure using 14-bit value over CC70 and CC102 should trigger
- // pressure method call after the MSB is sent
-
- // a) sending only the MSB
- test.processNextMidiEvent (MidiMessage::controllerEvent (3, 70, 120));
- expectEquals (test.pressureCallCounter, 2);
- expectEquals (test.lastMidiChannelReceived, 3);
- expectEquals (test.lastMPEValueReceived.as7BitInt(), 120);
-
- // b) sending LSB and MSB (only the MSB should trigger the call) - per MIDI channel!
- test.processNextMidiEvent (MidiMessage::controllerEvent (4, 102, 121));
- expectEquals (test.pressureCallCounter, 2);
- test.processNextMidiEvent (MidiMessage::controllerEvent (5, 102, 122));
- expectEquals (test.pressureCallCounter, 2);
- test.processNextMidiEvent (MidiMessage::controllerEvent (4, 70, 123));
- expectEquals (test.pressureCallCounter, 3);
- expectEquals (test.lastMidiChannelReceived, 4);
- expectEquals (test.lastMPEValueReceived.as14BitInt(), 121 + (123 << 7));
- test.processNextMidiEvent (MidiMessage::controllerEvent (5, 70, 124));
- expectEquals (test.pressureCallCounter, 4);
- expectEquals (test.lastMidiChannelReceived, 5);
- expectEquals (test.lastMPEValueReceived.as14BitInt(), 122 + (124 << 7));
- test.processNextMidiEvent (MidiMessage::controllerEvent (5, 70, 64));
- expectEquals (test.pressureCallCounter, 5);
- expectEquals (test.lastMidiChannelReceived, 5);
- expectEquals (test.lastMPEValueReceived.as7BitInt(), 64);
-
- // same for timbre 14-bit value over CC74 and CC106
- test.processNextMidiEvent (MidiMessage::controllerEvent (3, 74, 120));
- expectEquals (test.timbreCallCounter, 1);
- expectEquals (test.lastMidiChannelReceived, 3);
- expectEquals (test.lastMPEValueReceived.as7BitInt(), 120);
- test.processNextMidiEvent (MidiMessage::controllerEvent (4, 106, 121));
- expectEquals (test.timbreCallCounter, 1);
- test.processNextMidiEvent (MidiMessage::controllerEvent (5, 106, 122));
- expectEquals (test.timbreCallCounter, 1);
- test.processNextMidiEvent (MidiMessage::controllerEvent (4, 74, 123));
- expectEquals (test.timbreCallCounter, 2);
- expectEquals (test.lastMidiChannelReceived, 4);
- expectEquals (test.lastMPEValueReceived.as14BitInt(), 121 + (123 << 7));
- test.processNextMidiEvent (MidiMessage::controllerEvent (5, 74, 124));
- expectEquals (test.timbreCallCounter, 3);
- expectEquals (test.lastMidiChannelReceived, 5);
- expectEquals (test.lastMPEValueReceived.as14BitInt(), 122 + (124 << 7));
- test.processNextMidiEvent (MidiMessage::controllerEvent (5, 74, 64));
- expectEquals (test.timbreCallCounter, 4);
- expectEquals (test.lastMidiChannelReceived, 5);
- expectEquals (test.lastMPEValueReceived.as7BitInt(), 64);
-
- // sustain pedal message (CC64) should trigger sustainPedal method call
- test.processNextMidiEvent (MidiMessage::controllerEvent (1, 64, 127));
- expectEquals (test.sustainPedalCallCounter, 1);
- expectEquals (test.lastMidiChannelReceived, 1);
- expect (test.lastSustainPedalValueReceived);
- test.processNextMidiEvent (MidiMessage::controllerEvent (16, 64, 0));
- expectEquals (test.sustainPedalCallCounter, 2);
- expectEquals (test.lastMidiChannelReceived, 16);
- expect (! test.lastSustainPedalValueReceived);
-
- // sostenuto pedal message (CC66) should trigger sostenutoPedal method call
- test.processNextMidiEvent (MidiMessage::controllerEvent (1, 66, 127));
- expectEquals (test.sostenutoPedalCallCounter, 1);
- expectEquals (test.lastMidiChannelReceived, 1);
- expect (test.lastSostenutoPedalValueReceived);
- test.processNextMidiEvent (MidiMessage::controllerEvent (16, 66, 0));
- expectEquals (test.sostenutoPedalCallCounter, 2);
- expectEquals (test.lastMidiChannelReceived, 16);
- expect (! test.lastSostenutoPedalValueReceived);
- }
- {
- // MIDI messages modifying the zone layout should be correctly
- // forwarded to the internal zone layout and modify it.
- // (testing the actual logic of the zone layout is done in the
- // MPEZoneLayout unit tests)
- MPEInstrument test;
-
- MidiBuffer buffer;
- buffer.addEvents (MPEMessages::setLowerZone (5), 0, -1, 0);
- buffer.addEvents (MPEMessages::setUpperZone (6), 0, -1, 0);
-
- for (const auto metadata : buffer)
- test.processNextMidiEvent (metadata.getMessage());
-
- expect (test.getZoneLayout().getLowerZone().isActive());
- expect (test.getZoneLayout().getUpperZone().isActive());
- expectEquals (test.getZoneLayout().getLowerZone().getMasterChannel(), 1);
- expectEquals (test.getZoneLayout().getLowerZone().numMemberChannels, 5);
- expectEquals (test.getZoneLayout().getUpperZone().getMasterChannel(), 16);
- expectEquals (test.getZoneLayout().getUpperZone().numMemberChannels, 6);
- }
-
- beginTest ("MIDI all notes off");
- {
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (4, 61, MPEValue::from7BitInt (100));
- test.noteOn (15, 62, MPEValue::from7BitInt (100));
- test.noteOn (15, 63, MPEValue::from7BitInt (100));
- expectEquals (test.getNumPlayingNotes(), 4);
-
- // on note channel: ignore.
- test.processNextMidiEvent (MidiMessage::allControllersOff (3));
- expectEquals (test.getNumPlayingNotes(), 4);
-
- // on unused channel: ignore.
- test.processNextMidiEvent (MidiMessage::allControllersOff (9));
- expectEquals (test.getNumPlayingNotes(), 4);
-
- // on master channel: release notes in that zone only.
- test.processNextMidiEvent (MidiMessage::allControllersOff (1));
- expectEquals (test.getNumPlayingNotes(), 2);
- test.processNextMidiEvent (MidiMessage::allControllersOff (16));
- expectEquals (test.getNumPlayingNotes(), 0);
- }
-
- beginTest ("MIDI all notes off (legacy mode)");
- {
- UnitTestInstrument test;
- test.enableLegacyMode();
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- test.noteOn (4, 61, MPEValue::from7BitInt (100));
- test.noteOn (15, 62, MPEValue::from7BitInt (100));
- test.noteOn (15, 63, MPEValue::from7BitInt (100));
- expectEquals (test.getNumPlayingNotes(), 4);
-
- test.processNextMidiEvent (MidiMessage::allControllersOff (3));
- expectEquals (test.getNumPlayingNotes(), 3);
-
- test.processNextMidiEvent (MidiMessage::allControllersOff (15));
- expectEquals (test.getNumPlayingNotes(), 1);
-
- test.processNextMidiEvent (MidiMessage::allControllersOff (4));
- expectEquals (test.getNumPlayingNotes(), 0);
- }
-
- beginTest ("default initial values for pitchbend and timbre");
- {
- MPEInstrument test;
- test.setZoneLayout (testLayout);
-
- test.pitchbend (3, MPEValue::from14BitInt (3333)); // use for next note-on on ch. 3
- test.pitchbend (2, MPEValue::from14BitInt (4444)); // ignore
- test.pitchbend (2, MPEValue::from14BitInt (5555)); // ignore
-
- test.timbre (3, MPEValue::from7BitInt (66)); // use for next note-on on ch. 3
- test.timbre (2, MPEValue::from7BitInt (77)); // ignore
- test.timbre (2, MPEValue::from7BitInt (88)); // ignore
-
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
-
- expectNote (test.getMostRecentNote (3), 100, 0, 3333, 66, MPENote::keyDown);
- }
-
- beginTest ("Legacy mode");
- {
- {
- // basic check
- MPEInstrument test;
- expect (! test.isLegacyModeEnabled());
-
- test.setZoneLayout (testLayout);
- expect (! test.isLegacyModeEnabled());
-
- test.enableLegacyMode();
- expect (test.isLegacyModeEnabled());
-
- test.setZoneLayout (testLayout);
- expect (! test.isLegacyModeEnabled());
- }
- {
- // constructor w/o default arguments
- MPEInstrument test;
- test.enableLegacyMode (0, Range<int> (1, 11));
- expectEquals (test.getLegacyModePitchbendRange(), 0);
- expect (test.getLegacyModeChannelRange() == Range<int> (1, 11));
- }
- {
- // getters and setters
- MPEInstrument test;
- test.enableLegacyMode();
-
- expectEquals (test.getLegacyModePitchbendRange(), 2);
- expect (test.getLegacyModeChannelRange() == Range<int> (1, 17));
-
- test.setLegacyModePitchbendRange (96);
- expectEquals (test.getLegacyModePitchbendRange(), 96);
-
- test.setLegacyModeChannelRange (Range<int> (10, 12));
- expect (test.getLegacyModeChannelRange() == Range<int> (10, 12));
- }
- {
- // note on should trigger notes on all 16 channels
-
- UnitTestInstrument test;
- test.enableLegacyMode();
-
- test.noteOn (1, 60, MPEValue::from7BitInt (100));
- test.noteOn (2, 60, MPEValue::from7BitInt (100));
- test.noteOn (15, 60, MPEValue::from7BitInt (100));
- test.noteOn (16, 60, MPEValue::from7BitInt (100));
- expectEquals (test.getNumPlayingNotes(), 4);
-
- // polyphonic modulation should work across all 16 channels
-
- test.pitchbend (1, MPEValue::from14BitInt (9999));
- test.pressure (2, MPEValue::from7BitInt (88));
- test.timbre (15, MPEValue::from7BitInt (77));
-
- expectNote (test.getNote (1, 60), 100, 0, 9999, 64, MPENote::keyDown);
- expectNote (test.getNote (2, 60), 100, 88, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (15, 60), 100, 0, 8192, 77, MPENote::keyDown);
- expectNote (test.getNote (16, 60), 100, 0, 8192, 64, MPENote::keyDown);
-
- // note off should work in legacy mode
-
- test.noteOff (15, 60, MPEValue::from7BitInt (0));
- test.noteOff (1, 60, MPEValue::from7BitInt (0));
- test.noteOff (2, 60, MPEValue::from7BitInt (0));
- test.noteOff (16, 60, MPEValue::from7BitInt (0));
- expectEquals (test.getNumPlayingNotes(), 0);
- }
- {
- // legacy mode w/ custom channel range: note on should trigger notes only within range
-
- UnitTestInstrument test;
- test.enableLegacyMode (2, Range<int> (3, 8)); // channels 3-7
-
- test.noteOn (1, 60, MPEValue::from7BitInt (100));
- test.noteOn (2, 60, MPEValue::from7BitInt (100));
- test.noteOn (3, 60, MPEValue::from7BitInt (100)); // should trigger
- test.noteOn (4, 60, MPEValue::from7BitInt (100)); // should trigger
- test.noteOn (6, 60, MPEValue::from7BitInt (100)); // should trigger
- test.noteOn (7, 60, MPEValue::from7BitInt (100)); // should trigger
- test.noteOn (8, 60, MPEValue::from7BitInt (100));
- test.noteOn (16, 60, MPEValue::from7BitInt (100));
-
- expectEquals (test.getNumPlayingNotes(), 4);
- expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (4, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (6, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (7, 60), 100, 0, 8192, 64, MPENote::keyDown);
- }
- {
- // tracking mode in legacy mode
- {
- UnitTestInstrument test;
- test.enableLegacyMode();
-
- test.setPitchbendTrackingMode (MPEInstrument::lastNotePlayedOnChannel);
- test.noteOn (1, 60, MPEValue::from7BitInt (100));
- test.noteOn (1, 62, MPEValue::from7BitInt (100));
- test.noteOn (1, 61, MPEValue::from7BitInt (100));
- test.pitchbend (1, MPEValue::from14BitInt (9999));
- expectNote (test.getNote (1, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (1, 61), 100, 0, 9999, 64, MPENote::keyDown);
- expectNote (test.getNote (1, 62), 100, 0, 8192, 64, MPENote::keyDown);
- }
- {
- UnitTestInstrument test;
- test.enableLegacyMode();
-
- test.setPitchbendTrackingMode (MPEInstrument::lowestNoteOnChannel);
- test.noteOn (1, 60, MPEValue::from7BitInt (100));
- test.noteOn (1, 62, MPEValue::from7BitInt (100));
- test.noteOn (1, 61, MPEValue::from7BitInt (100));
- test.pitchbend (1, MPEValue::from14BitInt (9999));
- expectNote (test.getNote (1, 60), 100, 0, 9999, 64, MPENote::keyDown);
- expectNote (test.getNote (1, 61), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (1, 62), 100, 0, 8192, 64, MPENote::keyDown);
- }
- {
- UnitTestInstrument test;
- test.enableLegacyMode();
-
- test.setPitchbendTrackingMode (MPEInstrument::highestNoteOnChannel);
- test.noteOn (1, 60, MPEValue::from7BitInt (100));
- test.noteOn (1, 62, MPEValue::from7BitInt (100));
- test.noteOn (1, 61, MPEValue::from7BitInt (100));
- test.pitchbend (1, MPEValue::from14BitInt (9999));
- expectNote (test.getNote (1, 60), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (1, 61), 100, 0, 8192, 64, MPENote::keyDown);
- expectNote (test.getNote (1, 62), 100, 0, 9999, 64, MPENote::keyDown);
- }
- {
- UnitTestInstrument test;
- test.enableLegacyMode();
-
- test.setPitchbendTrackingMode (MPEInstrument::allNotesOnChannel);
- test.noteOn (1, 60, MPEValue::from7BitInt (100));
- test.noteOn (1, 62, MPEValue::from7BitInt (100));
- test.noteOn (1, 61, MPEValue::from7BitInt (100));
- test.pitchbend (1, MPEValue::from14BitInt (9999));
- expectNote (test.getNote (1, 60), 100, 0, 9999, 64, MPENote::keyDown);
- expectNote (test.getNote (1, 61), 100, 0, 9999, 64, MPENote::keyDown);
- expectNote (test.getNote (1, 62), 100, 0, 9999, 64, MPENote::keyDown);
- }
- }
- {
- // custom pitchbend range in legacy mode.
- UnitTestInstrument test;
- test.enableLegacyMode (11);
-
- test.pitchbend (1, MPEValue::from14BitInt (4096));
- test.noteOn (1, 60, MPEValue::from7BitInt (100));
- expectDoubleWithinRelativeError (test.getMostRecentNote (1).totalPitchbendInSemitones, -5.5, 0.01);
- }
- {
- // sustain pedal should be per channel in legacy mode.
- UnitTestInstrument test;
- test.enableLegacyMode();
-
- test.sustainPedal (1, true);
- test.noteOn (2, 61, MPEValue::from7BitInt (100));
- test.noteOff (2, 61, MPEValue::from7BitInt (100));
- test.noteOn (1, 60, MPEValue::from7BitInt (100));
- test.noteOff (1, 60, MPEValue::from7BitInt (100));
-
- expectEquals (test.getNumPlayingNotes(), 1);
- expectNote (test.getNote (1, 60), 100, 0, 8192, 64, MPENote::sustained);
-
- test.sustainPedal (1, false);
- expectEquals (test.getNumPlayingNotes(), 0);
-
- test.noteOn (2, 61, MPEValue::from7BitInt (100));
- test.sustainPedal (1, true);
- test.noteOff (2, 61, MPEValue::from7BitInt (100));
- expectEquals (test.getNumPlayingNotes(), 0);
-
- }
- {
- // sostenuto pedal should be per channel in legacy mode.
- UnitTestInstrument test;
- test.enableLegacyMode();
-
- test.noteOn (1, 60, MPEValue::from7BitInt (100));
- test.sostenutoPedal (1, true);
- test.noteOff (1, 60, MPEValue::from7BitInt (100));
- test.noteOn (2, 61, MPEValue::from7BitInt (100));
- test.noteOff (2, 61, MPEValue::from7BitInt (100));
-
- expectEquals (test.getNumPlayingNotes(), 1);
- expectNote (test.getNote (1, 60), 100, 0, 8192, 64, MPENote::sustained);
-
- test.sostenutoPedal (1, false);
- expectEquals (test.getNumPlayingNotes(), 0);
-
- test.noteOn (2, 61, MPEValue::from7BitInt (100));
- test.sostenutoPedal (1, true);
- test.noteOff (2, 61, MPEValue::from7BitInt (100));
- expectEquals (test.getNumPlayingNotes(), 0);
- }
- {
- // all notes released when switching layout
- UnitTestInstrument test;
- test.setZoneLayout (testLayout);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- expectEquals (test.getNumPlayingNotes(), 1);
-
- test.enableLegacyMode();
- expectEquals (test.getNumPlayingNotes(), 0);
- test.noteOn (3, 60, MPEValue::from7BitInt (100));
- expectEquals (test.getNumPlayingNotes(), 1);
-
- test.setZoneLayout (testLayout);
- expectEquals (test.getNumPlayingNotes(), 0);
- }
- }
- }
- JUCE_END_IGNORE_WARNINGS_MSVC
-
- private:
- //==============================================================================
- /* This mock class is used for unit testing whether the methods of
- MPEInstrument are called correctly.
- */
- class UnitTestInstrument : public MPEInstrument,
- private MPEInstrument::Listener
- {
- using Base = MPEInstrument;
-
- public:
- UnitTestInstrument()
- : noteOnCallCounter (0), noteOffCallCounter (0), pitchbendCallCounter (0),
- pressureCallCounter (0), timbreCallCounter (0), sustainPedalCallCounter (0),
- sostenutoPedalCallCounter (0), noteAddedCallCounter (0), notePressureChangedCallCounter (0),
- notePitchbendChangedCallCounter (0), noteTimbreChangedCallCounter (0),
- noteKeyStateChangedCallCounter (0), noteReleasedCallCounter (0),
- lastMidiChannelReceived (-1), lastMidiNoteNumberReceived (-1),
- lastSustainPedalValueReceived (false), lastSostenutoPedalValueReceived (false)
- {
- addListener (this);
- }
-
- void noteOn (int midiChannel, int midiNoteNumber, MPEValue midiNoteOnVelocity) override
- {
- Base::noteOn (midiChannel, midiNoteNumber, midiNoteOnVelocity);
-
- noteOnCallCounter++;
- lastMidiChannelReceived = midiChannel;
- lastMidiNoteNumberReceived = midiNoteNumber;
- lastMPEValueReceived = midiNoteOnVelocity;
- }
-
- void noteOff (int midiChannel, int midiNoteNumber, MPEValue midiNoteOffVelocity) override
- {
- Base::noteOff (midiChannel, midiNoteNumber, midiNoteOffVelocity);
-
- noteOffCallCounter++;
- lastMidiChannelReceived = midiChannel;
- lastMidiNoteNumberReceived = midiNoteNumber;
- lastMPEValueReceived = midiNoteOffVelocity;
- }
-
- void pitchbend (int midiChannel, MPEValue value) override
- {
- Base::pitchbend (midiChannel, value);
-
- pitchbendCallCounter++;
- lastMidiChannelReceived = midiChannel;
- lastMPEValueReceived = value;
- }
-
- void pressure (int midiChannel, MPEValue value) override
- {
- Base::pressure (midiChannel, value);
-
- pressureCallCounter++;
- lastMidiChannelReceived = midiChannel;
- lastMPEValueReceived = value;
- }
-
- void timbre (int midiChannel, MPEValue value) override
- {
- Base::timbre (midiChannel, value);
-
- timbreCallCounter++;
- lastMidiChannelReceived = midiChannel;
- lastMPEValueReceived = value;
- }
-
- void sustainPedal (int midiChannel, bool value) override
- {
- Base::sustainPedal (midiChannel, value);
-
- sustainPedalCallCounter++;
- lastMidiChannelReceived = midiChannel;
- lastSustainPedalValueReceived = value;
- }
-
- void sostenutoPedal (int midiChannel, bool value) override
- {
- Base::sostenutoPedal (midiChannel, value);
-
- sostenutoPedalCallCounter++;
- lastMidiChannelReceived = midiChannel;
- lastSostenutoPedalValueReceived = value;
- }
-
- void aftertouch (int midiChannel, int midiNoteNumber, MPEValue value)
- {
- const auto message = juce::MidiMessage::aftertouchChange (midiChannel, midiNoteNumber, value.as7BitInt());
- processNextMidiEvent (message);
- }
-
- int noteOnCallCounter, noteOffCallCounter, pitchbendCallCounter,
- pressureCallCounter, timbreCallCounter, sustainPedalCallCounter,
- sostenutoPedalCallCounter, noteAddedCallCounter,
- notePressureChangedCallCounter, notePitchbendChangedCallCounter,
- noteTimbreChangedCallCounter, noteKeyStateChangedCallCounter,
- noteReleasedCallCounter, lastMidiChannelReceived, lastMidiNoteNumberReceived;
-
- bool lastSustainPedalValueReceived, lastSostenutoPedalValueReceived;
- MPEValue lastMPEValueReceived;
- std::unique_ptr<MPENote> lastNoteFinished;
-
- private:
- //==============================================================================
- void noteAdded (MPENote) override { noteAddedCallCounter++; }
-
- void notePressureChanged (MPENote) override { notePressureChangedCallCounter++; }
- void notePitchbendChanged (MPENote) override { notePitchbendChangedCallCounter++; }
- void noteTimbreChanged (MPENote) override { noteTimbreChangedCallCounter++; }
- void noteKeyStateChanged (MPENote) override { noteKeyStateChangedCallCounter++; }
-
- void noteReleased (MPENote finishedNote) override
- {
- noteReleasedCallCounter++;
- lastNoteFinished.reset (new MPENote (finishedNote));
- }
- };
-
- //==============================================================================
- void expectNote (MPENote noteToTest,
- int noteOnVelocity7Bit,
- int pressure7Bit,
- int pitchbend14Bit,
- int timbre7Bit,
- MPENote::KeyState keyState)
- {
- expect (noteToTest.isValid());
- expectEquals (noteToTest.noteOnVelocity.as7BitInt(), noteOnVelocity7Bit);
- expectEquals (noteToTest.pressure.as7BitInt(), pressure7Bit);
- expectEquals (noteToTest.pitchbend.as14BitInt(), pitchbend14Bit);
- expectEquals (noteToTest.timbre.as7BitInt(),timbre7Bit);
- expect (noteToTest.keyState == keyState);
- }
-
- void expectHasFinishedNote (const UnitTestInstrument& test,
- int channel, int noteNumber, int noteOffVelocity7Bit)
- {
- expect (test.lastNoteFinished != nullptr);
- expectEquals (int (test.lastNoteFinished->midiChannel), channel);
- expectEquals (int (test.lastNoteFinished->initialNote), noteNumber);
- expectEquals (test.lastNoteFinished->noteOffVelocity.as7BitInt(), noteOffVelocity7Bit);
- expect (test.lastNoteFinished->keyState == MPENote::off);
- }
-
- void expectDoubleWithinRelativeError (double actual, double expected, double maxRelativeError)
- {
- const double maxAbsoluteError = jmax (1.0, std::abs (expected)) * maxRelativeError;
- expect (std::abs (expected - actual) < maxAbsoluteError);
- }
-
- //==============================================================================
- MPEZoneLayout testLayout;
- };
-
- static MPEInstrumentTests MPEInstrumentUnitTests;
-
- #endif
-
- } // namespace juce
|