summaryrefslogtreecommitdiffstats
path: root/kmix/mixer.cpp
blob: 778d6af9e2cdc51bb9d40bbc20102f769c1d4517 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/*
 *              KMix -- KDE's full featured mini mixer
 *
 *
 * Copyright (C) 1996-2004 Christian Esken - esken@kde.org
 *                    2002 Helio Chissini de Castro - helio@conectiva.com.br
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <tqtimer.h>

#include <tdelocale.h>
#include <tdeconfig.h>
#include <tdeglobal.h>
#include <kdebug.h>
#include <dcopobject.h>

#include "mixer.h"
#include "mixer_backend.h"
#include "kmix-platforms.cpp"
#include "volume.h"

//#define MIXER_MASTER_DEBUG

#ifdef MIXER_MASTER_DEBUG
#warning MIXER_MASTER_DEBUG is enabled. DO NOT SHIP KMIX LIKE THIS !!!
#endif

/**
 * Some general design hints. Hierachy is Mixer->MixDevice->Volume
 */

// !! Warning: Don't commit with "KMIX_DCOP_OBJID_TEST" #define'd (cesken)
#undef KMIX_DCOP_OBJID_TEST
int Mixer::_dcopID = 0;

TQPtrList<Mixer> Mixer::s_mixers;
TQString Mixer::_masterCard;
TQString Mixer::_masterCardDevice;

int Mixer::numDrivers()
{
    MixerFactory *factory = g_mixerFactories;
    int num = 0;
    while( factory->getMixer!=0 )
    {
        num++;
        factory++;
    }

    return num;
}

/*
 * Returns a reference of the current mixer list.
 */
TQPtrList<Mixer>& Mixer::mixers()
{
  return s_mixers;
}

bool Mixer::isValid(int driver, int device)
{
  getMixerFunc *mf = g_mixerFactories[driver].getMixer;
  if (mf)
  {
    Mixer_Backend *mb = mf(device);
    if (mb)
    {
      bool ret = mb->isValid();
      delete mb;
      return ret;
    }
  }
  return false;
}

Mixer::Mixer( int driver, int device ) : DCOPObject( "Mixer" )
{
  _pollingTimer = 0;

   _mixerBackend = 0;
   getMixerFunc *f = g_mixerFactories[driver].getMixer;
   if (f) {
     _mixerBackend = f(device );
   }

  readSetFromHWforceUpdate();  // enforce an initial update on first readSetFromHW()

  m_balance = 0;
  m_profiles.setAutoDelete( true );

  _pollingTimer = new TQTimer(); // will be started on open() and stopped on close()
  connect( _pollingTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(readSetFromHW()));

  TQCString objid;
#ifndef KMIX_DCOP_OBJID_TEST
  objid.setNum(_mixerBackend->m_devnum);
#else
// should use a nice name like the Unique-Card-ID instead !!
  objid.setNum(Mixer::_dcopID);
  Mixer::_dcopID ++;
#endif
  objid.prepend("Mixer");
  DCOPObject::setObjId( objid );
}

Mixer::~Mixer() {
   // Close the mixer. This might also free memory, depending on the called backend method
   close();
   delete _pollingTimer;
   if (_mixerBackend)
   {
     delete _mixerBackend;
   }
}

void Mixer::volumeSave( TDEConfig *config )
{
    //    kdDebug(67100) << "Mixer::volumeSave()" << endl;
    readSetFromHW();
    TQString grp("Mixer");
    grp.append(mixerName());
    _mixerBackend->m_mixDevices.write( config, grp );
}

