Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2026 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 : #include "dummyaudiodriver.h"
23 :
24 : #include <chrono>
25 :
26 : #include "global/runtime.h"
27 :
28 : #include "log.h"
29 :
30 : using namespace muse;
31 : using namespace muse::audio;
32 :
33 0 : DummyAudioDriver::~DummyAudioDriver()
34 : {
35 0 : close();
36 0 : }
37 :
38 0 : void DummyAudioDriver::init()
39 : {
40 0 : }
41 :
42 0 : std::string DummyAudioDriver::name() const
43 : {
44 0 : return NAME;
45 : }
46 :
47 0 : AudioDeviceID DummyAudioDriver::defaultDevice() const
48 : {
49 0 : return DEFAULT_DEVICE_ID;
50 : }
51 :
52 0 : bool DummyAudioDriver::open(const Spec& spec, Spec* activeSpec)
53 : {
54 0 : if (isOpened()) {
55 0 : close();
56 : }
57 :
58 0 : if (!spec.output.isValid() || !spec.callback) {
59 0 : LOGE() << "invalid spec: " << spec;
60 0 : return false;
61 : }
62 :
63 0 : m_activeSpec = spec;
64 0 : m_activeSpec.deviceId = DEFAULT_DEVICE_ID;
65 0 : if (activeSpec) {
66 0 : *activeSpec = m_activeSpec;
67 : }
68 :
69 0 : m_running = true;
70 0 : m_thread = std::thread([this]() { run(); });
71 :
72 0 : LOGI() << "audio output is disabled, the audio is discarded";
73 0 : m_activeSpecChanged.send(m_activeSpec);
74 0 : return true;
75 : }
76 :
77 0 : void DummyAudioDriver::run()
78 : {
79 0 : runtime::setThreadName("audio_dummy_driver");
80 :
81 0 : const OutputSpec& output = m_activeSpec.output;
82 0 : std::vector<float> buffer(output.samplesPerChannel * output.audioChannelCount);
83 0 : const int len = static_cast<int>(buffer.size() * sizeof(float));
84 :
85 : //! NOTE Pull the audio at the real time pace, as a device would do.
86 : //! Like a device, the first pull is after a period, not immediately on open:
87 : //! the audio engine is set up in parallel and must not be pulled before it is ready
88 0 : const auto period = std::chrono::nanoseconds(1'000'000'000LL * output.samplesPerChannel / output.sampleRate);
89 0 : auto next = std::chrono::steady_clock::now() + period;
90 :
91 0 : while (m_running) {
92 0 : std::this_thread::sleep_until(next);
93 0 : if (!m_running) {
94 0 : break;
95 : }
96 :
97 0 : m_activeSpec.callback(reinterpret_cast<uint8_t*>(buffer.data()), len);
98 0 : next += period;
99 : }
100 0 : }
101 :
102 0 : void DummyAudioDriver::close()
103 : {
104 0 : m_running = false;
105 0 : if (m_thread.joinable()) {
106 0 : m_thread.join();
107 : }
108 0 : }
109 :
110 0 : bool DummyAudioDriver::isOpened() const
111 : {
112 0 : return m_running;
113 : }
114 :
115 0 : const IAudioDriver::Spec& DummyAudioDriver::activeSpec() const
116 : {
117 0 : return m_activeSpec;
118 : }
119 :
120 0 : async::Channel<IAudioDriver::Spec> DummyAudioDriver::activeSpecChanged() const
121 : {
122 0 : return m_activeSpecChanged;
123 : }
124 :
125 0 : std::vector<samples_t> DummyAudioDriver::availableOutputDeviceBufferSizes() const
126 : {
127 0 : return { 256, 512, 1024, 2048, 4096 };
128 : }
129 :
130 0 : std::vector<sample_rate_t> DummyAudioDriver::availableOutputDeviceSampleRates() const
131 : {
132 0 : return { 44100, 48000, 88200, 96000 };
133 : }
134 :
135 0 : AudioDeviceList DummyAudioDriver::availableOutputDevices() const
136 : {
137 0 : return { AudioDevice { DEFAULT_DEVICE_ID, "Dummy" } };
138 0 : }
139 :
140 0 : async::Notification DummyAudioDriver::availableOutputDevicesChanged() const
141 : {
142 0 : return m_availableOutputDevicesChanged;
143 : }
|