Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2025 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "commonaudiodriverconfigurationmodel.h"
24 :
25 : #include "global/translation.h"
26 :
27 : #include "audio/common/audiotypes.h"
28 :
29 : using namespace mu::preferences;
30 : using namespace muse::audio;
31 :
32 0 : CommonAudioDriverConfigurationModel::CommonAudioDriverConfigurationModel(QObject* parent)
33 0 : : QObject(parent), muse::Contextable(muse::iocCtxForQmlObject(this))
34 : {
35 0 : }
36 :
37 0 : void CommonAudioDriverConfigurationModel::load()
38 : {
39 0 : audioDriverController()->currentAudioDriverChanged().onNotify(this, [this]() {
40 0 : emit deviceListChanged();
41 0 : emit currentDeviceIdChanged();
42 0 : emit sampleRateListChanged();
43 0 : emit sampleRateChanged();
44 0 : emit bufferSizeListChanged();
45 0 : emit bufferSizeChanged();
46 0 : });
47 :
48 0 : audioDriverController()->availableOutputDevicesChanged().onNotify(this, [this]() {
49 0 : emit deviceListChanged();
50 0 : emit currentDeviceIdChanged();
51 0 : });
52 :
53 0 : audioDriverController()->outputDeviceChanged().onNotify(this, [this]() {
54 0 : emit currentDeviceIdChanged();
55 0 : emit sampleRateListChanged();
56 0 : emit sampleRateChanged();
57 0 : emit bufferSizeListChanged();
58 0 : emit bufferSizeChanged();
59 0 : });
60 :
61 0 : audioDriverController()->outputDeviceSampleRateChanged().onNotify(this, [this]() {
62 0 : emit sampleRateChanged();
63 0 : });
64 :
65 0 : audioDriverController()->outputDeviceBufferSizeChanged().onNotify(this, [this]() {
66 0 : emit bufferSizeChanged();
67 0 : });
68 0 : }
69 :
70 0 : QString CommonAudioDriverConfigurationModel::currentDeviceId() const
71 : {
72 0 : AudioDeviceID device = audioDriverController()->outputDevice();
73 0 : return QString::fromStdString(device);
74 0 : }
75 :
76 0 : QVariantList CommonAudioDriverConfigurationModel::deviceList() const
77 : {
78 0 : AudioDeviceList devices = audioDriverController()->availableOutputDevices();
79 :
80 0 : QVariantList result;
81 0 : result.reserve(devices.size());
82 :
83 0 : for (const AudioDevice& device : devices) {
84 0 : QVariantMap obj;
85 0 : obj["value"] = QString::fromStdString(device.id);
86 0 : obj["text"] = QString::fromStdString(device.name);
87 :
88 0 : result << obj;
89 0 : }
90 :
91 0 : return result;
92 0 : }
93 :
94 0 : void CommonAudioDriverConfigurationModel::deviceSelected(const QString& deviceId)
95 : {
96 0 : bool ok = audioDriverController()->selectOutputDevice(deviceId.toStdString());
97 0 : if (!ok) {
98 0 : interactive()->error("",
99 0 : muse::trc("appshell/preferences", "The driver for this device could not be opened."));
100 : }
101 0 : }
102 :
103 0 : unsigned int CommonAudioDriverConfigurationModel::bufferSize() const
104 : {
105 0 : unsigned int val = audioDriverController()->activeSpec().output.samplesPerChannel;
106 0 : return val;
107 : }
108 :
109 0 : QList<unsigned int> CommonAudioDriverConfigurationModel::bufferSizeList() const
110 : {
111 0 : std::vector<samples_t> bufferSizes = audioDriverController()->availableOutputDeviceBufferSizes();
112 :
113 0 : QList<unsigned int> result;
114 0 : result.reserve(bufferSizes.size());
115 :
116 0 : for (samples_t bufferSize : bufferSizes) {
117 0 : result << static_cast<unsigned int>(bufferSize);
118 : }
119 :
120 0 : return result;
121 0 : }
122 :
123 0 : void CommonAudioDriverConfigurationModel::bufferSizeSelected(const QString& bufferSizeStr)
124 : {
125 0 : audioDriverController()->changeBufferSize(bufferSizeStr.toInt());
126 0 : }
127 :
128 0 : unsigned int CommonAudioDriverConfigurationModel::sampleRate() const
129 : {
130 0 : return audioDriverController()->activeSpec().output.sampleRate;
131 : }
132 :
133 0 : QList<unsigned int> CommonAudioDriverConfigurationModel::sampleRateList() const
134 : {
135 0 : std::vector<sample_rate_t> sampleRates = audioDriverController()->availableOutputDeviceSampleRates();
136 :
137 0 : QList<unsigned int> result;
138 0 : result.reserve(sampleRates.size());
139 :
140 0 : for (sample_rate_t sampleRate : sampleRates) {
141 0 : result << static_cast<unsigned int>(sampleRate);
142 : }
143 :
144 0 : return result;
145 0 : }
146 :
147 0 : void CommonAudioDriverConfigurationModel::sampleRateSelected(const QString& sampleRateStr)
148 : {
149 0 : audioDriverController()->changeSampleRate(sampleRateStr.toInt());
150 0 : }
|