Understanding Errors When Recording a Microphone in a Presense

Asked 2 years ago, Updated 2 years ago, 78 views

I use an analog microphone to record at Sony's Spresense.
The compilation environment is Arduino IDE and Windows 10.
My SD card is 32GB SR-32UX2A, a Sony MicroSDHC memory card.

It is recorded at 192 kHz and recorded several times, and the Wav file is saved on the SD card.
I also checked the Wav file on my PC, but it was recorded without any problems.

However, after recording several times,
Serial Monitor

Attention:module[4][0]attention id[1]/code[6]{objects/media_recorder/audio_recorder_sink.cpp L84}

After the message appears, there is an error every time and no recording occurs.

Power cycle and start recording from the first file.

I don't think it's a program or sensor connection problem because it's not a recording operation itself. What's the problem?

If you change the SD card, some SD cards will display this message from the start of boot.
I got an error and I can't record it, so I think it's related to the SD card, but I haven't come up with a solution.
*I have tried about 5 types of SD cards, but the current situation is that all of them have similar errors sooner or later.

The program is attached below.
Sleep(3) after FileNo+=1 at the end of the program is not required, but the probability of an error has decreased a little, so I added it.

/*
 *  recorder_wav.ino - Recorder example application for WAV (PCM)
 *  Copyright 2018 Sony Semiconductor Solutions Corporation
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation;either
 *  version 2.1 of the license, or (at your option) any later version.
 *
 *  This library is distributed in the hope that will be used,
 *  but WITHOUT ANY WARRANTY; without even the imported warranty of
 *  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

# include <SDHCI.h>
# include <Audio.h>

// For file numbering
# include <string.h>
# include <SoftwareSerial.h>

# include <arch/board/board.h>

SDClass theSD;
AudioClass* theAudio;

File myFile;

unsigned int FileNo, loop_no;
Strings_fileNo, filename;

boolErrEnd = false;

/**
 * @ brief Audio attention callback
 *
 * When audio internal error occur, this function will be called back.
 */

static void audio_attention_cb(const ErrorAttentionParam*atprm)
{
  puts("Attention!");

  if(atprm->error_code>=AS_ATTENTION_CODE_WARNING)
    {
      ErrEnd = true;
   }
}

/**
 * @ brief Setup recording of mp3 stream to file
 *
 * Select input device as microphone<br>
 * Initialize filetype to stereo wav with 48 Kb/s sampling rate <br>
 * Open "Sound.wav" file in write mode
 */

/* Sampling rate
 * Set 16000 or 48000
 */


static constuint32_trecoding_sampling_rate=192000;


/* Number of input channels
 * Set ether 1, 2, or 4.
 */

static constuint8_trecoding_channel_number = 1;

/* Audio bit depth
 * Set 16 or 24
 */

static constuint8_trecoding_bit_length=24;

/* Recording time [second] */

static constuint32_trecoding_time=40;

/* Bytes per second*/

static const int32_trecoding_byte_per_second=recoding_sampling_rate*
                                                recoding_channel_number*
                                                recoding_bit_length/8;

/* Total recording size*/

static const int32_trecoding_size =recoding_byte_per_second *recoding_time;
/* I'm taking four to adjust the recording time.
 */
void setup()
{

  pinMode (LED 0, OUTPUT);
  digitalWrite (LED 0, LOW);

  FileNo=1;

}

void loop() 
{
  err_terr;

init_WAV_file:
loop_no = 0;
filename = "Sound";
filename + = FileNo;
filename + = ".wav";
puts("OK");

 theAudio=AudioClass:getInstance();

  theAudio->begin(audio_attention_cb);

  puts("initialization Audio Library");

  /* Select input device as microphone*/
  theAudio->setRenderingClockMode(AS_CLKMODE_HIRES);
  theAudio->setRecorderMode(AS_SETRECDR_STS_INPUTDEVICE_MIC);

  /* Search for WAVDEC codec in"/mnt/sd0/BIN" directory*/
  theAudio->initRecorder(AS_CODECTYPE_WAV,
                         "/mnt/sd0/BIN",
                         AS_SAMPLINGRATE_192000,
                         AS_BITLENGTH_16,
                         AS_CHANNEL_MONO);
  puts("Init Recorder!");
    /* Open file for data write on SD card*/
  myFile=theSD.open(filename,FILE_WRITE);
  /* Verify file open*/

  if(!myFile)
    {
      printf("File open error\n");
      exit(1);
    }

  sleep(5);
  theAudio->writeWavHeader (myFile);
  puts("Write Header!");

  theAudio->startRecorder();
  puts("Recording Start!");
  digitalWrite (LED 0, HIGH);

  /* recording end condition*/  
  if_sentence:
    if(theAudio->getRecordingSize()>recoding_size)
    // if(loop_no>200000000)
      {
        theAudio->stopRecorder();
        sleep(1);
        err=theAudio->readFrames (myFile);
        puts("break!");
        goto exitRecording;
      }

  /* Read frames to record in file*/
  err=theAudio->readFrames (myFile);

  if(err!=AUDIOLIB_ECODE_OK)
    {
      printf("FileEnd!=%d\n", err);
      theAudio->stopRecorder();
      goto exitRecording;
    }

  if(ErrEnd)
    {
      printf("Error End\n";
      theAudio->stopRecorder();
      goto exitRecording;
    }
   /* This sleep is adjusted by the time to write the audio stream file.
     Please adjust in recording with the processing contents
     being processed at the same time by Application.
  */
 // usleep(10000);
  usleep(1000);
  loop_no++;
  goto if_sentence;
  return;

exitRecording:
  theAudio->closeOutputFile(myFile);
  myFile.close();

  theAudio->setReadyMode();
  theAudio ->end();

  puts("End Recording";
  digitalWrite (LED 0, LOW);
  FileNo+=1;
  sleep(3);
  goto init_WAV_file;

}

Please let me know.
Thank you for your cooperation.

spresense

2022-09-30 20:22

3 Answers

I was able to record 192kHz/24bit/Mono

  • Team 8GB class10
  • Toshiba 8GB class10

It seems that it is possible to use either of the .

One thing I changed was that I set the allocation unit size to 64 kilobytes when formatting.
I hope this will solve the problem.

By the way, 192kHz/24bit/Stereo seems to have an error.


2022-09-30 20:22

Comments on the program

/**
 * @ brief Setup recording of mp3 stream to file  
 *
 * Select input device as microphone<br>
 * Initialize filetype to stereo wav with 48 Kb/s sampling rate <br>
 * Open "Sound.wav" file in write mode
 */

/* Sampling rate
 * Set 16000 or 48000
 */

So I think it's a stereo recording program at 48Kb/s.
Forced to do so (ignoring the "sampling rate should be 16000 or 48000" warning) 192Kb/s and constantly working properly would be tough.

It is good to write data to a blank space when writing to a flash memory such as a USB memory or SD card, but when data is full and data is rewritten, data in a certain size area of flash memory is erased (this is why it is called flash memory).You can't write or load memory while you're in Flash, so you need to have a buffer in place to prevent data loss when Flash occurs.

The comment says Set 16000 or 48000 means that 48Kb/s is a program that doesn't cause problems even if Flash occurs.


2022-09-30 20:22

I also found an SD card whose recording speed has deteriorated considerably.
Quick formatting of the PC does not improve, so
SD Association, using SD memory card formatter
Reformatted the physical.

https://www.sdcard.org/jp/downloads/formatter/

When I did this, I was able to record the card with the error.

If you can't record it, it might be a good idea to try the physical format once.


2022-09-30 20:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.