summaryrefslogtreecommitdiff
path: root/src/model/alsadevice.cpp
blob: 4a03e7f640759cb03aa8b59f2fc1bf3c77a9838f (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
#include "alsadevice.h"
#include <algorithm>
#include <cmath>

AlsaDevice::AlsaDevice( Category cat, int cardIndex, snd_mixer_elem_t *elem,
                         const TQString &cardName, TQObject *parent )
    : TQObject(parent), m_category(cat), m_type(Slider), m_cardIndex(cardIndex),
      m_elem(elem), m_cardName(cardName),
      m_stereo(false), m_hasMute(false), m_volMin(0), m_volMax(0),
      m_volume(0), m_muted(false), m_pan(0), m_enumId(0)
{
    m_name = TQString::fromUtf8( snd_mixer_selem_get_name(elem) );

    if ( snd_mixer_selem_is_enumerated(elem) ) {
        m_type = Enum;
        int n = snd_mixer_selem_get_enum_items(elem);
        for ( int i = 0; i < n; ++i ) {
            char buf[128];
            if ( snd_mixer_selem_get_enum_item_name(elem, i, sizeof(buf), buf) >= 0 )
                m_enumValues.append( TQString::fromUtf8(buf) );
        }
    } else if ( snd_mixer_selem_has_playback_volume(elem) ||
                snd_mixer_selem_has_capture_volume(elem) ) {
        m_type = Slider;
        if ( cat == Output ) {
            m_stereo  = snd_mixer_selem_has_playback_channel(elem, SND_MIXER_SCHN_FRONT_RIGHT);
            m_hasMute = snd_mixer_selem_has_playback_switch(elem);
            snd_mixer_selem_get_playback_volume_range(elem, &m_volMin, &m_volMax);
        } else {
            m_stereo  = snd_mixer_selem_has_capture_channel(elem, SND_MIXER_SCHN_FRONT_RIGHT);
            m_hasMute = snd_mixer_selem_has_capture_switch(elem);
            snd_mixer_selem_get_capture_volume_range(elem, &m_volMin, &m_volMax);
        }
    } else {
        m_type    = Switch;
        m_hasMute = true;
    }

    snd_mixer_elem_set_callback(elem, elemCallback);
    snd_mixer_elem_set_callback_private(elem, this);

    syncFromAlsa();
}

int AlsaDevice::elemCallback( snd_mixer_elem_t *elem, unsigned int /*mask*/ )
{
    AlsaDevice *self = static_cast<AlsaDevice*>( snd_mixer_elem_get_callback_private(elem) );
    if ( self ) self->syncFromAlsa();
    return 0;
}

void AlsaDevice::syncFromAlsa()
{
    if ( m_type == Enum ) {
        unsigned int idx = 0;
        snd_mixer_selem_get_enum_item(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &idx);
        int newId = (int)idx;
        if ( newId != m_enumId ) { m_enumId = newId; emit enumChanged(m_enumId); }
        return;
    }

    if ( m_type == Switch ) {
        int sw = 1;
        if ( m_category == Output && snd_mixer_selem_has_playback_switch(m_elem) )
            snd_mixer_selem_get_playback_switch(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &sw);
        else if ( m_category == Input && snd_mixer_selem_has_capture_switch(m_elem) )
            snd_mixer_selem_get_capture_switch(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &sw);
        else if ( snd_mixer_selem_has_playback_switch(m_elem) )
            snd_mixer_selem_get_playback_switch(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &sw);
        bool newMute = (sw == 0);
        if ( newMute != m_muted ) { m_muted = newMute; emit muteChanged(m_muted); }
        return;
    }

    // Slider
    long range = m_volMax - m_volMin;
    if ( range <= 0 ) return;

    long l = m_volMin, r = m_volMin;
    int  sw = 1;

    if ( m_category == Output ) {
        snd_mixer_selem_get_playback_volume(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &l);
        if ( m_stereo )
            snd_mixer_selem_get_playback_volume(m_elem, SND_MIXER_SCHN_FRONT_RIGHT, &r);
        else
            r = l;
        if ( m_hasMute )
            snd_mixer_selem_get_playback_switch(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &sw);
    } else {
        snd_mixer_selem_get_capture_volume(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &l);
        if ( m_stereo )
            snd_mixer_selem_get_capture_volume(m_elem, SND_MIXER_SCHN_FRONT_RIGHT, &r);
        else
            r = l;
        if ( m_hasMute )
            snd_mixer_selem_get_capture_switch(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &sw);
    }

    long lv = l - m_volMin, rv = r - m_volMin;
    long mx = std::max(lv, rv);

    int  newVol  = (int)std::lround( (double)mx / range * 100.0 );
    bool newMute = (sw == 0);
    int  newPan  = 0;
    if ( m_stereo && mx > 0 ) {
        if ( lv >= rv )
            newPan = (int)std::lround( -50.0 * (1.0 - (double)rv / lv) );
        else
            newPan = (int)std::lround(  50.0 * (1.0 - (double)lv / rv) );
    }

    if ( newVol  != m_volume ) { m_volume = newVol;  emit volumeChanged(m_volume); }
    if ( newMute != m_muted  ) { m_muted  = newMute; emit muteChanged(m_muted); }
    if ( newPan  != m_pan    ) { m_pan    = newPan;  emit panChanged(m_pan); }
}

long AlsaDevice::toAlsaVol( int pct ) const
{
    if ( pct <= 0 )   return m_volMin;
    if ( pct >= 100 ) return m_volMax;
    return m_volMin + (long)std::lround( (double)pct / 100.0 * (m_volMax - m_volMin) );
}

void AlsaDevice::pushVolume()
{
    long vol = toAlsaVol( m_volume );
    double lf = m_pan >= 0 ? (50.0 - m_pan) / 50.0 : 1.0;
    double rf = m_pan <= 0 ? (50.0 + m_pan) / 50.0 : 1.0;
    long lv = (long)(vol * lf + 0.5);
    long rv = (long)(vol * rf + 0.5);

    if ( m_category == Output ) {
        snd_mixer_selem_set_playback_volume(m_elem, SND_MIXER_SCHN_FRONT_LEFT, lv);
        if ( m_stereo )
            snd_mixer_selem_set_playback_volume(m_elem, SND_MIXER_SCHN_FRONT_RIGHT, rv);
    } else {
        snd_mixer_selem_set_capture_volume(m_elem, SND_MIXER_SCHN_FRONT_LEFT, lv);
        if ( m_stereo )
            snd_mixer_selem_set_capture_volume(m_elem, SND_MIXER_SCHN_FRONT_RIGHT, rv);
    }
}

void AlsaDevice::setVolume( int v )
{
    v = v < 0 ? 0 : v > 100 ? 100 : v;
    m_volume = v;
    emit volumeChanged(v);
    pushVolume();
}

void AlsaDevice::setPan( int p )
{
    p = p < -50 ? -50 : p > 50 ? 50 : p;
    m_pan = p;
    emit panChanged(p);
    pushVolume();
}

void AlsaDevice::setMuted( bool m )
{
    m_muted = m;
    int sw = m ? 0 : 1;
    if ( m_type == Switch || m_type == Slider ) {
        if ( snd_mixer_selem_has_playback_switch(m_elem) )
            snd_mixer_selem_set_playback_switch_all(m_elem, sw);
        if ( snd_mixer_selem_has_capture_switch(m_elem) )
            snd_mixer_selem_set_capture_switch_all(m_elem, sw);
    }
    emit muteChanged(m);
}

void AlsaDevice::setEnumId( int id )
{
    if ( m_type != Enum ) return;
    if ( id < 0 || id >= (int)m_enumValues.count() ) return;
    m_enumId = id;
    emit enumChanged(id);
    for ( int ch = 0; ch <= SND_MIXER_SCHN_LAST; ++ch ) {
        snd_mixer_selem_channel_id_t chId = (snd_mixer_selem_channel_id_t)ch;
        if ( snd_mixer_selem_has_playback_channel(m_elem, chId) ||
             snd_mixer_selem_has_capture_channel(m_elem, chId) )
            snd_mixer_selem_set_enum_item(m_elem, chId, (unsigned int)id);
    }
}

#include "alsadevice.moc"