Introduction: DIY ESP32 Recorder

I made a music player with ESP32 in the past which attracted a lot of attention and someone contacts me for a consultation about playing WAV music. WAV is one of the common music formats, which is very common in daily work. Now, I want to refer to the previous audio player for remaking a special one that can play the audio with WAV format.

I have an idea during the design, I wonder why I didn’t add a microphone to the player in which both sound input and audio output can be performed. It sounds like making a simple voice recorder.

Step 1:

Step 2: How Do ESP32 Record Voice and Play

Follow the idea, I would make a board to match ESP32 for use. The board has to integrate one audio coding chip for record and play. WM8960 is a low-power, high-quality stereo CODEC, that provides two interface types: voice input and output. Connect WM8960 chip to ESP32 via I2S for collecting sounds and playing audio. In order to recording voice, the mics have been integrated on the board and connected to the input interface of WM8960. Also for playing, the board provides the 3.5mm audio jack as the player output.

In addition, the board has an SD card slot for suppling the SD card to save the recorded voice, and the board can play the audio file stored in the SD card.

The board is named Voice interface Hat. There is one picture above shown the working diagram of the board.

Step 3: Preparation

Step 4: Connection

Connect two boards according to the pin, Use the USB cable to connect them for power supply.

Step 5: Software

I have programmed it for record and play, and the program is available in Github: https://github.com/Makerfabs/Project_ESP32-Voice-Interaction/tree/master/example/ESP32_Record_Play

1. Please use the Arduino IDE upload program.

2. Set the WIFI SSID and password for ESP32 to connect WIFI in the code.

const char *ssid = "Makerfabs";
const char *password = "123456789";

3. Set the player volume by writing a value to the WM8960 register.

bool WM8960_Volume(float L_volume,float R_volume)
{
  L_volume = L_volume * 0xff;
  WM8960_Write_Reg(LEFT_DAC_VOLUME, (uint8_t)L_volume | 0x0100);
  R_volume = R_volume * 0xff;
  WM8960_Write_Reg(RIGHT_DAC_VOLUME, (uint8_t)R_volume | 0x0100);
  return true;
}

4. Record something by the Voice interface hat.

void WM8960_Record(String filename, char *buff, int record_time)
{
    int headerSize = 44;
    byte header[headerSize];
    int waveDataSize = record_time * 16000 * 16 * 2 / 8;
    int recode_time = millis();
    int part_time = recode_time;

    File file = SD.open(filename, FILE_WRITE);
    if (!file)
        return;

    Serial.println("Begin to record:");

    for (int j = 0; j < waveDataSize / sizeof(buff); ++j)
    {
        I2S_Read(buff, sizeof(buff));
        file.write((const byte *)buff, sizeof(buff));
        if ((millis() - part_time) > 1000)
        {
            Serial.print(".");
            part_time = millis();
        }
    }

    file.seek(0);
    CreateWavHeader(header, waveDataSize);
    file.write(header, headerSize);
    
    Serial.println("");
    Serial.println("Finish");
    Serial.println(millis() - recode_time);
    file.close();
}

5. Play the audio in which the format is WAV.

void WM8960_Play (String filename, char *buff)
{
    File file = SD.open(filename);
    if (! file) 
        return;
    Serial.println("Begin to play:");
    Serial.println(filename);
    file.seek(44);
    while (file.readBytes(buff, sizeof(buff)))
    {
        I2S_Write(buff, sizeof(buff));
    }
    Serial.println("Finish");
    file.close();
}

Step 6: How to Use

1. After loading the program to ESP32, reset the ESP32.

2. Turn up or down the button at the left of the board to select the menu, press to confirm the selection.

3. After entering the recording function, you need to keep pressing the button until the recording ends.

4. The playback function can display two recording files, one is the current recording completed, and the other is the last recorded file.

Step 7: Finally

If the wireless communication function of ESP32 can be used on the transmission of recorded voice, many points can be achieved such as sending a recording voice. That sounds interesting. I will try the voice transmission in air next. If you have other ideas about this board, please leave a message to me: gray@makerfabs.com.