Small microcontroller audio projects are designed to play very specific types of audio files. If your music sample is in MP3 format, or 44KHz .wav or if you have grabbed a sample from a source (or the Internet) and do not know how it was encoded, you'll want to convert it to the right format.
This way you will get the best sounding audio, and it'll make using audio with your code painless!
This page will show how to convert your sound file(s) into PCM 16-bit Mono WAV files at 22KHz sample rate, which is usually best for the current crop of microcontrollers which take WAV files and play them on a speaker.
Currently, we are recommending two ways to convert the files: via use of the program Audacity or via iTunes. Other software may do similar conversions, just remember the parameters above when you do the conversion.
The open-source software program Audacity is available for PC/Mac/Linux and is very easy to use to check files and convert the files if necessary.
Audacity can also be used to trim audio files to just a small clip you might want as a phrase or sound effect. The Audacity online manual has information on doing the trimming and more.
Go to https://www.audacityteam.org/ and click the Download button:
Select your operating system and download. For Windows, use the Windows Installer.
Run the downloaded installer program and then start Audacity.
(On macOS, double-click on the downloaded .dmg file, then drag the Audacity.app file to your Applications folder, then double-click the Audacity.app to launch.)
You should see a program window similar to the one below:
Select File -> Open to open your sound file. Audacity can open a wide range of files including MP3 and many obscure formats besides WAV (which is great!).
See the red circled text. That shows that the WAV file I chose is a 44.1 KHz, 32 bit stereo file which is above our microcontroller specifications noted (22 KHz, 16 bit, mono). We'll need to tell Audacity what parameters we want to convert the file.
If Audacity reports that you have a stereo file, you will have two blue waveform files. If you only have one waveform and Audacity says you have a mono file, skip this step.
Combining a stereo track into a mono track will produce the original best on a mono speaker.
In Audacity 2.2, this is easier than in previous versions, it's one step instead of several:
Click on the menu item Tracks -> Mix -> Mix Stereo Down to Mono
Your file should now have one waveform and the text on the left says Mono. This is your combined track.
If your audio rate is higher than 16-bit, you will want to downconvert it. The sample in the pictures above shows it is 32-bit.
Click on the track title and select Set Sample Format -> 16-bit
Your file bitrate should now show 16-bit PCM in the properties to the left.
Finally, make sure the audio file will be saved as 22 kHz or less. If the the track label says 44100 Hz or some number higher than 22050 you will want to convert it.
At the bottom of the window there is a little button named Project Rate (Hz) - Make sure this is 22 kHz or less.
The lower the number the smaller the number of samples and the smaller your file will be when saved. BUT, you lose sound quality with lower sampling rates. Be careful about your choices. Upconverting a low bitrate (say 8 kHz to 16 to 22 kHz) will not make your file sound better.
The file you have now is ready to be written to disk. Select File -> Export -> Export as WAV
This will bring up the save dialog. Be sure to select WAV (Microsoft) signed 16-bit PCM as the file type.
Now you have your audio file! You can put it on an SD Card for a music player or on an Express flash drive for playback or whatever!