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) 2021 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 "audiomodule.h"
23 :
24 : #include "common/iaudiotaskscheduler.h"
25 : #include "global/modularity/ioc.h"
26 : #include "rcommand/icommandsregister.h"
27 : #include "rcommand/icommandsstate.h"
28 :
29 : #include "audio/common/audiosanitizer.h"
30 : #include "audio/common/audiothreadsecurer.h"
31 : #ifdef Q_OS_WASM
32 : #include "audio/common/rpc/platform/web/webrpcchannel.h"
33 : #include "platform/web/websoundfontcontroller.h"
34 : #else
35 : #include "audio/common/rpc/platform/general/generalrpcchannel.h"
36 : #include "platform/general/generalsoundfontcontroller.h"
37 : #include "platform/general/generalsoundfontinstallscenario.h"
38 : #endif
39 :
40 : #include "internal/audioconfiguration.h"
41 : #include "internal/audioactionscontroller.h"
42 : #include "internal/audiocommandsregister.h"
43 : #include "internal/audiocommandsstate.h"
44 : #include "internal/transporteventscontroller.h"
45 : #include "internal/startaudiocontroller.h"
46 : #include "internal/playback.h"
47 : #include "internal/audiodrivercontroller.h"
48 : #include "internal/audiodriverfactory.h"
49 :
50 : #include "audio/engine/enginesetup.h"
51 :
52 : #include "diagnostics/idiagnosticspathsregister.h"
53 :
54 : #include "log.h"
55 :
56 : using namespace muse;
57 : using namespace muse::modularity;
58 : using namespace muse::audio;
59 :
60 0 : AudioModule::AudioModule()
61 : {
62 0 : AudioSanitizer::setupMainThread();
63 0 : }
64 :
65 : static const std::string mname("audio");
66 :
67 0 : std::string AudioModule::moduleName() const
68 : {
69 0 : return mname;
70 : }
71 :
72 0 : void AudioModule::registerExports()
73 : {
74 0 : m_configuration = std::make_shared<AudioConfiguration>();
75 0 : m_audioDriverController = std::make_shared<AudioDriverController>();
76 :
77 : #ifndef Q_OS_WASM
78 0 : m_engineGlobalSetup = std::make_shared<engine::EngineGlobalSetup>();
79 0 : m_engineGlobalSetup->registerExports();
80 : #endif
81 :
82 : #ifdef Q_OS_WASM
83 : m_rpcChannel = std::make_shared<rpc::WebRpcChannel>();
84 : m_soundFontController = std::make_shared<WebSoundFontController>();
85 : #else
86 0 : m_rpcChannel = std::make_shared<rpc::GeneralRpcChannel>();
87 0 : m_soundFontController = std::make_shared<GeneralSoundFontController>();
88 : #endif
89 :
90 0 : m_startAudioController = std::make_shared<StartAudioController>(m_rpcChannel);
91 :
92 0 : globalIoc()->registerExport<IAudioConfiguration>(mname, m_configuration);
93 0 : globalIoc()->registerExport<IAudioThreadSecurer>(mname, std::make_shared<AudioThreadSecurer>());
94 0 : globalIoc()->registerExport<rpc::IRpcChannel>(mname, m_rpcChannel);
95 0 : globalIoc()->registerExport<IAudioDriverController>(mname, m_audioDriverController);
96 0 : globalIoc()->registerExport<IAudioDriverFactory>(mname, std::make_shared<AudioDriverFactory>());
97 0 : globalIoc()->registerExport<IAudioTaskScheduler>(mname, m_audioDriverController->getAudioTaskScheduler());
98 0 : globalIoc()->registerExport<ISoundFontController>(mname, m_soundFontController);
99 0 : globalIoc()->registerExport<IStartAudioController>(mname, m_startAudioController);
100 0 : }
101 :
102 0 : void AudioModule::resolveImports()
103 : {
104 : #ifndef Q_OS_WASM
105 0 : m_engineGlobalSetup->resolveImports();
106 : #endif
107 :
108 0 : auto cr = globalIoc()->resolve<muse::rcommand::ICommandsRegister>(mname);
109 0 : if (cr) {
110 0 : cr->reg(std::make_shared<AudioCommandsRegister>());
111 : }
112 0 : }
113 :
114 0 : void AudioModule::onInit(const IApplication::RunMode& mode)
115 : {
116 0 : m_configuration->init();
117 :
118 0 : if (mode == IApplication::RunMode::AudioPluginRegistration) {
119 0 : return;
120 : }
121 :
122 : // rpc
123 0 : m_rpcChannel->setupOnMain();
124 : #ifndef Q_OS_WASM
125 0 : m_rpcTicker.start(1, [this]() {
126 0 : m_rpcChannel->process();
127 0 : }, Ticker::Mode::Repeat);
128 : #endif
129 :
130 0 : m_startAudioController->init();
131 :
132 : #ifndef Q_OS_WASM
133 0 : m_startAudioController->startAudioProcessing(mode);
134 : #endif
135 :
136 : //! --- Diagnostics ---
137 0 : auto pr = globalIoc()->resolve<muse::diagnostics::IDiagnosticsPathsRegister>(mname);
138 0 : if (pr) {
139 0 : std::vector<io::path_t> paths = m_configuration->soundFontDirectories();
140 0 : for (const io::path_t& p : paths) {
141 0 : pr->reg("soundfonts", p);
142 : }
143 0 : }
144 0 : }
145 :
146 0 : void AudioModule::onDeinit()
147 : {
148 0 : m_rpcTicker.stop();
149 0 : m_startAudioController->stopAudioProcessing();
150 : #ifndef Q_OS_WASM
151 0 : m_engineGlobalSetup->onDeinit();
152 : #endif
153 0 : }
154 :
155 0 : modularity::IContextSetup* AudioModule::newContext(const muse::modularity::ContextPtr& ctx) const
156 : {
157 0 : return new AudioContext(ctx);
158 : }
159 :
160 : // Context
161 :
162 0 : void AudioContext::registerExports()
163 : {
164 0 : m_actionsController = std::make_shared<AudioActionsController>(iocContext());
165 0 : m_transportEventsController = std::make_shared<TransportEventsController>(iocContext());
166 :
167 : #ifndef Q_OS_WASM
168 0 : ioc()->registerExport<ISoundFontInstallScenario>(mname, new GeneralSoundFontInstallScenario(iocContext()));
169 : #endif
170 :
171 0 : ioc()->registerExport<IPlayback>(mname, new Playback(iocContext()));
172 0 : }
173 :
174 0 : void AudioContext::resolveImports()
175 : {
176 0 : auto cs = ioc()->resolve<muse::rcommand::ICommandsState>(mname);
177 0 : if (cs) {
178 0 : cs->reg(std::make_shared<AudioCommandsState>(iocContext(), m_actionsController));
179 : }
180 0 : }
181 :
182 0 : void AudioContext::onInit(const IApplication::RunMode& mode)
183 : {
184 0 : if (mode == IApplication::RunMode::AudioPluginRegistration) {
185 0 : return;
186 : }
187 :
188 0 : m_audioInited = true;
189 :
190 0 : m_actionsController->init();
191 0 : m_transportEventsController->init();
192 : }
193 :
194 0 : void AudioContext::onDeinit()
195 : {
196 0 : m_transportEventsController->deinit();
197 0 : }
|