Upcoming maintenance
Dear Customers and Partners.
This website will be undergoing scheduled maintenance on June 14, 2023. Please be aware there may be disruption to the developer portal website and associated services during the scheduled maintenance period.
This upgrade is essential to ensure the continued performance, reliability, and security of Developer World.
We apologize for any inconvenience.
Custom data capture by PDM microphone
-
Hi,
I want custom data capture referred by the example pcm_capture,
I set to record one frame using one PDM microphone, but there is a ''attention and error'' then ''end'',
Could you please help me to check this?
Q1: why this attention happens?Q2: I add an 'unsleep(10)' to wait for the recording so that the function execute_aframe might just execute once, but it does not work.
The whole code is below:
#include <Audio.h>
AudioClass *theAudio;
static const int32_t recoding_frames = 1;
static const int32_t buffer_size = 1540; /768sample,4ch,16bit, 6144 = 768 42 bytes/
static char s_buffer[buffer_size];bool ErrEnd = false;
/**
- @brief Audio attention callback
- When audio internal error occurs, this function will be called back.
*/
void audio_attention_cb(const ErrorAttentionParam *atprm) {
puts("Attention!");if (atprm->error_code >= AS_ATTENTION_CODE_WARNING) {
ErrEnd = true;
}
}void setup() {
theAudio = AudioClass::getInstance();theAudio->begin(audio_attention_cb);
puts("initialization Audio Library");
theAudio->initRecorder(AS_CODECTYPE_PCM, "/mnt/sd0/BIN", AS_SAMPLINGRATE_48000,AS_BITLENGTH_16, AS_CHANNEL_MONO);
puts("Init Recorder!");puts("Rec!");
theAudio->startRecorder();
usleep(10);
}/**
- @brief Audio signal process for your application
*/
void signal_process(uint32_t size) {
/* The actual signal processing will be coding here.
For example, prints capture data. */printf("Size %ld [%02x %02x %02x %02x %02x %02x %02x %02x ...]\n",
size,
s_buffer[0],
s_buffer[1],
s_buffer[2],
s_buffer[3],
s_buffer[4],
s_buffer[5],
s_buffer[6],
s_buffer[7]);
}/**
- @brief Execute frames for FIFO empty
*/
void execute_frames() {
uint32_t read_size = 0;
do {
err_t err = execute_aframe(&read_size);
if ((err != AUDIOLIB_ECODE_OK)
&& (err != AUDIOLIB_ECODE_INSUFFICIENT_BUFFER_AREA)) {
break;
}
} while (read_size > 0);
}/**
- @brief Execute one frame
*/
err_t execute_aframe(uint32_t *size) {
printf("before Size %ld \n", *size);
err_t err = theAudio->readFrames(s_buffer, buffer_size, size);
printf("after Size %ld \n", *size);
if (((err == AUDIOLIB_ECODE_OK) || (err == AUDIOLIB_ECODE_INSUFFICIENT_BUFFER_AREA)) && (*size > 0)) {
signal_process(*size);
}return err;
}/**
- @brief Capture frames of PCM data into buffer
*/
void loop() {
static int32_t total_size = 0;
uint32_t read_size = 0;/* Execute audio data */
err_t err = execute_aframe(&read_size);
if (err != AUDIOLIB_ECODE_OK && err != AUDIOLIB_ECODE_INSUFFICIENT_BUFFER_AREA) {
theAudio->stopRecorder();
goto exitRecording;
} else if (read_size > 0) {
total_size += read_size;
}/* This sleep is adjusted by the time to write the audio stream file.
- Please adjust in according with the processing contents
- being processed at the same time by Application.
- The usleep() function suspends execution of the calling thread for usec
- microseconds. But the timer resolution depends on the OS system tick time
- which is 10 milliseconds (10,000 microseconds) by default. Therefore,
- it will sleep for a longer time than the time requested here.
*/
// usleep(10000);
/* Stop Recording */
if (total_size > (recoding_frames * buffer_size)) {
theAudio->stopRecorder();/* Get ramaining data(flushing) */ sleep(1); /* For data pipline stop */ execute_frames(); goto exitRecording;
}
if (ErrEnd) {
printf("Error End\n");
theAudio->stopRecorder();
goto exitRecording;
}return;
exitRecording:
theAudio->setReadyMode();
theAudio->end();puts("End Recording");
exit(1);
}\code end
Results in Serial monitor:
without writing for 'size': -
Hi @denko-2
Thank you for using the forum for your technical questions.
I'll analyse your question and get back to you as soon as possible -
@denko-2
Line 84 of the audio_recorder_sink.cpp mentions the following word AS_ATTENTION_SUB_CODE_SIMPLE_FIFO_OVERFLOW which indicates your buffer (FIFO) size for storing audio data is too small. Try to increase your recording_frames to 5 and buffer_size to 3x what you have now. -
Good catch, @dshilov-1-1
-
@dshilov-1-1
Thanks very much for your reply.Do you know how we can change the sample per frame in 'pcm capture' example? I find it fixed at 768.
-
@denko-2 If I recall correctly, the samples per frame are used only when pulling data out of FIFO and for setting total FIFO size (number of frames * frame size), and should be adapted for optimum performance for your application.
Change the variable SIMPLE_FIFO_READ_SIZE.
-
@dshilov-1-1 Thanks very much for your detailed answer.
-