espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.43k stars 7.38k forks source link

I2S_Record_to_Wav example error #9740

Closed raprakashvi closed 2 weeks ago

raprakashvi commented 4 months ago

Board

ESP32

Device Description

ESP-WROOM-32

Hardware Configuration

SD Card Standard INPUT

Version

latest master (checkout manually)

IDE Name

Arduino

Operating System

Windows 10

Flash frequency

80 Mhz

PSRAM enabled

no

Upload speed

115200

Description

Testing the microphone and SD card from the example and everytime the file is created but with 0 bytes. Seems like the record to wav function is not waiting for the process to finish.

Sketch

/*
  ESP32-S2-EYE I2S record to WAV example
  This simple example demonstrates using the I2S library to record
  5 seconds of audio data and write it to a WAV file on the SD card.

  Don't forget to select the OPI PSRAM, 8MB flash size and Enable USB CDC
  on boot in the Tools menu!

  Created for arduino-esp32 on 18 Dec, 2023
  by Lucas Saavedra Vaz (lucasssvaz)
*/

#include "ESP_I2S.h"
#include "FS.h"
#include "SD_MMC.h"

#define ONE_BIT_MODE   true

const uint8_t I2S_SCK = 21 ;
const uint8_t I2S_WS = 22;
const uint8_t I2S_DIN = 25;

const uint8_t SD_CMD = 15;
const uint8_t SD_CLK = 14;
const uint8_t SD_DATA0 = 2;

