daemon: replace colons by dash in per-machine directory names for compat with weird...
[profile/ivi/pulseaudio-panda.git] / src / tests / volume-ui.py
index a2b756e..7909b80 100644 (file)
@@ -1,9 +1,15 @@
 #!/usr/bin/python
 
-import pygtk, gtk
+import pygtk, gtk, sys
 from ctypes import *
 
-libpulse = cdll.LoadLibrary("../.libs/libpulse.so")
+try:
+    libpulse = cdll.LoadLibrary("../.libs/libpulse.so")
+except OSError:
+    try:
+        libpulse = cdll.LoadLibrary(".libs/libpulse.so")
+    except OSError:
+        libpulse = cdll.LoadLibrary("libpulse.so")
 
 class ChannelMap(Structure):
     _fields_ = [("channels", c_ubyte),
@@ -29,6 +35,18 @@ class ChannelMap(Structure):
     _position_to_pretty_string.restype = c_char_p
     _position_to_pretty_string.argtypes = [c_uint]
 
+    _can_balance = libpulse.pa_channel_map_can_balance
+    _can_balance.restype = c_int
+    _can_balance.argtypes = [c_void_p]
+
+    _can_fade = libpulse.pa_channel_map_can_fade
+    _can_fade.restype = c_int
+    _can_fade.argtypes = [c_void_p]
+
+    _parse = libpulse.pa_channel_map_parse
+    _parse.restype = c_void_p
+    _parse.argtypes = [c_void_p, c_char_p]
+
     def to_name(this):
         return this._to_name(byref(this))
 
@@ -42,7 +60,7 @@ class ChannelMap(Structure):
         if r is None:
             return None
         else:
-            return s.raw
+            return s.value
 
     def position_to_string(this, pos):
         return this._position_to_string(pos)
@@ -50,11 +68,21 @@ class ChannelMap(Structure):
     def position_to_pretty_string(this, pos):
         return this._position_to_pretty_string(pos)
 
+    def can_balance(this):
+        return bool(this._can_balance(byref(this)))
+
+    def can_fade(this):
+        return bool(this._can_fade(byref(this)))
+
+    def parse(this, s):
+        if this._parse(byref(this), s) is None:
+            raise Exception("Parse failure")
+
+
 class CVolume(Structure):
     _fields_ = [("channels", c_ubyte),
                 ("values", c_uint32 * 32)]
 
-
     _snprint = libpulse.pa_cvolume_snprint
     _snprint.restype = c_char_p
     _snprint.argtypes = [c_char_p, c_ulong, c_void_p]
@@ -83,6 +111,10 @@ class CVolume(Structure):
     _set_fade.restype = c_void_p
     _set_fade.argtypes = [c_void_p, c_void_p, c_float]
 
+    _to_dB = libpulse.pa_sw_volume_to_dB
+    _to_dB.restype = c_double
+    _to_dB.argytpes = [c_uint32]
+
     def snprint(this):
         s = create_string_buffer(320)
         r = this._snprint(s, len(s), byref(this))
@@ -110,39 +142,38 @@ class CVolume(Structure):
     def set_fade(this, cm, f):
         return this._set_fade(byref(this), byref(cm), f)
 
+    def to_dB(this, channel = None):
+        if channel is None:
+            return this._to_dB(this.max())
 
+        return this._to_dB(this.values[channel])
 
 cm = ChannelMap()
-cm.channels = 6
-cm.map[0] = 1
-cm.map[1] = 2
-cm.map[2] = 3
-cm.map[3] = 5
-cm.map[4] = 6
-cm.map[5] = 7
 
-print "Channel map name: %s" % cm.to_name()
-print "Channel map mapping: %s" % cm.snprint()
+if len(sys.argv) > 1:
+    cm.parse(sys.argv[1])
+else:
+    cm.parse("surround-51")
 
 v = CVolume()
 v.channels = cm.channels
 
 for i in range(cm.channels):
-    v.values[i] = 65536/2
+    v.values[i] = 65536
 
-print v.max()
-print v.snprint()
-print v.get_balance(cm)
-print v.get_fade(cm)
+title = cm.to_pretty_name()
+if title is None:
+    title = cm.snprint()
 
 window = gtk.Window(gtk.WINDOW_TOPLEVEL)
-window.set_title(cm.to_pretty_name())
+window.set_title(unicode(title))
 window.set_border_width(12)
 
 vbox = gtk.VBox(spacing=6)
 
 channel_labels = {}
 channel_scales = {}
+channel_dB_labels = {}
 
 def update_volume(update_channels = True, update_fade = True, update_balance = True, update_scale = True):
     if update_channels:
@@ -158,6 +189,11 @@ def update_volume(update_channels = True, update_fade = True, update_balance = T
     if update_fade:
         fade_scale.set_value(v.get_fade(cm))
 
+    for i in range(cm.channels):
+        channel_dB_labels[i].set_label("%0.2f dB" % v.to_dB(i))
+
+    value_dB_label.set_label("%0.2f dB" % v.to_dB())
+
 def fade_value_changed(fs):
     v.set_fade(cm, fade_scale.get_value())
     update_volume(update_fade = False)
@@ -180,19 +216,26 @@ for i in range(cm.channels):
     vbox.pack_start(channel_labels[i], expand=False, fill=True)
 
     channel_scales[i] = gtk.HScale()
-    channel_scales[i].set_range(0, 65536)
+    channel_scales[i].set_range(0, 65536*3/2)
     channel_scales[i].set_digits(0)
     channel_scales[i].set_value_pos(gtk.POS_RIGHT)
     vbox.pack_start(channel_scales[i], expand=False, fill=True)
 
+    channel_dB_labels[i] = gtk.Label("-xxx dB")
+    channel_dB_labels[i].set_alignment(1, 1)
+    vbox.pack_start(channel_dB_labels[i], expand=False, fill=True)
+
 value_label = gtk.Label("Value")
 value_label.set_alignment(0, .5)
 vbox.pack_start(value_label, expand=False, fill=True)
 value_scale = gtk.HScale()
-value_scale.set_range(0, 65536)
+value_scale.set_range(0, 65536*3/2)
 value_scale.set_value_pos(gtk.POS_RIGHT)
 value_scale.set_digits(0)
 vbox.pack_start(value_scale, expand=False, fill=True)
+value_dB_label = gtk.Label("-xxx dB")
+value_dB_label.set_alignment(1, 1)
+vbox.pack_start(value_dB_label, expand=False, fill=True)
 
 balance_label = gtk.Label("Balance")
 balance_label.set_alignment(0, .5)
@@ -213,7 +256,7 @@ fade_scale.set_digits(2)
 vbox.pack_start(fade_scale, expand=False, fill=True)
 
 window.add(vbox)
-window.set_default_size(600, 400)
+window.set_default_size(600, 50)
 
 update_volume()
 
@@ -223,5 +266,17 @@ fade_scale.connect("value_changed", fade_value_changed)
 balance_scale.connect("value_changed", balance_value_changed)
 value_scale.connect("value_changed", value_value_changed)
 
-window.show_all()
+vbox.show_all()
+
+if not cm.can_balance():
+    balance_label.hide()
+    balance_scale.hide()
+
+if not cm.can_fade():
+    fade_label.hide()
+    fade_scale.hide()
+
+
+window.show()
+
 gtk.main()