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.

146 lines
3.1KB

  1. #include <Audio.h>
  2. #include <Wire.h>
  3. //#include <WM8731.h>
  4. #include <SD.h>
  5. #include <SPI.h>
  6. #include "PlayMidiTones.h"
  7. unsigned char *sp = score;
  8. //AudioInputI2S adc;
  9. //AudioInputAnalog ana(16);
  10. AudioSynthWaveform sine0(AudioWaveformSine);
  11. AudioSynthWaveform sine1(AudioWaveformTriangle);
  12. AudioSynthWaveform sine2(AudioWaveformSquare);
  13. AudioSynthWaveform sine3(AudioWaveformSawtooth);
  14. AudioSynthWaveform sine4(AudioWaveformSine);
  15. AudioSynthWaveform sine5(AudioWaveformTriangle);
  16. AudioSynthWaveform sine6(AudioWaveformSquare);
  17. AudioSynthWaveform sine7(AudioWaveformSawtooth);
  18. AudioSynthWaveform *waves[8] = {
  19. &sine0,
  20. &sine1,
  21. &sine2,
  22. &sine3,
  23. &sine4,
  24. &sine5,
  25. &sine6,
  26. &sine7,
  27. };
  28. AudioMixer4 mixer1;
  29. AudioMixer4 mixer2;
  30. AudioOutputI2S audioOut;
  31. AudioConnection c0(sine0, 0, mixer1, 0);
  32. AudioConnection c1(sine1, 0, mixer1, 1);
  33. AudioConnection c2(sine2, 0, mixer1, 2);
  34. AudioConnection c3(sine3, 0, mixer1, 3);
  35. AudioConnection c4(sine4, 0, mixer2, 0);
  36. AudioConnection c5(sine5, 0, mixer2, 1);
  37. AudioConnection c6(sine6, 0, mixer2, 2);
  38. AudioConnection c7(sine7, 0, mixer2, 3);
  39. AudioConnection c11(mixer1, 0, audioOut, 0);
  40. AudioConnection c12(mixer2, 0, audioOut, 1);
  41. //AudioControl_WM8731 codec;
  42. AudioControlSGTL5000 codec;
  43. int volume = 50;
  44. void setup()
  45. {
  46. Serial.begin(115200);
  47. while (!Serial) ;
  48. delay(3000);
  49. // Audio connections require memory to work. For more
  50. // detailed information, see the MemoryAndCpuUsage example
  51. AudioMemory(8);
  52. codec.enable();
  53. codec.volume(0.5);
  54. // I want output on the line out too
  55. codec.unmuteLineout();
  56. // Comment out this code to hear what it sounds like
  57. // when the tones aren't ramped. (or change 88 to 0)
  58. // Set the ramp time for each wave
  59. for(int i = 0; i < 8;i++) {
  60. waves[i]->set_ramp_length(88);
  61. }
  62. delay(200);
  63. Serial.println("Begin PlayMidiTones");
  64. delay(50);
  65. Serial.println("setup done");
  66. AudioProcessorUsageMaxReset();
  67. AudioMemoryUsageMaxReset();
  68. }
  69. unsigned long last_time = millis();
  70. void loop()
  71. {
  72. unsigned char c,opcode,chan;
  73. unsigned long d_time;
  74. if(0) {
  75. // For PlayMidiTones this produces:
  76. // Proc = 6 (12), Mem = 2 (8)
  77. if(millis() - last_time >= 5000) {
  78. Serial.print("Proc = ");
  79. Serial.print(AudioProcessorUsage());
  80. Serial.print(" (");
  81. Serial.print(AudioProcessorUsageMax());
  82. Serial.print("), Mem = ");
  83. Serial.print(AudioMemoryUsage());
  84. Serial.print(" (");
  85. Serial.print(AudioMemoryUsageMax());
  86. Serial.println(")");
  87. last_time = millis();
  88. }
  89. }
  90. // Volume control
  91. int n = analogRead(15);
  92. if (n != volume) {
  93. volume = n;
  94. codec.volume((float)n / 1023);
  95. }
  96. c = *sp++;
  97. opcode = c & 0xf0;
  98. chan = c & 0x07;
  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. Serial.println("DONE");
  107. while(1);
  108. }
  109. // It is a command
  110. if(opcode == CMD_STOPNOTE) {
  111. waves[chan]->amplitude(0);
  112. return;
  113. }
  114. if(opcode == CMD_PLAYNOTE) {
  115. waves[chan]->frequency(tune_frequencies2_PGM[*sp++]);
  116. waves[chan]->amplitude(AMPLITUDE);
  117. return;
  118. }
  119. if(opcode == CMD_RESTART) {
  120. sp = score;
  121. return;
  122. }
  123. }