Appendix B. Capturing Audio on Arduino

The following text walks through the audio capture code from the wake-word application in Chapter 7. Since it’s not directly related to machine learning, it’s provided as an appendix.

The Arduino Nano 33 BLE Sense has an on-board microphone. To receive audio data from the microphone, we can register a callback function that is called when there is a chunk of new audio data ready.

Each time this happens, we’ll write the chunk of new data to a buffer that stores a reserve of data. Because audio data takes up a lot of memory, the buffer has room for only a set amount of data. This data is overwritten when the buffer becomes full.

Whenever our program is ready to run inference, it can read the last second’s worth of data from this buffer. As long as new data keeps coming in faster than we need to access it, there’ll always be enough new data in the buffer to preprocess and feed into our model.

Each cycle of preprocessing and inference is complex, and it takes some time to complete. Because of this, we’ll only be able to run inference a few times per second on an Arduino. This means that it will be easy for our buffer to stay full.

As we saw in Chapter 7, audio_provider.h implements these two functions:

  • GetAudioSamples(), which provides a pointer to a chunk of raw audio data

  • LatestAudioTimestamp(), which returns the timestamp of the most recently captured audio

The code that implements these for Arduino is located in arduino/audio_provider.cc ...

Get TinyML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.