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.

286 lines
7.4KB

  1. #include "NotePool.h"
  2. //XXX eliminate dependence on Part.h
  3. #include "../Misc/Part.h"
  4. #include "../Misc/Allocator.h"
  5. #include "../Synth/SynthNote.h"
  6. #include <cstring>
  7. #include <cassert>
  8. NotePool::NotePool(void)
  9. :needs_cleaning(0)
  10. {
  11. memset(ndesc, 0, sizeof(ndesc));
  12. memset(sdesc, 0, sizeof(ndesc));
  13. }
  14. NotePool::activeNotesIter NotePool::activeNotes(NoteDescriptor &n)
  15. {
  16. const int off_d1 = &n-ndesc;
  17. int off_d2 = 0;
  18. assert(off_d1 <= POLYPHONY);
  19. for(int i=0; i<off_d1; ++i)
  20. off_d2 += ndesc[i].size;
  21. return NotePool::activeNotesIter{sdesc+off_d2,sdesc+off_d2+n.size};
  22. }
  23. bool NotePool::NoteDescriptor::operator==(NoteDescriptor nd)
  24. {
  25. return age == nd.age && note == nd.note && sendto == nd.sendto && size == nd.size && status == nd.status;
  26. }
  27. //return either the first unused descriptor or the last valid descriptor which
  28. //matches note/sendto
  29. static int getMergeableDescriptor(uint8_t note, uint8_t sendto, bool legato,
  30. NotePool::NoteDescriptor *ndesc)
  31. {
  32. int desc_id = 0;
  33. for(int i=0; i<POLYPHONY; ++i, ++desc_id)
  34. if(ndesc[desc_id].status == Part::KEY_OFF)
  35. break;
  36. //Out of free descriptors
  37. if(ndesc[desc_id].status != Part::KEY_OFF)
  38. return -1;
  39. if(desc_id != 0) {
  40. auto &nd = ndesc[desc_id-1];
  41. if(nd.age == 0 && nd.note == note && nd.sendto == sendto
  42. && nd.status == Part::KEY_PLAYING && nd.legatoMirror == legato)
  43. return desc_id-1;
  44. }
  45. return desc_id;
  46. }
  47. NotePool::activeDescIter NotePool::activeDesc(void)
  48. {
  49. cleanup();
  50. return activeDescIter{*this};
  51. }
  52. NotePool::constActiveDescIter NotePool::activeDesc(void) const
  53. {
  54. const_cast<NotePool*>(this)->cleanup();
  55. return constActiveDescIter{*this};
  56. }
  57. void NotePool::insertNote(uint8_t note, uint8_t sendto, SynthDescriptor desc, bool legato)
  58. {
  59. //Get first free note descriptor
  60. int desc_id = getMergeableDescriptor(note, sendto, legato, ndesc);
  61. assert(desc_id != -1);
  62. ndesc[desc_id].note = note;
  63. ndesc[desc_id].sendto = sendto;
  64. ndesc[desc_id].size += 1;
  65. ndesc[desc_id].status = Part::KEY_PLAYING;
  66. ndesc[desc_id].legatoMirror = legato;
  67. //Get first free synth descriptor
  68. int sdesc_id = 0;
  69. while(sdesc[sdesc_id].note)
  70. sdesc_id++;
  71. sdesc[sdesc_id] = desc;
  72. };
  73. void NotePool::upgradeToLegato(void)
  74. {
  75. for(auto &d:activeDesc())
  76. if(d.status == Part::KEY_PLAYING)
  77. for(auto &s:activeNotes(d))
  78. insertLegatoNote(d.note, d.sendto, s);
  79. }
  80. void NotePool::insertLegatoNote(uint8_t note, uint8_t sendto, SynthDescriptor desc)
  81. {
  82. assert(desc.note);
  83. desc.note = desc.note->cloneLegato();
  84. insertNote(note, sendto, desc, true);
  85. };
  86. //There should only be one pair of notes which are still playing
  87. void NotePool::applyLegato(LegatoParams &par)
  88. {
  89. for(auto &desc:activeDesc()) {
  90. desc.note = par.midinote;
  91. for(auto &synth:activeNotes(desc))
  92. synth.note->legatonote(par);
  93. }
  94. };
  95. //Note that isn't KEY_PLAYING or KEY_RELASED_AND_SUSTAINING
  96. bool NotePool::existsRunningNote(void) const
  97. {
  98. //printf("runing note # =%d\n", getRunningNotes());
  99. return getRunningNotes();
  100. }
  101. int NotePool::getRunningNotes(void) const
  102. {
  103. bool running[256] = {0};
  104. for(auto &desc:activeDesc()) {
  105. //printf("note!(%d)\n", desc.note);
  106. if(desc.status == Part::KEY_PLAYING)
  107. running[desc.note] = true;
  108. }
  109. int running_count = 0;
  110. for(int i=0; i<256; ++i)
  111. running_count += running[i];
  112. return running_count;
  113. }
  114. int NotePool::enforceKeyLimit(int limit) const
  115. {
  116. //{
  117. //int oldestnotepos = -1;
  118. //if(notecount > keylimit) //find out the oldest note
  119. // for(int i = 0; i < POLYPHONY; ++i) {
  120. // int maxtime = 0;
  121. // if(((partnote[i].status == KEY_PLAYING) || (partnote[i].status == KEY_RELEASED_AND_SUSTAINED)) && (partnote[i].time > maxtime)) {
  122. // maxtime = partnote[i].time;
  123. // oldestnotepos = i;
  124. // }
  125. // }
  126. //if(oldestnotepos != -1)
  127. // ReleaseNotePos(oldestnotepos);
  128. //}
  129. printf("Unimplemented enforceKeyLimit()\n");
  130. return -1;
  131. }
  132. void NotePool::releasePlayingNotes(void)
  133. {
  134. for(auto &d:activeDesc()) {
  135. if(d.status == Part::KEY_PLAYING) {
  136. d.status = Part::KEY_RELEASED;
  137. for(auto s:activeNotes(d))
  138. s.note->releasekey();
  139. }
  140. }
  141. }
  142. void NotePool::release(NoteDescriptor &d)
  143. {
  144. d.status = Part::KEY_RELEASED;
  145. for(auto s:activeNotes(d))
  146. s.note->releasekey();
  147. }
  148. void NotePool::killAllNotes(void)
  149. {
  150. for(auto &d:activeDesc())
  151. kill(d);
  152. }
  153. void NotePool::killNote(uint8_t note)
  154. {
  155. for(auto &d:activeDesc()) {
  156. if(d.note == note)
  157. kill(d);
  158. }
  159. }
  160. void NotePool::kill(NoteDescriptor &d)
  161. {
  162. d.status = Part::KEY_OFF;
  163. for(auto &s:activeNotes(d))
  164. kill(s);
  165. }
  166. void NotePool::kill(SynthDescriptor &s)
  167. {
  168. //printf("Kill synth...\n");
  169. s.note->memory.dealloc(s.note);
  170. needs_cleaning = true;
  171. }
  172. const char *getStatus(int status_bits)
  173. {
  174. switch(status_bits)
  175. {
  176. case 0: return "OFF ";
  177. case 1: return "PLAY";
  178. case 2: return "SUST";
  179. case 3: return "RELA";
  180. default: return "INVD";
  181. }
  182. }
  183. void NotePool::cleanup(void)
  184. {
  185. if(!needs_cleaning)
  186. return;
  187. needs_cleaning = false;
  188. int new_length[POLYPHONY] = {0};
  189. int cur_length[POLYPHONY] = {0};
  190. //printf("Cleanup Start\n");
  191. //dump();
  192. //Identify the current length of all segments
  193. //and the lengths discarding invalid entries
  194. int last_valid_desc = 0;
  195. for(int i=0; i<POLYPHONY; ++i)
  196. if(ndesc[i].status != Part::KEY_OFF)
  197. last_valid_desc = i;
  198. //Find the real numbers of allocated notes
  199. {
  200. int cum_old = 0;
  201. for(int i=0; i<=last_valid_desc; ++i) {
  202. cur_length[i] = ndesc[i].size;
  203. for(int j=0; j<ndesc[i].size; ++j)
  204. new_length[i] += (bool)sdesc[cum_old++].note;
  205. }
  206. }
  207. //Move the note descriptors
  208. {
  209. int cum_new = 0;
  210. for(int i=0; i<=last_valid_desc; ++i) {
  211. ndesc[i].size = new_length[i];
  212. if(new_length[i] != 0)
  213. ndesc[cum_new++] = ndesc[i];
  214. else
  215. ndesc[i].status = Part::KEY_OFF;
  216. }
  217. memset(ndesc+cum_new, 0, sizeof(*ndesc)*(POLYPHONY-cum_new));
  218. }
  219. //Move the synth descriptors
  220. {
  221. int total_notes=0;
  222. for(int i=0; i<=last_valid_desc; ++i)
  223. total_notes+=cur_length[i];
  224. int cum_new = 0;
  225. for(int i=0; i<total_notes; ++i)
  226. if(sdesc[i].note)
  227. sdesc[cum_new++] = sdesc[i];
  228. memset(sdesc+cum_new, 0, sizeof(*sdesc)*(POLYPHONY*EXPECTED_USAGE-cum_new));
  229. }
  230. //printf("Cleanup Done\n");
  231. //dump();
  232. }
  233. void NotePool::dump(void)
  234. {
  235. printf("NotePool::dump<\n");
  236. const char *format =
  237. " Note %d:%d age(%d) note(%d) sendto(%d) status(%s) legato(%d) type(%d) kit(%d) ptr(%p)\n";
  238. int note_id=0;
  239. int descriptor_id=0;
  240. for(auto &d:activeDesc()) {
  241. descriptor_id += 1;
  242. for(auto &s:activeNotes(d)) {
  243. note_id += 1;
  244. printf(format,
  245. note_id, descriptor_id,
  246. d.age, d.note, d.sendto,
  247. getStatus(d.status), d.legatoMirror, s.type, s.kit, s.note);
  248. }
  249. }
  250. printf(">NotePool::dump\n");
  251. }