Last active
December 15, 2019 05:29
-
-
Save dghodgson/8406352 to your computer and use it in GitHub Desktop.
A python script for automatically adding a loopback module to Pulse Audio for a bluetooth audio source when it's connected, and automatically removing the loopback module when the bluetooth device disconnects.Original code found here: https://gist.github.com/joergschiller/1673341/#comment-802735
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# based on monitor-bluetooth | |
# Changes by Domen Puncer <[email protected]> | |
import gobject | |
import dbus | |
import dbus.mainloop.glib | |
import os | |
def property_changed(name, value, path, interface): | |
iface = interface[interface.rfind(".") + 1:] | |
val = str(value) | |
print "{%s.PropertyChanged} [%s] %s = %s" % (iface, path, name, val) | |
# we want this event: {Control.PropertyChanged} [/org/bluez/16797/hci0/dev_00_24_7E_51_F7_52] Connected = true | |
# and when that happens: pactl load-module module-loopback source=bluez_source.00_24_7E_51_F7_52 | |
if iface == "Control" and name == "Connected" and val == "1": | |
bt_addr = "_".join(path.split('/')[-1].split('_')[1:]) | |
cmd = "pactl load-module module-loopback source=bluez_source.%s" % bt_addr | |
os.system(cmd) | |
# here we want this event: {Control.PropertyChanged} [/org/bluez/16797/hci0/dev_00_24_7E_51_F7_52] Connected = false | |
# and when that happens, we unload all loopback modules whose source is our bluetooth device | |
elif iface == "Control" and name == "Connected" and val == "0": | |
bt_addr = "_".join(path.split('/')[-1].split('_')[1:]) | |
cmd = "for i in $(pactl list short modules | grep module-loopback | grep source=bluez_source.%s | cut -f 1); do pactl unload-module $i; done" % bt_addr | |
os.system(cmd) | |
def object_signal(value, path, interface, member): | |
iface = interface[interface.rfind(".") + 1:] | |
val = str(value) | |
print "{%s.%s} [%s] Path = %s" % (iface, member, path, val) | |
if __name__ == '__main__': | |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | |
bus = dbus.SystemBus() | |
bus.add_signal_receiver(property_changed, bus_name="org.bluez", signal_name = "PropertyChanged", path_keyword="path", interface_keyword="interface") | |
mainloop = gobject.MainLoop() | |
mainloop.run() |
Thanks a lot for this script! Very useful. I use it in my Debian server and it works just perfectly for me now. Love being able to hook up any smartphone, tablet, or laptop to my central server that's connected to the stereo. If anyone is ever interested I wrote my own little recipe for how I set things up (mainly for my own memory, should I ever have to do it again):
https://gist.github.com/boulund/8949499e17493e1c00db
Please,
For me work only it was changing the line 22 to:
alsaloop --rate=44100 --format=S16_LE --cdevice=pulse --buffer=10000 --tlatency=500000
Because alsaloop worked but the load-module module-loopback generate exception error the type "Failed load module"?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An alternative might be to monitor the AudioSource State = "connecting" signal, and load the loop-back module only on the first State = "connected" signal after a "connecting" state