It is possible to play back audio from sound files available to the micro:bit. Add the files attached to this article to the Python Editor filesystem. Copy this program into the editor then flash this program to the micro:bit.


import audio

# read the files from the microbit filesystem
def read_frame(f_list, frame):
    for file in f_list:
        ln = file.readinto(frame)
        while ln:
            yield frame
            ln = file.readinto(frame)

# craete a function to open and play each file in turn
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")

Thanks to fizban for the example.


Speech

Micropython also contains a speech synthesiser to enable the micro:bit to talk and sing.


This program uses speech.say() and speech.pronounce() with some parameters as an example of what we can do with the speech module. Copy this program into the editor and then Download/Flash this program to the micro:bit:

from microbit import *
import speech

while True:
    speech.say('I am a BBC microbit')
    sleep(100)
    speech.pronounce('BIHDDIY BIHDDIY BIHDDIY  BIHDDIY', speed=60, pitch=255)
    sleep(100)

You might want to use a powered speaker for this as the speech module is quiet.