~ubuntu-branches/ubuntu/natty/kdemultimedia/natty-proposed

« back to all changes in this revision

Viewing changes to kmix/volume.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Debian Qt/KDE Maintainers
  • Date: 2011-05-26 02:41:36 UTC
  • mfrom: (0.2.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 108.
  • Revision ID: james.westby@ubuntu.com-20110526024136-jjwsigfy402jhupm
Tags: upstream-4.6.3
ImportĀ upstreamĀ versionĀ 4.6.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * KMix -- KDE's full featured mini mixer
3
 
 *
4
 
 *
5
 
 * Copyright (C) 1996-2004 Christian Esken <esken@kde.org>
6
 
 *
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.
11
 
 *
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.
16
 
 *
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.
20
 
 */
21
 
 
22
 
// for operator<<()
23
 
#include <iostream>
24
 
 
25
 
#include <kdebug.h>
26
 
 
27
 
#include "volume.h"
28
 
 
29
 
 
30
 
int Volume::_channelMaskEnum[9] =
31
 
    { MLEFT, MRIGHT, MCENTER,
32
 
      MWOOFER,
33
 
      MSURROUNDLEFT, MSURROUNDRIGHT,
34
 
      MREARSIDELEFT, MREARSIDERIGHT,
35
 
      MREARCENTER
36
 
    };
37
 
 
38
 
Volume::Volume()
39
 
{
40
 
    init( Volume::MNONE, 0, 0, false, false);
41
 
}
42
 
 
43
 
Volume::Volume( ChannelMask chmask, long maxVolume, long minVolume, bool hasSwitch, bool isCapture  )
44
 
{
45
 
    init(chmask, maxVolume, minVolume, hasSwitch, isCapture );
46
 
}
47
 
 
48
 
 
49
 
// copy constructor
50
 
Volume::Volume( const Volume &v )
51
 
{
52
 
    _chmask          = v._chmask;
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";
60
 
}
61
 
 
62
 
void Volume::init( ChannelMask chmask, long maxVolume, long minVolume, bool hasSwitch, bool isCapture )
63
 
{
64
 
    for ( int i=0; i<= Volume::CHIDMAX; i++ ) {
65
 
        _volumes[i] = 0;
66
 
    }
67
 
    _chmask          = chmask;
68
 
    _maxVolume       = maxVolume;
69
 
    _minVolume       = minVolume;
70
 
    _hasSwitch       = hasSwitch;
71
 
    _isCapture       = isCapture;
72
 
    _muted           = false;
73
 
    _switchActivated = false;
74
 
}
75
 
 
76
 
// @ compatibility
77
 
void Volume::setAllVolumes(long vol)
78
 
{
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);
83
 
        }
84
 
    }
85
 
}
86
 
 
87
 
void Volume::changeAllVolumes( long step )
88
 
{
89
 
        for (unsigned int i=Volume::CHIDMIN; i <= Volume::CHIDMAX; i++)
90
 
        {
91
 
            int volToChange = getVolume((Volume::ChannelID)i);
92
 
            volToChange += (int)step;
93
 
            setVolume((Volume::ChannelID)i, volToChange);
94
 
        }
95
 
}
96
 
 
97
 
 
98
 
// @ compatibility
99
 
void Volume::setVolume( ChannelID chid, long vol)
100
 
{
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);
106
 
    }
107
 
}
108
 
 
109
 
/**
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.
112
 
 */
113
 
void Volume::setVolume(const Volume &v)
114
 
{
115
 
     setVolume(v, (ChannelMask)(v._chmask&_chmask) );
116
 
}
117
 
 
118
 
/**
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.
122
 
 */
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]);
128
 
        }
129
 
        else {
130
 
            // Safety first! Lets play safe here and put sane values in
131
 
            _volumes[i] = 0;
132
 
        }
133
 
    }
134
 
 
135
 
}
136
 
 
137
 
long Volume::maxVolume() {
138
 
  return _maxVolume;
139
 
}
140
 
 
141
 
long Volume::minVolume() {
142
 
  return _minVolume;
143
 
}
144
 
 
145
 
int Volume::percentage(long absoluteVolume)
146
 
