Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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