Grabbing audio (from inmp441) at specific times #1476
-
I am using an esp32S3 (specifically the lilygo-t-display-s3) with an inmp441 microphone and coding in the latest version of the Arduino IDE with the latest version of your library on Ubuntu 22.04. Apologies if this is a dumb question, I've looked over the example codes and wiki and could not figure this out. It seems like audio data from an i2s microphone is constant and the examples for streaming the data somewhere are continous and do not have a way to start/stop the collection of data at specific times. My goal is to start an action (in my case spinning a stepper motor) and get the audio of when that action starts to when it ends. The time is usually quite short, ~0.25 seconds depnding on how fast the motor is spinning, and I require the audio to be in sync with the start/end of the action. Although it does not matter if I have extra data before or after, as long as each recording is in sync (e.g. if the same sound is produced at exactly halfway through the motor movement then each audio sample should show the sound at the same place each time). Example pseudo code to describe what I'm trying to do: loop () { It does not matter whether I grab samples based on time or number of samples (I do always know exactly how long the motor will spin for so either grabbing all audio from the past x seconds or most recent x number of samples would be fine). Your example code for streaming to serial to show on the serial plotter works wonderfully and I have confirmed that it works. I have previously tried without this library and pass a semaphore to a secondary record task to record only when I want using i2s_read(). But it seems like the DMA buffer constantly gets audio from the microphone even without calling i2s_read(). So when I call i2s_read() at non regular intervals I get audio data that contains garbage/repeat audio data. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Redesign your spin_motor() method to make it non blocking or try to run things in parallel with a task |
Beta Was this translation helpful? Give feedback.
-
Thanks for the response! I have things running in parallel with a task and getting an error from StreamCopy.h somewhere in the record function. My motor will move left (left(6) in test() function is the last line to run) and then the error gets printed and all execution is stopped.
This is my task that records
And I'm starting to record with:
These are my variables:
|
Beta Was this translation helpful? Give feedback.
-
Actually, there was a bug in the calculation of the number of written bytes. I committed a correction and now your initial logic should work. Here is the test case: #include "AudioTools.h"
#define SAMPLE_COUNT 2048
#define SAMPLE_BUFFER_SIZE sizeof(int32_t) * SAMPLE_COUNT
unsigned char raw_data[SAMPLE_BUFFER_SIZE];
AudioInfo info(8000, 1, 32);
I2SStream i2sStream; // Access I2S as stream
MemoryStream raw_samples(raw_data, SAMPLE_BUFFER_SIZE, true, RAM);
StreamCopy copier(raw_samples, i2sStream, 1024); // copies sound
void setup() {
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);
auto cfg = i2sStream.defaultConfig(RX_MODE);
cfg.copyFrom(info);
i2sStream.begin(cfg);
raw_samples.begin();
copier.copy();
assert(raw_samples.available() == 1024);
assert(raw_samples.availableForWrite() == 7168);
}
void loop() {
// put your main code here, to run repeatedly:
} |
Beta Was this translation helpful? Give feedback.
https://github.com/pschatzmann/arduino-audio-tools/wiki/Introduction#additional-processing-in-the-loop
Redesign your spin_motor() method to make it non blocking or try to run things in parallel with a task