Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

215 rindas
5.8KB

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