Difference between revisions of "HackerHotel2019Badge/audio"

From Badge team
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 44: Line 44:
 
audio.volume()                                                                returns current volume level between 0 and 1024.
 
audio.volume()                                                                returns current volume level between 0 and 1024.
 
audio.volume(n)                                                              sets the current volume level.
 
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_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>
 
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 18:20, 13 February 2019

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))