Difference between revisions of "HackerHotel2019Badge/audio"
(Created page with "== HackerHotel2019 Badge Audio API == Enjoying audio is easy. ``` import audio audio.volume(11) audio.play_mp3_stream('https://badge.team/RoccoW_-_06_-_Pumped.mp3') ``` For...") |
(→NVS) |
||
(15 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== HackerHotel2019 Badge Audio API == | == HackerHotel2019 Badge Audio API == | ||
+ | |||
+ | Back to [[HackerHotel2019Badge/MicroPython]] | ||
+ | |||
+ | ===Audio API=== | ||
Enjoying audio is easy. | Enjoying audio is easy. | ||
− | + | <nowiki>import audio | |
− | import audio | + | audio.volume(128) |
− | audio.volume( | + | audio.play_mp3_stream('https://badge.team/RoccoW_-_06_-_Pumped.mp3')</nowiki> |
− | audio.play_mp3_stream('https://badge.team/RoccoW_-_06_-_Pumped.mp3') | ||
− | |||
For streaming audio, a wifi connection is of-course needed. | For streaming audio, a wifi connection is of-course needed. | ||
− | === Mixer ctl === | + | ===Backward compatibility=== |
+ | |||
+ | Since not all badges have audio, it's advisable to test for audio library availability, this can be done pretty easily . . | ||
+ | <nowiki>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</nowiki> | ||
+ | This can be simplified by skipping the <nowiki>as err</nowiki> and <nowiki>print(err)</nowiki> parts, and possibly the wifi part when using local mp3 files. | ||
+ | |||
+ | ===Mixer ctl=== | ||
+ | |||
+ | Since the [[HackerHotel2019Badge/Hardware#Audiojack_reversed|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 <nowiki>mixer_ctl</nowiki> api for other effects like balance between left and right, or swapping channels. | ||
+ | |||
+ | <nowiki>audio.mixer_ctl_0(-32, 0) | ||
+ | audio.mixer_ctl_1(32, 32)</nowiki> | ||
+ | |||
+ | ===Full audio API=== | ||
+ | |||
+ | <nowiki>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.</nowiki> | ||
+ | |||
+ | ==== NVS ==== | ||
+ | |||
+ | <nowiki>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))</nowiki> | |
− | |||
− | |||
− |
Latest revision as of 19:20, 13 February 2019
Contents
HackerHotel2019 Badge Audio API[edit]
Back to HackerHotel2019Badge/MicroPython
Audio API[edit]
Enjoying audio is easy.
import audio audio.volume(128) audio.play_mp3_stream('https://badge.team/RoccoW_-_06_-_Pumped.mp3')
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 . .
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[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.
NVS[edit]
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))