Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. ```
  2. /***************************************************
  3. This is an example for the Adafruit VS1053 Codec Breakout
  4.  
  5. Designed specifically to work with the Adafruit VS1053 Codec Breakout
  6. ----> https://www.adafruit.com/products/1381
  7.  
  8. Adafruit invests time and resources providing this open source code,
  9. please support Adafruit and open-source hardware by purchasing
  10. products from Adafruit!
  11.  
  12. Written by Limor Fried/Ladyada for Adafruit Industries.
  13. BSD license, all text above must be included in any redistribution
  14. ****************************************************/
  15.  
  16. // This is a very beta demo of Ogg Vorbis recording. It works...
  17. // Connect a button to digital 7 on the Arduino and use that to
  18. // start and stop recording.
  19.  
  20. // A mic or line-in connection is required. See page 13 of the
  21. // datasheet for wiring
  22.  
  23. // Don't forget to copy the v44k1q05.img patch to your micro SD
  24. // card before running this example!
  25.  
  26.  
  27. // include SPI, MP3 and SD libraries
  28. #include <SPI.h>
  29. #include <Adafruit_VS1053.h>
  30. #include <SD.h>
  31.  
  32. // define the pins used
  33. #define SHIELD_RESET -1 // VS1053 reset pin (unused!)
  34. #define SHIELD_CS 7 // VS1053 chip select pin (output)
  35. #define SHIELD_DCS 6 // VS1053 Data/command select pin (output)
  36. #define CLK 13 // SPI Clock, shared with SD card
  37. #define MISO 12 // Input data, from VS1053/SD card
  38. #define MOSI 11 // Output data, to VS1053/SD card
  39. #define CARDCS 4 // Card chip select pin
  40. #define DREQ 3 // VS1053 Data request, ideally an Interrupt pin
  41. #define REC_BUTTON 10
  42.  
  43. Adafruit_VS1053_FilePlayer musicPlayer =
  44. Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
  45.  
  46. File recording; // the file we will save our recording to
  47. #define RECBUFFSIZE 128 // 64 or 128 bytes.
  48. uint8_t recording_buffer[RECBUFFSIZE];
  49.  
  50. void setup() {
  51. Serial.begin(9600);
  52. Serial.println("Adafruit VS1053 Ogg Recording Test");
  53.  
  54. // initialise the music player
  55. if (!musicPlayer.begin()) {
  56. Serial.println("VS1053 not found");
  57. while (1); // don't do anything more
  58. }
  59.  
  60. musicPlayer.sineTest(0x44, 500); // Make a tone to indicate VS1053 is working
  61.  
  62. if (!SD.begin(CARDCS)) {
  63. Serial.println("SD failed, or not present");
  64. while (1); // don't do anything more
  65. }
  66. Serial.println("SD OK!"); // when the button is pressed, record!
  67.  
  68.  
  69. // Set volume for left, right channels. lower numbers == louder volume!
  70. musicPlayer.setVolume(10, 10);
  71.  
  72. pinMode(REC_BUTTON, INPUT);
  73. digitalWrite(REC_BUTTON, HIGH);
  74.  
  75. // load plugin from SD card! We'll use mono 44.1KHz, high quality
  76. if (! musicPlayer.prepareRecordOgg("v44k1q05.img")) {
  77. Serial.println("Couldn't load plugin!");
  78. while (1);
  79. }
  80. }
  81.  
  82. uint8_t isRecording = false;
  83.  
  84. void loop() {
  85. Serial.println(digitalRead(REC_BUTTON));
  86. if (!isRecording && !digitalRead(REC_BUTTON)) {
  87. Serial.println("Begin recording");
  88. isRecording = true;
  89. // Check if the file exists already
  90. char filename[15];
  91. strcpy(filename, "RECORD00.OGG");
  92. for (uint8_t i = 0; i < 100; i++) {
  93. filename[6] = '0' + i / 10;
  94. filename[7] = '0' + i % 10;
  95. // create if does not exist, do not open existing, write, sync after write
  96. if (! SD.exists(filename)) {
  97. break;
  98. }
  99. }
  100.  
  101. Serial.print("Recording to "); Serial.println(filename);
  102. recording = SD.open(filename, FILE_WRITE);
  103. if (! recording) {
  104. Serial.println("Couldn't open file to record!");
  105. while (1);
  106. }
  107. musicPlayer.startRecordOgg(true); // use microphone (for linein, pass in 'false')
  108. }
  109. if (isRecording)
  110. saveRecordedData(isRecording);
  111. if (isRecording && !digitalRead(REC_BUTTON)) {
  112. Serial.println("End recording");
  113. musicPlayer.stopRecordOgg();
  114. isRecording = false;
  115. // flush all the data!
  116. saveRecordedData(isRecording);
  117. // close it up
  118. recording.close();
  119. delay(1000);
  120. }
  121. }
  122.  
  123. uint16_t saveRecordedData(boolean isrecord) {
  124. uint16_t written = 0;
  125.  
  126. // read how many words are waiting for us
  127. uint16_t wordswaiting = musicPlayer.recordedWordsWaiting();
  128.  
  129. // try to process 256 words (512 bytes) at a time, for best speed
  130. while (wordswaiting > 256) {
  131. //Serial.print("Waiting: "); Serial.println(wordswaiting);
  132. // for example 128 bytes x 4 loops = 512 bytes
  133. for (int x = 0; x < 512 / RECBUFFSIZE; x++) {
  134. // fill the buffer!
  135. for (uint16_t addr = 0; addr < RECBUFFSIZE; addr += 2) {
  136. uint16_t t = musicPlayer.recordedReadWord();
  137. //Serial.println(t, HEX);
  138. recording_buffer[addr] = t >> 8;
  139. recording_buffer[addr + 1] = t;
  140. }
  141. if (! recording.write(recording_buffer, RECBUFFSIZE)) {
  142. Serial.print("Couldn't write "); Serial.println(RECBUFFSIZE);
  143. while (1);
  144. }
  145. }
  146. // flush 512 bytes at a time
  147. recording.flush();
  148. written += 256;
  149. wordswaiting -= 256;
  150. }
  151.  
  152. wordswaiting = musicPlayer.recordedWordsWaiting();
  153. if (!isrecord) {
  154. Serial.print(wordswaiting); Serial.println(" remaining");
  155. // wrapping up the recording!
  156. uint16_t addr = 0;
  157. for (int x = 0; x < wordswaiting - 1; x++) {
  158. // fill the buffer!
  159. uint16_t t = musicPlayer.recordedReadWord();
  160. recording_buffer[addr] = t >> 8;
  161. recording_buffer[addr + 1] = t;
  162. if (addr > RECBUFFSIZE) {
  163. if (! recording.write(recording_buffer, RECBUFFSIZE)) {
  164. Serial.println("Couldn't write!");
  165. while (1);
  166. }
  167. recording.flush();
  168. addr = 0;
  169. }
  170. }
  171. if (addr != 0) {
  172. if (!recording.write(recording_buffer, addr)) {
  173. Serial.println("Couldn't write!"); while (1);
  174. }
  175. written += addr;
  176. }
  177. musicPlayer.sciRead(VS1053_SCI_AICTRL3);
  178. if (! (musicPlayer.sciRead(VS1053_SCI_AICTRL3) & (1 << 2))) {
  179. recording.write(musicPlayer.recordedReadWord() & 0xFF);
  180. written++;
  181. }
  182. recording.flush();
  183. }
  184.  
  185. return written;
  186. }
  187. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement