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

334 行
12KB

  1. #include "bassoon_samples.h"
  2. #include "clarinet_samples.h"
  3. #include "distortiongt_samples.h"
  4. #include "epiano_samples.h"
  5. #include "flute_samples.h"
  6. #include "frenchhorn_samples.h"
  7. #include "glockenspiel_samples.h"
  8. #include "gtfretnoise_samples.h"
  9. #include "harmonica_samples.h"
  10. #include "harp_samples.h"
  11. #include "mutedgtr_samples.h"
  12. #include "nylonstrgtr_samples.h"
  13. #include "oboe_samples.h"
  14. #include "overdrivegt_samples.h"
  15. #include "recorder_samples.h"
  16. #include "standard_DRUMS_samples.h"
  17. #include "steelstrgtr_samples.h"
  18. #include "strings_samples.h"
  19. #include "timpani_samples.h"
  20. #include "trombone_samples.h"
  21. #include "trumpet_samples.h"
  22. #include "tuba_samples.h"
  23. #include "piano_samples.h"
  24. #include "vibraphone_samples.h"
  25. #include <Bounce.h>
  26. #include <Audio.h>
  27. #include <Wire.h>
  28. #include <SPI.h>
  29. #include <SD.h>
  30. #include <SerialFlash.h>
  31. //#define DEBUG_ALLOC
  32. const int TOTAL_VOICES = 64;
  33. const int TOTAL_MIXERS = 21;
  34. const int SECONDARY_MIXERS = 4;
  35. AudioControlSGTL5000 sgtl5000_1;
  36. AudioSynthWavetable wavetable[TOTAL_VOICES];
  37. AudioMixer4 mixer[TOTAL_MIXERS];
  38. AudioOutputI2S i2s1;
  39. AudioConnection patchCord[] = {
  40. {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},
  41. {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},
  42. {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},
  43. {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},
  44. {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},
  45. {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},
  46. {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},
  47. {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},
  48. {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},
  49. {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},
  50. {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},
  51. {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},
  52. {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},
  53. {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},
  54. {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},
  55. {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},
  56. {mixer[TOTAL_MIXERS - 2], 0, mixer[TOTAL_MIXERS - 1], 0},
  57. {mixer[TOTAL_MIXERS - 3], 0, mixer[TOTAL_MIXERS - 1], 1},
  58. {mixer[TOTAL_MIXERS - 4], 0, mixer[TOTAL_MIXERS - 1], 2},
  59. {mixer[TOTAL_MIXERS - 5], 0, mixer[TOTAL_MIXERS - 1], 3},
  60. {mixer[TOTAL_MIXERS - 1], 0, i2s1, 0},
  61. {mixer[TOTAL_MIXERS - 1], 0, i2s1, 1},
  62. };
  63. Bounce buttons[] = { {0, 15}, {1, 15}, {2, 15}, };
  64. const int TOTAL_BUTTONS = sizeof(buttons) / sizeof(Bounce);
  65. void guitarHeroMode();
  66. void printVoices();
  67. void setVolume() {
  68. sgtl5000_1.volume(0.8*(analogRead(PIN_A2) - 1) / 1022.0);
  69. }
  70. struct voice_t {
  71. int wavetable_id;
  72. byte channel;
  73. byte note;
  74. };
  75. voice_t voices[TOTAL_VOICES];
  76. IntervalTimer midiMapTimer;
  77. IntervalTimer guitarHeroTimer;
  78. IntervalTimer volumeTimer;
  79. void setup() {
  80. Serial.begin(115200);
  81. pinMode(0, INPUT_PULLUP);
  82. pinMode(1, INPUT_PULLUP);
  83. pinMode(2, INPUT_PULLUP);
  84. AudioMemory(120);
  85. sgtl5000_1.enable();
  86. sgtl5000_1.volume(0.8);
  87. for (int i = 0; i < TOTAL_VOICES; ++i) {
  88. wavetable[i].setInstrument(nylonstrgtr);
  89. voices[i].wavetable_id = i;
  90. voices[i].channel = voices[i].note = 0xFF;
  91. }
  92. for (int i = 0; i < TOTAL_MIXERS - 1; ++i)
  93. for (int j = 0; j < 4; ++j)
  94. mixer[i].gain(j, 0.50);
  95. for (int i = 0; i < 4; ++i)
  96. mixer[TOTAL_MIXERS - 1].gain(i, i < SECONDARY_MIXERS ? 2.0 / SECONDARY_MIXERS : 0.0);
  97. usbMIDI.setHandleNoteOn(OnNoteOn);
  98. usbMIDI.setHandleNoteOff(OnNoteOff);
  99. usbMIDI.setHandleProgramChange(OnProgramChange);
  100. usbMIDI.setHandleControlChange(OnControlChange);
  101. //volumeTimer.begin(setVolume, 100000);
  102. //guitarHeroTimer.begin(guitarHeroMode, 1000000 / 120);
  103. //midiMapTimer.begin(printVoices, 5000);
  104. delay(2000);
  105. }
  106. void loop() {
  107. usbMIDI.read();
  108. //for (int i = 0; i < TOTAL_BUTTONS; ++i) buttons[i].update();
  109. //if (buttons[0].fallingEdge()) AudioSynthWavetable::print_performance();
  110. //if (buttons[1].risingEdge()) {
  111. // midiMapTimer.end();
  112. // Serial.print('\n');
  113. //}
  114. //if (buttons[1].fallingEdge()) midiMapTimer.begin(printVoices, 5000);
  115. //if (buttons[2].risingEdge()) guitarHeroTimer.end();
  116. //if (buttons[2].fallingEdge())
  117. // guitarHeroTimer.begin(guitarHeroMode, 1000000/60);
  118. }
  119. int allocateVoice(byte channel, byte note);
  120. int findVoice(byte channel, byte note);
  121. void freeVoices();
  122. int used_voices = 0;
  123. int stopped_voices = 0;
  124. int evict_voice = 0;
  125. int notes_played = 0;
  126. void OnPress(int key)
  127. {
  128. Serial.print("key '");
  129. Serial.print((char)key);
  130. Serial.print("' ");
  131. Serial.println(key);
  132. //Serial.print("key ");
  133. //Serial.print((char)keyboard1.getKey());
  134. //Serial.print(" ");
  135. //Serial.print((char)keyboard2.getKey());
  136. //Serial.println();
  137. }
  138. const instrument_data* const midi_map[] = {
  139. &piano, &piano, &piano, &piano, &piano, &piano, &piano, &piano, // 000: pianos
  140. &vibraphone, &vibraphone, &vibraphone, &vibraphone, &vibraphone, &vibraphone, &vibraphone, &vibraphone, // 008: chrom percus
  141. &harmonica, &harmonica, &harmonica, &harmonica, &harmonica, &harmonica, &harmonica, &harmonica, // 016: organs
  142. &nylonstrgtr, &steelstrgtr, &nylonstrgtr, &nylonstrgtr, &mutedgtr, &overdrivegt, &distortiongt, &nylonstrgtr, // 024: guitars
  143. &piano, &piano, &piano, &piano, &piano, &piano, &piano, &piano, // 032: bass
  144. &strings, &strings, &strings, &strings, &strings, &strings, &harp, &timpani, // 040: strings
  145. &strings, &strings, &strings, &strings, &strings, &strings, &strings, &strings, // 048: ensemble
  146. &trumpet, &trombone, &tuba, &frenchhorn, &frenchhorn, &frenchhorn, &frenchhorn, &frenchhorn, // 056: brass
  147. &oboe, &oboe, &oboe, &oboe, &oboe, &oboe, &bassoon, &clarinet, // 064: reed
  148. &flute, &flute, &recorder, &flute, &flute, &flute, &flute, &flute, // 072: pipe
  149. &piano, &piano, &piano, &piano, &piano, &piano, &piano, &piano, // 080: synth lead
  150. &piano, &piano, &piano, &piano, &piano, &piano, &piano, &piano, // 088: synth pad
  151. &piano, &piano, &piano, &piano, &piano, &piano, &piano, &piano, // 096: synth effect
  152. &piano, &piano, &piano, &piano, &piano, &piano, &piano, &piano, // 104: ethnic
  153. &timpani, &timpani, &timpani, &timpani, &timpani, &timpani, &timpani, &timpani, // 112: percussive
  154. &gtfretnoise, &gtfretnoise, &gtfretnoise, &gtfretnoise, &gtfretnoise, &gtfretnoise, &gtfretnoise, &gtfretnoise, // 120: sound effects
  155. };
  156. const instrument_data* channel_map[17] = {
  157. &piano, &piano, &piano, &piano, &piano, &piano, &piano, &piano, &piano, &piano, &standard_DRUMS, &piano, &piano, &piano, &piano, &piano, &piano,
  158. };
  159. int channel_vol[] = {
  160. 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
  161. };
  162. void OnControlChange(byte channel, byte control, byte value)
  163. {
  164. switch (control) {
  165. case 7: //volume
  166. channel_vol[channel] = value;
  167. break;
  168. default:
  169. break;
  170. }
  171. Serial.print("Control Change, ch=");
  172. Serial.print(channel);
  173. Serial.print(", control=");
  174. Serial.print(control);
  175. Serial.print(", value=");
  176. Serial.print(value);
  177. Serial.println();
  178. }
  179. void OnProgramChange(byte channel, byte program) {
  180. channel_map[channel] = channel != 10 ? midi_map[program] : &standard_DRUMS;
  181. }
  182. void OnNoteOn(byte channel, byte note, byte velocity) {
  183. notes_played++;
  184. #ifdef DEBUG_ALLOC
  185. //Serial.printf("**** NoteOn: channel==%hhu,note==%hhu ****\n", channel, note);
  186. printVoices();
  187. #endif //DEBUG_ALLOC
  188. freeVoices();
  189. int wavetable_id = allocateVoice(channel, note);
  190. wavetable[wavetable_id].setInstrument(*channel_map[channel]);
  191. wavetable[wavetable_id].playNote(note, (velocity*channel_vol[channel] + 0x80) >> 7);
  192. #ifdef DEBUG_ALLOC
  193. printVoices();
  194. #endif //DEBUG_ALLOC
  195. }
  196. void OnNoteOff(byte channel, byte note, byte velocity) {
  197. #ifdef DEBUG_ALLOC
  198. //Serial.printf("\n**** NoteOff: channel==%hhu,note==%hhu ****", channel, note);
  199. printVoices();
  200. #endif //DEBUG_ALLOC
  201. int wavetable_id = findVoice(channel, note);
  202. if (wavetable_id != TOTAL_VOICES)
  203. wavetable[wavetable_id].stop();
  204. #ifdef DEBUG_ALLOC
  205. printVoices();
  206. #endif //DEBUG_ALLOC
  207. }
  208. int allocateVoice(byte channel, byte note) {
  209. int i;
  210. int nonfree_voices = stopped_voices + used_voices;
  211. if (nonfree_voices < TOTAL_VOICES) {
  212. for (i = nonfree_voices; i < TOTAL_VOICES && voices[i].channel != channel; ++i);
  213. if (i < TOTAL_VOICES) {
  214. voice_t temp = voices[i];
  215. voices[i] = voices[nonfree_voices];
  216. voices[nonfree_voices] = temp;
  217. }
  218. i = nonfree_voices;
  219. used_voices++;
  220. }
  221. else {
  222. if (stopped_voices) {
  223. i = evict_voice % stopped_voices;
  224. voice_t temp = voices[i];
  225. stopped_voices--;
  226. voices[i] = voices[stopped_voices];
  227. voices[stopped_voices] = temp;
  228. used_voices++;
  229. i = stopped_voices;
  230. }
  231. else
  232. i = evict_voice;
  233. }
  234. voices[i].channel = channel;
  235. voices[i].note = note;
  236. evict_voice++;
  237. evict_voice %= TOTAL_VOICES;
  238. return voices[i].wavetable_id;
  239. }
  240. int findVoice(byte channel, byte note) {
  241. int i;
  242. //find match
  243. int nonfree_voices = stopped_voices + used_voices;
  244. for (i = stopped_voices; i < nonfree_voices && !(voices[i].channel == channel && voices[i].note == note); ++i);
  245. //return TOTAL_VOICES if no match
  246. if (i == (nonfree_voices)) return TOTAL_VOICES;
  247. voice_t temp = voices[i];
  248. voices[i] = voices[stopped_voices];
  249. voices[stopped_voices] = temp;
  250. --used_voices;
  251. return voices[stopped_voices++].wavetable_id;
  252. }
  253. void freeVoices() {
  254. for (int i = 0; i < stopped_voices; i++)
  255. if (wavetable[voices[i].wavetable_id].isPlaying() == false) {
  256. voice_t temp = voices[i];
  257. --stopped_voices;
  258. voices[i] = voices[stopped_voices];
  259. int nonfree_voices = stopped_voices + used_voices;
  260. voices[stopped_voices] = voices[nonfree_voices];
  261. voices[nonfree_voices] = temp;
  262. }
  263. }
  264. void guitarHeroMode() { // now unicorn friendly
  265. const int RESET = 4;
  266. const int MIDI_NOTES = 128;
  267. static char line[MIDI_NOTES + 1] = { 0 };
  268. static int accumulated = 0;
  269. if (!accumulated) {
  270. for (int i = 0; i < MIDI_NOTES; ++i) line[i] = '-';
  271. ++accumulated;
  272. }
  273. for (int i = stopped_voices; i < used_voices + stopped_voices; ++i) line[voices[i].note] = '*';
  274. if (accumulated == RESET) {
  275. Serial.println(line);
  276. accumulated = 0;
  277. }
  278. else {
  279. ++accumulated;
  280. }
  281. }
  282. const char* note_map[] = {
  283. "C","C#","D","D#","E","F","F#","G","G#","A","A#","B"
  284. };
  285. void printVoices() {
  286. static int last_notes_played = notes_played;
  287. if (last_notes_played == notes_played)
  288. return;
  289. last_notes_played = notes_played;
  290. int usage = AudioProcessorUsage();
  291. Serial.printf("\nCPU:%03i voices:%02i CPU/Voice:%02i evict:%02i", usage, used_voices, usage / used_voices, evict_voice);
  292. for (int i = 0; i < used_voices; ++i)
  293. Serial.printf(" %02hhu %-2s", voices[i].channel, note_map[voices[i].note % 12]);
  294. }