How to record voice using VS1053 and ESP32 arduino IDE ?

Hi Class,

I'm testing a VS1053 board and an ESP32 devkit 1.0.

I compiled this code:

#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include <VS1053.h>

// MISO: D19    MOSI: D23   SCK: D18
#define VS1053_CS     5
#define VS1053_DCS    16
#define VS1053_DREQ   4
#define SDCARD_CS     22

const char *fileMP3 = "/teste.mp3";

VS1053 player(VS1053_CS, VS1053_DCS, VS1053_DREQ);

void playMP3() {

  File filemp3mp3;
  const int BUFFSIZE = 64;
  uint8_t mp3buff[BUFFSIZE];
  uint8_t bytesread = 0;

  Serial.println("Start play mp3");

  filemp3mp3 = SD.open(fileMP3);

  if (!filemp3mp3){
    Serial.println("File not found");
  }

  do {
    if (filemp3mp3) {
      bytesread = filemp3mp3.read(mp3buff, BUFFSIZE);
    }
    player.playChunk(mp3buff, bytesread);
  } while (bytesread);

  filemp3mp3.close();

  Serial.println("End play mp3");

}

void setup() {

  Serial.begin(115200);

  delay(10);

  if (!SD.begin(SDCARD_CS)) {
    Serial.println("SD card not found");
    return;
  }

  // start VS1053
  player.begin();
  player.switchToMp3Mode();
  player.setVolume(90);

  playMP3();
}