{
147
 
   int relativeVolume = 0;
148
 
   if ( _maxVolume == 0 )
149
 
      return 0;
150
 
 
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;
159
 
 
160
 
   return relativeVolume;
161
 
}
162
 
 
163
 
 
164
 
// @ compatibility
165
 
long Volume::operator[](int id) {
166
 
  return getVolume( (Volume::ChannelID) id );
167
 
}
168
 
 
169
 
long Volume::getVolume(ChannelID chid) {
170
 
  long vol = 0;
171
 
 
172
 
  if ( chid < 0 || chid > (Volume::CHIDMAX) ) {
173
 
    // should throw exception here. I will return 0 instead
174
 
  }
175
 
  else {
176
 
    // check if channel is supported
177
 
    int chmask = _channelMaskEnum[chid];
178
 
    if ( (chmask & _chmask) != 0 ) {
179
 
       // channel is supported
180
 
      vol = _volumes[chid];
181
 
    }
182
 
    else {
183
 
      // should throw exception here. I will return 0 instead
184
 
    }
185
 
  }
186
 
 
187
 
  return vol;
188
 
}
189
 
 
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 ) {
195
 
            avgVolumeCounter++;
196
 
            sumOfActiveVolumes += _volumes[i];
197
 
        }
198
 
    }
199
 
    if (avgVolumeCounter != 0) {
200
 
        sumOfActiveVolumes /= avgVolumeCounter;
201
 
    }
202
 
    else {
203
 
        // just return 0;
204
 
    }
205
 
    return (long)sumOfActiveVolumes;
206
 
}
207
 
 
208
 
 
209
 
int Volume::count() {
210
 
    int counter = 0;
211
 
    for ( int i=0; i<= Volume::CHIDMAX; i++ ) {
212
 
        if ( _channelMaskEnum[i] & _chmask ) {
213
 
            counter++;
214
 
        }
215
 
    }
216
 
    return counter;
217
 
}
218
 
 
219
 
/**
220
 
  * returns a "sane" volume level. This means, it is a volume level inside the
221
 
  * valid bounds
222
 
  */
223
 
long Volume::volrange( int vol )
224
 
{
225
 
    if ( vol < _minVolume ) {
226
 
         return _minVolume;
227
 
    }
228
 
    else if ( vol < _maxVolume ) {
229
 
         return vol;
230
 
   }
231
 
   else {
232
 
         return _maxVolume;
233
 
    }
234
 
}
235
 
 
236
 
 
237
 
std::ostream& operator<<(std::ostream& os, const Volume& vol) {
238
 
    os << "(";
239
 
    for ( int i=0; i<= Volume::CHIDMAX; i++ ) {
240
 
        if ( i != 0 ) {
241
 
            os << ",";
242
 
        }
243
 
        if ( Volume::_channelMaskEnum[i] & vol._chmask ) {
244
 
            // supported channel: Print Volume
245
 
            os << vol._volumes[i];
246
 
        }
247
 
        else {
248
 
            // unsupported channel: Print "x"
249
 
            os << "x";
250
 
        }
251
 
    } // all channels
252
 
    os << ")";
253
 
 
254
 
    os << " [" << vol._minVolume << "-" << vol._maxVolume;
255
 
    if ( vol._muted ) { os << " : muted ]"; } else { os << " : playing ]"; }
256
 
 
257
 
    return os;
258
 
}
259
 
 
260
 
kdbgstream& operator<<(kdbgstream &os, const Volume& vol) {
261
 
    os << "(";
262
 
    for ( int i=0; i<= Volume::CHIDMAX; i++ ) {
263
 
        if ( i != 0 ) {
264
 
            os << ",";
265
 
        }
266
 
        if ( Volume::_channelMaskEnum[i] & vol._chmask ) {
267
 
            // supported channel: Print Volume
268
 
            os << vol._volumes[i];
269
 
        }
270
 
        else {
271
 
            // unsupported channel: Print "x"
272
 
            os << "x";
273
 
        }
274
 
    } // all channels
275
 
    os << ")";
276
 
 
277
 
    os << " [" << vol._minVolume << "-" << vol._maxVolume;
278
 
    if ( vol._muted ) { os << " : muted ]"; } else { os << " : playing ]"; }
279
 
 
280
 
    return os;
281
 
}