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.

138 lines
2.9KB

  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. delay(200);
  57. Serial.println("Begin PlayMidiTones");
  58. delay(50);
  59. Serial.println("setup done");
  60. AudioProcessorUsageMaxReset();
  61. AudioMemoryUsageMaxReset();
  62. }
  63. unsigned long last_time = millis();
  64. void loop()
  65. {
  66. unsigned char c,opcode,chan;
  67. unsigned long d_time;
  68. if(0) {
  69. // For PlayMidiTones this produces:
  70. // Proc = 6 (12), Mem = 2 (8)
  71. if(millis() - last_time >= 5000) {
  72. Serial.print("Proc = ");
  73. Serial.print(AudioProcessorUsage());
  74. Serial.print(" (");
  75. Serial.print(AudioProcessorUsageMax());
  76. Serial.print("), Mem = ");
  77. Serial.print(AudioMemoryUsage());
  78. Serial.print(" (");
  79. Serial.print(AudioMemoryUsageMax());
  80. Serial.println(")");
  81. last_time = millis();
  82. }
  83. }
  84. // Volume control
  85. int n = analogRead(15);
  86. if (n != volume) {
  87. volume = n;
  88. codec.volume((float)n / 1023);
  89. }
  90. c = *sp++;
  91. opcode = c & 0xf0;
  92. chan = c & 0x07;
  93. if(c < 0x80) {
  94. // Delay
  95. d_time = (c << 8) | *sp++;
  96. delay(d_time);
  97. return;
  98. }
  99. if(*sp == CMD_STOP) {
  100. Serial.println("DONE");
  101. while(1);
  102. }
  103. // It is a command
  104. if(opcode == CMD_STOPNOTE) {
  105. waves[chan]->amplitude(0);
  106. return;
  107. }
  108. if(opcode == CMD_PLAYNOTE) {
  109. waves[chan]->frequency(tune_frequencies2_PGM[*sp++]);
  110. waves[chan]->amplitude(AMPLITUDE);
  111. return;
  112. }
  113. if(opcode == CMD_RESTART) {
  114. sp = score;
  115. return;
  116. }
  117. }