void Mixer::volumeLoad( TDEConfig *config )
{
   TQString grp("Mixer");
   grp.append(mixerName());
   if ( ! config->hasGroup(grp) ) {
      // no such group. Volumes (of this mixer) were never saved beforehand.
      // Thus don't restore anything (also see Bug #69320 for understanding the real reason)
      return; // make sure to bail out immediately
   }

   // else restore the volumes
   _mixerBackend->m_mixDevices.read( config, grp );

   // set new settings
   TQPtrListIterator<MixDevice> it( _mixerBackend->m_mixDevices );
   for(MixDevice *md=it.toFirst(); md!=0; md=++it )
   {
       //       kdDebug(67100) << "Mixer::volumeLoad() writeVolumeToHW(" << md->num() << ", "<< md->getVolume() << ")" << endl;
       // !! @todo Restore record source
       //setRecordSource( md->num(), md->isRecSource() );
       _mixerBackend->setRecsrcHW( md->num(), md->isRecSource() );
       _mixerBackend->writeVolumeToHW( md->num(), md->getVolume() );
       if ( md->isEnum() ) _mixerBackend->setEnumIdHW( md->num(), md->enumId() );
   }
}


/**
 * Opens the mixer.
 * Also, starts the polling timer, for polling the Volumes from the Mixer.
 *
 * @return 0, if OK. An Mixer::ERR_ error code otherwise
 */
int Mixer::open()
{
      int err = _mixerBackend->open();
      // A better ID is now calculated in mixertoolbox.cpp, and set via setID(),
      // but we want a somhow usable fallback just in case.
      _id = mixerName();

      if( err == ERR_INCOMPATIBLESET )   // !!! When does this happen ?!?
        {
          // Clear the mixdevices list
	  _mixerBackend->m_mixDevices.clear();
          // try again with fresh set
	  err = _mixerBackend->open();
        }

      MixDevice* recommendedMaster = _mixerBackend->recommendedMaster();
      if ( recommendedMaster != 0 ) {
         setMasterDevice(recommendedMaster->getPK() );
      }
      else {
         kdError(67100) << "Mixer::open() no master detected." << endl;
         TQString noMaster = "---no-master-detected---";
         setMasterDevice(noMaster); // no master
      }
	/*
   // --------- Copy the hardware values to the MixDevice -------------------
      MixSet &mset = _mixerBackend->m_mixDevices;
      if( !mset.isEmpty() ) {
       // Copy the initial mix set
       //       kdDebug(67100) << "Mixer::setupMixer() copy Set" << endl;
	MixDevice* md;
	for( md = mset.first(); md != 0; md = mset.next() )
	{
	  MixDevice* mdCopy = _mixerBackend->m_mixDevices.first();
	  while( mdCopy!=0 && mdCopy->num() != md->num() ) {
	    mdCopy = _mixerBackend->m_mixDevices.next();
	  }
	  if ( mdCopy != 0 ) {
	       // The "mdCopy != 0" was not checked before, but its safer to do so
	    setRecordSource( md->num(), md->isRecSource() );
	    Volume &vol = mdCopy->getVolume();
	    vol.setVolume( md->getVolume() );
	    mdCopy->setMuted( md->isMuted() );

	       // !! might need writeVolumeToHW( mdCopy->num(), mdCopy->getVolume() );
	  }
	}
      }
	*/
      if ( _mixerBackend->needsPolling() ) {
          _pollingTimer->start(50);
      }
      else {
          _mixerBackend->prepareSignalling(this);
          // poll once to give the GUI a chance to rebuild it's info
          TQTimer::singleShot( 50, this, TQT_SLOT( readSetFromHW() ) );
      }
      return err;
}


/**
 * Closes the mixer.
 * Also, stops the polling timer.
 *
 * @return 0 (always)
 */
int Mixer::close()
{
  _pollingTimer->stop();
  return _mixerBackend->close();
}


/* ------- WRAPPER METHODS. START ------------------------------ */
unsigned int Mixer::size() const
{
  return _mixerBackend->m_mixDevices.count();
}

MixDevice* Mixer::operator[](int num)
{
  MixDevice* md =  _mixerBackend->m_mixDevices.at( num );
  Q_ASSERT( md );
  return md;
}

MixSet Mixer::getMixSet()
{
  return _mixerBackend->m_mixDevices;
}

bool Mixer::isValid() {
  return _mixerBackend->isValid();
}

