summaryrefslogtreecommitdiffstats
path: root/pulseaudio/PulseVolume.py
blob: 1ff6b7a0dff77bd7e19a7def47274a15f553ab46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python
# vi: et sw=2
#
# PulseVolume.py
# Copyright (C) 2009  Harry Karvonen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# Author: Harry Karvonen <harry.karvonen@gmail.com>
#

from lib_pulseaudio import *
import math

# This contains all basic volume features
class PulseVolume:
  def __init__(self, vol = 0, channels = 2):
    self.channels = channels

    if vol > 100 or vol < 0:
      print "WARNING: Volume is invalid!"
      vol = 0

    self.values   = [vol] * self.channels

    return

  ##############################
  #
  # Type conversions
  #
  #def fromCtypes(self, pa_cvolume):
  #  self.channels = pa_cvolume.channels
  #  self.values   = map(lambda x: (math.ceil(float(x) * 100 / PA_VOLUME_NORM)),
  #                      pa_cvolume.values[0:self.channels])
  #  return self

  def toCtypes(self):
    ct = PA_CVOLUME()
    ct.channels = self.channels

    for x in range(0, self.channels):
      ct.values[x] = (self.values[x] * PA_VOLUME_NORM) / 100
    
    return ct

  ###

  def printDebug(self):
    print "PulseVolume"
    print "self.channels:", self.channels
    print "self.values:", self.values
    #print "self.proplist:", self.proplist

  ###

  def incVolume(self, vol):
    "Increment volume level (mono only)"
    vol += sum(self.values) / len(self.values)

    vol = int(vol)

    if vol > 100:
      vol = 100
    elif vol < 0:
      vol = 0

    self.setVolume(vol)

    return

  ###

  def setVolume(self, vol, balance = None):
    if not balance:
      self.values = [vol] * self.channels
    else:
      self.values[balance] = vol

    return

  ###

  def getVolume(self):
    "Return mono volume"
    return int(sum(self.values) / len(self.values))

  ###

  def __str__(self):
    return "Channels: " + str(self.channels) + \
           ", values: \"" + str(map(lambda x: str(x) + "%", self.values)) + "\""

################################################################################

class PulseVolumeCtypes(PulseVolume):
  def __init__(self, pa_cvolume):
    self.channels = pa_cvolume.channels
    self.values   = map(lambda x: (math.ceil(float(x) * 100 / PA_VOLUME_NORM)),
                        pa_cvolume.values[0:self.channels])
    return