hub.audio
Make sounds: beeps, notes, tunes, music files and sound files.
Only available on hubs with a speaker (hub.board.HAS_AUDIO == 1).
Only one sound plays at a time — starting a new one stops the old one.
Most functions wait until the sound is finished. Add block=False to
start the sound and carry on with your program straight away.
File paths must be complete, for example "/sd/sound.wav".
Functions
|
Play a beep at |
|
|
|
Play one musical note. |
|
Play a sound file ( |
|
Play a built-in tune. |
|
Play your own tune: a list of |
|
Play a MIDI music file, for example |
|
Set the tempo in beats per minute for |
|
Choose how sounds are shaped: |
|
Stop whatever sound is playing. |
Module Contents
- hub.audio.beep(freq: float, duration_ms: int, *, block: bool = True) None[source]
Play a beep at
freqhertz forduration_msmilliseconds.Higher numbers sound higher: 440 is the note A, 880 is one octave above. The sound uses the current wave type (see
set_wave_type()). Withblock=Falseyour program continues while it beeps.
- hub.audio.note(note: str | int, length: float, *, block: bool = True) None[source]
Play one musical note.
noteis a note name such as"C4","D#3"(sharp) or"Bb5"(flat),"REST"for silence, or a MIDI note number (60 = middle C).lengthis in beats: 1 is one beat, 0.5 is half a beat. The speed comes fromset_note_speed().
- hub.audio.play(path: str, *, rate: int = 44100, bits: int = 16, channels: int = 1, block: bool = True) None[source]
Play a sound file (
.wavor raw.pcm).For
.wavfiles everything is read from the file, so just give the path. Raw.pcmfiles have no header, so tell it the format:rate(samples per second),bits(8, 16, 24 or 32) andchannels(1 = mono, 2 = stereo). Withblock=Falseyour program continues while the sound plays.
- hub.audio.play_melody(name: str, *, block: bool = True) None[source]
Play a built-in tune.
Choose from
"startup","shutdown","success","error","dadadum","mario"and"nokia".
- hub.audio.play_melody_from_list(melody: list[tuple[str | int, float]], *, block: bool = True) None[source]
Play your own tune: a list of
(note, length)pairs.Each note works like in
note(), for example[("C4", 1), ("E4", 1), ("G4", 2)].
- hub.audio.play_midi(path: str, *, block: bool = True) None[source]
Play a MIDI music file, for example
"/sd/song.mid".Up to 8 notes can sound at the same time. Uses the current wave type.
- hub.audio.set_note_speed(bpm: float) None[source]
Set the tempo in beats per minute for
note()andplay_melody_from_list(). Starts at 120.