HackerHotel2019Badge/audio
Contents
HackerHotel2019 Badge Audio API
Back to HackerHotel2019Badge/MicroPython
Audio API
Enjoying audio is easy.
import audio audio.volume(11) audio.play_mp3_stream('https://badge.team/RoccoW_-_06_-_Pumped.mp3')
For streaming audio, a wifi connection is of-course needed.
Backward compatibility
Since not all badges have audio, it's advisable to test for audio library availability, this can be done pretty easily . .
try: import audio, wifi wifi.connect() if wifi.wait(10, True): audio.play_mp3_stream('https://annejan.com/media/badger.mp3') except ImportError as err: print(err) pass
This can be simplified by skipping the as err and print(err) parts, and possibly the wifi part when using local mp3 files.
Mixer ctl
Since the audio jack is reversed on the badges, and we don't want to force you to do a hardware modification, in software we "invert" channel 0 and add left and right to channel 1, this fixes the audio issue for passive headphones.
NB: This is automatically done by the firmware after the day 0 OTA. You can chose to disable this mod or use the mixer_ctl api for other effects like balance between left and right, or swapping channels.
audio.mixer_ctl_0(-128, 0) audio.mixer_ctl_1(128, 128)
Full audio API
audio.play_mp3_file('/media/icq.mp3') play a local mp3 file. audio.play_mp3_stream('http://streams.pinguinradio.com/PinguinRadio320.mp3') play a remote mp3 file or stream. audio.is_playing() returns True when audio is playing. audio.stop() stops audio from playing. audio.volume() returns current volume level (0-128). audio.volume(n) sets the current volume level. audio.mixer_ctl_0(n, m) configures the right audio signal. audio.mixer_ctl_1(n, m) configures the left audio signal.