Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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