SamplePlayer.ino 3.9KB

10 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <Audio.h>
  2. #include <Wire.h>
  3. #include <SPI.h>
  4. #include <SD.h>
  5. #include <SerialFlash.h>
  6. #include <Bounce.h>
  7. // WAV files converted to code by wav2sketch
  8. #include "AudioSampleSnare.h" // http://www.freesound.org/people/KEVOY/sounds/82583/
  9. #include "AudioSampleTomtom.h" // http://www.freesound.org/people/zgump/sounds/86334/
  10. #include "AudioSampleHihat.h" // http://www.freesound.org/people/mhc/sounds/102790/
  11. #include "AudioSampleKick.h" // http://www.freesound.org/people/DWSD/sounds/171104/
  12. #include "AudioSampleGong.h" // http://www.freesound.org/people/juskiddink/sounds/86773/
  13. #include "AudioSampleCashregister.h" // http://www.freesound.org/people/kiddpark/sounds/201159/
  14. // Create the Audio components. These should be created in the
  15. // order data flows, inputs/sources -> processing -> outputs
  16. //
  17. AudioPlayMemory sound0;
  18. AudioPlayMemory sound1; // six memory players, so we can play
  19. AudioPlayMemory sound2; // all six sounds simultaneously
  20. AudioPlayMemory sound3;
  21. AudioPlayMemory sound4;
  22. AudioPlayMemory sound5;
  23. AudioMixer4 mix1; // two 4-channel mixers are needed in
  24. AudioMixer4 mix2; // tandem to combine 6 audio sources
  25. AudioOutputI2S headphones;
  26. AudioOutputAnalog dac; // play to both I2S audio board and on-chip DAC
  27. // Create Audio connections between the components
  28. //
  29. AudioConnection c1(sound0, 0, mix1, 0);
  30. AudioConnection c2(sound1, 0, mix1, 1);
  31. AudioConnection c3(sound2, 0, mix1, 2);
  32. AudioConnection c4(sound3, 0, mix1, 3);
  33. AudioConnection c5(mix1, 0, mix2, 0); // output of mix1 into 1st input on mix2
  34. AudioConnection c6(sound4, 0, mix2, 1);
  35. AudioConnection c7(sound5, 0, mix2, 2);
  36. AudioConnection c8(mix2, 0, headphones, 0);
  37. AudioConnection c9(mix2, 0, headphones, 1);
  38. AudioConnection c10(mix2, 0, dac, 0);
  39. // Create an object to control the audio shield.
  40. //
  41. AudioControlSGTL5000 audioShield;
  42. // Bounce objects to read six pushbuttons (pins 0-5)
  43. //
  44. Bounce button0 = Bounce(0, 5);
  45. Bounce button1 = Bounce(1, 5); // 5 ms debounce time
  46. Bounce button2 = Bounce(2, 5);
  47. Bounce button3 = Bounce(3, 5);
  48. Bounce button4 = Bounce(4, 5);
  49. Bounce button5 = Bounce(5, 5);
  50. void setup() {
  51. // Configure the pushbutton pins for pullups.
  52. // Each button should connect from the pin to GND.
  53. pinMode(0, INPUT_PULLUP);
  54. pinMode(1, INPUT_PULLUP);
  55. pinMode(2, INPUT_PULLUP);
  56. pinMode(3, INPUT_PULLUP);
  57. pinMode(4, INPUT_PULLUP);
  58. pinMode(5, INPUT_PULLUP);
  59. // Audio connections require memory to work. For more
  60. // detailed information, see the MemoryAndCpuUsage example
  61. AudioMemory(10);
  62. // turn on the output
  63. audioShield.enable();
  64. audioShield.volume(0.5);
  65. // by default the Teensy 3.1 DAC uses 3.3Vp-p output
  66. // if your 3.3V power has noise, switching to the
  67. // internal 1.2V reference can give you a clean signal
  68. //dac.analogReference(INTERNAL);
  69. // reduce the gain on mixer channels, so more than 1
  70. // sound can play simultaneously without clipping
  71. mix1.gain(0, 0.4);
  72. mix1.gain(1, 0.4);
  73. mix1.gain(2, 0.4);
  74. mix1.gain(3, 0.4);
  75. mix2.gain(1, 0.4);
  76. mix2.gain(2, 0.4);
  77. }
  78. void loop() {
  79. // Update all the button objects
  80. button0.update();
  81. button1.update();
  82. button2.update();
  83. button3.update();
  84. button4.update();
  85. button5.update();
  86. // When the buttons are pressed, just start a sound playing.
  87. // The audio library will play each sound through the mixers
  88. // so any combination can play simultaneously.
  89. //
  90. if (button0.fallingEdge()) {
  91. sound0.play(AudioSampleSnare);
  92. }
  93. if (button1.fallingEdge()) {
  94. sound1.play(AudioSampleTomtom);
  95. }
  96. if (button2.fallingEdge()) {
  97. sound2.play(AudioSampleHihat);
  98. }
  99. if (button3.fallingEdge()) {
  100. sound3.play(AudioSampleKick);
  101. }
  102. if (button4.fallingEdge()) {
  103. // comment this line to work with Teensy 3.0.
  104. // the Gong sound is very long, too much for 3.0's memory
  105. sound4.play(AudioSampleGong);
  106. }
  107. if (button5.fallingEdge()) {
  108. sound5.play(AudioSampleCashregister);
  109. }
  110. }