• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3USAGE = """
4Usage:
5    python ladspa-dbus.py <sinkname> [values]
6
7The "sinkname" parameter is the name of the ladspa sink. The "values"
8parameter is a comma-separated list of ladspa sink parameter values. A
9value in the list can be either string "default" or a float.
10
11Example usage:
12
13    python ladspa-dbus.py ladspa_1 10.0,default,4.0,0.6,default
14
15This command will configure sink "ladspa_1" by setting the first value
16to 10.0, the second to the default value (specified in the ladspa
17filter), the third to 4.0 and so on.
18"""
19
20import dbus
21import os
22import sys
23
24def get_ladspa_property_interface(sinkname):
25
26    # do some D-Bus stuff to get to the real ladspa property object
27    session = dbus.SessionBus()
28
29    # get the private D-Bus socket address from PulseAudio properties
30    session_property_iface = dbus.Interface(session.get_object("org.PulseAudio1", "/org/pulseaudio/server_lookup1"), "org.freedesktop.DBus.Properties")
31    socket = session_property_iface.Get("org.PulseAudio.ServerLookup1", "Address")
32
33    # connect to the private PulseAudio D-Bus socket
34    connection = dbus.connection.Connection(socket)
35
36    # core object for listing the sinks
37    core = connection.get_object(object_path="/org/pulseaudio/core1")
38
39    # object path to the ladspa sink
40    ladspa_sink_path = core.GetSinkByName(sinkname)
41
42    # property interface proxy for the sink
43    ladspa_sink_property_iface = dbus.Interface(connection.get_object(object_path=ladspa_sink_path), "org.freedesktop.DBus.Properties")
44
45    return ladspa_sink_property_iface
46
47def parse_arguments(args):
48
49    sinkname = None
50    arguments = []
51    defaults = []
52
53    if len(args) >= 2:
54        sinkname = args[1]
55
56        if len(args) == 3:
57            tokens = args[2].split(",")
58
59            for token in tokens:
60                if token == "default":
61                    arguments.append(0.0)
62                    defaults.append(True)
63                else:
64                    arguments.append(float(token))
65                    defaults.append(False)
66
67    """
68    print("Input arguments:")
69    print("         sink: " + sink)
70    print("    arguments: " + str(arguments))
71    print("     defaults: " + str(defaults))
72    """
73
74    return sinkname, arguments, defaults
75
76def print_arguments(arguments, defaults):
77    for i in range(len(arguments)):
78        default = ""
79        if defaults[i]:
80            default = "default"
81        print(str(i) + " : " + str(arguments[i]) + " \t"  + default)
82
83
84sinkname, arguments, defaults = parse_arguments(sys.argv)
85
86if sinkname == None:
87    print USAGE
88    sys.exit(1)
89
90# get the D-Bus property interface of the sink
91ladspa = get_ladspa_property_interface(sinkname)
92
93# read the current sink arguments from PulseAudio
94oldarguments, olddefaults = ladspa.Get("org.PulseAudio.Ext.Ladspa1", "AlgorithmParameters")
95
96print("Current LADSPA parameters for sink " + sinkname + ":")
97print_arguments(oldarguments, olddefaults)
98
99if len(arguments) != 0:
100    # set new arguments if they were provided on the command line
101
102    # Set the arguments ...
103    ladspa.Set("org.PulseAudio.Ext.Ladspa1", "AlgorithmParameters", (dbus.Array(arguments), dbus.Array(defaults)))
104
105    # ... and read them back.
106    newarguments, newdefaults = ladspa.Get("org.PulseAudio.Ext.Ladspa1", "AlgorithmParameters")
107
108    print("New LADSPA parameters for sink " + sinkname + ":")
109    print_arguments(newarguments, newdefaults)
110
111# test the GetAll functionality
112# print(str(ladspa.GetAll("org.PulseAudio.Ext.Ladspa1")))
113