Phipps Electronics

Order within the next 

FREE SHIPPING OVER $199

50,000+ ORDERS

WORLDWIDE SHIPPING

SSL SECURED

Playing Audio Clips on your Microcontroller (Part 1)

Contents

Read this article to learn how to play simple audio clips on your microcontroller.

Introduction

Previously, you’ve learned how to produce sounds on your microcontroller through I2S using sine wave data. Now, you’ll see that playing audio clips or files on a microcontroller requires several more processes. The reason is that you have to consider the limited space and resources on your chip. With this, you must convert your audio files into an acceptable format. You also have to know how to prepare and put the audio data in your chip and how to output the sound to your speaker.

Basic Steps involved

As mentioned, these steps are needed to play audio clips on your microcontroller now in tabulated form.

  1. Convert your audio file into an acceptable format.
  2. Convert this file into something that your microcontroller can read.
  3. Output the audio on your microcontroller.

Convert Your Audio in an Acceptable Format

1. Download and run Audacity. It’s an open-source, free audio conversion and editing tool with a neat GUI.

2. Click File -> Open and choose your audio file, such as an mp3, to edit and convert.

3. As you can see, the sample file chosen has very high specifications intended for consumer audio listening. You’ll have to down-convert its bit resolution and sampling rate. You’ll also need to trim your audio length and convert it to monoaural output.

4. Combine the Stereo signal by going to Tracks -> Mix -> MIx Stereo down to Mono. Trim your audio by selecting a portion of the audio timeline and pressing Ctrl+T. You can also maximize the volume of your clip by going to Effects -> Volume and Compression -> Amplify…

5. Resample your audio to a reduced sampling rate. A 44100Hz rate is unacceptable for a microcontroller. Reduce it by going to Tracks -> Resample… There should be some degradation of signal quality based on the reduced sampling you’ve chosen. Here, a 16KHz sampling rate is used. You can choose an 8kHz sampling for smaller microcontrollers.

6. You’re now ready to export your audio file. Make sure to choose an uncompressed format. The header should be RAW (header-less). Also, choose a proper encoding method. A signed 16-bit PCM is used here to be compatible with I2S since I2S will be the chosen output processing scheme. You can choose other forms depending on your microcontroller’s audio processing capabilities. Examples of such are using PWMs and DACs. Take note of your PWM or DAC bit resolution and format. After exporting your data, you can recheck it through File -> Import -> Raw Data… Make sure to input the correct parameters.

Convert your File into a Readable formAT That your Microcontroller Understands

Now, you’re ready to put this audio data on your microcontroller. However, this is binary-stream data; your microcontroller cannot read it. You have to convert it into a C array form.

1. A tool called bin2header will help you convert binary-stream data into C array format. Download and install it and prepare to use the command prompt. 

2. The parameters are shown below for this current example. An output file named sample.c will be made from the file_example_MP3.raw file. The array will be packed into 16 bits per element and swapped for correct endianness for this particular microcontroller. The parameters must match what you have while exporting your raw file in Audacity.

				
					bin2header -o sample.c -p 16 -e swap file_example_MP3.raw
				
			

3. After executing, the bin2header output must state the successful conversion. Below, you’ll see 56200 bytes of data written. This is actually in words, not bytes. This represents the number of elements you now have in your array.

Your sample.c file should look like below in a notepad editor. It’s an array of 16-bit integers representing your audio clip.

Part 2 will continue with how to Output the Audio on your Microcontroller…

SHOP THIS PROJECT

SUBSCRIBE FOR NEW POST ALERTS

Subscribe to be the first to know when we publish a new article!
List Subscriptions(Required)

POPULAR POSTS

Scroll to Top