Converting MP3 to WAV Format
Welcome to Audio Conversion
In this lab, you will learn how to convert MP3 music files into WAV files that your Pico can play. You do this work on your computer, not on the Pico!
Why Convert to WAV?
In the last lab, you learned how to play WAV files stored on the Pico's flash memory. The Pico uses a simple WAV player that needs a specific format: 8,000 samples per second (8 kHz), 16-bit depth, mono channel. MP3 files use compression and are much harder to decode on a microcontroller.
All the audio conversion work happens on your computer. Your computer has powerful tools that can convert files in seconds — tools that would be too slow or too large to run on the Pico.
Method 1: Use the ffmpeg Command-Line Tool
ffmpeg is a free, open-source tool that converts audio and video files. You run it by typing commands in a terminal window.
Download it from ffmpeg.org. On a Mac, you will need to go into System Preferences and mark the downloaded programs as trusted before they will run.
Here are the direct download links for macOS:
Each download is a zip file. Unzip them and copy the programs to ~/bin. Then add that folder to your PATH so your terminal can find them:
1 | |
Add that line to your .bash_profile file. After you do, run this to check that it worked:
1 | |
This should print the path to ffmpeg. You can also see all its options:
1 | |
Converting One MP3 to 8 kHz, 16-bit WAV
Use the -i flag for the input file and -ar 8000 to set the output to 8,000 samples per second. The output file name must end in .wav:
1 | |
The default bit depth is 16 bits, which is what the Pico's wave player expects.
Monty's Tip
The output file name tells ffmpeg what format to use. If the name ends in .wav, ffmpeg automatically saves it as a WAV file.
Converting a Whole Folder at Once
You can convert every MP3 in a folder with one shell command. This uses ls, awk, and sed together:
1 | |
This command:
1. Lists all .mp3 files in the current folder.
2. Builds an ffmpeg command for each one.
3. Changes the output file extension from .mp3 to .wav.
4. Runs all the commands one by one.
Checking Your Converted Files
After converting, use the Unix file command to confirm the format:
1 2 | |
Sample output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Each file shows WAVE audio, Pulse-code Modulation (PCM), 16 bit, mono 8000 Hz — exactly the format the Pico's wave player needs.
Method 2: Use the pydub Python Module
Note
This method is for experienced Python developers who prefer to use Python instead of command-line tools.
Set Up a Python Environment With Conda
1 2 3 | |
Check the installed versions:
1 | |
Expected output:
1 2 3 | |
Convert a File With pydub
1 2 3 4 5 6 | |
What Each Line Does
AudioSegment.from_mp3(...)— loads the MP3 file into memory.sound.export(...)— saves it as a WAV file. Thetagsparameter adds optional metadata.
Transferring the Files to the Pico With rshell
After converting your files, copy them to the Pico using rshell:
1 2 3 | |
Watch Out!
The Pico only has 2 MB of flash memory. Check the total size of your WAV files before copying. If you fill up the flash, the Pico may not be able to save your code!
References
Great Work!
You can now convert any MP3 file into the WAV format your Pico needs. Next, you will learn about Musical Instrument Digital Interface (MIDI) and how to send music messages to other devices.