kmixer: implement volume range getters

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-03-06 14:41:52 +02:00
parent 4fa2bacb8e
commit 83f3cdd418
2 changed files with 102 additions and 1 deletions

View file

@ -132,6 +132,11 @@ int KSoundChannel::playbackVolume() const
return m_backend->playbackVolume(this);
}
KVolumeRange KSoundChannel::playbackRange() const
{
return m_backend->playbackRange(this);
}
bool KSoundChannel::setPlaybackVolume(const int volume)
{
return m_backend->setPlaybackVolume(this, volume);
@ -142,6 +147,11 @@ int KSoundChannel::captureVolume() const
return m_backend->captureVolume(this);
}
KVolumeRange KSoundChannel::captureRange() const
{
return m_backend->captureRange(this);
}
bool KSoundChannel::setCaptureVolume(const int volume)
{
return m_backend->setCaptureVolume(this, volume);
@ -401,6 +411,45 @@ int KALSABackend::playbackVolume(const KSoundChannel *channel) const
return 0;
}
KVolumeRange KALSABackend::playbackRange(const KSoundChannel *channel) const
{
if (!channel->hasPlayback()) {
kDebug() << "Not playback channel";
return KVolumeRange();
}
snd_mixer_t* alsamixer = KALSABackend::mixerForCard(channel->cardID());
if (!alsamixer) {
return KVolumeRange();
}
snd_mixer_elem_t *alsaelement = snd_mixer_first_elem(alsamixer);
for (; alsaelement; alsaelement = snd_mixer_elem_next(alsaelement)) {
if (!snd_mixer_elem_empty(alsaelement)) {
const QString alsaid = QString::number(snd_mixer_selem_get_index(alsaelement));
const QString alsaname = QString::fromLocal8Bit(snd_mixer_selem_get_name(alsaelement));
if (alsaid == channel->id() && alsaname == channel->name()) {
kDebug() << "Device" << channel->id() << channel->name();
long alsaplaybackvolumemin = 0;
long alsaplaybackvolumemax = 0;
const int alsaresult = snd_mixer_selem_get_playback_volume_range(alsaelement, &alsaplaybackvolumemin, &alsaplaybackvolumemax);
if (alsaresult != 0) {
kWarning() << "Could not get playback channel volume range" << snd_strerror(alsaresult);
snd_mixer_close(alsamixer);
return KVolumeRange();
}
snd_mixer_close(alsamixer);
KVolumeRange volumerange = { int(alsaplaybackvolumemin), int(alsaplaybackvolumemax) };
return volumerange;
}
}
}
kWarning() << "Could not find playback channel" << channel->id() << channel->name();
snd_mixer_close(alsamixer);
return KVolumeRange();
}
bool KALSABackend::setPlaybackVolume(const KSoundChannel *channel, const int volume)
{
if (!channel->hasPlayback()) {
@ -486,6 +535,45 @@ int KALSABackend::captureVolume(const KSoundChannel *channel) const
return 0;
}
KVolumeRange KALSABackend::captureRange(const KSoundChannel *channel) const
{
if (!channel->hasCapture()) {
kDebug() << "Not capture channel";
return KVolumeRange();
}
snd_mixer_t* alsamixer = KALSABackend::mixerForCard(channel->cardID());
if (!alsamixer) {
return KVolumeRange();
}
snd_mixer_elem_t *alsaelement = snd_mixer_first_elem(alsamixer);
for (; alsaelement; alsaelement = snd_mixer_elem_next(alsaelement)) {
if (!snd_mixer_elem_empty(alsaelement)) {
const QString alsaid = QString::number(snd_mixer_selem_get_index(alsaelement));
const QString alsaname = QString::fromLocal8Bit(snd_mixer_selem_get_name(alsaelement));
if (alsaid == channel->id() && alsaname == channel->name()) {
kDebug() << "Device" << channel->id() << channel->name();
long alsacapturevolumemin = 0;
long alsacapturevolumemax = 0;
const int alsaresult = snd_mixer_selem_get_capture_volume_range(alsaelement, &alsacapturevolumemin, &alsacapturevolumemax);
if (alsaresult != 0) {
kWarning() << "Could not get capture channel volume range" << snd_strerror(alsaresult);
snd_mixer_close(alsamixer);
return KVolumeRange();
}
snd_mixer_close(alsamixer);
KVolumeRange volumerange = { int(alsacapturevolumemin), int(alsacapturevolumemax) };
return volumerange;
}
}
}
kWarning() << "Could not find capture channel" << channel->id() << channel->name();
snd_mixer_close(alsamixer);
return KVolumeRange();
}
bool KALSABackend::setCaptureVolume(const KSoundChannel *channel, const int volume)
{
if (!channel->hasCapture()) {
@ -656,6 +744,7 @@ bool KMixer::start(const QString &backend)
kchannel.setPlaybackVolume(10);
kchannel.setCaptureVolume(10);
qDebug() << "Channel volume is" << kchannel.playbackVolume() << kchannel.captureVolume();
qDebug() << "Channel volume range is" << kchannel.playbackRange().minvolume << kchannel.playbackRange().maxvolume;
}
}
#endif
@ -671,6 +760,7 @@ bool KMixer::start(const QString &backend)
kchannel.setPlaybackVolume(10);
kchannel.setCaptureVolume(10);
qDebug() << "Channel volume is" << kchannel.playbackVolume() << kchannel.captureVolume();
qDebug() << "Channel volume range is" << kchannel.playbackRange().minvolume << kchannel.playbackRange().maxvolume;
}
}
#endif

View file

@ -28,6 +28,11 @@
class KMixerBackend;
struct KVolumeRange {
int minvolume;
int maxvolume;
};
class KSoundChannel {
public:
enum KSoundChannelType {
@ -57,10 +62,12 @@ public:
bool hasPlayback() const;
bool hasCapture() const;
// TODO: range for widgets
int playbackVolume() const;
KVolumeRange playbackRange() const;
bool setPlaybackVolume(const int volume);
int captureVolume() const;
KVolumeRange captureRange() const;
bool setCaptureVolume(const int volume);
private:
@ -102,9 +109,11 @@ public:
virtual QList<KSoundCard> soundCards() = 0;
virtual int playbackVolume(const KSoundChannel *channel) const = 0;
virtual KVolumeRange playbackRange(const KSoundChannel *channel) const = 0;
virtual bool setPlaybackVolume(const KSoundChannel *channel, const int volume) = 0;
virtual int captureVolume(const KSoundChannel *channel) const = 0;
virtual KVolumeRange captureRange(const KSoundChannel *channel) const = 0;
virtual bool setCaptureVolume(const KSoundChannel *channel, const int volume) = 0;
virtual bool mute(const KSoundChannel *channel) const = 0;
@ -120,9 +129,11 @@ public:
QList<KSoundCard> soundCards() final;
int playbackVolume(const KSoundChannel *channel) const final;
KVolumeRange playbackRange(const KSoundChannel *channel) const final;
bool setPlaybackVolume(const KSoundChannel *channel, const int volume) final;
int captureVolume(const KSoundChannel *channel) const final;
KVolumeRange captureRange(const KSoundChannel *channel) const final;
bool setCaptureVolume(const KSoundChannel *channel, const int volume) final;
bool mute(const KSoundChannel *channel) const final;