Package flumotion :: Package component :: Package effects :: Package volume :: Module admin_gtk
[hide private]

Source Code for Module flumotion.component.effects.volume.admin_gtk

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  from gettext import gettext as _ 
 23   
 24  import gtk 
 25  import os 
 26  import math 
 27   
 28  # import custom glade handler 
 29  from flumotion.ui import glade 
 30  from flumotion.ui.fvumeter import FVUMeter 
 31  from flumotion.component.base.effectsnode import EffectAdminGtkNode 
 32   
 33  __version__ = "$Rev: 7073 $" 
 34   
 35   
36 -def clamp(x, min, max):
37 if x < min: 38 return min 39 elif x > max: 40 return max 41 return x
42 43
44 -class VolumeAdminGtkNode(EffectAdminGtkNode):
45 logCategory = 'volume' 46 gladeFile = os.path.join('flumotion', 'component', 'effects', 47 'volume', 'volume.glade') 48 49 uiStateHandlers = None 50
51 - def haveWidgetTree(self):
52 self.widget = self.wtree.get_widget('volume-widget') 53 self.level_widgets = [] 54 self._volume_set_label = self.wtree.get_widget('volume-set-label') 55 self._volume_set_label.set_text('0') 56 self.shown = False 57 58 # now do the callbacks for the volume setting 59 self._hscale = self.wtree.get_widget('volume-set-hscale') 60 self._scale_changed_id = self._hscale.connect('value_changed', 61 self.cb_volume_set) 62 self._hscale.set_sensitive(False) 63 # callback for checkbutton 64 check = self.wtree.get_widget('volume-set-check') 65 check.set_sensitive(False) 66 check.connect('toggled', self._check_toggled_cb) 67 changeLabel = self.wtree.get_widget('volume-change-label') 68 changeLabel.set_sensitive(False)
69
70 - def setUIState(self, state):
71 EffectAdminGtkNode.setUIState(self, state) 72 if not self.uiStateHandlers: 73 self.uiStateHandlers = {'volume-volume': self.volumeSet, 74 'volume-peak': self.peakSet, 75 'volume-decay': self.decaySet} 76 for k, handler in self.uiStateHandlers.items(): 77 handler(state.get(k)) 78 # volume-allow-increase is static for lifetime of component 79 # for soundcard it is false, for others that have a gst volume 80 # element it is true 81 if state.get("volume-allow-increase"): 82 check = self.wtree.get_widget('volume-set-check') 83 if check is not None: 84 check.set_sensitive(True) 85 if state.get("volume-allow-set"): 86 self._hscale.set_sensitive(True) 87 changeLabel = self.wtree.get_widget('volume-change-label') 88 if changeLabel is not None: 89 changeLabel.set_sensitive(True)
90
91 - def _createEnoughLevelWidgets(self, numchannels):
92 """ 93 This method dynamically creates labels and level meters for channels 94 that currently do not have level meters. The glade file no longer 95 contains the labels or the level meters. Also the table size in the 96 glade file is set to 50 and the widgets inside the table that are 97 statically configured have a bottom y of 50 allowing about 23 channels 98 in the audio. 99 100 @param numchannels: total number of channels there is volume data for 101 """ 102 if numchannels > len(self.level_widgets): 103 totalLevelWidgets = len(self.level_widgets) 104 for chan in range(totalLevelWidgets, numchannels): 105 levelWidget = FVUMeter() 106 levelLabel = gtk.Label() 107 if chan == 0 and numchannels > 1: 108 levelLabel.set_text(_("Left channel level:")) 109 elif numchannels == 1: 110 levelLabel.set_text(_("Mono channel level:")) 111 elif chan == 1: 112 levelLabel.set_text(_("Right channel level:")) 113 else: 114 levelLabel.set_text(_("Channel %d level:") % chan) 115 levelLabel.set_property("xpad", 0) 116 levelLabel.set_property("ypad", 0) 117 levelLabel.set_property("xalign", 0) 118 levelLabel.set_property("yalign", 0.5) 119 levelLabel.set_justify(gtk.JUSTIFY_LEFT) 120 self.widget.attach(levelLabel, 0, 1, chan * 2, chan * 2 + 1, 121 xoptions=gtk.FILL, yoptions=0, xpadding=3, ypadding=3) 122 self.widget.attach(levelWidget, 0, 1, chan * 2 + 1, 123 chan * 2 + 2, yoptions=gtk.FILL, 124 xpadding=6, ypadding=3) 125 levelLabel.show() 126 levelWidget.show() 127 self.level_widgets.append(levelWidget)
128
129 - def peakSet(self, peak):
130 if len(peak) > len(self.level_widgets): 131 self._createEnoughLevelWidgets(len(peak)) 132 for i in range(0, len(peak)): 133 self.level_widgets[i].set_property('peak', 134 clamp(peak[i], -90.0, 0.0))
135
136 - def decaySet(self, decay):
137 if len(decay) > len(self.level_widgets): 138 self._createEnoughLevelWidgets(len(decay)) 139 for i in range(0, len(decay)): 140 self.level_widgets[i].set_property('decay', 141 clamp(decay[i], -90.0, 0.0))
142 143 # when volume has been set by another admin client
144 - def volumeSet(self, volume):
145 self._hscale.handler_block(self._scale_changed_id) 146 147 # Ensure that the scale has an adjustment. 148 # This is not a proper solution to a problem that 149 # can be when switching components, but it avoids an 150 # unexpected segfault in set_value which expects 151 # range->adjustment to be non-NULL 152 self._hscale.get_adjustment() 153 154 self._hscale.set_value(volume) 155 self.debug("volume: %f", volume) 156 dB = "- inf" 157 if volume: 158 dB = "%2.2f" % (20.0 * math.log10(volume)) 159 self._volume_set_label.set_text(dB) 160 self._hscale.handler_unblock(self._scale_changed_id)
161
162 - def stateSet(self, state, key, value):
163 handler = self.uiStateHandlers.get(key, None) 164 if handler: 165 handler(value)
166 167 # run when the scale is moved by user
168 - def cb_volume_set(self, widget):
169 # do something 170 volume = self._hscale.get_value() 171 #self.volumeSet(volume) 172 d = self.effectCallRemote("setVolume", volume) 173 d.addErrback(self.setVolumeErrback)
174
175 - def setVolumeErrback(self, failure):
176 self.warning("Failure %s setting volume: %s" % ( 177 failure.type, failure.getErrorMessage())) 178 return None
179
180 - def _update_volume_label(self):
181 # update the volume label's dB value 182 pass
183 184 # when the "increase volume" checkbutton is toggled
185 - def _check_toggled_cb(self, widget):
186 checked = widget.get_property('active') 187 self.debug('checkbutton toggled; now %r' % checked) 188 value = self._hscale.get_value() 189 if checked: 190 self._hscale.set_range(0.0, 4.0) 191 else: 192 if value > 1.0: value = 1.0 193 self._hscale.set_range(0.0, 1.0) 194 self.volumeSet(value)
195