Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_MPEInstrument.cpp 102KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. namespace
  20. {
  21. const uint8 noLSBValueReceived = 0xff;
  22. const Range<int> allChannels { 1, 17 };
  23. }
  24. //==============================================================================
  25. MPEInstrument::MPEInstrument() noexcept
  26. {
  27. std::fill_n (lastPressureLowerBitReceivedOnChannel, 16, noLSBValueReceived);
  28. std::fill_n (lastTimbreLowerBitReceivedOnChannel, 16, noLSBValueReceived);
  29. std::fill_n (isMemberChannelSustained, 16, false);
  30. pitchbendDimension.value = &MPENote::pitchbend;
  31. pressureDimension.value = &MPENote::pressure;
  32. timbreDimension.value = &MPENote::timbre;
  33. // the default value for pressure is 0, for all other dimension it is centre (= default MPEValue)
  34. std::fill_n (pressureDimension.lastValueReceivedOnChannel, 16, MPEValue::minValue());
  35. legacyMode.isEnabled = false;
  36. legacyMode.pitchbendRange = 2;
  37. legacyMode.channelRange = allChannels;
  38. }
  39. MPEInstrument::~MPEInstrument()
  40. {
  41. }
  42. //==============================================================================
  43. MPEZoneLayout MPEInstrument::getZoneLayout() const noexcept
  44. {
  45. return zoneLayout;
  46. }
  47. void MPEInstrument::setZoneLayout (MPEZoneLayout newLayout)
  48. {
  49. releaseAllNotes();
  50. const ScopedLock sl (lock);
  51. legacyMode.isEnabled = false;
  52. zoneLayout = newLayout;
  53. }
  54. //==============================================================================
  55. void MPEInstrument::enableLegacyMode (int pitchbendRange, Range<int> channelRange)
  56. {
  57. releaseAllNotes();
  58. const ScopedLock sl (lock);
  59. legacyMode.isEnabled = true;
  60. legacyMode.pitchbendRange = pitchbendRange;
  61. legacyMode.channelRange = channelRange;
  62. zoneLayout.clearAllZones();
  63. }
  64. bool MPEInstrument::isLegacyModeEnabled() const noexcept
  65. {
  66. return legacyMode.isEnabled;
  67. }
  68. Range<int> MPEInstrument::getLegacyModeChannelRange() const noexcept
  69. {
  70. return legacyMode.channelRange;
  71. }
  72. void MPEInstrument::setLegacyModeChannelRange (Range<int> channelRange)
  73. {
  74. jassert (allChannels.contains (channelRange));
  75. releaseAllNotes();
  76. const ScopedLock sl (lock);
  77. legacyMode.channelRange = channelRange;
  78. }
  79. int MPEInstrument::getLegacyModePitchbendRange() const noexcept
  80. {
  81. return legacyMode.pitchbendRange;
  82. }
  83. void MPEInstrument::setLegacyModePitchbendRange (int pitchbendRange)
  84. {
  85. jassert (pitchbendRange >= 0 && pitchbendRange <= 96);
  86. releaseAllNotes();
  87. const ScopedLock sl (lock);
  88. legacyMode.pitchbendRange = pitchbendRange;
  89. }
  90. //==============================================================================
  91. void MPEInstrument::setPressureTrackingMode (TrackingMode modeToUse)
  92. {
  93. pressureDimension.trackingMode = modeToUse;
  94. }
  95. void MPEInstrument::setPitchbendTrackingMode (TrackingMode modeToUse)
  96. {
  97. pitchbendDimension.trackingMode = modeToUse;
  98. }
  99. void MPEInstrument::setTimbreTrackingMode (TrackingMode modeToUse)
  100. {
  101. timbreDimension.trackingMode = modeToUse;
  102. }
  103. //==============================================================================
  104. void MPEInstrument::addListener (Listener* listenerToAdd)
  105. {
  106. listeners.add (listenerToAdd);
  107. }
  108. void MPEInstrument::removeListener (Listener* listenerToRemove)
  109. {
  110. listeners.remove (listenerToRemove);
  111. }
  112. //==============================================================================
  113. void MPEInstrument::processNextMidiEvent (const MidiMessage& message)
  114. {
  115. zoneLayout.processNextMidiEvent (message);
  116. if (message.isNoteOn (true)) processMidiNoteOnMessage (message);
  117. else if (message.isNoteOff (false)) processMidiNoteOffMessage (message);
  118. else if (message.isResetAllControllers()
  119. || message.isAllNotesOff()) processMidiResetAllControllersMessage (message);
  120. else if (message.isPitchWheel()) processMidiPitchWheelMessage (message);
  121. else if (message.isChannelPressure()) processMidiChannelPressureMessage (message);
  122. else if (message.isController()) processMidiControllerMessage (message);
  123. else if (message.isAftertouch()) processMidiAfterTouchMessage (message);
  124. }
  125. //==============================================================================
  126. void MPEInstrument::processMidiNoteOnMessage (const MidiMessage& message)
  127. {
  128. // Note: If a note-on with velocity = 0 is used to convey a note-off,
  129. // then the actual note-off velocity is not known. In this case,
  130. // the MPE convention is to use note-off velocity = 64.
  131. if (message.getVelocity() == 0)
  132. {
  133. noteOff (message.getChannel(),
  134. message.getNoteNumber(),
  135. MPEValue::from7BitInt (64));
  136. }
  137. else
  138. {
  139. noteOn (message.getChannel(),
  140. message.getNoteNumber(),
  141. MPEValue::from7BitInt (message.getVelocity()));
  142. }
  143. }
  144. //==============================================================================
  145. void MPEInstrument::processMidiNoteOffMessage (const MidiMessage& message)
  146. {
  147. noteOff (message.getChannel(),
  148. message.getNoteNumber(),
  149. MPEValue::from7BitInt (message.getVelocity()));
  150. }
  151. //==============================================================================
  152. void MPEInstrument::processMidiPitchWheelMessage (const MidiMessage& message)
  153. {
  154. pitchbend (message.getChannel(),
  155. MPEValue::from14BitInt (message.getPitchWheelValue()));
  156. }
  157. //==============================================================================
  158. void MPEInstrument::processMidiChannelPressureMessage (const MidiMessage& message)
  159. {
  160. pressure (message.getChannel(),
  161. MPEValue::from7BitInt (message.getChannelPressureValue()));
  162. }
  163. //==============================================================================
  164. void MPEInstrument::processMidiControllerMessage (const MidiMessage& message)
  165. {
  166. switch (message.getControllerNumber())
  167. {
  168. case 64: sustainPedal (message.getChannel(), message.isSustainPedalOn()); break;
  169. case 66: sostenutoPedal (message.getChannel(), message.isSostenutoPedalOn()); break;
  170. case 70: handlePressureMSB (message.getChannel(), message.getControllerValue()); break;
  171. case 74: handleTimbreMSB (message.getChannel(), message.getControllerValue()); break;
  172. case 102: handlePressureLSB (message.getChannel(), message.getControllerValue()); break;
  173. case 106: handleTimbreLSB (message.getChannel(), message.getControllerValue()); break;
  174. default: break;
  175. }
  176. }
  177. //==============================================================================
  178. void MPEInstrument::processMidiResetAllControllersMessage (const MidiMessage& message)
  179. {
  180. // in MPE mode, "reset all controllers" is per-zone and expected on the master channel;
  181. // in legacy mode, it is per MIDI channel (within the channel range used).
  182. if (legacyMode.isEnabled && legacyMode.channelRange.contains (message.getChannel()))
  183. {
  184. for (auto i = notes.size(); --i >= 0;)
  185. {
  186. auto& note = notes.getReference (i);
  187. if (note.midiChannel == message.getChannel())
  188. {
  189. note.keyState = MPENote::off;
  190. note.noteOffVelocity = MPEValue::from7BitInt (64); // some reasonable number
  191. listeners.call ([&] (Listener& l) { l.noteReleased (note); });
  192. notes.remove (i);
  193. }
  194. }
  195. }
  196. else if (isMasterChannel (message.getChannel()))
  197. {
  198. auto zone = (message.getChannel() == 1 ? zoneLayout.getLowerZone()
  199. : zoneLayout.getUpperZone());
  200. for (auto i = notes.size(); --i >= 0;)
  201. {
  202. auto& note = notes.getReference (i);
  203. if (zone.isUsing (note.midiChannel))
  204. {
  205. note.keyState = MPENote::off;
  206. note.noteOffVelocity = MPEValue::from7BitInt (64); // some reasonable number
  207. listeners.call ([&] (Listener& l) { l.noteReleased (note); });
  208. notes.remove (i);
  209. }
  210. }
  211. }
  212. }
  213. void MPEInstrument::processMidiAfterTouchMessage (const MidiMessage& message)
  214. {
  215. if (! isMasterChannel (message.getChannel()))
  216. return;
  217. polyAftertouch (message.getChannel(), message.getNoteNumber(),
  218. MPEValue::from7BitInt (message.getAfterTouchValue()));
  219. }
  220. //==============================================================================
  221. void MPEInstrument::handlePressureMSB (int midiChannel, int value) noexcept
  222. {
  223. auto lsb = lastPressureLowerBitReceivedOnChannel[midiChannel - 1];
  224. pressure (midiChannel, lsb == noLSBValueReceived ? MPEValue::from7BitInt (value)
  225. : MPEValue::from14BitInt (lsb + (value << 7)));
  226. }
  227. void MPEInstrument::handlePressureLSB (int midiChannel, int value) noexcept
  228. {
  229. lastPressureLowerBitReceivedOnChannel[midiChannel - 1] = uint8 (value);
  230. }
  231. void MPEInstrument::handleTimbreMSB (int midiChannel, int value) noexcept
  232. {
  233. auto lsb = lastTimbreLowerBitReceivedOnChannel[midiChannel - 1];
  234. timbre (midiChannel, lsb == noLSBValueReceived ? MPEValue::from7BitInt (value)
  235. : MPEValue::from14BitInt (lsb + (value << 7)));
  236. }
  237. void MPEInstrument::handleTimbreLSB (int midiChannel, int value) noexcept
  238. {
  239. lastTimbreLowerBitReceivedOnChannel[midiChannel - 1] = uint8 (value);
  240. }
  241. //==============================================================================
  242. void MPEInstrument::noteOn (int midiChannel,
  243. int midiNoteNumber,
  244. MPEValue midiNoteOnVelocity)
  245. {
  246. if (! isUsingChannel (midiChannel))
  247. return;
  248. MPENote newNote (midiChannel,
  249. midiNoteNumber,
  250. midiNoteOnVelocity,
  251. getInitialValueForNewNote (midiChannel, pitchbendDimension),
  252. getInitialValueForNewNote (midiChannel, pressureDimension),
  253. getInitialValueForNewNote (midiChannel, timbreDimension),
  254. isMemberChannelSustained[midiChannel - 1] ? MPENote::keyDownAndSustained : MPENote::keyDown);
  255. const ScopedLock sl (lock);
  256. updateNoteTotalPitchbend (newNote);
  257. if (auto* alreadyPlayingNote = getNotePtr (midiChannel, midiNoteNumber))
  258. {
  259. // pathological case: second note-on received for same note -> retrigger it
  260. alreadyPlayingNote->keyState = MPENote::off;
  261. alreadyPlayingNote->noteOffVelocity = MPEValue::from7BitInt (64); // some reasonable number
  262. listeners.call ([=] (Listener& l) { l.noteReleased (*alreadyPlayingNote); });
  263. notes.remove (alreadyPlayingNote);
  264. }
  265. notes.add (newNote);
  266. listeners.call ([&] (Listener& l) { l.noteAdded (newNote); });
  267. }
  268. //==============================================================================
  269. void MPEInstrument::noteOff (int midiChannel,
  270. int midiNoteNumber,
  271. MPEValue midiNoteOffVelocity)
  272. {
  273. if (notes.isEmpty() || ! isUsingChannel (midiChannel))
  274. return;
  275. const ScopedLock sl (lock);
  276. if (auto* note = getNotePtr (midiChannel, midiNoteNumber))
  277. {
  278. note->keyState = (note->keyState == MPENote::keyDownAndSustained) ? MPENote::sustained : MPENote::off;
  279. note->noteOffVelocity = midiNoteOffVelocity;
  280. // If no more notes are playing on this channel in mpe mode, reset the dimension values
  281. if (! legacyMode.isEnabled && getLastNotePlayedPtr (midiChannel) == nullptr)
  282. {
  283. pressureDimension.lastValueReceivedOnChannel[midiChannel - 1] = MPEValue::minValue();
  284. pitchbendDimension.lastValueReceivedOnChannel[midiChannel - 1] = MPEValue::centreValue();
  285. timbreDimension.lastValueReceivedOnChannel[midiChannel - 1] = MPEValue::centreValue();
  286. }
  287. if (note->keyState == MPENote::off)
  288. {
  289. listeners.call ([=] (Listener& l) { l.noteReleased (*note); });
  290. notes.remove (note);
  291. }
  292. else
  293. {
  294. listeners.call ([=] (Listener& l) { l.noteKeyStateChanged (*note); });
  295. }
  296. }
  297. }
  298. //==============================================================================
  299. void MPEInstrument::pitchbend (int midiChannel, MPEValue value)
  300. {
  301. const ScopedLock sl (lock);
  302. updateDimension (midiChannel, pitchbendDimension, value);
  303. }
  304. void MPEInstrument::pressure (int midiChannel, MPEValue value)
  305. {
  306. const ScopedLock sl (lock);
  307. updateDimension (midiChannel, pressureDimension, value);
  308. }
  309. void MPEInstrument::timbre (int midiChannel, MPEValue value)
  310. {
  311. const ScopedLock sl (lock);
  312. updateDimension (midiChannel, timbreDimension, value);
  313. }
  314. void MPEInstrument::polyAftertouch (int midiChannel, int midiNoteNumber, MPEValue value)
  315. {
  316. const ScopedLock sl (lock);
  317. for (auto i = notes.size(); --i >= 0;)
  318. {
  319. auto& note = notes.getReference (i);
  320. if (note.midiChannel == midiChannel
  321. && note.initialNote == midiNoteNumber
  322. && pressureDimension.getValue (note) != value)
  323. {
  324. pressureDimension.getValue (note) = value;
  325. callListenersDimensionChanged (note, pressureDimension);
  326. }
  327. }
  328. }
  329. MPEValue MPEInstrument::getInitialValueForNewNote (int midiChannel, MPEDimension& dimension) const
  330. {
  331. if (! legacyMode.isEnabled && getLastNotePlayedPtr (midiChannel) != nullptr)
  332. return &dimension == &pressureDimension ? MPEValue::minValue() : MPEValue::centreValue();
  333. return dimension.lastValueReceivedOnChannel[midiChannel - 1];
  334. }
  335. //==============================================================================
  336. void MPEInstrument::updateDimension (int midiChannel, MPEDimension& dimension, MPEValue value)
  337. {
  338. dimension.lastValueReceivedOnChannel[midiChannel - 1] = value;
  339. if (notes.isEmpty())
  340. return;
  341. if (isMemberChannel (midiChannel))
  342. {
  343. if (dimension.trackingMode == allNotesOnChannel)
  344. {
  345. for (auto i = notes.size(); --i >= 0;)
  346. {
  347. auto& note = notes.getReference (i);
  348. if (note.midiChannel == midiChannel)
  349. updateDimensionForNote (note, dimension, value);
  350. }
  351. }
  352. else
  353. {
  354. if (auto* note = getNotePtr (midiChannel, dimension.trackingMode))
  355. updateDimensionForNote (*note, dimension, value);
  356. }
  357. }
  358. else if (isMasterChannel (midiChannel))
  359. {
  360. updateDimensionMaster (midiChannel == 1, dimension, value);
  361. }
  362. }
  363. //==============================================================================
  364. void MPEInstrument::updateDimensionMaster (bool isLowerZone, MPEDimension& dimension, MPEValue value)
  365. {
  366. auto zone = (isLowerZone ? zoneLayout.getLowerZone()
  367. : zoneLayout.getUpperZone());
  368. if (! zone.isActive())
  369. return;
  370. for (auto i = notes.size(); --i >= 0;)
  371. {
  372. auto& note = notes.getReference (i);
  373. if (! zone.isUsing (note.midiChannel))
  374. continue;
  375. if (&dimension == &pitchbendDimension)
  376. {
  377. // master pitchbend is a special case: we don't change the note's own pitchbend,
  378. // instead we have to update its total (master + note) pitchbend.
  379. updateNoteTotalPitchbend (note);
  380. listeners.call ([&] (Listener& l) { l.notePitchbendChanged (note); });
  381. }
  382. else if (dimension.getValue (note) != value)
  383. {
  384. dimension.getValue (note) = value;
  385. callListenersDimensionChanged (note, dimension);
  386. }
  387. }
  388. }
  389. //==============================================================================
  390. void MPEInstrument::updateDimensionForNote (MPENote& note, MPEDimension& dimension, MPEValue value)
  391. {
  392. if (dimension.getValue (note) != value)
  393. {
  394. dimension.getValue (note) = value;
  395. if (&dimension == &pitchbendDimension)
  396. updateNoteTotalPitchbend (note);
  397. callListenersDimensionChanged (note, dimension);
  398. }
  399. }
  400. //==============================================================================
  401. void MPEInstrument::callListenersDimensionChanged (const MPENote& note, const MPEDimension& dimension)
  402. {
  403. if (&dimension == &pressureDimension) { listeners.call ([&] (Listener& l) { l.notePressureChanged (note); }); return; }
  404. if (&dimension == &timbreDimension) { listeners.call ([&] (Listener& l) { l.noteTimbreChanged (note); }); return; }
  405. if (&dimension == &pitchbendDimension) { listeners.call ([&] (Listener& l) { l.notePitchbendChanged (note); }); return; }
  406. }
  407. //==============================================================================
  408. void MPEInstrument::updateNoteTotalPitchbend (MPENote& note)
  409. {
  410. if (legacyMode.isEnabled)
  411. {
  412. note.totalPitchbendInSemitones = note.pitchbend.asSignedFloat() * legacyMode.pitchbendRange;
  413. }
  414. else
  415. {
  416. auto zone = zoneLayout.getLowerZone();
  417. if (! zone.isActive() || ! zone.isUsing (note.midiChannel))
  418. {
  419. auto upperZone = zoneLayout.getUpperZone();
  420. if (upperZone.isActive() && upperZone.isUsing (note.midiChannel))
  421. {
  422. zone = upperZone;
  423. }
  424. else
  425. {
  426. // this note doesn't belong to any zone!
  427. jassertfalse;
  428. return;
  429. }
  430. }
  431. auto notePitchbendInSemitones = 0.0f;
  432. if (zone.isUsingChannelAsMemberChannel (note.midiChannel))
  433. notePitchbendInSemitones = note.pitchbend.asSignedFloat() * zone.perNotePitchbendRange;
  434. auto masterPitchbendInSemitones = pitchbendDimension.lastValueReceivedOnChannel[zone.getMasterChannel() - 1]
  435. .asSignedFloat()
  436. * zone.masterPitchbendRange;
  437. note.totalPitchbendInSemitones = notePitchbendInSemitones + masterPitchbendInSemitones;
  438. }
  439. }
  440. //==============================================================================
  441. void MPEInstrument::sustainPedal (int midiChannel, bool isDown)
  442. {
  443. const ScopedLock sl (lock);
  444. handleSustainOrSostenuto (midiChannel, isDown, false);
  445. }
  446. void MPEInstrument::sostenutoPedal (int midiChannel, bool isDown)
  447. {
  448. const ScopedLock sl (lock);
  449. handleSustainOrSostenuto (midiChannel, isDown, true);
  450. }
  451. //==============================================================================
  452. void MPEInstrument::handleSustainOrSostenuto (int midiChannel, bool isDown, bool isSostenuto)
  453. {
  454. // in MPE mode, sustain/sostenuto is per-zone and expected on the master channel;
  455. // in legacy mode, sustain/sostenuto is per MIDI channel (within the channel range used).
  456. if (legacyMode.isEnabled ? (! legacyMode.channelRange.contains (midiChannel)) : (! isMasterChannel (midiChannel)))
  457. return;
  458. auto zone = (midiChannel == 1 ? zoneLayout.getLowerZone()
  459. : zoneLayout.getUpperZone());
  460. for (auto i = notes.size(); --i >= 0;)
  461. {
  462. auto& note = notes.getReference (i);
  463. if (legacyMode.isEnabled ? (note.midiChannel == midiChannel) : zone.isUsing (note.midiChannel))
  464. {
  465. if (note.keyState == MPENote::keyDown && isDown)
  466. note.keyState = MPENote::keyDownAndSustained;
  467. else if (note.keyState == MPENote::sustained && ! isDown)
  468. note.keyState = MPENote::off;
  469. else if (note.keyState == MPENote::keyDownAndSustained && ! isDown)
  470. note.keyState = MPENote::keyDown;
  471. if (note.keyState == MPENote::off)
  472. {
  473. listeners.call ([&] (Listener& l) { l.noteReleased (note); });
  474. notes.remove (i);
  475. }
  476. else
  477. {
  478. listeners.call ([&] (Listener& l) { l.noteKeyStateChanged (note); });
  479. }
  480. }
  481. }
  482. if (! isSostenuto)
  483. {
  484. isMemberChannelSustained[midiChannel - 1] = isDown;
  485. if (! legacyMode.isEnabled)
  486. {
  487. if (zone.isLowerZone())
  488. for (auto i = zone.getFirstMemberChannel(); i <= zone.getLastMemberChannel(); ++i)
  489. isMemberChannelSustained[i - 1] = isDown;
  490. else
  491. for (auto i = zone.getFirstMemberChannel(); i >= zone.getLastMemberChannel(); --i)
  492. isMemberChannelSustained[i - 1] = isDown;
  493. }
  494. }
  495. }
  496. //==============================================================================
  497. bool MPEInstrument::isMemberChannel (int midiChannel) const noexcept
  498. {
  499. if (legacyMode.isEnabled)
  500. return legacyMode.channelRange.contains (midiChannel);
  501. return zoneLayout.getLowerZone().isUsingChannelAsMemberChannel (midiChannel)
  502. || zoneLayout.getUpperZone().isUsingChannelAsMemberChannel (midiChannel);
  503. }
  504. bool MPEInstrument::isMasterChannel (int midiChannel) const noexcept
  505. {
  506. if (legacyMode.isEnabled)
  507. return false;
  508. const auto lowerZone = zoneLayout.getLowerZone();
  509. const auto upperZone = zoneLayout.getUpperZone();
  510. return (lowerZone.isActive() && midiChannel == lowerZone.getMasterChannel())
  511. || (upperZone.isActive() && midiChannel == upperZone.getMasterChannel());
  512. }
  513. bool MPEInstrument::isUsingChannel (int midiChannel) const noexcept
  514. {
  515. if (legacyMode.isEnabled)
  516. return legacyMode.channelRange.contains (midiChannel);
  517. return zoneLayout.getLowerZone().isUsing (midiChannel)
  518. || zoneLayout.getUpperZone().isUsing (midiChannel);
  519. }
  520. //==============================================================================
  521. int MPEInstrument::getNumPlayingNotes() const noexcept
  522. {
  523. return notes.size();
  524. }
  525. MPENote MPEInstrument::getNote (int midiChannel, int midiNoteNumber) const noexcept
  526. {
  527. if (auto* note = getNotePtr (midiChannel, midiNoteNumber))
  528. return *note;
  529. return {};
  530. }
  531. MPENote MPEInstrument::getNote (int index) const noexcept
  532. {
  533. return notes[index];
  534. }
  535. //==============================================================================
  536. MPENote MPEInstrument::getMostRecentNote (int midiChannel) const noexcept
  537. {
  538. if (auto* note = getLastNotePlayedPtr (midiChannel))
  539. return *note;
  540. return {};
  541. }
  542. MPENote MPEInstrument::getMostRecentNoteOtherThan (MPENote otherThanThisNote) const noexcept
  543. {
  544. for (auto i = notes.size(); --i >= 0;)
  545. {
  546. auto& note = notes.getReference (i);
  547. if (note != otherThanThisNote)
  548. return note;
  549. }
  550. return {};
  551. }
  552. //==============================================================================
  553. const MPENote* MPEInstrument::getNotePtr (int midiChannel, int midiNoteNumber) const noexcept
  554. {
  555. for (int i = 0; i < notes.size(); ++i)
  556. {
  557. auto& note = notes.getReference (i);
  558. if (note.midiChannel == midiChannel && note.initialNote == midiNoteNumber)
  559. return &note;
  560. }
  561. return nullptr;
  562. }
  563. MPENote* MPEInstrument::getNotePtr (int midiChannel, int midiNoteNumber) noexcept
  564. {
  565. return const_cast<MPENote*> (static_cast<const MPEInstrument&> (*this).getNotePtr (midiChannel, midiNoteNumber));
  566. }
  567. //==============================================================================
  568. const MPENote* MPEInstrument::getNotePtr (int midiChannel, TrackingMode mode) const noexcept
  569. {
  570. // for the "all notes" tracking mode, this method can never possibly
  571. // work because it returns 0 or 1 note but there might be more than one!
  572. jassert (mode != allNotesOnChannel);
  573. if (mode == lastNotePlayedOnChannel) return getLastNotePlayedPtr (midiChannel);
  574. if (mode == lowestNoteOnChannel) return getLowestNotePtr (midiChannel);
  575. if (mode == highestNoteOnChannel) return getHighestNotePtr (midiChannel);
  576. return nullptr;
  577. }
  578. MPENote* MPEInstrument::getNotePtr (int midiChannel, TrackingMode mode) noexcept
  579. {
  580. return const_cast<MPENote*> (static_cast<const MPEInstrument&> (*this).getNotePtr (midiChannel, mode));
  581. }
  582. //==============================================================================
  583. const MPENote* MPEInstrument::getLastNotePlayedPtr (int midiChannel) const noexcept
  584. {
  585. for (auto i = notes.size(); --i >= 0;)
  586. {
  587. auto& note = notes.getReference (i);
  588. if (note.midiChannel == midiChannel
  589. && (note.keyState == MPENote::keyDown || note.keyState == MPENote::keyDownAndSustained))
  590. return &note;
  591. }
  592. return nullptr;
  593. }
  594. MPENote* MPEInstrument::getLastNotePlayedPtr (int midiChannel) noexcept
  595. {
  596. return const_cast<MPENote*> (static_cast<const MPEInstrument&> (*this).getLastNotePlayedPtr (midiChannel));
  597. }
  598. //==============================================================================
  599. const MPENote* MPEInstrument::getHighestNotePtr (int midiChannel) const noexcept
  600. {
  601. int initialNoteMax = -1;
  602. const MPENote* result = nullptr;
  603. for (auto i = notes.size(); --i >= 0;)
  604. {
  605. auto& note = notes.getReference (i);
  606. if (note.midiChannel == midiChannel
  607. && (note.keyState == MPENote::keyDown || note.keyState == MPENote::keyDownAndSustained)
  608. && note.initialNote > initialNoteMax)
  609. {
  610. result = &note;
  611. initialNoteMax = note.initialNote;
  612. }
  613. }
  614. return result;
  615. }
  616. MPENote* MPEInstrument::getHighestNotePtr (int midiChannel) noexcept
  617. {
  618. return const_cast<MPENote*> (static_cast<const MPEInstrument&> (*this).getHighestNotePtr (midiChannel));
  619. }
  620. const MPENote* MPEInstrument::getLowestNotePtr (int midiChannel) const noexcept
  621. {
  622. int initialNoteMin = 128;
  623. const MPENote* result = nullptr;
  624. for (auto i = notes.size(); --i >= 0;)
  625. {
  626. auto& note = notes.getReference (i);
  627. if (note.midiChannel == midiChannel
  628. && (note.keyState == MPENote::keyDown || note.keyState == MPENote::keyDownAndSustained)
  629. && note.initialNote < initialNoteMin)
  630. {
  631. result = &note;
  632. initialNoteMin = note.initialNote;
  633. }
  634. }
  635. return result;
  636. }
  637. MPENote* MPEInstrument::getLowestNotePtr (int midiChannel) noexcept
  638. {
  639. return const_cast<MPENote*> (static_cast<const MPEInstrument&> (*this).getLowestNotePtr (midiChannel));
  640. }
  641. //==============================================================================
  642. void MPEInstrument::releaseAllNotes()
  643. {
  644. const ScopedLock sl (lock);
  645. for (auto i = notes.size(); --i >= 0;)
  646. {
  647. auto& note = notes.getReference (i);
  648. note.keyState = MPENote::off;
  649. note.noteOffVelocity = MPEValue::from7BitInt (64); // some reasonable number
  650. listeners.call ([&] (Listener& l) { l.noteReleased (note); });
  651. }
  652. notes.clear();
  653. }
  654. //==============================================================================
  655. //==============================================================================
  656. #if JUCE_UNIT_TESTS
  657. class MPEInstrumentTests : public UnitTest
  658. {
  659. public:
  660. MPEInstrumentTests()
  661. : UnitTest ("MPEInstrument class", UnitTestCategories::midi)
  662. {
  663. // using lower and upper MPE zones with the following layout for testing
  664. //
  665. // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  666. // * ...................| |........................ *
  667. testLayout.setLowerZone (5);
  668. testLayout.setUpperZone (6);
  669. }
  670. void runTest() override
  671. {
  672. beginTest ("initial zone layout");
  673. {
  674. MPEInstrument test;
  675. expect (! test.getZoneLayout().getLowerZone().isActive());
  676. expect (! test.getZoneLayout().getUpperZone().isActive());
  677. }
  678. beginTest ("get/setZoneLayout");
  679. {
  680. MPEInstrument test;
  681. test.setZoneLayout (testLayout);
  682. auto newLayout = test.getZoneLayout();
  683. expect (test.getZoneLayout().getLowerZone().isActive());
  684. expect (test.getZoneLayout().getUpperZone().isActive());
  685. expectEquals (newLayout.getLowerZone().getMasterChannel(), 1);
  686. expectEquals (newLayout.getLowerZone().numMemberChannels, 5);
  687. expectEquals (newLayout.getUpperZone().getMasterChannel(), 16);
  688. expectEquals (newLayout.getUpperZone().numMemberChannels, 6);
  689. }
  690. beginTest ("noteOn / noteOff");
  691. {
  692. {
  693. MPEInstrument test;
  694. test.setZoneLayout (testLayout);
  695. expectEquals (test.getNumPlayingNotes(), 0);
  696. }
  697. {
  698. UnitTestInstrument test;
  699. test.setZoneLayout (testLayout);
  700. // note-on on unused channel - ignore
  701. test.noteOn (7, 60, MPEValue::from7BitInt (100));
  702. expectEquals (test.getNumPlayingNotes(), 0);
  703. expectEquals (test.noteAddedCallCounter, 0);
  704. // note-on on member channel - create new note
  705. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  706. expectEquals (test.getNumPlayingNotes(), 1);
  707. expectEquals (test.noteAddedCallCounter, 1);
  708. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  709. // note-off
  710. test.noteOff (3, 60, MPEValue::from7BitInt (33));
  711. expectEquals (test.getNumPlayingNotes(), 0);
  712. expectEquals (test.noteReleasedCallCounter, 1);
  713. expectHasFinishedNote (test, 3, 60, 33);
  714. // note-on on master channel - create new note
  715. test.noteOn (1, 62, MPEValue::from7BitInt (100));
  716. expectEquals (test.getNumPlayingNotes(), 1);
  717. expectEquals (test.noteAddedCallCounter, 2);
  718. expectNote (test.getNote (1, 62), 100, 0, 8192, 64, MPENote::keyDown);
  719. // note-off
  720. test.noteOff (1, 62, MPEValue::from7BitInt (33));
  721. expectEquals (test.getNumPlayingNotes(), 0);
  722. expectEquals (test.noteReleasedCallCounter, 2);
  723. expectHasFinishedNote (test, 1, 62, 33);
  724. }
  725. {
  726. UnitTestInstrument test;
  727. test.setZoneLayout (testLayout);
  728. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  729. // note off with non-matching note number shouldn't do anything
  730. test.noteOff (3, 61, MPEValue::from7BitInt (33));
  731. expectEquals (test.getNumPlayingNotes(), 1);
  732. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  733. expectEquals (test.noteReleasedCallCounter, 0);
  734. // note off with non-matching midi channel shouldn't do anything
  735. test.noteOff (2, 60, MPEValue::from7BitInt (33));
  736. expectEquals (test.getNumPlayingNotes(), 1);
  737. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  738. expectEquals (test.noteReleasedCallCounter, 0);
  739. }
  740. {
  741. // can have multiple notes on the same channel
  742. UnitTestInstrument test;
  743. test.setZoneLayout (testLayout);
  744. test.noteOn (3, 0, MPEValue::from7BitInt (100));
  745. test.noteOn (3, 1, MPEValue::from7BitInt (100));
  746. test.noteOn (3, 2, MPEValue::from7BitInt (100));
  747. expectEquals (test.getNumPlayingNotes(), 3);
  748. expectNote (test.getNote (3, 0), 100, 0, 8192, 64, MPENote::keyDown);
  749. expectNote (test.getNote (3, 1), 100, 0, 8192, 64, MPENote::keyDown);
  750. expectNote (test.getNote (3, 2), 100, 0, 8192, 64, MPENote::keyDown);
  751. }
  752. {
  753. // pathological case: second note-on for same note should retrigger it.
  754. UnitTestInstrument test;
  755. test.setZoneLayout (testLayout);
  756. test.noteOn (3, 0, MPEValue::from7BitInt (100));
  757. test.noteOn (3, 0, MPEValue::from7BitInt (60));
  758. expectEquals (test.getNumPlayingNotes(), 1);
  759. expectNote (test.getNote (3, 0), 60, 0, 8192, 64, MPENote::keyDown);
  760. }
  761. }
  762. beginTest ("noteReleased after setZoneLayout");
  763. {
  764. UnitTestInstrument test;
  765. test.setZoneLayout (testLayout);
  766. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  767. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  768. test.noteOn (4, 61, MPEValue::from7BitInt (100));
  769. expectEquals (test.getNumPlayingNotes(), 3);
  770. expectEquals (test.noteReleasedCallCounter, 0);
  771. test.setZoneLayout (testLayout);
  772. expectEquals (test.getNumPlayingNotes(), 0);
  773. expectEquals (test.noteReleasedCallCounter, 3);
  774. }
  775. beginTest ("releaseAllNotes");
  776. {
  777. UnitTestInstrument test;
  778. test.setZoneLayout (testLayout);
  779. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  780. test.noteOn (4, 61, MPEValue::from7BitInt (100));
  781. test.noteOn (15, 62, MPEValue::from7BitInt (100));
  782. expectEquals (test.getNumPlayingNotes(), 3);
  783. test.releaseAllNotes();
  784. expectEquals (test.getNumPlayingNotes(), 0);
  785. }
  786. beginTest ("sustainPedal");
  787. {
  788. UnitTestInstrument test;
  789. test.setZoneLayout (testLayout);
  790. test.noteOn (3, 60, MPEValue::from7BitInt (100)); // note in lower zone
  791. test.noteOn (10, 60, MPEValue::from7BitInt (100)); // note in upper zone
  792. // sustain pedal on per-note channel shouldn't do anything.
  793. test.sustainPedal (3, true);
  794. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  795. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  796. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  797. expectEquals (test.noteKeyStateChangedCallCounter, 0);
  798. // sustain pedal on non-zone channel shouldn't do anything either.
  799. test.sustainPedal (7, true);
  800. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  801. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  802. expectEquals (test.noteKeyStateChangedCallCounter, 0);
  803. // sustain pedal on master channel should sustain notes on _that_ zone.
  804. test.sustainPedal (1, true);
  805. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDownAndSustained);
  806. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  807. expectEquals (test.noteKeyStateChangedCallCounter, 1);
  808. // release
  809. test.sustainPedal (1, false);
  810. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  811. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  812. expectEquals (test.noteKeyStateChangedCallCounter, 2);
  813. // should also sustain new notes added after the press
  814. test.sustainPedal (1, true);
  815. expectEquals (test.noteKeyStateChangedCallCounter, 3);
  816. test.noteOn (4, 51, MPEValue::from7BitInt (100));
  817. expectNote (test.getNote (4, 51), 100, 0, 8192, 64, MPENote::keyDownAndSustained);
  818. expectEquals (test.noteKeyStateChangedCallCounter, 3);
  819. // ...but only if that sustain came on the master channel of that zone!
  820. test.sustainPedal (11, true);
  821. test.noteOn (11, 52, MPEValue::from7BitInt (100));
  822. expectNote (test.getNote (11, 52), 100, 0, 8192, 64, MPENote::keyDown);
  823. test.noteOff (11, 52, MPEValue::from7BitInt (100));
  824. expectEquals (test.noteReleasedCallCounter, 1);
  825. // note-off should not turn off sustained notes inside the same zone
  826. test.noteOff (3, 60, MPEValue::from7BitInt (100));
  827. test.noteOff (4, 51, MPEValue::from7BitInt (100));
  828. test.noteOff (10, 60, MPEValue::from7BitInt (100)); // not affected!
  829. expectEquals (test.getNumPlayingNotes(), 2);
  830. expectEquals (test.noteReleasedCallCounter, 2);
  831. expectEquals (test.noteKeyStateChangedCallCounter, 5);
  832. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::sustained);
  833. expectNote (test.getNote (4, 51), 100, 0, 8192, 64, MPENote::sustained);
  834. // notes should be turned off when pedal is released
  835. test.sustainPedal (1, false);
  836. expectEquals (test.getNumPlayingNotes(), 0);
  837. expectEquals (test.noteReleasedCallCounter, 4);
  838. }
  839. beginTest ("sostenutoPedal");
  840. {
  841. UnitTestInstrument test;
  842. test.setZoneLayout (testLayout);
  843. test.noteOn (3, 60, MPEValue::from7BitInt (100)); // note in lower zone
  844. test.noteOn (10, 60, MPEValue::from7BitInt (100)); // note in upper zone
  845. // sostenuto pedal on per-note channel shouldn't do anything.
  846. test.sostenutoPedal (3, true);
  847. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  848. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  849. expectEquals (test.noteKeyStateChangedCallCounter, 0);
  850. // sostenuto pedal on non-zone channel shouldn't do anything either.
  851. test.sostenutoPedal (9, true);
  852. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  853. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  854. expectEquals (test.noteKeyStateChangedCallCounter, 0);
  855. // sostenuto pedal on master channel should sustain notes on *that* zone.
  856. test.sostenutoPedal (1, true);
  857. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDownAndSustained);
  858. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  859. expectEquals (test.noteKeyStateChangedCallCounter, 1);
  860. // release
  861. test.sostenutoPedal (1, false);
  862. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  863. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  864. expectEquals (test.noteKeyStateChangedCallCounter, 2);
  865. // should only sustain notes turned on *before* the press (difference to sustain pedal)
  866. test.sostenutoPedal (1, true);
  867. expectEquals (test.noteKeyStateChangedCallCounter, 3);
  868. test.noteOn (4, 51, MPEValue::from7BitInt (100));
  869. expectEquals (test.getNumPlayingNotes(), 3);
  870. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDownAndSustained);
  871. expectNote (test.getNote (4, 51), 100, 0, 8192, 64, MPENote::keyDown);
  872. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  873. expectEquals (test.noteKeyStateChangedCallCounter, 3);
  874. // note-off should not turn off sustained notes inside the same zone,
  875. // but only if they were turned on *before* the sostenuto pedal (difference to sustain pedal)
  876. test.noteOff (3, 60, MPEValue::from7BitInt (100));
  877. test.noteOff (4, 51, MPEValue::from7BitInt (100));
  878. test.noteOff (10, 60, MPEValue::from7BitInt (100)); // not affected!
  879. expectEquals (test.getNumPlayingNotes(), 1);
  880. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::sustained);
  881. expectEquals (test.noteReleasedCallCounter, 2);
  882. expectEquals (test.noteKeyStateChangedCallCounter, 4);
  883. // notes should be turned off when pedal is released
  884. test.sustainPedal (1, false);
  885. expectEquals (test.getNumPlayingNotes(), 0);
  886. expectEquals (test.noteReleasedCallCounter, 3);
  887. }
  888. beginTest ("getMostRecentNote");
  889. {
  890. MPEInstrument test;
  891. test.setZoneLayout (testLayout);
  892. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  893. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  894. {
  895. auto note = test.getMostRecentNote (2);
  896. expect (! note.isValid());
  897. }
  898. {
  899. auto note = test.getMostRecentNote (3);
  900. expect (note.isValid());
  901. expectEquals (int (note.midiChannel), 3);
  902. expectEquals (int (note.initialNote), 61);
  903. }
  904. test.sustainPedal (1, true);
  905. test.noteOff (3, 61, MPEValue::from7BitInt (100));
  906. {
  907. auto note = test.getMostRecentNote (3);
  908. expect (note.isValid());
  909. expectEquals (int (note.midiChannel), 3);
  910. expectEquals (int (note.initialNote), 60);
  911. }
  912. test.sustainPedal (1, false);
  913. test.noteOff (3, 60, MPEValue::from7BitInt (100));
  914. {
  915. auto note = test.getMostRecentNote (3);
  916. expect (! note.isValid());
  917. }
  918. }
  919. beginTest ("getMostRecentNoteOtherThan");
  920. {
  921. MPENote testNote (3, 60,
  922. MPEValue::centreValue(), MPEValue::centreValue(),
  923. MPEValue::centreValue(), MPEValue::centreValue());
  924. {
  925. // case 1: the note to exclude is not the most recent one.
  926. MPEInstrument test;
  927. test.setZoneLayout (testLayout);
  928. expect (! test.getMostRecentNoteOtherThan (testNote).isValid());
  929. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  930. expect (! test.getMostRecentNoteOtherThan (testNote).isValid());
  931. test.noteOn (4, 61, MPEValue::from7BitInt (100));
  932. expect (test.getMostRecentNoteOtherThan (testNote).isValid());
  933. expect (test.getMostRecentNoteOtherThan (testNote).midiChannel == 4);
  934. expect (test.getMostRecentNoteOtherThan (testNote).initialNote == 61);
  935. }
  936. {
  937. // case 2: the note to exclude is the most recent one.
  938. MPEInstrument test;
  939. test.setZoneLayout (testLayout);
  940. expect (! test.getMostRecentNoteOtherThan (testNote).isValid());
  941. test.noteOn (4, 61, MPEValue::from7BitInt (100));
  942. expect (test.getMostRecentNoteOtherThan (testNote).isValid());
  943. expect (test.getMostRecentNoteOtherThan (testNote).midiChannel == 4);
  944. expect (test.getMostRecentNoteOtherThan (testNote).initialNote == 61);
  945. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  946. expect (test.getMostRecentNoteOtherThan (testNote).isValid());
  947. expect (test.getMostRecentNoteOtherThan (testNote).midiChannel == 4);
  948. expect (test.getMostRecentNoteOtherThan (testNote).initialNote == 61);
  949. }
  950. }
  951. beginTest ("pressure");
  952. {
  953. {
  954. UnitTestInstrument test;
  955. test.setZoneLayout (testLayout);
  956. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  957. test.noteOn (4, 60, MPEValue::from7BitInt (100));
  958. test.noteOn (10, 60, MPEValue::from7BitInt (100));
  959. // applying pressure on a per-note channel should modulate one note
  960. test.pressure (3, MPEValue::from7BitInt (33));
  961. expectNote (test.getNote (3, 60), 100, 33, 8192, 64, MPENote::keyDown);
  962. expectNote (test.getNote (4, 60), 100, 0, 8192, 64, MPENote::keyDown);
  963. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  964. expectEquals (test.notePressureChangedCallCounter, 1);
  965. // applying pressure on a master channel should modulate all notes in this zone
  966. test.pressure (1, MPEValue::from7BitInt (44));
  967. expectNote (test.getNote (3, 60), 100, 44, 8192, 64, MPENote::keyDown);
  968. expectNote (test.getNote (4, 60), 100, 44, 8192, 64, MPENote::keyDown);
  969. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  970. expectEquals (test.notePressureChangedCallCounter, 3);
  971. // applying pressure on an unrelated channel should be ignored
  972. test.pressure (8, MPEValue::from7BitInt (55));
  973. expectNote (test.getNote (3, 60), 100, 44, 8192, 64, MPENote::keyDown);
  974. expectNote (test.getNote (4, 60), 100, 44, 8192, 64, MPENote::keyDown);
  975. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  976. expectEquals (test.notePressureChangedCallCounter, 3);
  977. }
  978. {
  979. UnitTestInstrument test;
  980. test.setZoneLayout (testLayout);
  981. // two notes on same channel - only last added should be modulated
  982. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  983. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  984. test.pressure (3, MPEValue::from7BitInt (66));
  985. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  986. expectNote (test.getNote (3, 61), 100, 66, 8192, 64, MPENote::keyDown);
  987. expectEquals (test.notePressureChangedCallCounter, 1);
  988. }
  989. {
  990. UnitTestInstrument test;
  991. test.setZoneLayout (testLayout);
  992. // edge case: two notes on same channel, one gets released,
  993. // then the other should be modulated
  994. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  995. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  996. test.noteOff (3, 61, MPEValue::from7BitInt (100));
  997. test.pressure (3, MPEValue::from7BitInt (77));
  998. expectEquals (test.getNumPlayingNotes(), 1);
  999. expectNote (test.getNote (3, 60), 100, 77, 8192, 64, MPENote::keyDown);
  1000. expectEquals (test.notePressureChangedCallCounter, 1);
  1001. }
  1002. {
  1003. UnitTestInstrument test;
  1004. test.setZoneLayout (testLayout);
  1005. // if no pressure is sent before note-on, default = 0 should be used
  1006. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1007. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1008. }
  1009. {
  1010. UnitTestInstrument test;
  1011. test.setZoneLayout (testLayout);
  1012. // if pressure is sent before note-on, use that
  1013. test.pressure (3, MPEValue::from7BitInt (77));
  1014. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1015. expectNote (test.getNote (3, 60), 100, 77, 8192, 64, MPENote::keyDown);
  1016. }
  1017. {
  1018. UnitTestInstrument test;
  1019. test.setZoneLayout (testLayout);
  1020. // if pressure is sent before note-on, but it belonged to another note
  1021. // on the same channel that has since been turned off, use default = 0
  1022. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1023. test.pressure (3, MPEValue::from7BitInt (77));
  1024. test.noteOff (3, 61, MPEValue::from7BitInt (100));
  1025. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1026. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1027. }
  1028. {
  1029. UnitTestInstrument test;
  1030. test.setZoneLayout (testLayout);
  1031. // edge case: two notes on the same channel simultaneously. the second one should use
  1032. // pressure = 0 initially but then react to additional pressure messages
  1033. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1034. test.pressure (3, MPEValue::from7BitInt (77));
  1035. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1036. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1037. test.pressure (3, MPEValue::from7BitInt (78));
  1038. expectNote (test.getNote (3, 60), 100, 78, 8192, 64, MPENote::keyDown);
  1039. expectNote (test.getNote (3, 61), 100, 77, 8192, 64, MPENote::keyDown);
  1040. }
  1041. {
  1042. UnitTestInstrument test;
  1043. test.setZoneLayout (testLayout);
  1044. // master channel will use poly-aftertouch for pressure
  1045. test.noteOn (16, 60, MPEValue::from7BitInt (100));
  1046. expectNote (test.getNote (16, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1047. test.aftertouch (16, 60, MPEValue::from7BitInt (27));
  1048. expectNote (test.getNote (16, 60), 100, 27, 8192, 64, MPENote::keyDown);
  1049. // member channels will not respond to poly-aftertouch
  1050. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1051. test.aftertouch (3, 60, MPEValue::from7BitInt (50));
  1052. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1053. }
  1054. }
  1055. beginTest ("pitchbend");
  1056. {
  1057. {
  1058. UnitTestInstrument test;
  1059. test.setZoneLayout (testLayout);
  1060. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1061. test.noteOn (4, 60, MPEValue::from7BitInt (100));
  1062. test.noteOn (10, 60, MPEValue::from7BitInt (100));
  1063. // applying pitchbend on a per-note channel should modulate one note
  1064. test.pitchbend (3, MPEValue::from14BitInt (1111));
  1065. expectNote (test.getNote (3, 60), 100, 0, 1111, 64, MPENote::keyDown);
  1066. expectNote (test.getNote (4, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1067. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1068. expectEquals (test.notePitchbendChangedCallCounter, 1);
  1069. // applying pitchbend on a master channel should be ignored for the
  1070. // value of per-note pitchbend. Tests covering master pitchbend below.
  1071. // Note: noteChanged will be called anyway for notes in that zone
  1072. // because the total pitchbend for those notes has changed
  1073. test.pitchbend (1, MPEValue::from14BitInt (2222));
  1074. expectNote (test.getNote (3, 60), 100, 0, 1111, 64, MPENote::keyDown);
  1075. expectNote (test.getNote (4, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1076. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1077. expectEquals (test.notePitchbendChangedCallCounter, 3);
  1078. // applying pitchbend on an unrelated channel should do nothing.
  1079. test.pitchbend (8, MPEValue::from14BitInt (3333));
  1080. expectNote (test.getNote (3, 60), 100, 0, 1111, 64, MPENote::keyDown);
  1081. expectNote (test.getNote (4, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1082. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1083. expectEquals (test.notePitchbendChangedCallCounter, 3);
  1084. }
  1085. {
  1086. UnitTestInstrument test;
  1087. test.setZoneLayout (testLayout);
  1088. // two notes on same channel - only last added should be bent
  1089. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1090. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1091. test.pitchbend (3, MPEValue::from14BitInt (4444));
  1092. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1093. expectNote (test.getNote (3, 61), 100, 0, 4444, 64, MPENote::keyDown);
  1094. expectEquals (test.notePitchbendChangedCallCounter, 1);
  1095. }
  1096. {
  1097. UnitTestInstrument test;
  1098. test.setZoneLayout (testLayout);
  1099. // edge case: two notes on same channel, one gets released,
  1100. // then the other should be bent
  1101. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1102. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1103. test.noteOff (3, 61, MPEValue::from7BitInt (100));
  1104. test.pitchbend (3, MPEValue::from14BitInt (5555));
  1105. expectEquals (test.getNumPlayingNotes(), 1);
  1106. expectNote (test.getNote (3, 60), 100, 0, 5555, 64, MPENote::keyDown);
  1107. expectEquals (test.notePitchbendChangedCallCounter, 1);
  1108. }
  1109. {
  1110. UnitTestInstrument test;
  1111. test.setZoneLayout (testLayout);
  1112. // Richard's edge case:
  1113. // - press one note
  1114. // - press sustain (careful: must be sent on master channel)
  1115. // - release first note (is still sustained!)
  1116. // - press another note (happens to be on the same MIDI channel!)
  1117. // - pitchbend that other note
  1118. // - the first note should not be bent, only the second one.
  1119. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1120. test.sustainPedal (1, true);
  1121. test.noteOff (3, 60, MPEValue::from7BitInt (64));
  1122. expectEquals (test.getNumPlayingNotes(), 1);
  1123. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::sustained);
  1124. expectEquals (test.noteKeyStateChangedCallCounter, 2);
  1125. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1126. test.pitchbend (3, MPEValue::from14BitInt (6666));
  1127. expectEquals (test.getNumPlayingNotes(), 2);
  1128. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::sustained);
  1129. expectNote (test.getNote (3, 61), 100, 0, 6666, 64, MPENote::keyDownAndSustained);
  1130. expectEquals (test.notePitchbendChangedCallCounter, 1);
  1131. }
  1132. {
  1133. UnitTestInstrument test;
  1134. test.setZoneLayout (testLayout);
  1135. // Zsolt's edge case:
  1136. // - press one note
  1137. // - modulate pitchbend or timbre
  1138. // - release the note
  1139. // - press same note again without sending a pitchbend or timbre message before the note-on
  1140. // - the note should be turned on with a default value for pitchbend/timbre,
  1141. // and *not* the last value received on channel.
  1142. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1143. test.pitchbend (3, MPEValue::from14BitInt (5555));
  1144. expectNote (test.getNote (3, 60), 100, 0, 5555, 64, MPENote::keyDown);
  1145. test.noteOff (3, 60, MPEValue::from7BitInt (100));
  1146. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1147. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1148. }
  1149. {
  1150. // applying per-note pitchbend should set the note's totalPitchbendInSemitones
  1151. // correctly depending on the per-note pitchbend range of the zone.
  1152. UnitTestInstrument test;
  1153. MPEZoneLayout layout = testLayout;
  1154. test.setZoneLayout (layout); // default should be +/- 48 semitones
  1155. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1156. test.pitchbend (3, MPEValue::from14BitInt (4096));
  1157. expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, -24.0, 0.01);
  1158. layout.setLowerZone (5, 96);
  1159. test.setZoneLayout (layout);
  1160. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1161. test.pitchbend (3, MPEValue::from14BitInt (0)); // -max
  1162. expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, -96.0, 0.01);
  1163. layout.setLowerZone (5, 1);
  1164. test.setZoneLayout (layout);
  1165. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1166. test.pitchbend (3, MPEValue::from14BitInt (16383)); // +max
  1167. expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, 1.0, 0.01);
  1168. layout.setLowerZone (5, 0); // pitchbendrange = 0 --> no pitchbend at all
  1169. test.setZoneLayout (layout);
  1170. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1171. test.pitchbend (3, MPEValue::from14BitInt (12345));
  1172. expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, 0.0, 0.01);
  1173. }
  1174. {
  1175. // applying master pitchbend should set the note's totalPitchbendInSemitones
  1176. // correctly depending on the master pitchbend range of the zone.
  1177. UnitTestInstrument test;
  1178. MPEZoneLayout layout = testLayout;
  1179. test.setZoneLayout (layout); // default should be +/- 2 semitones
  1180. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1181. test.pitchbend (1, MPEValue::from14BitInt (4096)); //halfway between -max and centre
  1182. expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, -1.0, 0.01);
  1183. layout.setLowerZone (5, 48, 96);
  1184. test.setZoneLayout (layout);
  1185. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1186. test.pitchbend (1, MPEValue::from14BitInt (0)); // -max
  1187. expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, -96.0, 0.01);
  1188. layout.setLowerZone (5, 48, 1);
  1189. test.setZoneLayout (layout);
  1190. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1191. test.pitchbend (1, MPEValue::from14BitInt (16383)); // +max
  1192. expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, 1.0, 0.01);
  1193. layout.setLowerZone (5, 48, 0); // pitchbendrange = 0 --> no pitchbend at all
  1194. test.setZoneLayout (layout);
  1195. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1196. test.pitchbend (1, MPEValue::from14BitInt (12345));
  1197. expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, 0.0, 0.01);
  1198. }
  1199. {
  1200. // applying both per-note and master pitchbend simultaneously should set
  1201. // the note's totalPitchbendInSemitones to the sum of both, correctly
  1202. // weighted with the per-note and master pitchbend range, respectively.
  1203. UnitTestInstrument test;
  1204. MPEZoneLayout layout = testLayout;
  1205. layout.setLowerZone (5, 12, 1);
  1206. test.setZoneLayout (layout);
  1207. test.pitchbend (1, MPEValue::from14BitInt (4096)); // master pitchbend 0.5 semitones down
  1208. test.pitchbend (3, MPEValue::from14BitInt (0)); // per-note pitchbend 12 semitones down
  1209. // additionally, note should react to both pitchbend messages
  1210. // correctly even if they arrived before the note-on.
  1211. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1212. expectDoubleWithinRelativeError (test.getMostRecentNote (3).totalPitchbendInSemitones, -12.5, 0.01);
  1213. }
  1214. }
  1215. beginTest ("timbre");
  1216. {
  1217. {
  1218. UnitTestInstrument test;
  1219. test.setZoneLayout (testLayout);
  1220. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1221. test.noteOn (4, 60, MPEValue::from7BitInt (100));
  1222. test.noteOn (10, 60, MPEValue::from7BitInt (100));
  1223. // modulating timbre on a per-note channel should modulate one note
  1224. test.timbre (3, MPEValue::from7BitInt (33));
  1225. expectNote (test.getNote (3, 60), 100, 0, 8192, 33, MPENote::keyDown);
  1226. expectNote (test.getNote (4, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1227. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1228. expectEquals (test.noteTimbreChangedCallCounter, 1);
  1229. // modulating timbre on a master channel should modulate all notes in this zone
  1230. test.timbre (1, MPEValue::from7BitInt (44));
  1231. expectNote (test.getNote (3, 60), 100, 0, 8192, 44, MPENote::keyDown);
  1232. expectNote (test.getNote (4, 60), 100, 0, 8192, 44, MPENote::keyDown);
  1233. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1234. expectEquals (test.noteTimbreChangedCallCounter, 3);
  1235. // modulating timbre on an unrelated channel should be ignored
  1236. test.timbre (9, MPEValue::from7BitInt (55));
  1237. expectNote (test.getNote (3, 60), 100, 0, 8192, 44, MPENote::keyDown);
  1238. expectNote (test.getNote (4, 60), 100, 0, 8192, 44, MPENote::keyDown);
  1239. expectNote (test.getNote (10, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1240. expectEquals (test.noteTimbreChangedCallCounter, 3);
  1241. }
  1242. {
  1243. UnitTestInstrument test;
  1244. test.setZoneLayout (testLayout);
  1245. // two notes on same channel - only last added should be modulated
  1246. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1247. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1248. test.timbre (3, MPEValue::from7BitInt (66));
  1249. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1250. expectNote (test.getNote (3, 61), 100, 0, 8192, 66, MPENote::keyDown);
  1251. expectEquals (test.noteTimbreChangedCallCounter, 1);
  1252. }
  1253. {
  1254. UnitTestInstrument test;
  1255. test.setZoneLayout (testLayout);
  1256. // edge case: two notes on same channel, one gets released,
  1257. // then the other should be modulated
  1258. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1259. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1260. test.noteOff (3, 61, MPEValue::from7BitInt (100));
  1261. test.timbre (3, MPEValue::from7BitInt (77));
  1262. expectEquals (test.getNumPlayingNotes(), 1);
  1263. expectNote (test.getNote (3, 60), 100, 0, 8192, 77, MPENote::keyDown);
  1264. expectEquals (test.noteTimbreChangedCallCounter, 1);
  1265. }
  1266. {
  1267. UnitTestInstrument test;
  1268. test.setZoneLayout (testLayout);
  1269. // Zsolt's edge case for timbre
  1270. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1271. test.timbre (3, MPEValue::from7BitInt (42));
  1272. expectNote (test.getNote (3, 60), 100, 0, 8192, 42, MPENote::keyDown);
  1273. test.noteOff (3, 60, MPEValue::from7BitInt (100));
  1274. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1275. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1276. }
  1277. }
  1278. beginTest ("setPressureTrackingMode");
  1279. {
  1280. {
  1281. // last note played (= default)
  1282. UnitTestInstrument test;
  1283. test.setZoneLayout (testLayout);
  1284. test.setPressureTrackingMode (MPEInstrument::lastNotePlayedOnChannel);
  1285. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1286. test.noteOn (3, 62, MPEValue::from7BitInt (100));
  1287. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1288. test.pressure (3, MPEValue::from7BitInt (99));
  1289. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1290. expectNote (test.getNote (3, 62), 100, 0, 8192, 64, MPENote::keyDown);
  1291. expectNote (test.getNote (3, 61), 100, 99, 8192, 64, MPENote::keyDown);
  1292. expectEquals (test.notePressureChangedCallCounter, 1);
  1293. }
  1294. {
  1295. // lowest note
  1296. UnitTestInstrument test;
  1297. test.setZoneLayout (testLayout);
  1298. test.setPressureTrackingMode (MPEInstrument::lowestNoteOnChannel);
  1299. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1300. test.noteOn (3, 62, MPEValue::from7BitInt (100));
  1301. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1302. test.pressure (3, MPEValue::from7BitInt (99));
  1303. expectNote (test.getNote (3, 60), 100, 99, 8192, 64, MPENote::keyDown);
  1304. expectNote (test.getNote (3, 62), 100, 0, 8192, 64, MPENote::keyDown);
  1305. expectNote (test.getNote (3, 61), 100, 0, 8192, 64, MPENote::keyDown);
  1306. expectEquals (test.notePressureChangedCallCounter, 1);
  1307. }
  1308. {
  1309. // highest note
  1310. UnitTestInstrument test;
  1311. test.setZoneLayout (testLayout);
  1312. test.setPressureTrackingMode (MPEInstrument::highestNoteOnChannel);
  1313. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1314. test.noteOn (3, 62, MPEValue::from7BitInt (100));
  1315. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1316. test.pressure (3, MPEValue::from7BitInt (99));
  1317. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1318. expectNote (test.getNote (3, 62), 100, 99, 8192, 64, MPENote::keyDown);
  1319. expectNote (test.getNote (3, 61), 100, 0, 8192, 64, MPENote::keyDown);
  1320. expectEquals (test.notePressureChangedCallCounter, 1);
  1321. }
  1322. {
  1323. // all notes
  1324. UnitTestInstrument test;
  1325. test.setZoneLayout (testLayout);
  1326. test.setPressureTrackingMode (MPEInstrument::allNotesOnChannel);
  1327. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1328. test.noteOn (3, 62, MPEValue::from7BitInt (100));
  1329. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1330. test.pressure (3, MPEValue::from7BitInt (99));
  1331. expectNote (test.getNote (3, 60), 100, 99, 8192, 64, MPENote::keyDown);
  1332. expectNote (test.getNote (3, 62), 100, 99, 8192, 64, MPENote::keyDown);
  1333. expectNote (test.getNote (3, 61), 100, 99, 8192, 64, MPENote::keyDown);
  1334. expectEquals (test.notePressureChangedCallCounter, 3);
  1335. }
  1336. }
  1337. beginTest ("setPitchbendTrackingMode");
  1338. {
  1339. {
  1340. // last note played (= default)
  1341. UnitTestInstrument test;
  1342. test.setZoneLayout (testLayout);
  1343. test.setPitchbendTrackingMode (MPEInstrument::lastNotePlayedOnChannel);
  1344. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1345. test.noteOn (3, 62, MPEValue::from7BitInt (100));
  1346. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1347. test.pitchbend (3, MPEValue::from14BitInt (9999));
  1348. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1349. expectNote (test.getNote (3, 62), 100, 0, 8192, 64, MPENote::keyDown);
  1350. expectNote (test.getNote (3, 61), 100, 0, 9999, 64, MPENote::keyDown);
  1351. expectEquals (test.notePitchbendChangedCallCounter, 1);
  1352. }
  1353. {
  1354. // lowest note
  1355. UnitTestInstrument test;
  1356. test.setZoneLayout (testLayout);
  1357. test.setPitchbendTrackingMode (MPEInstrument::lowestNoteOnChannel);
  1358. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1359. test.noteOn (3, 62, MPEValue::from7BitInt (100));
  1360. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1361. test.pitchbend (3, MPEValue::from14BitInt (9999));
  1362. expectNote (test.getNote (3, 60), 100, 0, 9999, 64, MPENote::keyDown);
  1363. expectNote (test.getNote (3, 62), 100, 0, 8192, 64, MPENote::keyDown);
  1364. expectNote (test.getNote (3, 61), 100, 0, 8192, 64, MPENote::keyDown);
  1365. expectEquals (test.notePitchbendChangedCallCounter, 1);
  1366. }
  1367. {
  1368. // highest note
  1369. UnitTestInstrument test;
  1370. test.setZoneLayout (testLayout);
  1371. test.setPitchbendTrackingMode (MPEInstrument::highestNoteOnChannel);
  1372. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1373. test.noteOn (3, 62, MPEValue::from7BitInt (100));
  1374. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1375. test.pitchbend (3, MPEValue::from14BitInt (9999));
  1376. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1377. expectNote (test.getNote (3, 62), 100, 0, 9999, 64, MPENote::keyDown);
  1378. expectNote (test.getNote (3, 61), 100, 0, 8192, 64, MPENote::keyDown);
  1379. expectEquals (test.notePitchbendChangedCallCounter, 1);
  1380. }
  1381. {
  1382. // all notes
  1383. UnitTestInstrument test;
  1384. test.setZoneLayout (testLayout);
  1385. test.setPitchbendTrackingMode (MPEInstrument::allNotesOnChannel);
  1386. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1387. test.noteOn (3, 62, MPEValue::from7BitInt (100));
  1388. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1389. test.pitchbend (3, MPEValue::from14BitInt (9999));
  1390. expectNote (test.getNote (3, 60), 100, 0, 9999, 64, MPENote::keyDown);
  1391. expectNote (test.getNote (3, 62), 100, 0, 9999, 64, MPENote::keyDown);
  1392. expectNote (test.getNote (3, 61), 100, 0, 9999, 64, MPENote::keyDown);
  1393. expectEquals (test.notePitchbendChangedCallCounter, 3);
  1394. }
  1395. }
  1396. beginTest ("setTimbreTrackingMode");
  1397. {
  1398. {
  1399. // last note played (= default)
  1400. UnitTestInstrument test;
  1401. test.setZoneLayout (testLayout);
  1402. test.setTimbreTrackingMode (MPEInstrument::lastNotePlayedOnChannel);
  1403. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1404. test.noteOn (3, 62, MPEValue::from7BitInt (100));
  1405. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1406. test.timbre (3, MPEValue::from7BitInt (99));
  1407. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1408. expectNote (test.getNote (3, 62), 100, 0, 8192, 64, MPENote::keyDown);
  1409. expectNote (test.getNote (3, 61), 100, 0, 8192, 99, MPENote::keyDown);
  1410. expectEquals (test.noteTimbreChangedCallCounter, 1);
  1411. }
  1412. {
  1413. // lowest note
  1414. UnitTestInstrument test;
  1415. test.setZoneLayout (testLayout);
  1416. test.setTimbreTrackingMode (MPEInstrument::lowestNoteOnChannel);
  1417. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1418. test.noteOn (3, 62, MPEValue::from7BitInt (100));
  1419. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1420. test.timbre (3, MPEValue::from7BitInt (99));
  1421. expectNote (test.getNote (3, 60), 100, 0, 8192, 99, MPENote::keyDown);
  1422. expectNote (test.getNote (3, 62), 100, 0, 8192, 64, MPENote::keyDown);
  1423. expectNote (test.getNote (3, 61), 100, 0, 8192, 64, MPENote::keyDown);
  1424. expectEquals (test.noteTimbreChangedCallCounter, 1);
  1425. }
  1426. {
  1427. // highest note
  1428. UnitTestInstrument test;
  1429. test.setZoneLayout (testLayout);
  1430. test.setTimbreTrackingMode (MPEInstrument::highestNoteOnChannel);
  1431. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1432. test.noteOn (3, 62, MPEValue::from7BitInt (100));
  1433. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1434. test.timbre (3, MPEValue::from7BitInt (99));
  1435. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1436. expectNote (test.getNote (3, 62), 100, 0, 8192, 99, MPENote::keyDown);
  1437. expectNote (test.getNote (3, 61), 100, 0, 8192, 64, MPENote::keyDown);
  1438. expectEquals (test.noteTimbreChangedCallCounter, 1);
  1439. }
  1440. {
  1441. // all notes
  1442. UnitTestInstrument test;
  1443. test.setZoneLayout (testLayout);
  1444. test.setTimbreTrackingMode (MPEInstrument::allNotesOnChannel);
  1445. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1446. test.noteOn (3, 62, MPEValue::from7BitInt (100));
  1447. test.noteOn (3, 61, MPEValue::from7BitInt (100));
  1448. test.timbre (3, MPEValue::from7BitInt (99));
  1449. expectNote (test.getNote (3, 60), 100, 0, 8192, 99, MPENote::keyDown);
  1450. expectNote (test.getNote (3, 62), 100, 0, 8192, 99, MPENote::keyDown);
  1451. expectNote (test.getNote (3, 61), 100, 0, 8192, 99, MPENote::keyDown);
  1452. expectEquals (test.noteTimbreChangedCallCounter, 3);
  1453. }
  1454. }
  1455. beginTest ("processNextMidiEvent");
  1456. {
  1457. UnitTestInstrument test;
  1458. // note on should trigger noteOn method call
  1459. test.processNextMidiEvent (MidiMessage::noteOn (3, 42, uint8 (92)));
  1460. expectEquals (test.noteOnCallCounter, 1);
  1461. expectEquals (test.lastMidiChannelReceived, 3);
  1462. expectEquals (test.lastMidiNoteNumberReceived, 42);
  1463. expectEquals (test.lastMPEValueReceived.as7BitInt(), 92);
  1464. // note off should trigger noteOff method call
  1465. test.processNextMidiEvent (MidiMessage::noteOff (4, 12, uint8 (33)));
  1466. expectEquals (test.noteOffCallCounter, 1);
  1467. expectEquals (test.lastMidiChannelReceived, 4);
  1468. expectEquals (test.lastMidiNoteNumberReceived, 12);
  1469. expectEquals (test.lastMPEValueReceived.as7BitInt(), 33);
  1470. // note on with velocity = 0 should trigger noteOff method call
  1471. // with a note off velocity of 64 (centre value)
  1472. test.processNextMidiEvent (MidiMessage::noteOn (5, 11, uint8 (0)));
  1473. expectEquals (test.noteOffCallCounter, 2);
  1474. expectEquals (test.lastMidiChannelReceived, 5);
  1475. expectEquals (test.lastMidiNoteNumberReceived, 11);
  1476. expectEquals (test.lastMPEValueReceived.as7BitInt(), 64);
  1477. // pitchwheel message should trigger pitchbend method call
  1478. test.processNextMidiEvent (MidiMessage::pitchWheel (1, 3333));
  1479. expectEquals (test.pitchbendCallCounter, 1);
  1480. expectEquals (test.lastMidiChannelReceived, 1);
  1481. expectEquals (test.lastMPEValueReceived.as14BitInt(), 3333);
  1482. // pressure using channel pressure message (7-bit value) should
  1483. // trigger pressure method call
  1484. test.processNextMidiEvent (MidiMessage::channelPressureChange (10, 35));
  1485. expectEquals (test.pressureCallCounter, 1);
  1486. expectEquals (test.lastMidiChannelReceived, 10);
  1487. expectEquals (test.lastMPEValueReceived.as7BitInt(), 35);
  1488. // pressure using 14-bit value over CC70 and CC102 should trigger
  1489. // pressure method call after the MSB is sent
  1490. // a) sending only the MSB
  1491. test.processNextMidiEvent (MidiMessage::controllerEvent (3, 70, 120));
  1492. expectEquals (test.pressureCallCounter, 2);
  1493. expectEquals (test.lastMidiChannelReceived, 3);
  1494. expectEquals (test.lastMPEValueReceived.as7BitInt(), 120);
  1495. // b) sending LSB and MSB (only the MSB should trigger the call) - per MIDI channel!
  1496. test.processNextMidiEvent (MidiMessage::controllerEvent (4, 102, 121));
  1497. expectEquals (test.pressureCallCounter, 2);
  1498. test.processNextMidiEvent (MidiMessage::controllerEvent (5, 102, 122));
  1499. expectEquals (test.pressureCallCounter, 2);
  1500. test.processNextMidiEvent (MidiMessage::controllerEvent (4, 70, 123));
  1501. expectEquals (test.pressureCallCounter, 3);
  1502. expectEquals (test.lastMidiChannelReceived, 4);
  1503. expectEquals (test.lastMPEValueReceived.as14BitInt(), 121 + (123 << 7));
  1504. test.processNextMidiEvent (MidiMessage::controllerEvent (5, 70, 124));
  1505. expectEquals (test.pressureCallCounter, 4);
  1506. expectEquals (test.lastMidiChannelReceived, 5);
  1507. expectEquals (test.lastMPEValueReceived.as14BitInt(), 122 + (124 << 7));
  1508. test.processNextMidiEvent (MidiMessage::controllerEvent (5, 70, 64));
  1509. expectEquals (test.pressureCallCounter, 5);
  1510. expectEquals (test.lastMidiChannelReceived, 5);
  1511. expectEquals (test.lastMPEValueReceived.as7BitInt(), 64);
  1512. // same for timbre 14-bit value over CC74 and CC106
  1513. test.processNextMidiEvent (MidiMessage::controllerEvent (3, 74, 120));
  1514. expectEquals (test.timbreCallCounter, 1);
  1515. expectEquals (test.lastMidiChannelReceived, 3);
  1516. expectEquals (test.lastMPEValueReceived.as7BitInt(), 120);
  1517. test.processNextMidiEvent (MidiMessage::controllerEvent (4, 106, 121));
  1518. expectEquals (test.timbreCallCounter, 1);
  1519. test.processNextMidiEvent (MidiMessage::controllerEvent (5, 106, 122));
  1520. expectEquals (test.timbreCallCounter, 1);
  1521. test.processNextMidiEvent (MidiMessage::controllerEvent (4, 74, 123));
  1522. expectEquals (test.timbreCallCounter, 2);
  1523. expectEquals (test.lastMidiChannelReceived, 4);
  1524. expectEquals (test.lastMPEValueReceived.as14BitInt(), 121 + (123 << 7));
  1525. test.processNextMidiEvent (MidiMessage::controllerEvent (5, 74, 124));
  1526. expectEquals (test.timbreCallCounter, 3);
  1527. expectEquals (test.lastMidiChannelReceived, 5);
  1528. expectEquals (test.lastMPEValueReceived.as14BitInt(), 122 + (124 << 7));
  1529. test.processNextMidiEvent (MidiMessage::controllerEvent (5, 74, 64));
  1530. expectEquals (test.timbreCallCounter, 4);
  1531. expectEquals (test.lastMidiChannelReceived, 5);
  1532. expectEquals (test.lastMPEValueReceived.as7BitInt(), 64);
  1533. // sustain pedal message (CC64) should trigger sustainPedal method call
  1534. test.processNextMidiEvent (MidiMessage::controllerEvent (1, 64, 127));
  1535. expectEquals (test.sustainPedalCallCounter, 1);
  1536. expectEquals (test.lastMidiChannelReceived, 1);
  1537. expect (test.lastSustainPedalValueReceived);
  1538. test.processNextMidiEvent (MidiMessage::controllerEvent (16, 64, 0));
  1539. expectEquals (test.sustainPedalCallCounter, 2);
  1540. expectEquals (test.lastMidiChannelReceived, 16);
  1541. expect (! test.lastSustainPedalValueReceived);
  1542. // sostenuto pedal message (CC66) should trigger sostenutoPedal method call
  1543. test.processNextMidiEvent (MidiMessage::controllerEvent (1, 66, 127));
  1544. expectEquals (test.sostenutoPedalCallCounter, 1);
  1545. expectEquals (test.lastMidiChannelReceived, 1);
  1546. expect (test.lastSostenutoPedalValueReceived);
  1547. test.processNextMidiEvent (MidiMessage::controllerEvent (16, 66, 0));
  1548. expectEquals (test.sostenutoPedalCallCounter, 2);
  1549. expectEquals (test.lastMidiChannelReceived, 16);
  1550. expect (! test.lastSostenutoPedalValueReceived);
  1551. }
  1552. {
  1553. // MIDI messages modifying the zone layout should be correctly
  1554. // forwarded to the internal zone layout and modify it.
  1555. // (testing the actual logic of the zone layout is done in the
  1556. // MPEZoneLayout unit tests)
  1557. MPEInstrument test;
  1558. MidiBuffer buffer;
  1559. buffer.addEvents (MPEMessages::setLowerZone (5), 0, -1, 0);
  1560. buffer.addEvents (MPEMessages::setUpperZone (6), 0, -1, 0);
  1561. for (const auto metadata : buffer)
  1562. test.processNextMidiEvent (metadata.getMessage());
  1563. expect (test.getZoneLayout().getLowerZone().isActive());
  1564. expect (test.getZoneLayout().getUpperZone().isActive());
  1565. expectEquals (test.getZoneLayout().getLowerZone().getMasterChannel(), 1);
  1566. expectEquals (test.getZoneLayout().getLowerZone().numMemberChannels, 5);
  1567. expectEquals (test.getZoneLayout().getUpperZone().getMasterChannel(), 16);
  1568. expectEquals (test.getZoneLayout().getUpperZone().numMemberChannels, 6);
  1569. }
  1570. beginTest ("MIDI all notes off");
  1571. {
  1572. UnitTestInstrument test;
  1573. test.setZoneLayout (testLayout);
  1574. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1575. test.noteOn (4, 61, MPEValue::from7BitInt (100));
  1576. test.noteOn (15, 62, MPEValue::from7BitInt (100));
  1577. test.noteOn (15, 63, MPEValue::from7BitInt (100));
  1578. expectEquals (test.getNumPlayingNotes(), 4);
  1579. // on note channel: ignore.
  1580. test.processNextMidiEvent (MidiMessage::allControllersOff (3));
  1581. expectEquals (test.getNumPlayingNotes(), 4);
  1582. // on unused channel: ignore.
  1583. test.processNextMidiEvent (MidiMessage::allControllersOff (9));
  1584. expectEquals (test.getNumPlayingNotes(), 4);
  1585. // on master channel: release notes in that zone only.
  1586. test.processNextMidiEvent (MidiMessage::allControllersOff (1));
  1587. expectEquals (test.getNumPlayingNotes(), 2);
  1588. test.processNextMidiEvent (MidiMessage::allControllersOff (16));
  1589. expectEquals (test.getNumPlayingNotes(), 0);
  1590. }
  1591. beginTest ("MIDI all notes off (legacy mode)");
  1592. {
  1593. UnitTestInstrument test;
  1594. test.enableLegacyMode();
  1595. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1596. test.noteOn (4, 61, MPEValue::from7BitInt (100));
  1597. test.noteOn (15, 62, MPEValue::from7BitInt (100));
  1598. test.noteOn (15, 63, MPEValue::from7BitInt (100));
  1599. expectEquals (test.getNumPlayingNotes(), 4);
  1600. test.processNextMidiEvent (MidiMessage::allControllersOff (3));
  1601. expectEquals (test.getNumPlayingNotes(), 3);
  1602. test.processNextMidiEvent (MidiMessage::allControllersOff (15));
  1603. expectEquals (test.getNumPlayingNotes(), 1);
  1604. test.processNextMidiEvent (MidiMessage::allControllersOff (4));
  1605. expectEquals (test.getNumPlayingNotes(), 0);
  1606. }
  1607. beginTest ("default initial values for pitchbend and timbre");
  1608. {
  1609. MPEInstrument test;
  1610. test.setZoneLayout (testLayout);
  1611. test.pitchbend (3, MPEValue::from14BitInt (3333)); // use for next note-on on ch. 3
  1612. test.pitchbend (2, MPEValue::from14BitInt (4444)); // ignore
  1613. test.pitchbend (2, MPEValue::from14BitInt (5555)); // ignore
  1614. test.timbre (3, MPEValue::from7BitInt (66)); // use for next note-on on ch. 3
  1615. test.timbre (2, MPEValue::from7BitInt (77)); // ignore
  1616. test.timbre (2, MPEValue::from7BitInt (88)); // ignore
  1617. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1618. expectNote (test.getMostRecentNote (3), 100, 0, 3333, 66, MPENote::keyDown);
  1619. }
  1620. beginTest ("Legacy mode");
  1621. {
  1622. {
  1623. // basic check
  1624. MPEInstrument test;
  1625. expect (! test.isLegacyModeEnabled());
  1626. test.setZoneLayout (testLayout);
  1627. expect (! test.isLegacyModeEnabled());
  1628. test.enableLegacyMode();
  1629. expect (test.isLegacyModeEnabled());
  1630. test.setZoneLayout (testLayout);
  1631. expect (! test.isLegacyModeEnabled());
  1632. }
  1633. {
  1634. // constructor w/o default arguments
  1635. MPEInstrument test;
  1636. test.enableLegacyMode (0, Range<int> (1, 11));
  1637. expectEquals (test.getLegacyModePitchbendRange(), 0);
  1638. expect (test.getLegacyModeChannelRange() == Range<int> (1, 11));
  1639. }
  1640. {
  1641. // getters and setters
  1642. MPEInstrument test;
  1643. test.enableLegacyMode();
  1644. expectEquals (test.getLegacyModePitchbendRange(), 2);
  1645. expect (test.getLegacyModeChannelRange() == Range<int> (1, 17));
  1646. test.setLegacyModePitchbendRange (96);
  1647. expectEquals (test.getLegacyModePitchbendRange(), 96);
  1648. test.setLegacyModeChannelRange (Range<int> (10, 12));
  1649. expect (test.getLegacyModeChannelRange() == Range<int> (10, 12));
  1650. }
  1651. {
  1652. // note on should trigger notes on all 16 channels
  1653. UnitTestInstrument test;
  1654. test.enableLegacyMode();
  1655. test.noteOn (1, 60, MPEValue::from7BitInt (100));
  1656. test.noteOn (2, 60, MPEValue::from7BitInt (100));
  1657. test.noteOn (15, 60, MPEValue::from7BitInt (100));
  1658. test.noteOn (16, 60, MPEValue::from7BitInt (100));
  1659. expectEquals (test.getNumPlayingNotes(), 4);
  1660. // polyphonic modulation should work across all 16 channels
  1661. test.pitchbend (1, MPEValue::from14BitInt (9999));
  1662. test.pressure (2, MPEValue::from7BitInt (88));
  1663. test.timbre (15, MPEValue::from7BitInt (77));
  1664. expectNote (test.getNote (1, 60), 100, 0, 9999, 64, MPENote::keyDown);
  1665. expectNote (test.getNote (2, 60), 100, 88, 8192, 64, MPENote::keyDown);
  1666. expectNote (test.getNote (15, 60), 100, 0, 8192, 77, MPENote::keyDown);
  1667. expectNote (test.getNote (16, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1668. // note off should work in legacy mode
  1669. test.noteOff (15, 60, MPEValue::from7BitInt (0));
  1670. test.noteOff (1, 60, MPEValue::from7BitInt (0));
  1671. test.noteOff (2, 60, MPEValue::from7BitInt (0));
  1672. test.noteOff (16, 60, MPEValue::from7BitInt (0));
  1673. expectEquals (test.getNumPlayingNotes(), 0);
  1674. }
  1675. {
  1676. // legacy mode w/ custom channel range: note on should trigger notes only within range
  1677. UnitTestInstrument test;
  1678. test.enableLegacyMode (2, Range<int> (3, 8)); // channels 3-7
  1679. test.noteOn (1, 60, MPEValue::from7BitInt (100));
  1680. test.noteOn (2, 60, MPEValue::from7BitInt (100));
  1681. test.noteOn (3, 60, MPEValue::from7BitInt (100)); // should trigger
  1682. test.noteOn (4, 60, MPEValue::from7BitInt (100)); // should trigger
  1683. test.noteOn (6, 60, MPEValue::from7BitInt (100)); // should trigger
  1684. test.noteOn (7, 60, MPEValue::from7BitInt (100)); // should trigger
  1685. test.noteOn (8, 60, MPEValue::from7BitInt (100));
  1686. test.noteOn (16, 60, MPEValue::from7BitInt (100));
  1687. expectEquals (test.getNumPlayingNotes(), 4);
  1688. expectNote (test.getNote (3, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1689. expectNote (test.getNote (4, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1690. expectNote (test.getNote (6, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1691. expectNote (test.getNote (7, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1692. }
  1693. {
  1694. // tracking mode in legacy mode
  1695. {
  1696. UnitTestInstrument test;
  1697. test.enableLegacyMode();
  1698. test.setPitchbendTrackingMode (MPEInstrument::lastNotePlayedOnChannel);
  1699. test.noteOn (1, 60, MPEValue::from7BitInt (100));
  1700. test.noteOn (1, 62, MPEValue::from7BitInt (100));
  1701. test.noteOn (1, 61, MPEValue::from7BitInt (100));
  1702. test.pitchbend (1, MPEValue::from14BitInt (9999));
  1703. expectNote (test.getNote (1, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1704. expectNote (test.getNote (1, 61), 100, 0, 9999, 64, MPENote::keyDown);
  1705. expectNote (test.getNote (1, 62), 100, 0, 8192, 64, MPENote::keyDown);
  1706. }
  1707. {
  1708. UnitTestInstrument test;
  1709. test.enableLegacyMode();
  1710. test.setPitchbendTrackingMode (MPEInstrument::lowestNoteOnChannel);
  1711. test.noteOn (1, 60, MPEValue::from7BitInt (100));
  1712. test.noteOn (1, 62, MPEValue::from7BitInt (100));
  1713. test.noteOn (1, 61, MPEValue::from7BitInt (100));
  1714. test.pitchbend (1, MPEValue::from14BitInt (9999));
  1715. expectNote (test.getNote (1, 60), 100, 0, 9999, 64, MPENote::keyDown);
  1716. expectNote (test.getNote (1, 61), 100, 0, 8192, 64, MPENote::keyDown);
  1717. expectNote (test.getNote (1, 62), 100, 0, 8192, 64, MPENote::keyDown);
  1718. }
  1719. {
  1720. UnitTestInstrument test;
  1721. test.enableLegacyMode();
  1722. test.setPitchbendTrackingMode (MPEInstrument::highestNoteOnChannel);
  1723. test.noteOn (1, 60, MPEValue::from7BitInt (100));
  1724. test.noteOn (1, 62, MPEValue::from7BitInt (100));
  1725. test.noteOn (1, 61, MPEValue::from7BitInt (100));
  1726. test.pitchbend (1, MPEValue::from14BitInt (9999));
  1727. expectNote (test.getNote (1, 60), 100, 0, 8192, 64, MPENote::keyDown);
  1728. expectNote (test.getNote (1, 61), 100, 0, 8192, 64, MPENote::keyDown);
  1729. expectNote (test.getNote (1, 62), 100, 0, 9999, 64, MPENote::keyDown);
  1730. }
  1731. {
  1732. UnitTestInstrument test;
  1733. test.enableLegacyMode();
  1734. test.setPitchbendTrackingMode (MPEInstrument::allNotesOnChannel);
  1735. test.noteOn (1, 60, MPEValue::from7BitInt (100));
  1736. test.noteOn (1, 62, MPEValue::from7BitInt (100));
  1737. test.noteOn (1, 61, MPEValue::from7BitInt (100));
  1738. test.pitchbend (1, MPEValue::from14BitInt (9999));
  1739. expectNote (test.getNote (1, 60), 100, 0, 9999, 64, MPENote::keyDown);
  1740. expectNote (test.getNote (1, 61), 100, 0, 9999, 64, MPENote::keyDown);
  1741. expectNote (test.getNote (1, 62), 100, 0, 9999, 64, MPENote::keyDown);
  1742. }
  1743. }
  1744. {
  1745. // custom pitchbend range in legacy mode.
  1746. UnitTestInstrument test;
  1747. test.enableLegacyMode (11);
  1748. test.pitchbend (1, MPEValue::from14BitInt (4096));
  1749. test.noteOn (1, 60, MPEValue::from7BitInt (100));
  1750. expectDoubleWithinRelativeError (test.getMostRecentNote (1).totalPitchbendInSemitones, -5.5, 0.01);
  1751. }
  1752. {
  1753. // sustain pedal should be per channel in legacy mode.
  1754. UnitTestInstrument test;
  1755. test.enableLegacyMode();
  1756. test.sustainPedal (1, true);
  1757. test.noteOn (2, 61, MPEValue::from7BitInt (100));
  1758. test.noteOff (2, 61, MPEValue::from7BitInt (100));
  1759. test.noteOn (1, 60, MPEValue::from7BitInt (100));
  1760. test.noteOff (1, 60, MPEValue::from7BitInt (100));
  1761. expectEquals (test.getNumPlayingNotes(), 1);
  1762. expectNote (test.getNote (1, 60), 100, 0, 8192, 64, MPENote::sustained);
  1763. test.sustainPedal (1, false);
  1764. expectEquals (test.getNumPlayingNotes(), 0);
  1765. test.noteOn (2, 61, MPEValue::from7BitInt (100));
  1766. test.sustainPedal (1, true);
  1767. test.noteOff (2, 61, MPEValue::from7BitInt (100));
  1768. expectEquals (test.getNumPlayingNotes(), 0);
  1769. }
  1770. {
  1771. // sostenuto pedal should be per channel in legacy mode.
  1772. UnitTestInstrument test;
  1773. test.enableLegacyMode();
  1774. test.noteOn (1, 60, MPEValue::from7BitInt (100));
  1775. test.sostenutoPedal (1, true);
  1776. test.noteOff (1, 60, MPEValue::from7BitInt (100));
  1777. test.noteOn (2, 61, MPEValue::from7BitInt (100));
  1778. test.noteOff (2, 61, MPEValue::from7BitInt (100));
  1779. expectEquals (test.getNumPlayingNotes(), 1);
  1780. expectNote (test.getNote (1, 60), 100, 0, 8192, 64, MPENote::sustained);
  1781. test.sostenutoPedal (1, false);
  1782. expectEquals (test.getNumPlayingNotes(), 0);
  1783. test.noteOn (2, 61, MPEValue::from7BitInt (100));
  1784. test.sostenutoPedal (1, true);
  1785. test.noteOff (2, 61, MPEValue::from7BitInt (100));
  1786. expectEquals (test.getNumPlayingNotes(), 0);
  1787. }
  1788. {
  1789. // all notes released when switching layout
  1790. UnitTestInstrument test;
  1791. test.setZoneLayout (testLayout);
  1792. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1793. expectEquals (test.getNumPlayingNotes(), 1);
  1794. test.enableLegacyMode();
  1795. expectEquals (test.getNumPlayingNotes(), 0);
  1796. test.noteOn (3, 60, MPEValue::from7BitInt (100));
  1797. expectEquals (test.getNumPlayingNotes(), 1);
  1798. test.setZoneLayout (testLayout);
  1799. expectEquals (test.getNumPlayingNotes(), 0);
  1800. }
  1801. }
  1802. }
  1803. private:
  1804. //==============================================================================
  1805. /* This mock class is used for unit testing whether the methods of
  1806. MPEInstrument are called correctly.
  1807. */
  1808. class UnitTestInstrument : public MPEInstrument,
  1809. private MPEInstrument::Listener
  1810. {
  1811. using Base = MPEInstrument;
  1812. public:
  1813. UnitTestInstrument()
  1814. : noteOnCallCounter (0), noteOffCallCounter (0), pitchbendCallCounter (0),
  1815. pressureCallCounter (0), timbreCallCounter (0), sustainPedalCallCounter (0),
  1816. sostenutoPedalCallCounter (0), noteAddedCallCounter (0), notePressureChangedCallCounter (0),
  1817. notePitchbendChangedCallCounter (0), noteTimbreChangedCallCounter (0),
  1818. noteKeyStateChangedCallCounter (0), noteReleasedCallCounter (0),
  1819. lastMidiChannelReceived (-1), lastMidiNoteNumberReceived (-1),
  1820. lastSustainPedalValueReceived (false), lastSostenutoPedalValueReceived (false)
  1821. {
  1822. addListener (this);
  1823. }
  1824. void noteOn (int midiChannel, int midiNoteNumber, MPEValue midiNoteOnVelocity) override
  1825. {
  1826. Base::noteOn (midiChannel, midiNoteNumber, midiNoteOnVelocity);
  1827. noteOnCallCounter++;
  1828. lastMidiChannelReceived = midiChannel;
  1829. lastMidiNoteNumberReceived = midiNoteNumber;
  1830. lastMPEValueReceived = midiNoteOnVelocity;
  1831. }
  1832. void noteOff (int midiChannel, int midiNoteNumber, MPEValue midiNoteOffVelocity) override
  1833. {
  1834. Base::noteOff (midiChannel, midiNoteNumber, midiNoteOffVelocity);
  1835. noteOffCallCounter++;
  1836. lastMidiChannelReceived = midiChannel;
  1837. lastMidiNoteNumberReceived = midiNoteNumber;
  1838. lastMPEValueReceived = midiNoteOffVelocity;
  1839. }
  1840. void pitchbend (int midiChannel, MPEValue value) override
  1841. {
  1842. Base::pitchbend (midiChannel, value);
  1843. pitchbendCallCounter++;
  1844. lastMidiChannelReceived = midiChannel;
  1845. lastMPEValueReceived = value;
  1846. }
  1847. void pressure (int midiChannel, MPEValue value) override
  1848. {
  1849. Base::pressure (midiChannel, value);
  1850. pressureCallCounter++;
  1851. lastMidiChannelReceived = midiChannel;
  1852. lastMPEValueReceived = value;
  1853. }
  1854. void timbre (int midiChannel, MPEValue value) override
  1855. {
  1856. Base::timbre (midiChannel, value);
  1857. timbreCallCounter++;
  1858. lastMidiChannelReceived = midiChannel;
  1859. lastMPEValueReceived = value;
  1860. }
  1861. void sustainPedal (int midiChannel, bool value) override
  1862. {
  1863. Base::sustainPedal (midiChannel, value);
  1864. sustainPedalCallCounter++;
  1865. lastMidiChannelReceived = midiChannel;
  1866. lastSustainPedalValueReceived = value;
  1867. }
  1868. void sostenutoPedal (int midiChannel, bool value) override
  1869. {
  1870. Base::sostenutoPedal (midiChannel, value);
  1871. sostenutoPedalCallCounter++;
  1872. lastMidiChannelReceived = midiChannel;
  1873. lastSostenutoPedalValueReceived = value;
  1874. }
  1875. void aftertouch (int midiChannel, int midiNoteNumber, MPEValue value)
  1876. {
  1877. const auto message = juce::MidiMessage::aftertouchChange (midiChannel, midiNoteNumber, value.as7BitInt());
  1878. processNextMidiEvent (message);
  1879. }
  1880. int noteOnCallCounter, noteOffCallCounter, pitchbendCallCounter,
  1881. pressureCallCounter, timbreCallCounter, sustainPedalCallCounter,
  1882. sostenutoPedalCallCounter, noteAddedCallCounter,
  1883. notePressureChangedCallCounter, notePitchbendChangedCallCounter,
  1884. noteTimbreChangedCallCounter, noteKeyStateChangedCallCounter,
  1885. noteReleasedCallCounter, lastMidiChannelReceived, lastMidiNoteNumberReceived;
  1886. bool lastSustainPedalValueReceived, lastSostenutoPedalValueReceived;
  1887. MPEValue lastMPEValueReceived;
  1888. std::unique_ptr<MPENote> lastNoteFinished;
  1889. private:
  1890. //==============================================================================
  1891. void noteAdded (MPENote) override { noteAddedCallCounter++; }
  1892. void notePressureChanged (MPENote) override { notePressureChangedCallCounter++; }
  1893. void notePitchbendChanged (MPENote) override { notePitchbendChangedCallCounter++; }
  1894. void noteTimbreChanged (MPENote) override { noteTimbreChangedCallCounter++; }
  1895. void noteKeyStateChanged (MPENote) override { noteKeyStateChangedCallCounter++; }
  1896. void noteReleased (MPENote finishedNote) override
  1897. {
  1898. noteReleasedCallCounter++;
  1899. lastNoteFinished.reset (new MPENote (finishedNote));
  1900. }
  1901. };
  1902. //==============================================================================
  1903. void expectNote (MPENote noteToTest,
  1904. int noteOnVelocity7Bit,
  1905. int pressure7Bit,
  1906. int pitchbend14Bit,
  1907. int timbre7Bit,
  1908. MPENote::KeyState keyState)
  1909. {
  1910. expect (noteToTest.isValid());
  1911. expectEquals (noteToTest.noteOnVelocity.as7BitInt(), noteOnVelocity7Bit);
  1912. expectEquals (noteToTest.pressure.as7BitInt(), pressure7Bit);
  1913. expectEquals (noteToTest.pitchbend.as14BitInt(), pitchbend14Bit);
  1914. expectEquals (noteToTest.timbre.as7BitInt(),timbre7Bit);
  1915. expect (noteToTest.keyState == keyState);
  1916. }
  1917. void expectHasFinishedNote (const UnitTestInstrument& test,
  1918. int channel, int noteNumber, int noteOffVelocity7Bit)
  1919. {
  1920. expect (test.lastNoteFinished != nullptr);
  1921. expectEquals (int (test.lastNoteFinished->midiChannel), channel);
  1922. expectEquals (int (test.lastNoteFinished->initialNote), noteNumber);
  1923. expectEquals (test.lastNoteFinished->noteOffVelocity.as7BitInt(), noteOffVelocity7Bit);
  1924. expect (test.lastNoteFinished->keyState == MPENote::off);
  1925. }
  1926. void expectDoubleWithinRelativeError (double actual, double expected, double maxRelativeError)
  1927. {
  1928. const double maxAbsoluteError = jmax (1.0, std::abs (expected)) * maxRelativeError;
  1929. expect (std::abs (expected - actual) < maxAbsoluteError);
  1930. }
  1931. //==============================================================================
  1932. MPEZoneLayout testLayout;
  1933. };
  1934. static MPEInstrumentTests MPEInstrumentUnitTests;
  1935. #endif
  1936. } // namespace juce