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 1245 times

User avatar
deshipu
Posts: 1388
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

slowDan
Posts: 1
Joined: Sun Jan 17, 2021 3:48 pm

Re: Playing audio files on the micro:bit

Post by slowDan » Sun Jan 17, 2021 3:56 pm

Sorry to dig up an old post! :)

I'm trying to get this working myself, but the Python Editor tells me I can only add .py or .hex files to the file system.

I tried renaming the files to .hex but then it just says that they don't contain any valid python code. Had to be worth a try! :lol:

Anyone have any clues on how to cheat the editor?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Playing audio files on the micro:bit

Post by jimmo » Mon Jan 18, 2021 4:31 am

slowDan wrote:
Sun Jan 17, 2021 3:56 pm
Sorry to dig up an old post! :)

I'm trying to get this working myself, but the Python Editor tells me I can only add .py or .hex files to the file system.

I tried renaming the files to .hex but then it just says that they don't contain any valid python code. Had to be worth a try! :lol:

Anyone have any clues on how to cheat the editor?
Which editor are you using?

The background is that the micro:bit is programmed with a .hex file which is the complete firmware. When you use MicroPython, the generated .hex file is the MicroPython runtime combined with an encoded copy of your Python script. Any micro:bit aware editor will be able to generate this .hex file for you from your Python code. I mostly recommend Mu -- https://codewith.mu/

mfitzp
Posts: 3
Joined: Mon Jul 17, 2017 1:56 pm

Re: Playing audio files on the micro:bit

Post by mfitzp » Sun Feb 28, 2021 9:21 pm

The example above (and this https://gist.github.com/nbogie/4b2e68e1 ... 32b9c38ece ) doesn't seem to work with the new Microbit v2 boards with the built-in speaker -- there is audio, but it's completely distorted/crackling as you'd expect with a mismatch in the format and plays about 2x too long.

I've tested the example robot (R2D2) audio above and my own audio -- I've attached a comparison between the R2D2 audio, and what comes out of the speaker.

The v2 docs all suggest the AudioFrame format/etc. are the same as far as I can see + the built in sounds e.g. `Sound.YAWN` work fine and sound clear. Any ideas?
Attachments
microbitaudio.zip
(40.3 KiB) Downloaded 406 times

anmo
Posts: 1
Joined: Sun Mar 14, 2021 10:20 am

Re: Playing audio files on the micro:bit

Post by anmo » Sun Mar 14, 2021 10:28 am

Hi,

Playing custom sounds does not seem supported yet on v2.

I just found these pages where we can follow the issue :
https://github.com/microbit-foundation/ ... /issues/47
https://github.com/lancaster-university ... 2/issues/4

kjw
Posts: 10
Joined: Wed Jun 17, 2020 11:34 am

Re: Playing audio files on the micro:bit

Post by kjw » Fri Aug 06, 2021 11:14 pm

I have some audio playing (unsigned 8bit raw files) on a V2 as I type this.

Code: Select all

from microbit import *

import audio
import math

def frames_from_file(sfile, frame):
    # 32 is length of AudioFrame
    while(sfile.readinto(frame, 32) > 0):
        yield frame

def play_file(filename):
    frame = audio.AudioFrame()
    with open(filename, "rb") as snd_file:
        audio.play(frames_from_file(snd_file, frame), wait=True)
        audio.stop()

# external speaker on P0 + V2 one!
speaker.on()
   
while True:
    play_file("testaudio.raw")
    sleep(5000)

Post Reply