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.

343 lines
11KB

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