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

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