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