From Badge team
Jump to navigation Jump to search

HackerHotel2019 Badge Audio API[edit]

Back to HackerHotel2019Badge/MicroPython

Audio API[edit]

Enjoying audio is easy.

import audio

For streaming audio, a wifi connection is of-course needed.

Backward compatibility[edit]

Since not all badges have audio, it's advisable to test for audio library availability, this can be done pretty easily . .

    import audio, wifi
    if wifi.wait(10, True):
except ImportError as err:

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[edit]

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(-32, 0)
audio.mixer_ctl_1(32, 32)

Full audio API[edit]

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 between 0 and 1024.
audio.volume(n)                                                               sets the current volume level.
audio.mixer_ctl_0()                                                           returns the current right audio mixer settings.
audio.mixer_ctl_0(n, m)                                                       configures the right audio signal, both values are between -32 and 32.
audio.mixer_ctl_1()                                                           returns the current left audio mixer settings.
audio.mixer_ctl_1(n, m)                                                       configures the left audio signal.


badge.nvs_set_u16("modaudio", "volume", 11)     # this thing goes to eleven and way way beyond :) 

inv0_0 = 1
inv0_1 = 0
vol0_0 = 32
vol0_1 = 0

badge.nvs_set_u16("modaudio", "mixer_ctl_0", (inv0_1 << 15) + (vol0_1 << 8) + (inv0_0 << 7) + (vol0_0 << 0))

inv1_0 = 0
inv1_1 = 0
vol1_0 = 32
vol1_1 = 32

badge.nvs_set_u16("modaudio", "mixer_ctl_1", (inv1_1 << 15) + (vol1_1 << 8) + (inv1_0 << 7) + (vol1_0 << 0))