Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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()
@richardmalone

This comment has been minimized.

Copy link

richardmalone commented Jan 24, 2014

Very useful script, however I found that I needed to change line 22:

elif iface == "Control"     >>     elif iface == "Device"

to make it unload the module-loopback on a Ubuntu 12.10 derivative, otherwise it just layers more of the same modules on top of each other. I am amazed that this sort of functionality is not configurable in pulseaudio, have you discovered any other ways of doing this? Thanks for sharing.

RLM

@Toasty27

This comment has been minimized.

Copy link
Owner Author

Toasty27 commented Feb 3, 2014

I actually had a similar problem. Disconnecting the bluetooth device properly triggered the removal of all loopback modules for the bluetooth devices, but pausing and restarting an audio stream would layer on more loopback modules. You had to disconnect to remove the modules.

I just revised the script though, and it seems to work for me. The problem was that the org.bluez.AudioSource interface would spit out "state=connected" every time an audio stream was re-started. The solution was to switch to org.bluez.Control and check for "Connected=true" instead.

I tried org.bluez.Device instead, but that causes the script to attempt loading the loopback module before PulseAudio could load the bluetooth-device module, so the loopback module would fail to load. Monitoring the Control interface solved that issue, and it's just as consistent as the Device interface.

Let me know if you have any problems with it though. I only have two android devices to test with.

EDIT: Actually, I'm curious as to what the output of dbus-monitor sender=org.bluez is on your system when you add/remove a bluetooth device.

@Douglas6

This comment has been minimized.

Copy link

Douglas6 commented Feb 9, 2014

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

@boulund

This comment has been minimized.

Copy link

boulund commented Oct 12, 2014

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

@rogercorrea

This comment has been minimized.

Copy link

rogercorrea commented Feb 27, 2016

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
You can’t perform that action at this time.