File: play_file.py

package info (click to toggle)
firmware-microbit-micropython 1.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 25,448 kB
  • sloc: ansic: 83,496; cpp: 27,664; python: 2,475; asm: 274; makefile: 245; javascript: 41; sh: 25
file content (14 lines) | stat: -rw-r--r-- 415 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#Plays a file on the specified pins.
import audio

def audio_generator(file, frame):
    ln = -1
    while ln:
        ln = file.readinto(frame)
        yield frame

def play_file(name, pin=None, return_pin=None):
    #Do allocation here, as we can't do it in an interrupt.
    frame = audio.AudioFrame()
    with open(name) as file:
        audio.play(audio_generator(file, frame), pin=pin, return_pin=return_pin)