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.

2159 lines
97KB

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