Recording Audio mp4 & playing MP3 form SD function conflict

In examples-> AmebaMultimedia-> RecordMP4->AudioOnly , we use the mp4.begin() to recording audio file. but if doing once examples SDCardplayMP3 and wait for playing done, then call mp4.begin() again will never work again.

Log information:

  1. call mp4.begin() before mp3 play
  • Start MP4 recording (1 files)*
    STORAGE_INIT
    open file (0:/TestRecordingAudioOnly.mp4) len = 5 seconds
    STORAGE_INIT → STORAGE_START
    STORAGE_START 4
    STORAGE_STOP
    video_len = 0 audio_len = 80 moov_len = 2781 total_size = 20025
    STORAGE_STOP → STORAGE_IDLE
  1. call mp4.begin() once after mp3 play
    Start MP4 recording (1 files)
  2. second time call mp4.begin() again after mp3 play
    [Error] MP4 is recording

Hi @Howard ,

It seems like you have started the second recording before completing the first recording. Kindly ensure that you have inserted adequate delay interval (>=recording duration) between two consequent recordings.

Thank you.

Dear @KevinKL ,

Thanks for your reply, We’re sure the first recording is finish, and between first and second recording we playing the example “Audio_test.mp3”, then the second recording will show Log like my statement 2, seems like not do some action like STORAGE_INIT…

Actually, If play Audio_test.mp3 first, then first recording will have same situation.

The duplicate recording and duplicate playing it look like good, but can’t recording after playing once.

Hi @Howard ,

Thanks for your description. I was able to replicate your issue based on your description. Here’s the workaround, you might need to split your play mp3 and record mp4 tasks into two separate functions, constructors for each tasks will be called separately within task specific function. This is to ensure deinitialization of the audio module and to avoid overlapping of underlying variables. Please refer to the example code below for testing,

#include "StreamIO.h"
#include "AudioStream.h"
#include "AudioEncoder.h"
#include "MP4Recording.h"
#include "AmebaFatFS.h"
#include "AmebaFatFSFile.h"

// Default audio preset configurations:
// 0 :  8kHz Mono Analog Mic
// 1 : 16kHz Mono Analog Mic
// 2 :  8kHz Mono Digital PDM Mic
// 3 : 16kHz Mono Digital PDM Mic

#define FILENAME "Audio_test"

void setup()
{
    Serial.begin(115200);

    playMP3();

    recordMP4("1");

    Serial.println("--------First Recording done");

    playMP3();

    recordMP4("2");

    Serial.println("--------Second Recording done");
    
    Serial.println("done");

}

void loop()
{
    // do nothing
}

void playMP3(void)
{
    AmebaFatFS fs;
    File file;
    Serial.println("Start to play mp3");
      
    fs.begin();

    file = fs.open(String(fs.getRootPath()) + String(FILENAME) + String(".mp3"));

    file.close();

    fs.end();

    Serial.println("Play mp3 done");
}

void recordMP4(String c)
{
    AudioSetting configA(0);
    Audio audio;
    AAC aac;
    MP4Recording mp4;

    StreamIO audioStreamer1(1, 1);    // 1 Input Audio -> 1 Output AAC
    StreamIO audioStreamer2(1, 1);    // 1 Input AAC -> 1 Output MP4

    // Configure audio peripheral for audio data output
    audio.configAudio(configA);
    audio.begin();
    // Configure AAC audio encoder
    aac.configAudio(configA);
    aac.begin();

    // Configure MP4 recording settings
    mp4.configAudio(configA, CODEC_AAC);
    mp4.setRecordingDuration(5);
    mp4.setRecordingFileCount(1);
    mp4.setRecordingFileName("TestRecordingAudioOnly" + c);
    mp4.setRecordingDataType(STORAGE_AUDIO);    // Set MP4 to record audio only

    // Configure StreamIO object to stream data from audio channel to AAC encoder
    audioStreamer1.registerInput(audio);
    audioStreamer1.registerOutput(aac);
    if (audioStreamer1.begin() != 0) {
        Serial.println("StreamIO link start failed");
    }

    // Configure StreamIO object to stream data from AAC encoder to MP4
    audioStreamer2.registerInput(aac);
    audioStreamer2.registerOutput(mp4);
    if (audioStreamer2.begin() != 0) {
        Serial.println("StreamIO link start failed");
    }
    
    // Start recording MP4 data to SD card
    mp4.begin();

    delay(7000);

    Serial.println("------------------------------");
    Serial.println("- Summary of Streaming -");
    Serial.println("------------------------------");
    Serial.println("- Audio Information -");
    audio.printInfo();
    Serial.println("- MP4 Recording Information -");
    mp4.printInfo();
}

Thank you.

1 Like

Besides,

  1. kindly go to helix_mp3_drv.c in your Arduino SDK,
  2. in line 77, please add audio_deinit(&g_taudio);

This is to ensure that audio module is deinitialized after playing mp3.
We will update this API in the next release version.

Thanks.

1 Like