summaryrefslogtreecommitdiffstats
path: root/app/mixer.cpp
blob: 8c2d2af0e8cd790c5fe1c362bbc908074b547cf4 (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
#include <QJsonArray>
#include <QJsonObject>
#include <QtDebug>
#include "mixer.h"

Mixer::Mixer(QObject* parent)
    : QObject(parent)
    , m_masterVolume{0}
    , m_pcmVolume{0}
    , m_microphoneVolume{0}
{
    connect(&m_client, SIGNAL(connected()), this, SLOT(onClientConnected()));
}

QVariantList Mixer::hals() const
{
    return m_hallist;
}

int Mixer::masterVolume() const
{
    return m_masterVolume;
}

int Mixer::pcmVolume() const
{
    return m_pcmVolume;
}

int Mixer::microphoneVolume() const
{
    return m_microphoneVolume;
}

void Mixer::setMasterVolume(int v)
{
    qDebug() << "Mixer::setMasterVolume(" << v << ")";
    if (v != m_masterVolume)
    {
        m_masterVolumePending = v;
        QJsonObject arg;
        arg["label"] = "Master_Playback_Volume";
        arg["val"] = static_cast<int>(v);
        m_setMasterVolume = m_client.call(m_activeHal, "ctlset", arg);
        connect(m_setMasterVolume.data(), SIGNAL(closed()), this, SLOT(onSetMasterVolume()));
    }
}

void Mixer::setPcmVolume(int v)
{
    qDebug() << "Mixer::setPcmVolume(" << v << ")";
    if (v != m_pcmVolume)
    {
        m_pcmVolumePending = v;
        QJsonObject arg;
        arg["label"] = "PCM_Playback_Volume";
        arg["val"] = static_cast<int>(v);
        m_setPcmVolume = m_client.call(m_activeHal, "ctlset", arg);
        connect(m_setPcmVolume.data(), SIGNAL(closed()), this, SLOT(onSetPcmVolume()));
    }
}

void Mixer::setMicrophoneVolume(int v)
{
    qDebug() << "Mixer::setMicrophoneVolume(" << v << ")";
    if (v != m_microphoneVolume)
    {
        m_microphoneVolumePending = v;
        QJsonObject arg;
        arg["label"] = "Capture_Volume";
        arg["val"] = static_cast<int>(v);
        m_setMicrophoneVolume = m_client.call(m_activeHal, "ctlset", arg);
        connect(m_setMicrophoneVolume.data(), SIGNAL(closed()), this, SLOT(onSetMicrophoneVolume()));
    }
}

QString Mixer::activeHal() const
{
    return m_activeHal;
}

void Mixer::setActiveHal(QString h)
{
    if (h != m_activeHal)
    {
        m_activeHal = h;
        qDebug() << "Mixer::setActiveHal: " << h;
        // Get volumes for this card

        QJsonObject arg;

        arg["label"] = "Master_Playback_Volume";
        m_getMasterVolume = m_client.call(m_activeHal, "ctlget", arg);
        connect(m_getMasterVolume.data(), SIGNAL(closed()), this, SLOT(onGetMasterVolume()));

        arg["label"] = "PCM_Playback_Volume";
        m_getPcmVolume = m_client.call(m_activeHal, "ctlget", arg);
        connect(m_getPcmVolume.data(), SIGNAL(closed()), this, SLOT(onGetPcmVolume()));

        arg["label"] = "Capture_Volume";
        m_getMicrophoneVolume = m_client.call(m_activeHal, "ctlget", arg);
        connect(m_getMicrophoneVolume.data(), SIGNAL(closed()), this, SLOT(onGetMicrophoneVolume()));

        emit activeHalChanged();
    }
}

void Mixer::open(const QUrl &url)
{
    m_client.open(url);
}

void Mixer::onClientConnected()
{
    // Call HAL to populate list
    m_alsacoreHallist = m_client.call("alsacore", "hallist");
    connect(m_alsacoreHallist.data(), SIGNAL(closed()), this, SLOT(onHalListClosed()));
}

void Mixer::onHalListClosed()
{
    qDebug() << "Mixer::onHalListClosed";
    if(m_alsacoreHallist->messageType() == AfMsgType::RetOk)
    {
        m_hallist.clear();

        QJsonArray cards = m_alsacoreHallist->value().toObject()["response"].toArray();
        qDebug() << "Mixer::onHalListClosed - founds: " << cards;
        foreach (const QJsonValue& card, cards)
        {
            QJsonObject c = card.toObject();
            QVariant v(c["api"].toString());
            m_hallist.append(v);
            qDebug() << "Mixer::onHalListClosed - added this HAL: " << v;
        }

        m_alsacoreHallist.clear();

        setActiveHal(m_hallist[0].toString());

        emit halsChanged();
    }
}

void Mixer::onGetMasterVolume()
{
    qDebug() << "Mixer::onGetMasterVolume()";
    disconnect(m_getMasterVolume.data(), SIGNAL(closed()), this, SLOT(onGetMasterVolume()));
    if (m_getMasterVolume->messageType() == AfMsgType::RetOk && m_getMasterVolume->value().isObject())
    {
        setMasterVolume(m_getMasterVolume->value().toObject()["response"].toObject()["val"].toArray()[0].toInt());
    }
}

void Mixer::onGetPcmVolume()
{
    qDebug() << "Mixer::onGetPcmVolume()";
    disconnect(m_getPcmVolume.data(), SIGNAL(closed()), this, SLOT(onGetPcmVolume()));
    if (m_getPcmVolume->messageType() == AfMsgType::RetOk && m_getPcmVolume->value().isObject())
    {
        setPcmVolume(m_getPcmVolume->value().toObject()["response"].toObject()["val"].toArray()[0].toInt());
    }
}

void Mixer::onGetMicrophoneVolume()
{
    qDebug() << "Mixer::onGetMicrophoneVolume()";
    disconnect(m_getMicrophoneVolume.data(), SIGNAL(closed()), this, SLOT(onGetMicrophoneVolume()));
    if (m_getMicrophoneVolume->messageType() == AfMsgType::RetOk && m_getMicrophoneVolume->value().isObject())
    {
        setMicrophoneVolume(m_getMicrophoneVolume->value().toObject()["response"].toObject()["val"].toArray()[0].toInt());
    }
}

void Mixer::onSetMasterVolume()
{
    qDebug() << "Mixer::onSetMasterVolume()";
    disconnect(m_setMasterVolume.data(), SIGNAL(closed()), this, SLOT(onSetMasterVolume()));
    if (m_setMasterVolume->messageType() == AfMsgType::RetOk && m_setMasterVolume->value().isObject())
    {
        m_masterVolume = m_masterVolumePending;
        emit masterVolumeChanged();
    }
}

void Mixer::onSetPcmVolume()
{
    qDebug() << "Mixer::onSetPcmVolume()";
    disconnect(m_setPcmVolume.data(), SIGNAL(closed()), this, SLOT(onSetPcmVolume()));
    if (m_setPcmVolume->messageType() == AfMsgType::RetOk && m_setPcmVolume->value().isObject())
    {
        m_pcmVolume = m_pcmVolumePending;
        emit pcmVolumeChanged();
    }
}

void Mixer::onSetMicrophoneVolume()
{
    qDebug() << "Mixer::onSetMicrophoneVolume()";
    disconnect(m_setMicrophoneVolume.data(), SIGNAL(closed()), this, SLOT(onSetMicrophoneVolume()));
    if (m_setMicrophoneVolume->messageType() == AfMsgType::RetOk && m_setMicrophoneVolume->value().isObject())
    {
        m_microphoneVolume = m_microphoneVolumePending;
        qDebug() << "Mixer::onSetMicrophoneVolume() - ";
        emit microphoneVolumeChanged();
    }
}