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,2008,2009 Fluendo, S.L. 
  6  # Copyright (C) 2010,2011 Flumotion Services, S.A. 
  7  # All rights reserved. 
  8  # 
  9  # This file may be distributed and/or modified under the terms of 
 10  # the GNU Lesser General Public License version 2.1 as published by 
 11  # the Free Software Foundation. 
 12  # This file is distributed without any warranty; without even the implied 
 13  # warranty of merchantability or fitness for a particular purpose. 
 14  # See "LICENSE.LGPL" in the source distribution for more information. 
 15  # 
 16  # Headers in this file shall remain intact. 
 17   
 18  from math import frexp 
 19  from flumotion.component import feedcomponent 
 20   
 21  __version__ = "$Rev$" 
 22   
 23   
24 -class Volume(feedcomponent.Effect):
25 """ 26 I am an effect that can be added to any component that has a level 27 element and a way of controlling volume. 28 29 My component should implement setVolume() and getVolume() 30 """ 31 logCategory = "volume" 32
33 - def __init__(self, name, element, pipeline, allowIncrease=True, 34 allowVolumeSet=True):
35 """ 36 @param element: the level element 37 @param pipeline: the pipeline 38 @param allowIncrease: whether the component allows > 1.0 volume level 39 @param allowVolumeSet: whether the component allows setting volume 40 """ 41 feedcomponent.Effect.__init__(self, name) 42 self._element = element 43 # FIXME: set notification to every 1/5th sec, but maybe make 44 # configurable ? 45 element.set_property('interval', 200000000) 46 bus = pipeline.get_bus() 47 bus.add_signal_watch() 48 bus.connect('message::element', self._bus_message_received_cb) 49 self.firstVolumeValueReceived = False 50 self.allowIncrease = allowIncrease 51 self.allowVolumeSet = allowVolumeSet
52
53 - def setUIState(self, state):
54 feedcomponent.Effect.setUIState(self, state) 55 if state: 56 for k in 'peak', 'decay', 'rms': 57 state.addKey('volume-%s' % k, [-100.0]) 58 state.addKey('volume-volume', self.effect_getVolume()) 59 state.addKey('volume-allow-increase', self.allowIncrease) 60 state.addKey('volume-allow-set', self.allowVolumeSet)
61
62 - def _bus_message_received_cb(self, bus, message):
63 """ 64 @param bus: the message bus sending the message 65 @param message: the message received 66 """ 67 if message.structure.get_name() == 'level': 68 s = message.structure 69 peak = list(s['peak']) 70 decay = list(s['decay']) 71 rms = list(s['rms']) 72 for l in peak, decay, rms: 73 for index, v in enumerate(l): 74 try: 75 v = frexp(v) 76 except (SystemError, OverflowError, ValueError): 77 # It was an invalid value (e.g. -Inf), so clamp to 78 # something appropriate 79 l[index] = -100.0 80 if not self.uiState: 81 self.warning("effect %s doesn't have a uiState" % 82 self.name) 83 else: 84 for k, v in ('peak', peak), ('decay', decay), ('rms', rms): 85 self.uiState.set('volume-%s' % k, v) 86 if not self.firstVolumeValueReceived: 87 self.uiState.set('volume-volume', self.effect_getVolume()) 88 self.firstVolumeValueReceived = True
89
90 - def effect_setVolume(self, value):
91 """ 92 Sets volume 93 94 @param value: what value to set volume to (float between 0.0 and 4.0) 95 96 Returns: the actual value it was set to 97 """ 98 if self.allowVolumeSet: 99 self.component.setVolume(value) 100 # notify admin clients 101 self.uiState.set('volume-volume', value) 102 return value
103
104 - def effect_getVolume(self):
105 """ 106 Gets current volume setting. 107 108 @return: what value the volume is set to 109 @rtype: float (between 0.0 and 4.0) 110 """ 111 if self.allowVolumeSet: 112 return self.component.getVolume() 113 else: 114 return 1.0
115