Programmatically detect if headphones are connected

Hi. This is not a direct question of myself, but for my friend. My friend has a bluetooth speaker and bluetooth headphone. The issue she is facing with them:

The issue is that I set the configuration in Pulse Effects for the speakers, the bluetooth ones, not internal. Sounds right. But when I use my headphones this settings do not work and the sound is rather bad.
So I configure Pulse Effects again to fit the headphones
And then the speakers sound horrible.
The speakers suffer more than the headphones, but still.
Ideally I would want to have two profiles that could be swapped/switched automatically when I change the device.

I thought of making a script that sets the Pulse Effects preset when the device is swapped. I used Gst and here is the demo script I made to get devices:

Source code
import gi, sys
gi.require_version("Gst",  "1.0")
from gi.repository import GLib, Gst

def device_edited(bus, message, arg):

    if message.type == Gst.MessageType.DEVICE_ADDED:

        device = message.parse_device_added()
        print(f"You added device {device.get_display_name()}")

    elif message.type == Gst.MessageType.DEVICE_REMOVED:

        device = message.parse_device_removed()
        print(f"You removed device {device.get_display_name()}")

    return True

Gst.init(sys.argv)

monitor = Gst.DeviceMonitor()

bus = monitor.get_bus()
bus.add_watch(0, device_edited, None)

monitor.start()
devs = monitor.get_devices()

print("These are the devices you already have :")
for dev in devs:
    print(dev.get_display_name())
print()

print("Running Loop")
GLib.MainLoop().run()

The problem is the script detects only the speaker and not the headphone. Later we came to that Pulse Effects can change presets based on device, and even it was not able to detect “headphone” as a device.
But I have seen (in Preferences > Sound) that the settings can show that my speakers are headphone, when I plug them.

1. Do you have any idea or tool or software or plugin that can change the audio profile/presets when the device is swapped?
2. How to detect headphones ? What changes do I need to make to my script?

Any kind of help is welcome :slight_smile:

Hello,
I have done something similar for long time ago in the Audio-recorder project.
Maybe you need to set and call the gst_device_monitor_add_filter() ?
Please check the src/gst-devices.c file in
https://code.launchpad.net/~audio-recorder/audio-recorder/trunk

Audio-recorder is an old project that needs a complete re-design and re-write.
Though it still does basic recording quite well.

It is a long time since, but I would like to know how to check if the added/detected device is a microphone (mic, sound input device).

EDIT:
it also has ready-made .deb packages and it is easy to compile from source (in case you want to check if it finds your headphone). Ref: https://launchpad.net/~audio-recorder/+archive/ubuntu/ppa

1 Like

Thanks, will surely look into it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.