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

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

  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 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 math import frexp 
 23  from flumotion.component import feedcomponent 
 24   
 25  __version__ = "$Rev: 6125 $" 
 26   
 27   
28 -class Volume(feedcomponent.Effect):
29 """ 30 I am an effect that can be added to any component that has a level 31 element and a way of controlling volume. 32 33 My component should implement setVolume() and getVolume() 34 """ 35 logCategory = "volume" 36
37 - def __init__(self, name, element, pipeline, allowIncrease=True, 38 allowVolumeSet=True):
39 """ 40 @param element: the level element 41 @param pipeline: the pipeline 42 @param allowIncrease: whether the component allows > 1.0 volume level 43 @param allowVolumeSet: whether the component allows setting volume 44 """ 45 feedcomponent.Effect.__init__(self, name) 46 self._element = element 47 # FIXME: set notification to every 1/5th sec, but maybe make 48 # configurable ? 49 element.set_property('interval', 200000000) 50 bus = pipeline.get_bus() 51 bus.add_signal_watch() 52 bus.connect('message::element', self._bus_message_received_cb) 53 self.firstVolumeValueReceived = False 54 self.allowIncrease = allowIncrease 55 self.allowVolumeSet = allowVolumeSet
56
57 - def setUIState(self, state):
58 feedcomponent.Effect.setUIState(self, state) 59 if state: 60 for k in 'peak', 'decay', 'rms': 61 state.addKey('volume-%s' % k, [-100.0]) 62 state.addKey('volume-volume', self.effect_getVolume()) 63 state.addKey('volume-allow-increase', self.allowIncrease) 64 state.addKey('volume-allow-set', self.allowVolumeSet)
65
66 - def _bus_message_received_cb(self, bus, message):
67 """ 68 @param bus: the message bus sending the message 69 @param message: the message received 70 """ 71 if message.structure.get_name() == 'level': 72 s = message.structure 73 peak = list(s['peak']) 74 decay = list(s['decay']) 75 rms = list(s['rms']) 76 for l in peak, decay, rms: 77 for index, v in enumerate(l): 78 try: 79 v = frexp(v) 80 except (SystemError, OverflowError, ValueError): 81 # It was an invalid value (e.g. -Inf), so clamp to 82 # something appropriate 83 l[index] = -100.0 84 if not self.uiState: 85 self.warning("effect %s doesn't have a uiState" % 86 self.name) 87 else: 88 for k, v in ('peak', peak), ('decay', decay), ('rms', rms): 89 self.uiState.set('volume-%s' % k, v) 90 if not self.firstVolumeValueReceived: 91 self.uiState.set('volume-volume', self.effect_getVolume()) 92 self.firstVolumeValueReceived = True
93
94 - def effect_setVolume(self, value):
95 """ 96 Sets volume 97 98 @param value: what value to set volume to (float between 0.0 and 4.0) 99 100 Returns: the actual value it was set to 101 """ 102 if self.allowVolumeSet: 103 self.component.setVolume(value) 104 # notify admin clients 105 self.uiState.set('volume-volume', value) 106 return value
107
108 - def effect_getVolume(self):
109 """ 110 Gets current volume setting. 111 112 @return: what value the volume is set to 113 @rtype: float (between 0.0 and 4.0) 114 """ 115 if self.allowVolumeSet: 116 return self.component.getVolume() 117 else: 118 return 1.0
119