bool Mixer::isOpen() const {
    if ( _mixerBackend == 0 )
        return false;
    else
        return _mixerBackend->isOpen();
}

/* ------- WRAPPER METHODS. END -------------------------------- */

/**
 * After calling this, readSetFromHW() will do a complete update. This will
 * trigger emitting the appropriate signals like newVolumeLevels().
 *
 * This method is useful, if you need to get a "refresh signal" - used at:
 * 1) Start of KMix - so that we can be sure an initial signal is emitted
 * 2) When reconstructing any MixerWidget (e.g. DockIcon after applying preferences)
 */
void Mixer::readSetFromHWforceUpdate() const {
   _readSetFromHWforceUpdate = true;
}

/**
   You can call this to retrieve the freshest information from the mixer HW.
   This method is also called regulary by the mixer timer.
*/
void Mixer::readSetFromHW()
{
  if ( ! _mixerBackend->isOpen() ) {
     // bail out immediately, if the mixer is not open.
     // This can happen currently only, if the user executes the DCOP close() call.
     return;
  }
  bool updated = _mixerBackend->prepareUpdateFromHW();
  if ( (! updated) && (! _readSetFromHWforceUpdate) ) {
    // Some drivers (ALSA) are smart. We don't need to run the following
    // time-consuming update loop if there was no change
    return;
  }
  _readSetFromHWforceUpdate = false;
  MixDevice* md;
  for( md = _mixerBackend->m_mixDevices.first(); md != 0; md = _mixerBackend->m_mixDevices.next() )
    {
      Volume& vol = md->getVolume();
      _mixerBackend->readVolumeFromHW( md->num(), vol );
      md->setRecSource( _mixerBackend->isRecsrcHW( md->num() ) );
      if (md->isEnum() ) {
	md->setEnumId( _mixerBackend->enumIdHW(md->num()) );
      }
    }
  // Trivial implementation. Without looking at the devices
  //  kdDebug(67100) << "Mixer::readSetFromHW(): emit newVolumeLevels()" << endl;
  emit newVolumeLevels();
  emit newRecsrc(); // cheap, but works
}


void Mixer::setBalance(int balance)
{
  // !! BAD, because balance only works on the master device. If you have not Master, the slider is a NOP
  if( balance == m_balance ) {
      // balance unchanged => return
      return;
  }

  m_balance = balance;

  MixDevice* master = masterDevice();
  if ( master == 0 ) {
      // no master device available => return
      return;
  }

  Volume& vol = master->getVolume();
  _mixerBackend->readVolumeFromHW( master->num(), vol );

  int left = vol[ Volume::LEFT ];
  int right = vol[ Volume::RIGHT ];
  int refvol = left > right ? left : right;
  if( balance < 0 ) // balance left
    {
      vol.setVolume( Volume::LEFT,  refvol);
      vol.setVolume( Volume::RIGHT, (balance * refvol) / 100 + refvol );
    }
  else
    {
      vol.setVolume( Volume::LEFT, -(balance * refvol) / 100 + refvol );
      vol.setVolume( Volume::RIGHT,  refvol);
    }

    _mixerBackend->writeVolumeToHW( master->num(), vol );

  emit newBalance( vol );
}

TQString Mixer::mixerName()
{
  return _mixerBackend->m_mixerName;
}

int Mixer::devnum()
{
  return _mixerBackend->m_devnum;
}

TQString Mixer::driverName( int driver )
{
    getDriverNameFunc *f = g_mixerFactories[driver].getDriverName;
    if( f!=0 )
        return f();
    else
        return "unknown";
}

void Mixer::setID(TQString& ref_id)
{
  _id = ref_id;
}


TQString& Mixer::id()
{
  return _id;
}

void Mixer::setMasterCard(const TQString& ref_id)
{
  // The value is taken over without checking on existance. This allows the User to define
  // a MasterCard that is not always available (e.g. it is an USB hotplugging device).
  // Also you can set the master at any time you like, e.g. after reading the KMix configuration file
  // and before actually constructing the Mixer instances (hint: this mehtod is static!).
  _masterCard = ref_id;
}

