Record Audio from an I2S Mic with the Arduino Sound library (SAMD21 board based)

How can I record audio from an I2S microphone with the Arduino Sound library (https://www.arduino.cc/en/Reference/ArduinoSound), e.g. for writing on a SD card?

There is a AudioInI2S class but I don't know if it is usable for recording and storing sound on a SD card. The library can play wav files. Is the Cortex M0 / SAMD21 capable to decode MP3 or even encode MP3?

Some further investigations: I have found this code from the I2S library. The I2S lib is the base of Arduino's Sound library a obligatory prerequisite:

https://www.arduino.cc/en/Tutorial/I2SInputSerialPlotter: This example shows you how to read and visualize on the serial plotter I2S audio data coming from an I2S microphone.

I wonder if I do not need the sound library, but only the I2S lib and write al incomming data (to serial in the example above) to a SD card.

What kind of data is this? How can I make a wav or better mp3 or ogg from this stream. Is this possible with a Cortex M0 board?

What kind of data is this?

You'll have to check the specs on the microphone. I assume it's up to you "clock" the data at your chosen sample rate.

If you want to make a [u]WAV file[/u], it's just a 44-byte header followed by your audio samples.

You'll probably have to re-format/re-arrange the bytes to get them in the correct order unless the microphone is spitting-out 8-bit data. However, 8-bit WAVs use unsigned integers, whereas 16 & 24-bit WAVs use signed integers so you may have to sort that out too.

In an application like this it's usually easy to write the header because you're probably always writing the same format (44.1kHz, 16-bit, mono, etc.).

However, when you read the header you usually have to parse/decode it, at least to check that it matches what you expect. And, in some cases you may have to re-sample the data to match the sample-rate and bit-depth of your playback hardware.

How can I make a wav or better mp3 or ogg from this stream. Is this possible with a Cortex M0 board?

I don't know if that processor is fast enough but LAME (for MP3) is open source (and the MP3 patents expired about a year ago) and OGG is open source, but you might have to "port" the code for your processor.

Normally I2S runs at rates like 48kSPS, 24 bits per sample, stereo, meaning data rates of 300kB/s or so might be coming from the microphone chip. This is going to require native-mode support for the SDcard interface unless the microphone can be configured slower I think.

Thanks DVDdoug and MarkT, writing and reading or better recording and playing of the raw digital data should work. I found this thread in the meantime: https://github.com/arduino-libraries/ArduinoSound/issues/2

There is also example code from Paul Stoffregen for the Teensy with the Teensy Audio Library - just as note, this will not work with the Arduino Audio lib - but you will find from line 148 on an optimized method to write sound data to SD: https://github.com/PaulStoffregen/Audio/blob/master/examples/Recorder/Recorder.ino

Yes, writing to a storage medium will be challenging. The Arduino MKR ZERO has a "dedicated SPI interfaces (SPI1)", let's try if this is sufficient. But the I2S lib allows also to specify sampleRate and bitsPerSample in begin().

I2S is not SPI