Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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