Friday, October 18, 2024

Pico Audio Part 3

For an index to all my stories click this text.

In this story I show how to install the necessary MicroPython drivers to play some real audio on the Raspberry Pico. And trust me it is worth while. The Pico is able to produce some excellent audio quality with the aid of just some resistors and capacitors !!

This is the third story on how to play audio files on the Raspberry Pi Pico.
The first story showed how to work with Audacity to record your own voice or convert existing audio to the wav format which can be played by the Pico. You can read that story here: http://lucstechblog.blogspot.com/2024/10/audacity-pico-audio-part-1.html

The second story told how to build the hardware setup. You just need 4 resistors and 2 capacitors. The total cost of this project is about 1 Euro/USD. That is without the Raspberry Pi Pico itself. You can use the Pico or the Pico W for this. They work equally well. You can read how to build the hardware here:
http://lucstechblog.blogspot.com/2024/10/audio-on-pico-part-2-hardware.html

Now the preparations are done we can install the software.

MicroPython drivers.

To play audio (wav) files on the Pico with MicroPython we need some drivers.

From MicroPython we need two drivers called chunk.py and wave.py. You can get them here:
https://github.com/joeky888/awesome-micropython-lib/tree/master/Audio
Just read the text and halfway the page you will find the links.

Next to these you will need some extra drivers which you can find here:
https://github.com/danjperron/PicoAudioPWM
From this link you will need to copy: myDMA.py, myPWM.py and wavePlayer.py. These drivers are provided courtesy of Daniel Per.ron

Just download and copy these five drivers to your Pico's lib directory.

For your convenience I have made a collection of all necessary drivers in my Github repositry. Here is the link:

https://github.com/Lucvolders/MicroPython/tree/main/Libraries/PicoAudio

Every file is just a textfile and can be copied and then pasted in Thonny. You can then save the file in Thonny's lib directory. Make sure to give the file exact the same name as they have in the repositry.

Your Pico's lib directory should now look like this:



Undoubtedly you will have more libraries loaded but these should be present to play audio.

Back to Audacity

In a previous story on this weblog I showed how you can record your own voice with Audacity. You can read that story here:
http://lucstechblog.blogspot.com/2024/10/audacity-pico-audio-part-1.html

In that story I gave you some "homework". I asked you to record the sentence "Hello world, this is the Raspberry Pi Pico talking" and save it on your computer using the name hello.wav

If you haven't done that please do it now because we are going to use that as a test for our audio player.

Make a new directory on your Pico and call it testsounds. Copy hello.wav to that directory.

If you do not know how to copy data files to a directory on your Pico you can follow this tutorial: http://lucstechblog.blogspot.com/2023/07/a-few-small-thonny-tips.html



This is how your Pico's directory should look now in Thonny. Well that is if you are using a freshly installed Pico like I did.

Let's play some sound.

To play a sound we need just a small MicroPython program. Here it is:

from wavePlayer import wavePlayer
import time

player = wavePlayer()

while True:
    player.play('/testsounds/hello.wav')
    time.sleep(3)

I think this is self-explanatory nevertheless I will give some brief explanation.

from wavePlayer import wavePlayer
import time


The wavePlayer driver is imported in the Pico and so is the time library.
As stated in the beginning of this story we need 5 libraries to get the audio working. The wavePlayer library uses the other 4 libraries and loads them automatically.

player = wavePlayer()

The wavePlayer library is activated and we call it player.

while True:
    player.play('/testsounds/hello.wav')
    time.sleep(3)


An endless loop is created and in that loop player.play('/testsounds/hello.wav') plays the sound. Then there is a 3 second pause and the sound is repeated.

Space limitations.

Wave files take a lot of memory. My simple sentence "Hello world, this is the Raspberry Pi Pico talking" is converted in a 128k file. AS MicroPython itself also occupies a lot of flash memory there is in this example just 616K memory left for programs and libraries.

In the first story (the one about Audacity) I asked you to record several audio files. These take about 447k memory. So there is not much room for many or large audio files. But there is a solution for that which I will show in an upcoming story.

Ideas ???

Well this can be used in a lot of ways. In upcoming stories I'll show how to build a talking thermometer and a talking clock. Besides these how about:

- a talking doorbell
- audio feedback when a button was pressed
- an audio alarm when someone opens the cookie jar
- a piano that plays real recorded tones
- a musical greeting card
- an audio alarm for sensors

And last but not least. I will show you how to build a real audio player that plays music, podcasts etc as long as they are converted to 8k wav files !!!

Till next time
have fun

Luc Volders