PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

noteList.h 8.6KB

3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*!
  2. * \file noteList.h
  3. * \author Francois Best
  4. * \date 24/05/2013
  5. * \brief Linked list of notes, for Low, Last & High playing modes.
  6. * \license GPL v3.0 - Copyright Forty Seven Effects 2013
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #pragma once
  22. #include <inttypes.h>
  23. typedef uint8_t byte;
  24. // -----------------------------------------------------------------------------
  25. struct MidiNote
  26. {
  27. inline MidiNote();
  28. inline MidiNote(byte inPitch, byte inVelocity);
  29. inline MidiNote(const MidiNote& inOther);
  30. inline MidiNote& operator= (const MidiNote& inOther);
  31. byte pitch;
  32. byte velocity;
  33. };
  34. // -----------------------------------------------------------------------------
  35. template<byte Size>
  36. class MidiNoteList
  37. {
  38. private:
  39. struct Cell
  40. {
  41. inline Cell();
  42. inline Cell(const Cell& inOther);
  43. inline Cell& operator= (const Cell& inOther);
  44. MidiNote note;
  45. bool active;
  46. Cell* next;
  47. Cell* prev;
  48. };
  49. public:
  50. inline MidiNoteList();
  51. inline ~MidiNoteList();
  52. public:
  53. inline void add(const MidiNote& inNote);
  54. inline void remove(byte inPitch);
  55. public:
  56. inline bool get(byte inIndex, byte& outPitch) const;
  57. inline bool getLast(byte& outPitch) const;
  58. inline bool getHigh(byte& outPitch) const;
  59. inline bool getLow(byte& outPitch) const;
  60. public:
  61. inline bool empty() const;
  62. inline byte size() const;
  63. private:
  64. inline Cell* getFirstEmptyCell();
  65. inline void print() const;
  66. private:
  67. Cell mArray[Size];
  68. Cell* mHead;
  69. Cell* mTail;
  70. byte mSize;
  71. };
  72. // ########################################################################## //
  73. // Inline implementation
  74. inline MidiNote::MidiNote()
  75. : pitch(0)
  76. , velocity(0)
  77. {
  78. }
  79. inline MidiNote::MidiNote(byte inPitch, byte inVelocity)
  80. : pitch(inPitch)
  81. , velocity(inVelocity)
  82. {
  83. }
  84. inline MidiNote::MidiNote(const MidiNote& inOther)
  85. : pitch(inOther.pitch)
  86. , velocity(inOther.velocity)
  87. {
  88. }
  89. inline MidiNote& MidiNote::operator= (const MidiNote& inOther)
  90. {
  91. pitch = inOther.pitch;
  92. velocity = inOther.velocity;
  93. return *this;
  94. }
  95. // ########################################################################## //
  96. template<byte Size>
  97. inline MidiNoteList<Size>::Cell::Cell()
  98. : note()
  99. , active(false)
  100. , next(0)
  101. , prev(0)
  102. {
  103. }
  104. template<byte Size>
  105. inline MidiNoteList<Size>::Cell::Cell(const Cell& inOther)
  106. : note(inOther.note)
  107. , active(inOther.active)
  108. , next(inOther.next)
  109. , prev(inOther.prev)
  110. {
  111. }
  112. template<byte Size>
  113. inline typename MidiNoteList<Size>::Cell& MidiNoteList<Size>::Cell::operator= (const Cell& inOther)
  114. {
  115. note = inOther.note;
  116. active = inOther.active;
  117. next = inOther.next;
  118. prev = inOther.prev;
  119. return *this;
  120. }
  121. // ########################################################################## //
  122. template<byte Size>
  123. inline MidiNoteList<Size>::MidiNoteList()
  124. {
  125. }
  126. template<byte Size>
  127. inline MidiNoteList<Size>::~MidiNoteList()
  128. {
  129. }
  130. // -----------------------------------------------------------------------------
  131. /*! \brief Add a note, sorting it by time.
  132. Call this when receiving a NoteOn event. This will add the new note as the tail
  133. of the list.
  134. */
  135. template<byte Size>
  136. inline void MidiNoteList<Size>::add(const MidiNote& inNote)
  137. {
  138. if (mHead == 0)
  139. {
  140. mArray[0].note = inNote;
  141. mArray[0].active = true;
  142. mArray[0].next = 0;
  143. mArray[0].prev = 0;
  144. mHead = mArray;
  145. mTail = mArray;
  146. }
  147. else
  148. {
  149. // Find the first inactive cell, and use it as tail.
  150. Cell* const oldTail = mTail;
  151. Cell* const newTail = getFirstEmptyCell();
  152. newTail->active = true;
  153. newTail->note = inNote;
  154. oldTail->next = newTail;
  155. newTail->prev = oldTail;
  156. newTail->next = 0;
  157. mTail = newTail;
  158. }
  159. mSize++;
  160. print();
  161. }
  162. /*! \brief Remove a note
  163. Call this when receiving a NoteOff event.
  164. */
  165. template<byte Size>
  166. inline void MidiNoteList<Size>::remove(byte inPitch)
  167. {
  168. if (mTail != 0)
  169. {
  170. for (Cell* it = mTail; it != 0; it = it->prev)
  171. {
  172. if (it->note.pitch == inPitch)
  173. {
  174. Cell* const prev = it->prev;
  175. Cell* const next = it->next;
  176. it->active = false;
  177. it->next = 0;
  178. it->prev = 0;
  179. // Reconnect both ends
  180. if (it == mHead)
  181. {
  182. //AVR_ASSERT(prev == 0);
  183. mHead = next;
  184. }
  185. else
  186. {
  187. //AVR_ASSERT(prev != 0);
  188. prev->next = next;
  189. }
  190. if (it == mTail)
  191. {
  192. //AVR_ASSERT(next == 0);
  193. mTail = prev;
  194. }
  195. else
  196. {
  197. //AVR_ASSERT(next != 0);
  198. next->prev = prev;
  199. }
  200. mSize--;
  201. break;
  202. }
  203. }
  204. }
  205. print();
  206. }
  207. // -----------------------------------------------------------------------------
  208. /*! \brief Get a note at an arbitrary position
  209. This can be interesting for duo/multi/polyphony operations.
  210. */
  211. template<byte Size>
  212. inline bool MidiNoteList<Size>::get(byte inIndex, byte& outPitch) const
  213. {
  214. if (mTail)
  215. {
  216. const Cell* it = mTail;
  217. for (byte i = 0; i < inIndex; ++i)
  218. {
  219. if (it->prev)
  220. {
  221. it = it->prev;
  222. }
  223. }
  224. print();
  225. //AVR_LOG("Index " << inIndex << ": " << it->note.pitch);
  226. outPitch = it->note.pitch;
  227. return true;
  228. }
  229. return false;
  230. }
  231. /*! \brief Get the last active note played
  232. This implements the Mono Last playing mode.
  233. */
  234. template<byte Size>
  235. inline bool MidiNoteList<Size>::getLast(byte& outPitch) const
  236. {
  237. if (!mTail)
  238. {
  239. return false;
  240. }
  241. outPitch = mTail->note.pitch;
  242. return true;
  243. }
  244. /*! \brief Get the highest pitched active note
  245. This implements the Mono High playing mode.
  246. */
  247. template<byte Size>
  248. inline bool MidiNoteList<Size>::getHigh(byte& outPitch) const
  249. {
  250. if (!mTail)
  251. {
  252. return false;
  253. }
  254. outPitch = 0;
  255. const Cell* it = mTail;
  256. for (byte i = 0; i < mSize; ++i)
  257. {
  258. if (it->note.pitch > outPitch)
  259. {
  260. outPitch = it->note.pitch;
  261. }
  262. if (it->prev)
  263. {
  264. it = it->prev;
  265. }
  266. }
  267. return true;
  268. }
  269. /*! \brief Get the lowest pitched active note
  270. This implements the Mono Low playing mode.
  271. */
  272. template<byte Size>
  273. inline bool MidiNoteList<Size>::getLow(byte& outPitch) const
  274. {
  275. if (!mTail)
  276. {
  277. return false;
  278. }
  279. outPitch = 0xff;
  280. const Cell* it = mTail;
  281. for (byte i = 0; i < mSize; ++i)
  282. {
  283. if (it->note.pitch < outPitch)
  284. {
  285. outPitch = it->note.pitch;
  286. }
  287. if (it->prev)
  288. {
  289. it = it->prev;
  290. }
  291. }
  292. return true;
  293. }
  294. // -----------------------------------------------------------------------------
  295. template<byte Size>
  296. inline bool MidiNoteList<Size>::empty() const
  297. {
  298. return mSize == 0;
  299. }
  300. /*! \brief Get the number of active notes.
  301. */
  302. template<byte Size>
  303. inline byte MidiNoteList<Size>::size() const
  304. {
  305. return mSize;
  306. }
  307. // -----------------------------------------------------------------------------
  308. // Private implementations, for internal use only.
  309. template<byte Size>
  310. inline typename MidiNoteList<Size>::Cell* MidiNoteList<Size>::getFirstEmptyCell()
  311. {
  312. for (byte i = 0; i < Size; ++i)
  313. {
  314. if (mArray[i].active == false)
  315. {
  316. return mArray + i;
  317. }
  318. }
  319. return 0;
  320. }
  321. template<byte Size>
  322. inline void MidiNoteList<Size>::print() const
  323. {
  324. //#ifndef NDEBUG
  325. // AVR_DBG("Note List: [ ");
  326. // if (mHead)
  327. // {
  328. // for (const Cell* it = mHead; it != 0; it = it->next)
  329. // {
  330. // AVR_DBG(it->note.pitch);
  331. // if (it->next)
  332. // AVR_DBG(" -> ");
  333. // }
  334. // }
  335. // AVR_LOG(" ]");
  336. //#endif
  337. }