Mixer* Mixer::masterCard()
{
  kdDebug(67100) << "Mixer::masterCard() searching for id=" << _masterCard << "\n";
  for (Mixer *mixer = Mixer::mixers().first(); mixer; mixer = Mixer::mixers().next())
  {
     if ( mixer->id() == _masterCard ) {
#ifdef MIXER_MASTER_DEBUG
        kdDebug(67100) << "Mixer::masterCard() found id=" << mixer->id() << "\n";
#endif
        return mixer;
     }
  }
#ifdef MIXER_MASTER_DEBUG
  kdDebug(67100) << "Mixer::masterCard() found no Mixer* mixer \n";
#endif
  return NULL;
}

void Mixer::setMasterCardDevice(const TQString &ref_id)
{
  // The value is taken over without checking on existance. This allows the User to define
  // a MasterCard that is not always available (e.g. it is an USB hotplugging device).
  // Also you can set the master at any time you like, e.g. after reading the KMix configuration file
  // and before actually constructing the Mixer instances (hint: this mehtod is static!).
  _masterCardDevice = ref_id;
#ifdef MIXER_MASTER_DEBUG
   kdDebug(67100) << "Mixer::setMasterCardDevice(\"" << ref_id << "\")\n";
#endif
}

MixDevice* Mixer::masterCardDevice()
{
  MixDevice* md = 0;
  Mixer *mixer = masterCard();
  if ( mixer != 0 ) {
     for( md = mixer->_mixerBackend->m_mixDevices.first(); md != 0; md = mixer->_mixerBackend->m_mixDevices.next() ) {


       if ( md->getPK() == _masterCardDevice )
        {
#ifdef MIXER_MASTER_DEBUG
         kdDebug(67100) << "Mixer::masterCardDevice() getPK()="
         << md->getPK() << " , _masterCardDevice="
         << _masterCardDevice << "\n";
#endif
          break;
        }
     }
  }

#ifdef MIXER_MASTER_DEBUG
  if ( md == 0) kdDebug(67100) << "Mixer::masterCardDevice() found no MixDevice* md" "\n";
#endif

  return md;
}




/**
   Used internally by the Mixer class and as DCOP method
*/
void Mixer::setRecordSource( int devnum, bool on )
{
  if( !_mixerBackend->setRecsrcHW( devnum, on ) ) // others have to be updated
  {
    for( MixDevice* md = _mixerBackend->m_mixDevices.first(); md != 0; md = _mixerBackend->m_mixDevices.next() ) {
	  bool isRecsrc =  _mixerBackend->isRecsrcHW( md->num() );
//		kdDebug(67100) << "Mixer::setRecordSource(): isRecsrcHW(" <<  md->num() << ") =" <<  isRecsrc << endl;
		md->setRecSource( isRecsrc );
	}
	// emitting is done after read
	//emit newRecsrc(); // like "emit newVolumeLevels()", but for record source
  }
  else {
	// just the actual mixdevice
    for( MixDevice* md = _mixerBackend->m_mixDevices.first(); md != 0; md = _mixerBackend->m_mixDevices.next() ) {
		  if( md->num() == devnum ) {
		    bool isRecsrc =  _mixerBackend->isRecsrcHW( md->num() );
			md->setRecSource( isRecsrc );
		  }
	}
	// emitting is done after read
	//emit newRecsrc(); // like "emit newVolumeLevels()", but for record source
  }

}


MixDevice* Mixer::masterDevice()
{
  return find( _masterDevicePK );
}

void Mixer::setMasterDevice(TQString &devPK)
{
    _masterDevicePK = devPK;
}


MixDevice* Mixer::find(TQString& devPK)
{
    MixDevice* md = 0;
    for( md = _mixerBackend->m_mixDevices.first(); md != 0; md = _mixerBackend->m_mixDevices.next() ) {
        if( devPK == md->getPK() ) {
           break;
        }
    }
    return md;
}


