Skip to content

Instantly share code, notes, and snippets.

@dghodgson
Last active December 15, 2019 05:29
Show Gist options
  • Save dghodgson/8406352 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python
# based on monitor-bluetooth
# Changes by Domen Puncer <domen@cba.si>
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()
@rogercorrea
Copy link

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