void setup() {
  // Create an instance of the I2SClass
  I2SClass i2s;

  pinMode(2, INPUT_PULLUP);
  pinMode(4,INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(13,INPUT_PULLUP);
  pinMode(15, INPUT_PULLUP);

  // Create variables to store the audio data
  uint8_t *wav_buffer;
  size_t wav_size;

  // Initialize the serial port
  Serial.begin(115200);
  while (!Serial) {
    delay(10);
  }

  Serial.println("Initializing I2S bus...");

  // Set up the pins used for audio input
  i2s.setPins(I2S_SCK, I2S_WS, -1, I2S_DIN);

  // Initialize the I2S bus in standard mode
  if (!i2s.begin(I2S_MODE_STD, 16000, I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_MONO, I2S_STD_SLOT_LEFT)) {
    Serial.println("Failed to initialize I2S bus!");
    return;
  }

  Serial.println("I2S bus initialized.");
  Serial.println("Initializing SD card...");

  // Set up the pins used for SD card access
  if (!SD_MMC.setPins(SD_CLK, SD_CMD, SD_DATA0)) {
    Serial.println("Failed to set SD pins!");
    return;
  }

  // Mount the SD card
  if (!SD_MMC.begin("/sdcard", true)) {
    Serial.println("Failed to initialize SD card!");
    return;
  }

  Serial.println("SD card initialized.");
  Serial.println("Recording 5 seconds of audio data...");

  // Record 5 seconds of audio data
  wav_buffer = i2s.recordWAV(5, &wav_size);

  // Create a file on the SD card
  File file = SD_MMC.open("/test.wav", FILE_WRITE);
  if (!file) {
    Serial.println("Failed to open file for writing!");
    return;
  }

  Serial.println("Writing audio data to file...");

  // Write the audio data to the file
  if (file.write(wav_buffer, wav_size) != wav_size) {
    Serial.println("Failed to write audio data to file!");
    return;
  }

  // Close the file
  file.close();

  Serial.println("Application complete.");
}

void loop() {}

Debug Message

Standard output with but file is empty.

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

lucasssvaz commented 4 months ago

Can you please enable the verbose debug mode and post the output here ?

raprakashvi commented 4 months ago

Hi @lucasssvaz , I enabled the verbose mode during compiling and upload and below is the output.

23:26:26.580 -> I2S bus initialized.
23:26:26.580 -> Initializing SD card...
23:26:26.645 -> SD card initialized.
23:26:26.678 -> Recording 5 seconds of audio data...
23:26:26.678 -> Writing audio data to file...
23:26:26.678 -> Application complete.
me-no-dev commented 4 months ago

we need debug enabled in the core (for your board) not for compiling and uploading in the IDE

VojtechBartoska commented 3 months ago

@raprakashvi can you please provide us more information as stated above? thanks.

raprakashvi commented 3 months ago

@VojtechBartoska I am having trouble figuring out the steps to enter debug mode and hence the delay, would appreciate pointers

lucasssvaz commented 3 months ago

@raprakashvi select this option in the menu and upload the code to the board again

image

VojtechBartoska commented 3 months ago

any updates on this @raprakashvi?

raprakashvi commented 3 months ago

Hi @VojtechBartoska thanks a lot for your patience. Here is the code and verbose output. Seems buffer size issue.

Initializing I2S bus...
I2S bus initialized.
Initializing SD card...
SD card initialized.
Recording 5 seconds of audio data...
Writing audio data to file...
Application complete.
an.c:160] perimanSetPinBus(): Pin 15 successfully set to type SDMMC_CMD (42) with bus 0x3ffc1150
[  1065][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 14 successfully set to type SDMMC_CLK (41) with bus 0x3ffc1150
[  1076][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 2 successfully set to type SDMMC_D0 (43) with bus 0x3ffc1150
SD card initialized.
Recording 5 seconds of audio data...
[  1087][D][ESP_I2S.cpp:965] recordWAV(): Record WAV: rate:16000, bits:32, channels:1, size:320000
[  1101][E][ESP_I2S.cpp:969] recordWAV(): Failed to allocate WAV buffer with size 320044
Writing audio data to file...
Application complete.
[  1116][E][esp32-hal-periman.c:122] perimanSetPinBus(): Invalid pin: 255
[  1124][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 21 successfully set to type INIT (0) with bus 0x0
[  1134][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 22 successfully set to type INIT (0) with bus 0x0
[  1144][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 25 successfully set to type INIT (0) with bus 0x0
=========== After Setup Start ============
INTERNAL Memory Info:
------------------------------------------
  Total Size        :   379728 B ( 370.8 KB)  
 Free Bytes        :   318308 B ( 310.8 KB)
  Allocated Bytes   :    53684 B (  52.4 KB)
  Minimum Free Bytes:   301768 B ( 294.7 KB)
  Largest Free Block:   110580 B ( 108.0 KB)
------------------------------------------
GPIO Info:
------------------------------------------
  GPIO : BUS_TYPE[bus/unit][chan]
  --------------------------------------  
     1 : UART_TX[0]
     2 : SDMMC_D0
     3 : UART_RX[0]
     4 : GPIO
    12 : GPIO
    13 : GPIO
    14 : SDMMC_CLK
    15 : SDMMC_CMD
============ After Setup End =============
me-no-dev commented 3 months ago

You need PSRAM to record that much data with this function. It's trying to allocate 320KB buffer and that is more than you can do on internal RAM

raprakashvi commented 3 months ago

I see. Then it won't work with my current ESP Wroom 32 boards as they only have up to 4 MB flash and the PSRAM shows 0. Seems like S3 boards might be better for testing this function. Meanwhile, I did get the recording and writing part working where I wrote a custom function to record and save the Wav file in real time to SD card. Might be a good idea to add that as a function option in the future in the official implementation as the MMC is already being used,

VojtechBartoska commented 3 weeks ago

Hello @raprakashvi, can I consider this ticket as solved?

raprakashvi commented 2 weeks ago

Yes. This part is solved. I am happy to help if anyone has any questions on this or related issue.

On Mon, Sep 16, 2024 at 7:46 AM Vojtěch Bartoška @.***> wrote:

Hello @raprakashvi https://github.com/raprakashvi, can I consider this ticket as solved?

— Reply to this email directly, view it on GitHub https://github.com/espressif/arduino-esp32/issues/9740#issuecomment-2352691049, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMREUJOPBUSE3IKIXPHUPFTZW3AJZAVCNFSM6AAAAABITNQGDOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNJSGY4TCMBUHE . You are receiving this because you were mentioned.Message ID: @.***>