Add AlsaMixer.app to repository
[dockapps.git] / AlsaMixer.app / AMixer / AItem.cc
blob7272268aa7d392900d8a70db46a74fe94e41cac8
1 // AItem.cc, Petr Hlavka, 2004
3 #include "AItem.h"
4 #include "AMixer.h"
6 #include <alsa/asoundlib.h>
7 #include <vector>
9 #define ROUND_POS(x) (long ((long (x) + 0.5 > (x)) ? (x) : (x) + 1))
12 // load only playback channels, don't care about common items
13 AItem::AItem(AMixer *m, snd_mixer_elem_t *e) {
14 mixer = m;
15 aElem = e;
17 name = snd_mixer_selem_get_name(aElem);
19 hPVolume = snd_mixer_selem_has_playback_volume(aElem);
20 hPSwitch = snd_mixer_selem_has_playback_switch(aElem);
22 if (hPVolume)
23 snd_mixer_selem_get_playback_volume_range(aElem, &minPVolume, &maxPVolume);
24 else
25 minPVolume = maxPVolume = 0;
27 for (int channel = 0; channel <= (int) SND_MIXER_SCHN_LAST; channel++) {
28 if (snd_mixer_selem_has_playback_channel(aElem, (SNDCHID_T) channel)) {
29 AChannel *ch = new AChannel(this, (SNDCHID_T) channel);
30 pbChannels.push_back(ch);
36 AItem::~AItem() {
37 for (unsigned int i = 0; i < pbChannels.size(); i++)
38 delete pbChannels[i];
42 // set same volume for all channels in this item in percent
43 void AItem::setVolumePerc(unsigned int percent) {
44 if (percent > 100)
45 percent = 100;
47 snd_mixer_selem_set_playback_volume_all(aElem, (long) ROUND_POS((minPVolume + (maxPVolume - minPVolume) * percent / 100.0)));
51 // get max channel volume of all channels in this item in percent
52 unsigned int AItem::getVolumePerc() {
53 long max_vol = 0, act_vol;
55 // find the max volume
56 for (unsigned int i = 0; i < pbChannels.size(); i++)
57 if ((act_vol = pbChannels[i]->getVolume()) > max_vol)
58 max_vol = act_vol;
60 // convert it into percent
61 if (minPVolume != maxPVolume)
62 return ((unsigned int) ROUND_POS((max_vol - minPVolume) * 100.0 / (maxPVolume - minPVolume)));
63 else
64 return (0);
68 // in this app side of view, item is muted if all channels is muted
69 bool AItem::isMuted() {
70 for (unsigned int i = 0; i < pbChannels.size(); i++)
71 if (!pbChannels[i]->isMuted())
72 return (false);
74 return (true);
78 // mute all channels
79 void AItem::mute() {
80 snd_mixer_selem_set_playback_switch_all(aElem, false);
84 // unmute all channels
85 void AItem::unmute() {
86 snd_mixer_selem_set_playback_switch_all(aElem, true);