MixDevice *Mixer::mixDeviceByType( int deviceidx )
{
  unsigned int i=0;
  while (i<size() && (*this)[i]->num()!=deviceidx) i++;
  if (i==size()) return 0;

  return (*this)[i];
}

// @dcop
// Used also by the setMasterVolume() method.
void Mixer::setVolume( int deviceidx, int percentage )
{
  MixDevice *mixdev= mixDeviceByType( deviceidx );
  if (!mixdev) return;

  Volume vol=mixdev->getVolume();
  // @todo The next call doesn't handle negative volumes correctly.
  vol.setAllVolumes( (percentage*vol.maxVolume())/100 );
  _mixerBackend->writeVolumeToHW(deviceidx, vol);
  // Make sure volume reading is synced
  readSetFromHWforceUpdate();
  TQTimer::singleShot(50, this, TQT_SLOT(readSetFromHW()));
}

/**
   Call this if you have a *reference* to a Volume object and have modified that locally.
   Pass the MixDevice associated to that Volume to this method for writing back
   the changed value to the mixer.
   Hint: Why do we do it this way?
   - It is fast               (no copying of Volume objects required)
   - It is easy to understand ( read - modify - commit )
*/
void Mixer::commitVolumeChange( MixDevice* md ) {
  _mixerBackend->writeVolumeToHW(md->num(), md->getVolume() );
  _mixerBackend->setEnumIdHW(md->num(), md->enumId() );

  // Muting/unmuting PulseAudio directly does not send back any notification to the mixer
  // so we make sure we always update the tray icon after each operation.
  readSetFromHWforceUpdate();
  TQTimer::singleShot(50, this, TQT_SLOT(readSetFromHW()));
}

// @dcop only
void Mixer::setMasterVolume( int percentage )
{
  MixDevice *master = masterDevice();
  if (master != 0 ) {
    setVolume( master->num(), percentage );
  }
}

// @dcop
int Mixer::volume( int deviceidx )
{
  MixDevice *mixdev= mixDeviceByType( deviceidx );
  if (!mixdev) return 0;

  Volume vol=mixdev->getVolume();
  // @todo This will not work, if minVolume != 0      !!! 
  //       e.g.: minVolume=5 or minVolume=-10
  // The solution is to check two cases:
  //     volume < 0 => use minVolume for volumeRange
  //     volume > 0 => use maxVolume for volumeRange
  //     If chosen volumeRange==0 => return 0
  // As this is potentially used often (Sliders, ...), it
  // should beimplemented in the Volume class. 

  // For now we go with "maxVolume()", like in the rest of KMix.
  long volumeRange = vol.maxVolume(); // -vol.minVolume() ;
  if ( volumeRange == 0 )
  {
    return 0;
  }
  else
  {
     // Make sure to round correctly, otherwise the volume level will always be 1% too low
     // and increments of 1% of top of the value read will result in no change to the actual level
     return ((100.0 * vol.getVolume(Volume::LEFT) + volumeRange / 2) / volumeRange);
  }
}

// @dcop , especially for use in KMilo
void Mixer::setAbsoluteVolume( int deviceidx, long absoluteVolume ) {
  MixDevice *mixdev= mixDeviceByType( deviceidx );
  if (!mixdev) return;

  Volume vol=mixdev->getVolume();
  vol.setAllVolumes( absoluteVolume );
  _mixerBackend->writeVolumeToHW(deviceidx, vol);
  // Make sure volume reading is synced
  readSetFromHWforceUpdate();
  TQTimer::singleShot(50, this, TQT_SLOT(readSetFromHW()));
}

// @dcop , especially for use in KMilo
long Mixer::absoluteVolume(int deviceidx)
{
   MixDevice *mixdev= mixDeviceByType(deviceidx);
   if (!mixdev) return 0;

   Volume vol=mixdev->getVolume();
   long avgVolume=vol.getAvgVolume((Volume::ChannelMask)(Volume::MLEFT | Volume::MRIGHT));
   return avgVolume;
}

