3 // Copyright (C) 2004, Petr Hlavka
5 // SPDX-License-Identifier: GPL-2.0+
10 #include <alsa/asoundlib.h>
13 #define ROUND_POS(x) (long ((long (x) + 0.5 > (x)) ? (x) : (x) + 1))
16 // load only playback channels, don't care about common items
17 AItem::AItem(AMixer
*m
, snd_mixer_elem_t
*e
) {
21 name
= snd_mixer_selem_get_name(aElem
);
23 hPVolume
= snd_mixer_selem_has_playback_volume(aElem
);
24 hPSwitch
= snd_mixer_selem_has_playback_switch(aElem
);
27 snd_mixer_selem_get_playback_volume_range(aElem
, &minPVolume
, &maxPVolume
);
29 minPVolume
= maxPVolume
= 0;
31 for (int channel
= 0; channel
<= (int) SND_MIXER_SCHN_LAST
; channel
++) {
32 if (snd_mixer_selem_has_playback_channel(aElem
, (SNDCHID_T
) channel
)) {
33 AChannel
*ch
= new AChannel(this, (SNDCHID_T
) channel
);
34 pbChannels
.push_back(ch
);
41 for (unsigned int i
= 0; i
< pbChannels
.size(); i
++)
46 // set same volume for all channels in this item in percent
47 void AItem::setVolumePerc(unsigned int percent
) {
51 snd_mixer_selem_set_playback_volume_all(aElem
, (long) ROUND_POS((minPVolume
+ (maxPVolume
- minPVolume
) * percent
/ 100.0)));
55 // get max channel volume of all channels in this item in percent
56 unsigned int AItem::getVolumePerc() {
57 long max_vol
= 0, act_vol
;
59 // find the max volume
60 for (unsigned int i
= 0; i
< pbChannels
.size(); i
++)
61 if ((act_vol
= pbChannels
[i
]->getVolume()) > max_vol
)
64 // convert it into percent
65 if (minPVolume
!= maxPVolume
)
66 return ((unsigned int) ROUND_POS((max_vol
- minPVolume
) * 100.0 / (maxPVolume
- minPVolume
)));
72 // in this app side of view, item is muted if all channels is muted
73 bool AItem::isMuted() {
74 for (unsigned int i
= 0; i
< pbChannels
.size(); i
++)
75 if (!pbChannels
[i
]->isMuted())
84 snd_mixer_selem_set_playback_switch_all(aElem
, false);
88 // unmute all channels
89 void AItem::unmute() {
90 snd_mixer_selem_set_playback_switch_all(aElem
, true);