Showing the changes to interface/src/scripting/AudioDeviceScriptingInterface.cpp (1 of 7 files). Show all
Unified · Split
| @@ -33,11 +33,17 @@ bool AudioDeviceScriptingInterface::muted() · 32 unmodified lines | |||
| 33 | return getMuted(); | 33 | return getMuted(); |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | AudioDeviceScriptingInterface::AudioDeviceScriptingInterface() { | 36 | AudioDeviceScriptingInterface::AudioDeviceScriptingInterface(): QAbstractListModel(nullptr) { |
| 37 | connect(DependencyManager::get<AudioClient>().data(), &AudioClient::muteToggled, | 37 | connect(DependencyManager::get<AudioClient>().data(), &AudioClient::muteToggled, |
| 38 | this, &AudioDeviceScriptingInterface::muteToggled); | 38 | this, &AudioDeviceScriptingInterface::muteToggled); |
| 39 | connect(DependencyManager::get<AudioClient>().data(), &AudioClient::deviceChanged, | 39 | connect(DependencyManager::get<AudioClient>().data(), &AudioClient::deviceChanged, |
| 40 | this, &AudioDeviceScriptingInterface::deviceChanged); | 40 | this, &AudioDeviceScriptingInterface::onDeviceChanged, Qt::QueuedConnection); |
| 41 | connect(DependencyManager::get<AudioClient>().data(), &AudioClient::currentInputDeviceChanged, | ||
| 42 | this, &AudioDeviceScriptingInterface::onCurrentInputDeviceChanged, Qt::QueuedConnection); | ||
| 43 | connect(DependencyManager::get<AudioClient>().data(), &AudioClient::currentOutputDeviceChanged, | ||
| 44 | this, &AudioDeviceScriptingInterface::onCurrentOutputDeviceChanged, Qt::QueuedConnection); | ||
| 45 | //fill up model | ||
| 46 | onDeviceChanged(); | ||
| 41 | } | 47 | } |
| 42 | 48 | ||
| 43 | bool AudioDeviceScriptingInterface::setInputDevice(const QString& deviceName) { | 49 | bool AudioDeviceScriptingInterface::setInputDevice(const QString& deviceName) { |
| @@ -51,13 +57,27 @@ bool AudioDeviceScriptingInterface::setInputDevice(const QString& deviceName) { · 7 unmodified lines | |||
| 51 | 57 | ||
| 52 | bool AudioDeviceScriptingInterface::setOutputDevice(const QString& deviceName) { | 58 | bool AudioDeviceScriptingInterface::setOutputDevice(const QString& deviceName) { |
| 53 | bool result; | 59 | bool result; |
| 60 | QTime a; a.start(); | ||
| 54 | QMetaObject::invokeMethod(DependencyManager::get<AudioClient>().data(), "switchOutputToAudioDevice", | 61 | QMetaObject::invokeMethod(DependencyManager::get<AudioClient>().data(), "switchOutputToAudioDevice", |
| 55 | Qt::BlockingQueuedConnection, | 62 | Qt::BlockingQueuedConnection, |
| 56 | Q_RETURN_ARG(bool, result), | 63 | Q_RETURN_ARG(bool, result), |
| 57 | Q_ARG(const QString&, deviceName)); | 64 | Q_ARG(const QString&, deviceName)); |
| 65 | qDebug() << "switching to" << deviceName << "elapsed" << a.elapsed(); | ||
| 58 | return result; | 66 | return result; |
| 59 | } | 67 | } |
| 60 | 68 | ||
| 69 | void AudioDeviceScriptingInterface::setInputDeviceAsync(const QString& deviceName) { | ||
| 70 | QMetaObject::invokeMethod(DependencyManager::get<AudioClient>().data(), "switchInputToAudioDevice", | ||
| 71 | Qt::QueuedConnection, | ||
| 72 | Q_ARG(const QString&, deviceName)); | ||
| 73 | } | ||
| 74 | |||
| 75 | void AudioDeviceScriptingInterface::setOutputDeviceAsync(const QString& deviceName) { | ||
| 76 | QMetaObject::invokeMethod(DependencyManager::get<AudioClient>().data(), "switchOutputToAudioDevice", | ||
| 77 | Qt::QueuedConnection, | ||
| 78 | Q_ARG(const QString&, deviceName)); | ||
| 79 | } | ||
| 80 | |||
| 61 | QString AudioDeviceScriptingInterface::getInputDevice() { | 81 | QString AudioDeviceScriptingInterface::getInputDevice() { |
| 62 | return DependencyManager::get<AudioClient>()->getDeviceName(QAudio::AudioInput); | 82 | return DependencyManager::get<AudioClient>()->getDeviceName(QAudio::AudioInput); |
| 63 | } | 83 | } |
| @@ -116,3 +136,94 @@ void AudioDeviceScriptingInterface::setMuted(bool muted) · 52 unmodified lines | |||
| 116 | bool AudioDeviceScriptingInterface::getMuted() { | 136 | bool AudioDeviceScriptingInterface::getMuted() { |
| 117 | return DependencyManager::get<AudioClient>()->isMuted(); | 137 | return DependencyManager::get<AudioClient>()->isMuted(); |
| 118 | } | 138 | } |
| 139 | |||
| 140 | QVariant AudioDeviceScriptingInterface::data(const QModelIndex& index, int role) const { | ||
| 141 | //sanity | ||
| 142 | if (!index.isValid() || index.row() >= _devices.size()) | ||
| 143 | return QVariant(); | ||
| 144 | |||
| 145 | |||
| 146 | if (role == Qt::DisplayRole || role == DisplayNameRole) { | ||
| 147 | return _devices.at(index.row()).name; | ||
| 148 | } else if (role == SelectedRole) { | ||
| 149 | return _devices.at(index.row()).selected; | ||
| 150 | } else if (role == AudioModeRole) { | ||
| 151 | return (int)_devices.at(index.row()).mode; | ||
| 152 | } | ||
| 153 | return QVariant(); | ||
| 154 | } | ||
| 155 | |||
| 156 | int AudioDeviceScriptingInterface::rowCount(const QModelIndex& parent) const { | ||
| 157 | Q_UNUSED(parent) | ||
| 158 | return _devices.size(); | ||
| 159 | } | ||
| 160 | |||
| 161 | QHash<int, QByteArray> AudioDeviceScriptingInterface::roleNames() const { | ||
| 162 | QHash<int, QByteArray> roles; | ||
| 163 | roles.insert(DisplayNameRole, "devicename"); | ||
| 164 | roles.insert(SelectedRole, "devicechecked"); | ||
| 165 | roles.insert(AudioModeRole, "devicemode"); | ||
| 166 | return roles; | ||
| 167 | } | ||
| 168 | |||
| 169 | void AudioDeviceScriptingInterface::onDeviceChanged() | ||
| 170 | { | ||
| 171 | beginResetModel(); | ||
| 172 | _devices.clear(); | ||
| 173 | const QString &outDevice = getOutputDevice(); | ||
| 174 | for (QString name: getOutputDevices()) { | ||
| 175 | AudioDeviceInfo di; | ||
| 176 | di.name = name; | ||
| 177 | di.selected = (name == outDevice); | ||
| 178 | di.mode = QAudio::AudioOutput; | ||
| 179 | _devices.append(di); | ||
| 180 | } | ||
| 181 | const QString &inDevice = getInputDevice(); | ||
| 182 | for (QString name: getInputDevices()) { | ||
| 183 | AudioDeviceInfo di; | ||
| 184 | di.name = name; | ||
| 185 | di.selected = (name == inDevice); | ||
| 186 | di.mode = QAudio::AudioInput; | ||
| 187 | _devices.append(di); | ||
| 188 | } | ||
| 189 | |||
| 190 | endResetModel(); | ||
| 191 | emit deviceChanged(); | ||
| 192 | } | ||
| 193 | |||
| 194 | void AudioDeviceScriptingInterface::onCurrentInputDeviceChanged() | ||
| 195 | { | ||
| 196 | currentDeviceUpdate(getInputDevice(), QAudio::AudioInput); | ||
| 197 | emit currentInputDeviceChanged(); | ||
| 198 | } | ||
| 199 | |||
| 200 | void AudioDeviceScriptingInterface::onCurrentOutputDeviceChanged() | ||
| 201 | { | ||
| 202 | currentDeviceUpdate(getOutputDevice(), QAudio::AudioOutput); | ||
| 203 | emit currentOutputDeviceChanged(); | ||
| 204 | } | ||
| 205 | |||
| 206 | void AudioDeviceScriptingInterface::currentDeviceUpdate(const QString &name, QAudio::Mode mode) | ||
| 207 | { | ||
| 208 | QVector<int> role; | ||
| 209 | role.append(SelectedRole); | ||
| 210 | |||
| 211 | qDebug() << "device update" << name << mode; | ||
| 212 | |||
| 213 | for (int i = 0; i < _devices.size(); i++) { | ||
| 214 | AudioDeviceInfo di = _devices.at(i); | ||
| 215 | if (di.mode != mode) | ||
| 216 | continue; | ||
| 217 | if (di.selected && di.name != name ) { | ||
| 218 | di.selected = false; | ||
| 219 | _devices[i] = di; | ||
| 220 | emit dataChanged(index(i, 0), index(i, 0), role); | ||
| 221 | } | ||
| 222 | if (di.name == name) { | ||
| 223 | di.selected = true; | ||
| 224 | _devices[i] = di; | ||
| 225 | emit dataChanged(index(i, 0), index(i, 0), role); | ||
| 226 | qDebug() << "device updated" << di.name << i; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | } | ||