您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

294 行
9.8KB

  1. #include "Pizzicato_samples.h"
  2. #include "FrenchHorns_samples.h"
  3. #include "Viola_samples.h"
  4. #include "BasicFlute1_samples.h"
  5. #include "Ocarina_samples.h"
  6. #include <Bounce.h>
  7. #include <Audio.h>
  8. #include <Wire.h>
  9. #include <SPI.h>
  10. #include <SD.h>
  11. #include <SerialFlash.h>
  12. //#define DEBUG_ALLOC
  13. const int TOTAL_VOICES = 64;
  14. const int TOTAL_MIXERS = 21;
  15. const int SECONDARY_MIXERS = 4;
  16. AudioControlSGTL5000 sgtl5000_1;
  17. AudioSynthWavetable wavetable[TOTAL_VOICES];
  18. AudioMixer4 mixer[TOTAL_MIXERS];
  19. AudioOutputI2S i2s1;
  20. AudioConnection patchCord[] = {
  21. {wavetable[0], 0, mixer[0], 0}, {wavetable[1], 0, mixer[0], 1}, {wavetable[2], 0, mixer[0], 2}, {wavetable[3], 0, mixer[0], 3}, {mixer[0], 0, mixer[TOTAL_MIXERS - 2], 0},
  22. {wavetable[4], 0, mixer[1], 0}, {wavetable[5], 0, mixer[1], 1}, {wavetable[6], 0, mixer[1], 2}, {wavetable[7], 0, mixer[1], 3}, {mixer[1], 0, mixer[TOTAL_MIXERS - 2], 1},
  23. {wavetable[8], 0, mixer[2], 0}, {wavetable[9], 0, mixer[2], 1}, {wavetable[10], 0, mixer[2], 2}, {wavetable[11], 0, mixer[2], 3}, {mixer[2], 0, mixer[TOTAL_MIXERS - 2], 2},
  24. {wavetable[12], 0, mixer[3], 0}, {wavetable[13], 0, mixer[3], 1}, {wavetable[14], 0, mixer[3], 2}, {wavetable[15], 0, mixer[3], 3}, {mixer[3], 0, mixer[TOTAL_MIXERS - 2], 3},
  25. {wavetable[16], 0, mixer[4], 0}, {wavetable[17], 0, mixer[4], 1}, {wavetable[18], 0, mixer[4], 2}, {wavetable[19], 0, mixer[4], 3}, {mixer[4], 0, mixer[TOTAL_MIXERS - 3], 0},
  26. {wavetable[20], 0, mixer[5], 0}, {wavetable[21], 0, mixer[5], 1}, {wavetable[22], 0, mixer[5], 2}, {wavetable[23], 0, mixer[5], 3}, {mixer[5], 0, mixer[TOTAL_MIXERS - 3], 1},
  27. {wavetable[24], 0, mixer[6], 0}, {wavetable[25], 0, mixer[6], 1}, {wavetable[26], 0, mixer[6], 2}, {wavetable[27], 0, mixer[6], 3}, {mixer[6], 0, mixer[TOTAL_MIXERS - 3], 2},
  28. {wavetable[28], 0, mixer[7], 0}, {wavetable[29], 0, mixer[7], 1}, {wavetable[30], 0, mixer[7], 2}, {wavetable[31], 0, mixer[7], 3}, {mixer[7], 0, mixer[TOTAL_MIXERS - 3], 3},
  29. {wavetable[32], 0, mixer[8], 0}, {wavetable[33], 0, mixer[8], 1}, {wavetable[34], 0, mixer[8], 2}, {wavetable[35], 0, mixer[8], 3}, {mixer[8], 0, mixer[TOTAL_MIXERS - 4], 0},
  30. {wavetable[36], 0, mixer[9], 0}, {wavetable[37], 0, mixer[9], 1}, {wavetable[38], 0, mixer[9], 2}, {wavetable[39], 0, mixer[9], 3}, {mixer[9], 0, mixer[TOTAL_MIXERS - 4], 1},
  31. {wavetable[40], 0, mixer[10], 0}, {wavetable[41], 0, mixer[10], 1}, {wavetable[42], 0, mixer[10], 2}, {wavetable[43], 0, mixer[10], 3}, {mixer[10], 0, mixer[TOTAL_MIXERS - 4], 2},
  32. {wavetable[44], 0, mixer[11], 0}, {wavetable[45], 0, mixer[11], 1}, {wavetable[46], 0, mixer[11], 2}, {wavetable[47], 0, mixer[11], 3}, {mixer[11], 0, mixer[TOTAL_MIXERS - 4], 3},
  33. {wavetable[48], 0, mixer[12], 0}, {wavetable[49], 0, mixer[12], 1}, {wavetable[50], 0, mixer[12], 2}, {wavetable[51], 0, mixer[12], 3}, {mixer[12], 0, mixer[TOTAL_MIXERS - 5], 0},
  34. {wavetable[52], 0, mixer[13], 0}, {wavetable[53], 0, mixer[13], 1}, {wavetable[54], 0, mixer[13], 2}, {wavetable[55], 0, mixer[13], 3}, {mixer[13], 0, mixer[TOTAL_MIXERS - 5], 1},
  35. {wavetable[56], 0, mixer[14], 0}, {wavetable[57], 0, mixer[14], 1}, {wavetable[58], 0, mixer[14], 2}, {wavetable[59], 0, mixer[14], 3}, {mixer[14], 0, mixer[TOTAL_MIXERS - 5], 2},
  36. {wavetable[60], 0, mixer[15], 0}, {wavetable[61], 0, mixer[15], 1}, {wavetable[62], 0, mixer[15], 2}, {wavetable[63], 0, mixer[15], 3}, {mixer[15], 0, mixer[TOTAL_MIXERS - 5], 3},
  37. {mixer[TOTAL_MIXERS - 2], 0, mixer[TOTAL_MIXERS - 1], 0},
  38. {mixer[TOTAL_MIXERS - 3], 0, mixer[TOTAL_MIXERS - 1], 1},
  39. {mixer[TOTAL_MIXERS - 4], 0, mixer[TOTAL_MIXERS - 1], 2},
  40. {mixer[TOTAL_MIXERS - 5], 0, mixer[TOTAL_MIXERS - 1], 3},
  41. {mixer[TOTAL_MIXERS - 1], 0, i2s1, 0},
  42. {mixer[TOTAL_MIXERS - 1], 0, i2s1, 1},
  43. };
  44. Bounce buttons[] = { {0, 15}, {1, 15}, {2, 15}, };
  45. const int TOTAL_BUTTONS = sizeof(buttons) / sizeof(Bounce);
  46. void guitarHeroMode();
  47. void printVoices();
  48. void setVolume() {
  49. sgtl5000_1.volume(0.8*(analogRead(PIN_A2) - 1) / 1022.0);
  50. }
  51. struct voice_t {
  52. int wavetable_id;
  53. byte channel;
  54. byte note;
  55. };
  56. voice_t voices[TOTAL_VOICES];
  57. IntervalTimer midiMapTimer;
  58. IntervalTimer guitarHeroTimer;
  59. IntervalTimer volumeTimer;
  60. void setup() {
  61. Serial.begin(115200);
  62. pinMode(0, INPUT_PULLUP);
  63. pinMode(1, INPUT_PULLUP);
  64. pinMode(2, INPUT_PULLUP);
  65. AudioMemory(120);
  66. sgtl5000_1.enable();
  67. sgtl5000_1.volume(0.8);
  68. for (int i = 0; i < TOTAL_VOICES; ++i) {
  69. wavetable[i].setInstrument(Pizzicato);
  70. wavetable[i].amplitude(1);
  71. voices[i].wavetable_id = i;
  72. voices[i].channel = voices[i].note = 0xFF;
  73. }
  74. for (int i = 0; i < TOTAL_MIXERS - 1; ++i)
  75. for (int j = 0; j < 4; ++j)
  76. mixer[i].gain(j, 0.50);
  77. for (int i = 0; i < 4; ++i)
  78. mixer[TOTAL_MIXERS - 1].gain(i, i < SECONDARY_MIXERS ? 1.0 / SECONDARY_MIXERS : 0.0);
  79. usbMIDI.setHandleNoteOn(OnNoteOn);
  80. usbMIDI.setHandleNoteOff(OnNoteOff);
  81. //volumeTimer.begin(setVolume, 100000);
  82. //guitarHeroTimer.begin(guitarHeroMode, 1000000 / 120);
  83. //midiMapTimer.begin(printVoices, 5000);
  84. delay(2000);
  85. }
  86. void loop() {
  87. usbMIDI.read();
  88. //for (int i = 0; i < TOTAL_BUTTONS; ++i) buttons[i].update();
  89. //if (buttons[0].fallingEdge()) AudioSynthWavetable::print_performance();
  90. //if (buttons[1].risingEdge()) {
  91. // midiMapTimer.end();
  92. // Serial.print('\n');
  93. //}
  94. //if (buttons[1].fallingEdge()) midiMapTimer.begin(printVoices, 5000);
  95. //if (buttons[2].risingEdge()) guitarHeroTimer.end();
  96. //if (buttons[2].fallingEdge())
  97. // guitarHeroTimer.begin(guitarHeroMode, 1000000/60);
  98. }
  99. int allocateVoice(byte channel, byte note);
  100. int findVoice(byte channel, byte note);
  101. void freeVoices();
  102. int used_voices = 0;
  103. int stopped_voices = 0;
  104. int evict_voice = 0;
  105. int notes_played = 0;
  106. void OnPress(int key)
  107. {
  108. Serial.print("key '");
  109. Serial.print((char)key);
  110. Serial.print("' ");
  111. Serial.println(key);
  112. //Serial.print("key ");
  113. //Serial.print((char)keyboard1.getKey());
  114. //Serial.print(" ");
  115. //Serial.print((char)keyboard2.getKey());
  116. //Serial.println();
  117. }
  118. void OnControlChange(byte channel, byte control, byte value)
  119. {
  120. Serial.print("Control Change, ch=");
  121. Serial.print(channel);
  122. Serial.print(", control=");
  123. Serial.print(control);
  124. Serial.print(", value=");
  125. Serial.print(value);
  126. Serial.println();
  127. }
  128. void OnNoteOn(byte channel, byte note, byte velocity) {
  129. notes_played++;
  130. #ifdef DEBUG_ALLOC
  131. //Serial.printf("**** NoteOn: channel==%hhu,note==%hhu ****\n", channel, note);
  132. printVoices();
  133. #endif //DEBUG_ALLOC
  134. freeVoices();
  135. int wavetable_id = allocateVoice(channel, note);
  136. switch (channel) {
  137. case 1:
  138. wavetable[wavetable_id].setInstrument(BasicFlute1);
  139. break;
  140. case 2:
  141. wavetable[wavetable_id].setInstrument(FrenchHorns);
  142. break;
  143. case 3:
  144. wavetable[wavetable_id].setInstrument(Ocarina);
  145. break;
  146. case 4:
  147. wavetable[wavetable_id].setInstrument(Ocarina);
  148. break;
  149. case 5:
  150. wavetable[wavetable_id].setInstrument(Pizzicato);
  151. break;
  152. default:
  153. wavetable[wavetable_id].setInstrument(Pizzicato);
  154. break;
  155. }
  156. wavetable[wavetable_id].playNote(note, velocity);
  157. #ifdef DEBUG_ALLOC
  158. printVoices();
  159. #endif //DEBUG_ALLOC
  160. }
  161. void OnNoteOff(byte channel, byte note, byte velocity) {
  162. #ifdef DEBUG_ALLOC
  163. //Serial.printf("\n**** NoteOff: channel==%hhu,note==%hhu ****", channel, note);
  164. printVoices();
  165. #endif //DEBUG_ALLOC
  166. int wavetable_id = findVoice(channel, note);
  167. if (wavetable_id != TOTAL_VOICES)
  168. wavetable[wavetable_id].stop();
  169. #ifdef DEBUG_ALLOC
  170. printVoices();
  171. #endif //DEBUG_ALLOC
  172. }
  173. int allocateVoice(byte channel, byte note) {
  174. int i;
  175. int nonfree_voices = stopped_voices + used_voices;
  176. if (nonfree_voices < TOTAL_VOICES) {
  177. for (i = nonfree_voices; i < TOTAL_VOICES && voices[i].channel != channel; ++i);
  178. if (i < TOTAL_VOICES) {
  179. voice_t temp = voices[i];
  180. voices[i] = voices[nonfree_voices];
  181. voices[nonfree_voices] = temp;
  182. }
  183. i = nonfree_voices;
  184. used_voices++;
  185. }
  186. else {
  187. if (stopped_voices) {
  188. i = evict_voice % stopped_voices;
  189. voice_t temp = voices[i];
  190. stopped_voices--;
  191. voices[i] = voices[stopped_voices];
  192. voices[stopped_voices] = temp;
  193. used_voices++;
  194. i = stopped_voices;
  195. }
  196. else
  197. i = evict_voice;
  198. }
  199. voices[i].channel = channel;
  200. voices[i].note = note;
  201. evict_voice++;
  202. evict_voice %= TOTAL_VOICES;
  203. return voices[i].wavetable_id;
  204. }
  205. int findVoice(byte channel, byte note) {
  206. int i;
  207. //find match
  208. int nonfree_voices = stopped_voices + used_voices;
  209. for (i = stopped_voices; i < nonfree_voices && !(voices[i].channel == channel && voices[i].note == note); ++i);
  210. //return TOTAL_VOICES if no match
  211. if (i == (nonfree_voices)) return TOTAL_VOICES;
  212. voice_t temp = voices[i];
  213. voices[i] = voices[stopped_voices];
  214. voices[stopped_voices] = temp;
  215. --used_voices;
  216. return voices[stopped_voices++].wavetable_id;
  217. }
  218. void freeVoices() {
  219. for (int i = 0; i < stopped_voices; i++)
  220. if (wavetable[voices[i].wavetable_id].isPlaying() == false) {
  221. voice_t temp = voices[i];
  222. --stopped_voices;
  223. voices[i] = voices[stopped_voices];
  224. int nonfree_voices = stopped_voices + used_voices;
  225. voices[stopped_voices] = voices[nonfree_voices];
  226. voices[nonfree_voices] = temp;
  227. }
  228. }
  229. void guitarHeroMode() { // now unicorn friendly
  230. const int RESET = 4;
  231. const int MIDI_NOTES = 128;
  232. static char line[MIDI_NOTES + 1] = { 0 };
  233. static int accumulated = 0;
  234. if (!accumulated) {
  235. for (int i = 0; i < MIDI_NOTES; ++i) line[i] = '-';
  236. ++accumulated;
  237. }
  238. for (int i = stopped_voices; i < used_voices + stopped_voices; ++i) line[voices[i].note] = '*';
  239. if (accumulated == RESET) {
  240. Serial.println(line);
  241. accumulated = 0;
  242. }
  243. else {
  244. ++accumulated;
  245. }
  246. }
  247. const char* note_map[] = {
  248. "C","C#","D","D#","E","F","F#","G","G#","A","A#","B"
  249. };
  250. void printVoices() {
  251. static int last_notes_played = notes_played;
  252. if (last_notes_played == notes_played)
  253. return;
  254. last_notes_played = notes_played;
  255. int usage = AudioProcessorUsage();
  256. Serial.printf("\nCPU:%03i voices:%02i CPU/Voice:%02i evict:%02i", usage, used_voices, usage / used_voices, evict_voice);
  257. for (int i = 0; i < used_voices; ++i)
  258. Serial.printf(" %02hhu %-2s", voices[i].channel, note_map[voices[i].note % 12]);
  259. }