- C 83.3%
- CMake 16.7%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| ideas | ||
| .gitignore | ||
| CMakeLists.txt | ||
| conanfile.txt | ||
| main.c | ||
| pico_sdk_import.cmake | ||
| README.md | ||
Pico 2 (RP2350) I2S INMP441 Rhythm Pattern Matcher & BPM Estimator
This project implements a real-time, tempo-agnostic rhythm pattern matching system for the INMP441 I2S MEMS Microphone running on the Raspberry Pi Pico 2 (RP2350). It uses the static library driver lf-inmp441 located in the parent directory.
⚡ Features
- Transient Detector: Isolates the low-to-mid frequency band (62.5 Hz to 375.0 Hz) and triggers on rapid energy attacks (e.g., hand claps or desk taps).
- Tempo-Agnostic Pattern Matching: Uses a rolling 5-interval queue to detect a specific syncopated rhythm template (
3:1:2:1:1relative ratios) corresponding to a 4/4 measure of:- Quarter, Eighth Rest, Eighth, Quarter, Two Eighths (Hit step grid:
[ x . . x x . x x ])
- Quarter, Eighth Rest, Eighth, Quarter, Two Eighths (Hit step grid:
- Phase Independence: Evaluates all 5 circular shifts of the template so it can lock onto the rhythm regardless of which beat in the measure it begins tracking.
- Real-time BPM Estimation: Computes the estimated tempo in Beats Per Minute (BPM) based on the total duration of the matched measure:
\text{BPM} = \frac{240,000}{\text{Measure Duration in ms}} - Onboard LED Feedback: Flashes the Pico 2 onboard green LED (GP25) briefly on each detected beat.
- Consecutive Bar Tracking: Counts consecutive bars matched and triggers an achievement message when 16+ consecutive bars are locked in.
- Automatic Silence Reset: Resets the bar counter to zero if no beats are detected for more than 5 seconds.
🔌 Hardware Wiring Guide
The INMP441 operates as an I2S slave. The Pico 2 acts as the I2S master, generating the bit clock (SCK) and word select clock (WS/LRCK) signals.
Important
The PIO clock generation code requires that the WS pin be connected to the GPIO pin immediately following the SCK pin (SCK pin + 1).
| INMP441 Pin | Pin Marking | Function | Pico 2 GP Pin | Pico 2 Physical Pin | Connection Description |
|---|---|---|---|---|---|
| VDD | VDD | Power Input | 3V3 (OUT) | Pin 36 | 3.3V Power Supply (1.8V - 3.6V max) |
| GND | GND | Ground | GND | Pin 3 (or any GND) | Power Ground (0V) |
| SCK | SCK | Serial Bit Clock | GP20 | Pin 26 | SPI/I2S Clock generated by Pico 2 |
| WS | WS | Word Select (LRCK) | GP21 | Pin 27 | Frame sync signal (Left/Right channel clock) |
| SD | SD | Serial Data Out | GP22 | Pin 29 | Digital audio data line to Pico 2 |
| L/R | L/R | Channel Select | GND | GND | Pull to GND for Left channel mono output |
🛠️ Build and Compiling Instructions
Prerequisites
Ensure you have the Raspberry Pi Pico SDK installed and the PICO_SDK_PATH environment variable set. For example:
export PICO_SDK_PATH=/home/bethany/code/pico-sdk
Compiling the Code
- Navigate to the
noise-detectordirectory:cd noise-detector - Create a build directory and run CMake (specifying
pico2as the target board):mkdir build cd build cmake -DPICO_BOARD=pico2 .. - Compile the project:
make -j$(nproc)
This will produce the compiled firmware files in the build/ directory, including noise_detector.uf2.
⚡ Flashing the Firmware
- Unplug your Pico 2 from the computer.
- Hold down the white BOOTSEL button on the board.
- While holding the button, plug the Pico 2 back into your computer's USB port.
- Release the BOOTSEL button. The Pico 2 will mount as a USB mass storage device named
RP2350. - Drag and drop the
noise_detector.uf2file into the mounted directory. - The board will flash, reboot automatically, and start running the visualizer.
🖥️ Monitoring the Output
You can monitor the output from a terminal over USB CDC Serial. On Linux, you can run:
minicom -b 115200 -o -D /dev/ttyACM0
Output Example
When you perform the syncopated rhythm (e.g. by tapping your fingers on the desk), the console will log the intervals and matches:
Beat transient! Interval: 704 ms
=======================================================
>>> [MATCH] Bar #24 Detected! (Est. Tempo: 90.4 BPM)
Intervals: [704, 320, 352, 928, 352] ms
=======================================================
!!! [ACHIEVEMENT] Target met: 16+ consecutive bars locked! !!!
If you stop tapping or break the rhythm, the system resets:
[SILENCE] Rhythm broken. Resetting counter.