Playing audio files on the micro:bit

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
fizban
Posts: 24
Joined: Mon Oct 24, 2016 2:32 pm

Playing audio files on the micro:bit

Post by fizban » Mon Mar 27, 2017 7:40 pm

I have been experimenting with the audio module. The example provided in the documentation opens the possibility to play audio files. I have not been able to store files larger than 4KB so the only solution I have found is to split larger files into 4KB chunks. This can be easily done in Audacity with Analyze-Regular interval labels and then File-Export Multiple. Although the values accepted by the audio.play function are supposed to be signed bytes between -128 and 127 I have been successful exporting the files in raw format, header-less, with unsigned 8 bits PCM. They sound quite OK on the micro:bit.

Assuming around 30KB of storage, at a 8KHz sampling frequency it gives around 3.5 seconds of recording sound.

Here is a sample code to play the attached files. They are based on the sound at https://www.freesound.org/people/urupin/sounds/199905/ , which has a CC0 license.

Code: Select all

import audio


def read_frame(f_list, frame):
    for file in f_list:
        ln = file.readinto(frame)
        while ln:
            yield frame
            ln = file.readinto(frame)


def play_file(f):
    with open(f + "-01.raw", "rb") as file1, \
         open(f + "-02.raw", "rb") as file2, \
         open(f + "-03.raw", "rb") as file3, \
         open(f + "-04.raw", "rb") as file4:
        f_list = [file1, file2, file3, file4]
        audio.play(read_frame(f_list, frame), wait=True)


# Allocate memory outside the interrupt
frame = audio.AudioFrame()
ln = -1
file = 1
# play the files
play_file("robot")
Anyone can think of a way of storing more than 3.5 seconds (without using an external sd card)? I was thinking on some kind of basic ADPCM, but it seems that the 2ms that the interrupt has to process the sound is not enough to do even a simple loop through the 32 elements of the AudioFrame...
Attachments
robot_sound.zip
R2D2 like sound
(14.14 KiB) Downloaded 534 times

User avatar
deshipu
Posts: 1374
Joined: Thu May 28, 2015 5:54 pm

Re: Playing audio files on the micro:bit

Post by deshipu » Tue Mar 28, 2017 7:28 am

I think that you can connect an SD card over SPI to the microbit. Not sure if it's possible to mount it as a filesystem, but in the worst case you can simply read raw blocks of data from it. An SPI flash or EEPROM chip would work too.

fizban
Posts: 24
Joined: Mon Oct 24, 2016 2:32 pm

Re: Playing audio files on the micro:bit

Post by fizban » Tue Mar 28, 2017 7:57 am

Thanks, deshipu. Yes, that is the alternative I was thinking of as well, but I was wondering whether it would be possible to store the information in a more efficient way in the micro:bit without the need of external storage devices and also be able to fit the decompression routine within the interrupt in python... Being able to store up to 10 seconds would be nice...

I believe this little thing has less than 4K and stores 10s. I know it is not comparable, but if I have to use an external device, I would probably use an external mp3 player...

olliec002004
Posts: 1
Joined: Mon Jun 15, 2020 1:55 pm

Re: Playing audio files on the micro:bit

Post by olliec002004 » Mon Jun 15, 2020 2:01 pm

Hi people,

Im very much new to microbit and python.

Im trying to make an electric drum kit for my mate to play in a band
My question is how can I get the microbit to play drum audio files through a regular speaker and quarter inch jack and crocodile clips as shown in LINK. "completed circuit" with ground will play the sound.

cheers. modified code of version above would be very helpful.

https://www.youtube.com/watch?v=bm7MGKspk0o

Post Reply