WAVRecorder icon indicating copy to clipboard operation
WAVRecorder copied to clipboard

Record as long as I want

Open aibnus opened this issue 1 year ago • 4 comments

Is there any method to record audio as long as I press a button? Like a proper recorder

aibnus avatar Sep 07 '23 11:09 aibnus

Hi! Thanks for reaching out. It can record audio in adjustable chunks of time. When you call the API for recording you give it the period of recording. And you can for example set chunk size to 10 sec and record for infinite time in a loop.

Hope this helps! Regards A-R-S-D

AlirezaSalehy avatar Sep 09 '23 21:09 AlirezaSalehy

It kinda work but, I got a new problem when I record it only record for a few seconds and then soft wdt reset happened. I try to record when I press a button using interrupt

#include "src/WAVRecorder.h"

#define SAMPLE_RATE 16000
#define SAMPLE_LEN 8

// Hardware SPI's CS pin which is different in each board
#define CS_PIN D4

// The analog pins (ADC inputs) which microphone outputs are connected to.
#define MIC_PIN_1 A0

#define PUSH_BUTTON D1

#define NUM_CHANNELS 1
channel_t channels[] = { { MIC_PIN_1 } };

char file_name[13] = "/audio00.wav";
int file_number = 0;
File dataFile;

bool record_flag = false;

WAVRecorder* wr;
WAVGenerator* wgen;

void record();

void setup() {
  for (int i = 0; i < sizeof(channels) / sizeof(channel_t); i++) {
    pinMode(channels[i].ADCPin, INPUT);
  }

  pinMode(PUSH_BUTTON, INPUT_PULLUP);
  attachInterrupt(PUSH_BUTTON, record, FALLING);

  Serial.begin(115200);
  Serial.println();

  // put your setup code here, to run once:
  if (!SD.begin(CS_PIN)) {
    Serial.println("Failes to initialize SD!");
  } else {
    Serial.println("SD opened successfuly");
  }
  SPI.setClockDivider(SPI_CLOCK_DIV2); // This is becuase feeding SD Card with more than 40 Mhz, leads to unstable operation.
  // (Also depends on SD class) ESP8266 & ESP32 SPI clock with no division is 80 Mhz.

  wr = new WAVRecorder(12, channels, NUM_CHANNELS, SAMPLE_RATE, SAMPLE_LEN, &Serial);
}

void loop() {
  while (record_flag) {
    // Setting file to store recodring
    wr->setFile(&dataFile);

    wr->startBlocking(1000);
  }

  yield();
  delay(100);
}

void ICACHE_RAM_ATTR record() {
  if (!record_flag) {
    record_flag = true;

    if (SD.exists(file_name)) {
      file_number++;
    }

    file_name[6] = (file_number / 10) + 48;
    file_name[7] = (file_number % 10) + 48;

    dataFile = SD.open(file_name, FILE_WRITE);
    if (!dataFile) {
      Serial.println("Failed to open the file!");
      return;
    }

    Serial.println("Opening the file");
  } else {
    record_flag = false;

    Serial.println("File Created");
  }
}

This is what on the serial monitor

<<<stack<<<

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3424, room 16 
tail 0
chksum 0x2e
load 0x3fff20b8, len 40, room 8 
tail 0
chksum 0x2b
csum 0x2b
v0004c4a0
~ld

aibnus avatar Sep 10 '23 04:09 aibnus

Sorry for my very late response due to the fact that I haven't been able to make time and prepare proper requirements to investigate the issue that you have encountered.

At the moment I'm also not able to do so. So please accept my sincere apology. By the way, have you been able to figure it out on your own? If anybody else has encountered such an issue please let us know.

With kind regards!

AlirezaSalehy avatar Oct 09 '23 16:10 AlirezaSalehy

What if you create another method called startblockingInfinite() and use a global variable called blockingstopped with another method called stopblocking:

bool blockingstopped = false

void WAVRecorder::startBlockingInfinite() {

start();

while (!blockingstopped) {
	wg->writeChunks();
}
blockingstopped = true
stop();

}

void stopblocking() {

blockingstopped = true.

}

rickyelqasem avatar Feb 04 '24 10:02 rickyelqasem