void loop() {


I recorded an MP3 on the SDHC card (test.MP3) and I can listen to it normally by connecting headphones to the VS1053 board.

The board has a microphone. How do I test it? I want to say something and also listen to it on the headphones and record it on the SD card.

Anybody know ? Or does anyone have a link with already tested code that does this ?

Thanks

I would read the datasheet and application notes.
Try oncle Google on ESP32 + VS1053.

Updating with more information:
The board I have is this:

vs1003

I'm trying this other code. I think it was written for Arduino Uno. I am using ESP32. Even so, with hope, I did a pin assignment but the serial monitor only showed: VS1053 not found.

/*************************************************** 
  This is an example for the Adafruit VS1053 Codec Breakout

  Designed specifically to work with the Adafruit VS1053 Codec Breakout 
  ----> https://www.adafruit.com/products/1381

  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

// This is a very beta demo of Ogg Vorbis recording. It works...
// Connect a button to digital 7 on the Arduino and use that to
// start and stop recording.

// A mic or line-in connection is required. See page 13 of the 
// datasheet for wiring

// Don't forget to copy the v44k1q05.img patch to your micro SD 
// card before running this example!


// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// define the pins used
#define RESET 15       // VS1053 reset pin (output)
#define CS 5           // VS1053 chip select pin (output)
#define DCS 35         // VS1053 Data/command select pin (output)
#define CARDCS 34      // Card chip select pin
#define DREQ 33        // VS1053 Data request, ideally an Interrupt pin

#define REC_BUTTON 14

Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(RESET, CS, DCS, DREQ, CARDCS);

File recording;          // the file we will save our recording to
#define RECBUFFSIZE 128  // 64 or 128 bytes.
uint8_t recording_buffer[RECBUFFSIZE];

void setup() {
  Serial.begin(115200);
  Serial.println("Adafruit VS1053 Ogg Recording Test");

  // initialise the music player
  if (!musicPlayer.begin()) {
    Serial.println("VS1053 not found");
    while (1);  // don't do anything more
  }

  musicPlayer.sineTest(0x44, 500);    // Make a tone to indicate VS1053 is working
 
  if (!SD.begin(CARDCS)) {
    Serial.println("SD failed, or not present");
    while (1);  // don't do anything more
  }
  Serial.println("SD OK!");
  
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(10,10);
  
  // when the button is pressed, record!
  pinMode(REC_BUTTON, INPUT);
  digitalWrite(REC_BUTTON, HIGH);
  
  // load plugin from SD card! We'll use mono 44.1KHz, high quality
  if (! musicPlayer.prepareRecordOgg("v44k1q05.img")) {
     Serial.println("Couldn't load plugin!");
     while (1);    
  }
}

uint8_t isRecording = false;

void loop() {  
  if (!isRecording && !digitalRead(REC_BUTTON)) {
    Serial.println("Begin recording");
    isRecording = true;
    
    // Check if the file exists already
    char filename[15];
    strcpy(filename, "RECORD00.OGG");
    for (uint8_t i = 0; i < 100; i++) {
      filename[6] = '0' + i/10;
      filename[7] = '0' + i%10;
      // create if does not exist, do not open existing, write, sync after write
      if (! SD.exists(filename)) {
        break;
      }
    }
    Serial.print("Recording to "); Serial.println(filename);
    recording = SD.open(filename, FILE_WRITE);
    if (! recording) {
       Serial.println("Couldn't open file to record!");
       while (1);
    }
    musicPlayer.startRecordOgg(true); // use microphone (for linein, pass in 'false')
  }
  if (isRecording)
    saveRecordedData(isRecording);
  if (isRecording && digitalRead(REC_BUTTON)) {
    Serial.println("End recording");
    musicPlayer.stopRecordOgg();
    isRecording = false;
    // flush all the data!
    saveRecordedData(isRecording);
    // close it up
    recording.close();
    delay(1000);
  }
}

uint16_t saveRecordedData(boolean isrecord) {
  uint16_t written = 0;
  
    // read how many words are waiting for us
  uint16_t wordswaiting = musicPlayer.recordedWordsWaiting();
  
  // try to process 256 words (512 bytes) at a time, for best speed
  while (wordswaiting > 256) {
    //Serial.print("Waiting: "); Serial.println(wordswaiting);
    // for example 128 bytes x 4 loops = 512 bytes
    for (int x=0; x < 512/RECBUFFSIZE; x++) {
      // fill the buffer!
      for (uint16_t addr=0; addr < RECBUFFSIZE; addr+=2) {
        uint16_t t = musicPlayer.recordedReadWord();
        //Serial.println(t, HEX);
        recording_buffer[addr] = t >> 8; 
        recording_buffer[addr+1] = t;
      }
      if (! recording.write(recording_buffer, RECBUFFSIZE)) {
            Serial.print("Couldn't write "); Serial.println(RECBUFFSIZE); 
            while (1);
      }
    }
    // flush 512 bytes at a time
    recording.flush();
    written += 256;
    wordswaiting -= 256;
  }
  
  wordswaiting = musicPlayer.recordedWordsWaiting();
  if (!isrecord) {
    Serial.print(wordswaiting); Serial.println(" remaining");
    // wrapping up the recording!
    uint16_t addr = 0;
    for (int x=0; x < wordswaiting-1; x++) {
      // fill the buffer!
      uint16_t t = musicPlayer.recordedReadWord();
      recording_buffer[addr] = t >> 8; 
      recording_buffer[addr+1] = t;
      if (addr > RECBUFFSIZE) {
          if (! recording.write(recording_buffer, RECBUFFSIZE)) {
                Serial.println("Couldn't write!");
                while (1);
          }
          recording.flush();
          addr = 0;
      }
    }
    if (addr != 0) {
      if (!recording.write(recording_buffer, addr)) {
        Serial.println("Couldn't write!"); while (1);
      }
      written += addr;
    }
    musicPlayer.sciRead(VS1053_SCI_AICTRL3);
    if (! (musicPlayer.sciRead(VS1053_SCI_AICTRL3) & (1 << 2))) {
       recording.write(musicPlayer.recordedReadWord() & 0xFF);
       written++;
    }
    recording.flush();
  }

  return written;
}

I use a separate SD CARD module because the VS1003 board does not have one.

https://www.arduino.cc/reference/en/libraries/adafruit-vs1053-library/

Hi masters,

Before dismantling this assembly permanently and trying others, I decided to ask for help on the VSDSP forum. See my diagram in post 2:

http://www.vsdsp-forum.com/phpbb/viewtopic.php?t=3070

The PASI colleague responded by indicating another topic. I visited this other topic and went to the page that contains example codes for several microcontrollers. There I downloaded this ZIP which has 3 source codes and 1 PDF:

http://www.fileconvoy.com/dfl.php?id=gd161e8408577fa7610005237855cb1aa2d432c9509

But, unfortunately, my knowledge (almost zero) prevents me from creating a test code in the Arduino IDE so that I can record on the esp32 and achieve my goal, which is just recording my voice on the SD CARD in OGG or MP3 format.

That's why I'm making this new post here in the hope that someone who wants to help can make this interaction. It will definitely help a lot of people who might be looking for the same thing.

Thanks

Here is a page with generic examples:
https://www.vlsi.fi/en/support/software/microcontrollersoftware.html

What I can do so far is listen to MP3 audio stored on the SD CARD module. And listen to web radio.

It remains to record my voice by generating MP3 or OGG files on the memory card.

Has anyone tried and succeeded using VS1003 board with ESP32? If so, can you put the code snippet here? Or would someone have one...try this...

See Post #5

Thanks

The board I'm using has the VS1003b chip and not VS1053. That's why it doesn't record in OGG or MP3. I bought it in a hurry and didn't know about this detail. In fact, what made me not look so closely was seeing it painted white on the edge of the VS1003 / VS1053 board.

Recording from the microphone to the memory card in OGG or MP3 will not work with this card.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.