This project converts an MP3 audio file into a melody that can be played by an Arduino using a buzzer. It processes an MP3 file to extract pitches and durations, then generates Arduino-compatible arrays for melodies and note durations.
- Converts MP3 files into arrays of pitches and durations for Arduino.
- Extracts pitches using
librosa
'spyin
algorithm. - Quantizes note durations based on a specified tempo.
- Generates Arduino arrays ready to be pasted into your sketch.
- Python 3.6 or higher
- An MP3 file containing monophonic melody (single notes, no chords)
- Arduino IDE for uploading sketches to your Arduino board
- A buzzer or speaker connected to your Arduino
-
Clone the Repository
git clone https://github.com/arashjafari/audio-to-arduino.git cd audio-to-arduino
-
Install Python Dependencies
Ensure you have
pip
installed, then run:pip install -r requirements.txt
Requirements (
requirements.txt
):pydub librosa numpy
- Note: You may need additional system libraries for
librosa
andpydub
. For example,ffmpeg
is required for audio processing.
- Note: You may need additional system libraries for
-
Install FFmpeg
-
Windows: Download from FFmpeg Builds and add it to your system PATH.
-
macOS: Install via Homebrew:
brew install ffmpeg
-
Linux (Debian/Ubuntu):
sudo apt-get install ffmpeg
-
python audio_to_arduino.py <input_file.mp3> [options]
Options:
--tempo <tempo>
: Set the tempo in beats per minute (BPM). Default is120
.--max-size <max_size>
: Maximum number of notes in the output arrays.--method <truncate|downsample>
: Method to limit array size (default: truncate).
Example:
python audio_to_arduino.py melody.mp3 --tempo 100
After running the script, you will see output similar to:
int melody[] = {
NOTE_C4, NOTE_D4, NOTE_E4, // ... and so on
};
int durations[] = {
4, 4, 2, // ... and so on
};
Copy these arrays into your Arduino sketch.
To play the generated melody on your Arduino, you'll need to set up a simple circuit with a buzzer or speaker.
- Arduino Board (e.g., Arduino Uno)
- Piezo Buzzer or small speaker
- Connecting Wires
- Breadboard (optional, but helpful for prototyping)
Figure: Wiring diagram for connecting the buzzer to the Arduino.
-
Buzzer/Speaker Connections:
- Positive Lead (+): Connect to Digital Pin 2 on the Arduino (or the pin defined as
BUZZER_PIN
in your sketch). - Negative Lead (-): Connect to Ground (GND) on the Arduino.
- Positive Lead (+): Connect to Digital Pin 2 on the Arduino (or the pin defined as
-
Arduino Connections:
- Ensure your Arduino is connected to your computer via USB for programming.
- After uploading the sketch, you can power the Arduino via USB or an external power source.
Note: If using a small speaker instead of a piezo buzzer, you may need to include a resistor (e.g., 100Ω) in series to limit the current.
Below is a sample Arduino sketch that uses the generated melody
and durations
arrays to play the melody through a buzzer.
#include "pitches.h"
#define BUZZER_PIN 2
int melody[] = {
// Paste your melody array here
};
int durations[] = {
// Paste your durations array here
};
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
PlayMusic(melody, durations, sizeof(melody) / sizeof(int));
delay(5000); // Wait 5 seconds before replaying
}
void PlayMusic(int melody[], int durations[], int size) {
for (int note = 0; note < size; note++) {
int duration = 1000 / durations[note];
tone(BUZZER_PIN, melody[note], duration);
int pauseBetweenNotes = duration * 1.30;
delay(pauseBetweenNotes);
noTone(BUZZER_PIN);
}
}
Note: You'll need the pitches.h
file, which contains definitions for note frequencies. You can find it in the Arduino IDE examples or download it online.
-
Convert MP3 to WAV
The script first converts the input MP3 file to WAV format using
pydub
. -
Extract Pitches
It then uses
librosa
'spyin
algorithm to extract the fundamental frequencies (pitches) from the audio file. -
Map Frequencies to MIDI Notes
The frequencies are converted to MIDI note numbers, which are then mapped to Arduino note constants (e.g.,
NOTE_C4
). -
Calculate Durations
Durations between notes are calculated based on the timestamps of the detected pitches.
-
Quantize Durations
Durations are quantized to standard musical note lengths (whole, half, quarter, eighth, sixteenth) based on the specified tempo.
-
Group Repeated Notes
Consecutive identical notes are grouped together, and their durations are summed to simplify the melody.
-
Generate Arduino Arrays
Finally, the script prints out the
melody
anddurations
arrays in a format ready to be pasted into an Arduino sketch.
- Monophonic Audio Only: The script is designed for monophonic melodies (one note at a time). Polyphonic audio (chords, multiple instruments) will not produce accurate results.
- Audio Quality: The accuracy of pitch detection depends on the quality of the input audio. Noisy recordings may lead to incorrect pitches.
- Unsupported Formats: Currently, only MP3 files are supported. You can modify the script to handle other formats if needed.
-
Estimating Memory Usage
-
Each
int
uses 2 bytes. -
Since the number of elements in notes and duration are always the same, we can simplify the calculation:
Total Memory (bytes) = Number of notes * 4
-
-
Limiting Array Size
- Use the
--max-size
option to limit the number of notes. - Choose between
truncate
anddownsample
methods to fit your needs.
- Use the
-
No Pitches Detected
- Ensure your audio file contains clear, monophonic melodies.
- Try using a different audio file or adjusting the tempo.
-
Compilation Errors on Arduino
- If you encounter memory-related errors, reduce the
max_size
or optimize your code usingPROGMEM
.
- If you encounter memory-related errors, reduce the
-
Unsupported File Type
- The script supports
.mp3
file. Ensure your input file has a supported extension.
- The script supports
This project is licensed under the MIT License.
Feel free to contribute to this project by submitting issues or pull requests.
Disclaimer: This tool is intended for educational purposes and personal projects. Always ensure you have the rights to use the audio files you process.