|
| 1 | +/* |
| 2 | + * Copyright 2021 Benoit Pelletier |
| 3 | + * |
| 4 | + * This file is part of Sound Generator. |
| 5 | + * |
| 6 | + * Sound Generator is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * Sound Generator is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with Sound Generator. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +#include "ActionAudioDevice.h" |
| 22 | +#include <QtCore> |
| 23 | +#include <QAudioDeviceInfo> |
| 24 | + |
| 25 | +void DebugLogDeviceInfo(const QAudioDeviceInfo& _info) |
| 26 | +{ |
| 27 | + qDebug() << "Device: " << _info.deviceName() |
| 28 | + << " | samples rates: " << _info.supportedSampleRates() |
| 29 | + << " | channel count: " << _info.supportedChannelCounts() |
| 30 | + << " | samples size: " << _info.supportedSampleSizes() |
| 31 | + << " | codecs: " << _info.supportedCodecs() |
| 32 | + << " | byte orders: " << _info.supportedByteOrders() |
| 33 | + << " | sample types: " << _info.supportedSampleTypes(); |
| 34 | +} |
| 35 | + |
| 36 | +ActionAudioDevice::ActionAudioDevice(const QString& _title, QWidget* _parent) |
| 37 | + : QMenu(_title, _parent) |
| 38 | +{ |
| 39 | + connect(this, &QMenu::aboutToShow, this, &ActionAudioDevice::beforeShowing); |
| 40 | + connect(this, &QMenu::triggered, this, &ActionAudioDevice::onTriggered); |
| 41 | + |
| 42 | + m_currentInfo = QAudioDeviceInfo::defaultOutputDevice(); |
| 43 | + DebugLogDeviceInfo(m_currentInfo); |
| 44 | +} |
| 45 | + |
| 46 | +void ActionAudioDevice::beforeShowing() |
| 47 | +{ |
| 48 | + clear(); |
| 49 | + |
| 50 | + const QList<QAudioDeviceInfo> deviceList = _listAvalaibleDevices(); |
| 51 | + for(QAudioDeviceInfo device : deviceList) |
| 52 | + { |
| 53 | + QVariant data; |
| 54 | + data.setValue(device); |
| 55 | + |
| 56 | + QAction* act = new QAction(device.deviceName(), this); |
| 57 | + act->setCheckable(true); |
| 58 | + act->setChecked(device == m_currentInfo); |
| 59 | + if(device == m_currentInfo) |
| 60 | + { |
| 61 | + DebugLogDeviceInfo(device); |
| 62 | + } |
| 63 | + |
| 64 | + act->setData(data); |
| 65 | + addAction(act); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +void ActionAudioDevice::onTriggered(QAction* _action) |
| 70 | +{ |
| 71 | + QAudioDeviceInfo info = qvariant_cast<QAudioDeviceInfo>(_action->data()); |
| 72 | + qDebug() << "Clicked on action!"; |
| 73 | + DebugLogDeviceInfo(info); |
| 74 | + if(info != m_currentInfo) |
| 75 | + { |
| 76 | + m_currentInfo = info; |
| 77 | + emit deviceChanged(info); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +const QList<QAudioDeviceInfo> ActionAudioDevice::_listAvalaibleDevices() |
| 82 | +{ |
| 83 | + QMap<QString, QAudioDeviceInfo> infoMap; |
| 84 | + QList<QAudioDeviceInfo> deviceList = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput); |
| 85 | + for(QAudioDeviceInfo device : deviceList) |
| 86 | + { |
| 87 | + QMap<QString, QAudioDeviceInfo>::iterator it = infoMap.find(device.deviceName()); |
| 88 | + if(it != infoMap.end()) |
| 89 | + { |
| 90 | + int score = 0; |
| 91 | + score += (device.supportedSampleRates().count() > it->supportedSampleRates().count()) |
| 92 | + ? 1 : -1; |
| 93 | + score += (device.supportedSampleSizes().count() > it->supportedSampleSizes().count()) |
| 94 | + ? 1 : -1; |
| 95 | + score += (device.supportedChannelCounts().count() > it->supportedChannelCounts().count()) |
| 96 | + ? 1 : -1; |
| 97 | + |
| 98 | + if(score > 0) |
| 99 | + { |
| 100 | + infoMap[device.deviceName()] = device; |
| 101 | + } |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + infoMap.insert(device.deviceName(), device); |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + return infoMap.values(); |
| 110 | +} |
0 commit comments