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.

212 lines
5.7KB

  1. // Record sound as raw data to a SD card, and play it back.
  2. //
  3. // Requires the audio shield:
  4. // http://www.pjrc.com/store/teensy3_audio.html
  5. //
  6. // Three pushbuttons need to be connected:
  7. // Record Button: pin 0 to GND
  8. // Stop Button: pin 1 to GND
  9. // Play Button: pin 2 to GND
  10. //
  11. // This example code is in the public domain.
  12. #include <Bounce.h>
  13. #include <Audio.h>
  14. #include <Wire.h>
  15. #include <SPI.h>
  16. #include <SD.h>
  17. #include <SerialFlash.h>
  18. // GUItool: begin automatically generated code
  19. AudioInputI2S i2s2; //xy=105,63
  20. AudioAnalyzePeak peak1; //xy=278,108
  21. AudioRecordQueue queue1; //xy=281,63
  22. AudioPlaySdRaw playRaw1; //xy=302,157
  23. AudioOutputI2S i2s1; //xy=470,120
  24. AudioConnection patchCord1(i2s2, 0, queue1, 0);
  25. AudioConnection patchCord2(i2s2, 0, peak1, 0);
  26. AudioConnection patchCord3(playRaw1, 0, i2s1, 0);
  27. AudioConnection patchCord4(playRaw1, 0, i2s1, 1);
  28. AudioControlSGTL5000 sgtl5000_1; //xy=265,212
  29. // GUItool: end automatically generated code
  30. // Bounce objects to easily and reliably read the buttons
  31. Bounce buttonRecord = Bounce(0, 8);
  32. Bounce buttonStop = Bounce(1, 8); // 8 = 8 ms debounce time
  33. Bounce buttonPlay = Bounce(2, 8);
  34. // which input on the audio shield will be used?
  35. const int myInput = AUDIO_INPUT_LINEIN;
  36. //const int myInput = AUDIO_INPUT_MIC;
  37. // Use these with the Teensy Audio Shield
  38. #define SDCARD_CS_PIN 10
  39. #define SDCARD_MOSI_PIN 7
  40. #define SDCARD_SCK_PIN 14
  41. // Use these with the Teensy 3.5 & 3.6 SD card
  42. //#define SDCARD_CS_PIN BUILTIN_SDCARD
  43. //#define SDCARD_MOSI_PIN 11 // not actually used
  44. //#define SDCARD_SCK_PIN 13 // not actually used
  45. // Use these for the SD+Wiz820 or other adaptors
  46. //#define SDCARD_CS_PIN 4
  47. //#define SDCARD_MOSI_PIN 11
  48. //#define SDCARD_SCK_PIN 13
  49. // Remember which mode we're doing
  50. int mode = 0; // 0=stopped, 1=recording, 2=playing
  51. // The file where data is recorded
  52. File frec;
  53. void setup() {
  54. // Configure the pushbutton pins
  55. pinMode(0, INPUT_PULLUP);
  56. pinMode(1, INPUT_PULLUP);
  57. pinMode(2, INPUT_PULLUP);
  58. // Audio connections require memory, and the record queue
  59. // uses this memory to buffer incoming audio.
  60. AudioMemory(60);
  61. // Enable the audio shield, select input, and enable output
  62. sgtl5000_1.enable();
  63. sgtl5000_1.inputSelect(myInput);
  64. sgtl5000_1.volume(0.5);
  65. // Initialize the SD card
  66. SPI.setMOSI(SDCARD_MOSI_PIN);
  67. SPI.setSCK(SDCARD_SCK_PIN);
  68. if (!(SD.begin(SDCARD_CS_PIN))) {
  69. // stop here if no SD card, but print a message
  70. while (1) {
  71. Serial.println("Unable to access the SD card");
  72. delay(500);
  73. }
  74. }
  75. }
  76. void loop() {
  77. // First, read the buttons
  78. buttonRecord.update();
  79. buttonStop.update();
  80. buttonPlay.update();
  81. // Respond to button presses
  82. if (buttonRecord.fallingEdge()) {
  83. Serial.println("Record Button Press");
  84. if (mode == 2) stopPlaying();
  85. if (mode == 0) startRecording();
  86. }
  87. if (buttonStop.fallingEdge()) {
  88. Serial.println("Stop Button Press");
  89. if (mode == 1) stopRecording();
  90. if (mode == 2) stopPlaying();
  91. }
  92. if (buttonPlay.fallingEdge()) {
  93. Serial.println("Play Button Press");
  94. if (mode == 1) stopRecording();
  95. if (mode == 0) startPlaying();
  96. }
  97. // If we're playing or recording, carry on...
  98. if (mode == 1) {
  99. continueRecording();
  100. }
  101. if (mode == 2) {
  102. continuePlaying();
  103. }
  104. // when using a microphone, continuously adjust gain
  105. if (myInput == AUDIO_INPUT_MIC) adjustMicLevel();
  106. }
  107. void startRecording() {
  108. Serial.println("startRecording");
  109. if (SD.exists("RECORD.RAW")) {
  110. // The SD library writes new data to the end of the
  111. // file, so to start a new recording, the old file
  112. // must be deleted before new data is written.
  113. SD.remove("RECORD.RAW");
  114. }
  115. frec = SD.open("RECORD.RAW", FILE_WRITE);
  116. if (frec) {
  117. queue1.begin();
  118. mode = 1;
  119. }
  120. }
  121. void continueRecording() {
  122. if (queue1.available() >= 2) {
  123. byte buffer[512];
  124. // Fetch 2 blocks from the audio library and copy
  125. // into a 512 byte buffer. The Arduino SD library
  126. // is most efficient when full 512 byte sector size
  127. // writes are used.
  128. memcpy(buffer, queue1.readBuffer(), 256);
  129. queue1.freeBuffer();
  130. memcpy(buffer+256, queue1.readBuffer(), 256);
  131. queue1.freeBuffer();
  132. // write all 512 bytes to the SD card
  133. elapsedMicros usec = 0;
  134. frec.write(buffer, 512);
  135. // Uncomment these lines to see how long SD writes
  136. // are taking. A pair of audio blocks arrives every
  137. // 5802 microseconds, so hopefully most of the writes
  138. // take well under 5802 us. Some will take more, as
  139. // the SD library also must write to the FAT tables
  140. // and the SD card controller manages media erase and
  141. // wear leveling. The queue1 object can buffer
  142. // approximately 301700 us of audio, to allow time
  143. // for occasional high SD card latency, as long as
  144. // the average write time is under 5802 us.
  145. //Serial.print("SD write, us=");
  146. //Serial.println(usec);
  147. }
  148. }
  149. void stopRecording() {
  150. Serial.println("stopRecording");
  151. queue1.end();
  152. if (mode == 1) {
  153. while (queue1.available() > 0) {
  154. frec.write((byte*)queue1.readBuffer(), 256);
  155. queue1.freeBuffer();
  156. }
  157. frec.close();
  158. }
  159. mode = 0;
  160. }
  161. void startPlaying() {
  162. Serial.println("startPlaying");
  163. playRaw1.play("RECORD.RAW");
  164. mode = 2;
  165. }
  166. void continuePlaying() {
  167. if (!playRaw1.isPlaying()) {
  168. playRaw1.stop();
  169. mode = 0;
  170. }
  171. }
  172. void stopPlaying() {
  173. Serial.println("stopPlaying");
  174. if (mode == 2) playRaw1.stop();
  175. mode = 0;
  176. }
  177. void adjustMicLevel() {
  178. // TODO: read the peak1 object and adjust sgtl5000_1.micGain()
  179. // if anyone gets this working, please submit a github pull request :-)
  180. }