// @dcop , especially for use in KMilo
long Mixer::absoluteVolumeMax(int deviceidx)
{
   MixDevice *mixdev= mixDeviceByType(deviceidx);
   if (!mixdev) return 0;

   Volume vol=mixdev->getVolume();
   long maxVolume=vol.maxVolume();
   return maxVolume;
}

// @dcop , especially for use in KMilo
long Mixer::absoluteVolumeMin(int deviceidx)
{
   MixDevice *mixdev= mixDeviceByType(deviceidx);
   if (!mixdev) return 0;

   Volume vol=mixdev->getVolume();
   long minVolume=vol.minVolume();
   return minVolume;
}

// @dcop
int Mixer::masterVolume()
{
  int vol = 0;
  MixDevice *master = masterDevice();
  if (master != 0 ) {
    vol = volume( master->num() );
  }
  return vol;
}

// @dcop
void Mixer::increaseVolume(int deviceidx, int percentage)
{
  MixDevice *mixdev= mixDeviceByType(deviceidx);
  if (mixdev && percentage > 0)
  {
    Volume vol = mixdev->getVolume();
    long maxVol = vol.maxVolume();
    if (maxVol > 0)
    {
      for (int i = 0; i < vol.count(); i++)
      {
        double perc = 100.0 * vol[i] / maxVol;
        perc += percentage;
        if (perc > 100.0)
        {
          perc = 100.0;
        }
        long newVal = (long)(perc * maxVol / 100.0);
        mixdev->setVolume(i, newVal);
      }
      commitVolumeChange(mixdev);
    }
  }
}

// @dcop
void Mixer::decreaseVolume(int deviceidx, int percentage)
{
  MixDevice *mixdev= mixDeviceByType(deviceidx);
  if (mixdev && percentage > 0)
  {
    Volume vol = mixdev->getVolume();
    long maxVol = vol.maxVolume();
    if (maxVol > 0)
    {
      for (int i = 0; i < vol.count(); i++)
      {
        double perc = 100.0 * vol[i] / maxVol;
        perc -= percentage;
        if (perc < 0.0)
        {
          perc = 0.0;
        }
        long newVal = (long)(perc * maxVol / 100.0);
        mixdev->setVolume(i, newVal);
      }
      commitVolumeChange(mixdev);
    }
  }
}

// @dcop
void Mixer::setMute(int deviceidx, bool on)
{
  MixDevice *mixdev= mixDeviceByType(deviceidx);
  if (!mixdev)
  {
    return;
  }

  mixdev->setMuted(on);
  commitVolumeChange(mixdev);
}

// @dcop only
void Mixer::setMasterMute(bool on)
{
  MixDevice *md = masterDevice();
  if (md)
  {
    setMute(md->num(), on);
  }
}


// @dcop
void Mixer::toggleMute( int deviceidx )
{
  MixDevice *mixdev= mixDeviceByType( deviceidx );
  if (!mixdev) return;

  mixdev->setMuted(!mixdev->isMuted());
  commitVolumeChange(mixdev);
}

// @dcop only
void Mixer::toggleMasterMute()
{
  MixDevice *master = masterDevice();
  if (master != 0 ) {
    toggleMute( master->num() );
  }
}


// @dcop
bool Mixer::mute( int deviceidx )
{
  MixDevice *mixdev= mixDeviceByType( deviceidx );
  if (!mixdev) return true;

  return mixdev->isMuted();
}

// @dcop only
bool Mixer::masterMute()
{
  MixDevice *master = masterDevice();
  if (master != 0 ) {
    return mute( master->num() );
  }
  return true;
}

// @dcop only
int Mixer::masterDeviceIndex()
{
  return masterDevice()->num();
}

bool Mixer::isRecordSource( int deviceidx )
{
  MixDevice *mixdev= mixDeviceByType( deviceidx );
  if (!mixdev) return false;

  return mixdev->isRecSource();
}

// @dcop
bool Mixer::isAvailableDevice( int deviceidx )
{
  return (mixDeviceByType(deviceidx) != NULL);
}

#include "mixer.moc"