2
* KMix -- KDE's full featured mini mixer
5
* Copyright (C) 1996-2004 Christian Esken <esken@kde.org>
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Library General Public
9
* License as published by the Free Software Foundation; either
10
* version 2 of the License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Library General Public License for more details.
17
* You should have received a copy of the GNU Library General Public
18
* License along with this program; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30
int Volume::_channelMaskEnum[9] =
31
{ MLEFT, MRIGHT, MCENTER,
33
MSURROUNDLEFT, MSURROUNDRIGHT,
34
MREARSIDELEFT, MREARSIDERIGHT,
40
init( Volume::MNONE, 0, 0, false, false);
43
Volume::Volume( ChannelMask chmask, long maxVolume, long minVolume, bool hasSwitch, bool isCapture )
45
init(chmask, maxVolume, minVolume, hasSwitch, isCapture );
50
Volume::Volume( const Volume &v )
53
_maxVolume = v._maxVolume;
54
_minVolume = v._minVolume;
55
_hasSwitch = v._hasSwitch;
56
_switchActivated = v._switchActivated;
57
_isCapture = v._isCapture;
58
setVolume(v, (ChannelMask)v._chmask);
59
// kDebug(67100) << "Volume::copy-constructor initialized " << v << "\n";
62
void Volume::init( ChannelMask chmask, long maxVolume, long minVolume, bool hasSwitch, bool isCapture )
64
for ( int i=0; i<= Volume::CHIDMAX; i++ ) {
68
_maxVolume = maxVolume;
69
_minVolume = minVolume;
70
_hasSwitch = hasSwitch;
71
_isCapture = isCapture;
73
_switchActivated = false;
77
void Volume::setAllVolumes(long vol)
79
for ( int i=0; i<= Volume::CHIDMAX; i++ ) {
80
if ( (_channelMaskEnum[i]) & _chmask ) {
81
// we are supposed to set it
82
_volumes[i] = volrange(vol);
87
void Volume::changeAllVolumes( long step )
89
for (unsigned int i=Volume::CHIDMIN; i <= Volume::CHIDMAX; i++)
91
int volToChange = getVolume((Volume::ChannelID)i);
92
volToChange += (int)step;
93
setVolume((Volume::ChannelID)i, volToChange);
99
void Volume::setVolume( ChannelID chid, long vol)
101
if ( chid>=0 && chid<=Volume::CHIDMAX ) {
102
// accepted. we don't care if we support the channel,
103
// because there is NO good action we could take.
104
// Anyway: getVolume() on an unsupported channel will return 0 all the time
105
_volumes[chid] = volrange(vol);
110
* Copy the volume elements contained in v to this Volume object.
111
* Only those elments are copied, that are supported in BOTH Volume objects.
113
void Volume::setVolume(const Volume &v)
115
setVolume(v, (ChannelMask)(v._chmask&_chmask) );
119
* Copy the volume elements contained in v to this Volume object.
120
* Only those elments are copied, that are supported in BOTH Volume objects
121
* and match the ChannelMask given by chmask.
123
void Volume::setVolume(const Volume &v, ChannelMask chmask) {
124
for ( int i=0; i<= Volume::CHIDMAX; i++ ) {
125
if ( _channelMaskEnum[i] & _chmask & (int)chmask ) {
126
// we are supposed to copy it
127
_volumes[i] = volrange(v._volumes[i]);
130
// Safety first! Lets play safe here and put sane values in
137
long Volume::maxVolume() {
141
long Volume::minVolume() {
145
int Volume::percentage(long absoluteVolume)
147
int relativeVolume = 0;
148
if ( _maxVolume == 0 )
151
if ( absoluteVolume > _maxVolume )
152
relativeVolume = 100;
153
else if ( absoluteVolume < _minVolume )
154
relativeVolume = -100;
155
else if ( absoluteVolume > 0 )
156
relativeVolume = ( 100*absoluteVolume) / _maxVolume;
157
else if ( absoluteVolume < 0 )
158
relativeVolume = ( 100*absoluteVolume) / _minVolume;
160
return relativeVolume;
165
long Volume::operator[](int id) {
166
return getVolume( (Volume::ChannelID) id );
169
long Volume::getVolume(ChannelID chid) {
172
if ( chid < 0 || chid > (Volume::CHIDMAX) ) {
173
// should throw exception here. I will return 0 instead
176
// check if channel is supported
177
int chmask = _channelMaskEnum[chid];
178
if ( (chmask & _chmask) != 0 ) {
179
// channel is supported
180
vol = _volumes[chid];
183
// should throw exception here. I will return 0 instead
190
long Volume::getAvgVolume(ChannelMask chmask) {
191
int avgVolumeCounter = 0;
192
long long sumOfActiveVolumes = 0;
193
for ( int i=0; i<= Volume::CHIDMAX; i++ ) {
194
if ( (_channelMaskEnum[i] & _chmask) & (int)chmask ) {
196
sumOfActiveVolumes += _volumes[i];
199
if (avgVolumeCounter != 0) {
200
sumOfActiveVolumes /= avgVolumeCounter;
205
return (long)sumOfActiveVolumes;
209
int Volume::count() {
211
for ( int i=0; i<= Volume::CHIDMAX; i++ ) {
212
if ( _channelMaskEnum[i] & _chmask ) {
220
* returns a "sane" volume level. This means, it is a volume level inside the
223
long Volume::volrange( int vol )
225
if ( vol < _minVolume ) {
228
else if ( vol < _maxVolume ) {
237
std::ostream& operator<<(std::ostream& os, const Volume& vol) {
239
for ( int i=0; i<= Volume::CHIDMAX; i++ ) {
243
if ( Volume::_channelMaskEnum[i] & vol._chmask ) {
244
// supported channel: Print Volume
245
os << vol._volumes[i];
248
// unsupported channel: Print "x"
254
os << " [" << vol._minVolume << "-" << vol._maxVolume;
255
if ( vol._muted ) { os << " : muted ]"; } else { os << " : playing ]"; }
260
kdbgstream& operator<<(kdbgstream &os, const Volume& vol) {
262
for ( int i=0; i<= Volume::CHIDMAX; i++ ) {
266
if ( Volume::_channelMaskEnum[i] & vol._chmask ) {
267
// supported channel: Print Volume
268
os << vol._volumes[i];
271
// unsupported channel: Print "x"
277
os << " [" << vol._minVolume << "-" << vol._maxVolume;
278
if ( vol._muted ) { os << " : muted ]"; } else { os << " : playing ]"; }