How can I record audio from an I2S microphone with the Arduino Sound library (ArduinoSound - Arduino Reference), 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:
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.
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: Audio/Recorder.ino at master · PaulStoffregen/Audio · GitHub
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().