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 97KB

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