Guest User

audio tools record wav

a guest
Aug 31st, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 KB | None | 0 0
  1. /**
  2.  * @file player-sd-i2s.ino
  3.  * @brief example using the SD library
  4.  *
  5.  * @author Phil Schatzmann
  6.  * @copyright GPLv3
  7.  WAV FIOLES NEED BE 16B PCM
  8.  */
  9. #include <ButtonEvent.h>
  10.  
  11. #include "AudioTools.h"
  12. #include <SD.h>
  13. //#include "FS.h"
  14.  
  15. #include "AudioLibs/AudioSourceSDFAT.h"
  16. #include "AudioCodecs/CodecWAV.h"
  17.  
  18. #define i_bck 39//5
  19. #define i_ws 6
  20. #define i_data 40 //4
  21. #define i_rx 18
  22.  
  23.  
  24. #define buttonPin  0
  25. #define buttonPin2 16
  26.  
  27. const char *dirSounds = "/sounds";
  28. const char *dirRec = "/rec";
  29.  
  30. File filePlay;
  31. File fileRec;
  32.  
  33. I2SStream i2s;
  34. AudioInfo info(16000, 1, 16);
  35.  
  36. EncodedAudioStream decoder(&i2s,new WAVDecoder());
  37. EncodedAudioStream encoder(&fileRec, new WAVEncoder());
  38. StreamCopy copier(decoder,filePlay);
  39.  
  40. long bootTime = millis();
  41. void setup() {
  42.   Serial.begin(115200);    while(!Serial){if(millis()-bootTime>2000){break;}}//wait for serial unless its going to take too long
  43.  
  44.   if(!SD.begin()){Serial.println("SD Init Failed!"); stop();}
  45.   listSounds(dirSounds);
  46.   filePlay = SD.open("/sounds/blip2.wav"); //bicycle_bell.wav , blurp_x2.wav, boing_x2.wav
  47.   AudioLogger::instance().begin(Serial, AudioLogger::Info);
  48.  
  49.   pinMode(buttonPin, INPUT_PULLUP);
  50.   pinMode(buttonPin2, INPUT_PULLUP);
  51.  
  52.   ButtonEvent.addButton(buttonPin,       //button pin
  53.                         onB1D,   //onDown event function
  54.                         onB1U,     //onUp event function
  55.                         NULL,   //onHold event function
  56.                         750,NULL,NULL     //hold time in milliseconds
  57.                         );     //double time interval      
  58.  
  59.   ButtonEvent.addButton(buttonPin2,       //button pin
  60.                         onB2D,   //onDown event function
  61.                         onB2U,     //onUp event function
  62.                         NULL,   //onHold event function
  63.                         750,NULL,NULL     //hold time in milliseconds
  64.                         );     //double time interval      
  65.  
  66.  
  67.   // setup output
  68.   auto cfg = i2s.defaultConfig(RXTX_MODE);
  69.   cfg.copyFrom(info);
  70.  
  71.   cfg.pin_ws = i_ws;
  72.   cfg.pin_bck = i_bck;
  73.   cfg.pin_data = i_data;
  74.   cfg.pin_data_rx = i_rx;
  75.  
  76.   i2s.begin(cfg);
  77.   decoder.begin(cfg);
  78.   encoder.begin(info);
  79.   copier.begin();
  80. }
  81. void loop() {
  82.   //player.copy();
  83.   ButtonEvent.loop();
  84.  copier.copy();
  85.  
  86. }
  87.  
  88. void recStart(){
  89.   decoder.flush();
  90.   char newFileName[14];
  91.   sprintf(newFileName, "REC-%06d.wav", rand() % 1000000);
  92. //  fileRec = SD.open(fullPath(dirRec,newFileName),FILE_WRITE);
  93. fileRec = SD.open("/rec/r1.wav",FILE_WRITE);
  94.   if(fileRec){
  95.     Serial.printf("Opened %s to record. \n",fileRec.name());
  96.     decoder.end();
  97.     copier.end();
  98.     encoder.begin(info);
  99.     copier.begin(encoder,i2s);
  100.   }else{
  101.     Serial.printf("Failed to create file %s \n",newFileName);
  102.   }
  103. }
  104.  
  105. void recStop(){
  106.     Serial.println("recStop()");
  107.     copier.end();
  108.     encoder.end();
  109.    if (fileRec) {
  110.       fileRec.flush();
  111.       Serial.print("File has ");
  112.       Serial.print(fileRec.size());
  113.       Serial.println(" bytes");
  114.       fileRec.close();
  115.     }
  116.     decoder.begin();
  117.     //copier.begin(decoder, filePlay);
  118. }
  119.  
  120. void playSound(char *filename){
  121.   Serial.printf("playSound(%s)/n",filename);
  122.   filePlay = SD.open(filename);
  123.   decoder.flush();
  124.   copier.begin(decoder, filePlay);
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment