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 0 : m_engineGlobalSetup = std::make_shared<engine::EngineGlobalSetup>();
78 0 : m_engineGlobalSetup->registerExports();
79 :
80 : #ifdef Q_OS_WASM
81 : m_rpcChannel = std::make_shared<rpc::WebRpcChannel>();
82 : m_soundFontController = std::make_shared<WebSoundFontController>();
83 : #else
84 0 : m_rpcChannel = std::make_shared<rpc::GeneralRpcChannel>();
85 0 : m_soundFontController = std::make_shared<GeneralSoundFontController>();
86 : #endif
87 :
88 0 : m_startAudioController = std::make_shared<StartAudioController>(m_rpcChannel);
89 :
90 0 : globalIoc()->registerExport<IAudioConfiguration>(mname, m_configuration);
91 0 : globalIoc()->registerExport<IAudioThreadSecurer>(mname, std::make_shared<AudioThreadSecurer>());
92 0 : globalIoc()->registerExport<rpc::IRpcChannel>(mname, m_rpcChannel);
93 0 : globalIoc()->registerExport<IAudioDriverController>(mname, m_audioDriverController);
94 0 : globalIoc()->registerExport<IAudioDriverFactory>(mname, std::make_shared<AudioDriverFactory>());
95 0 : globalIoc()->registerExport<IAudioTaskScheduler>(mname, m_audioDriverController->getAudioTaskScheduler());
96 0 : globalIoc()->registerExport<ISoundFontController>(mname, m_soundFontController);
97 0 : globalIoc()->registerExport<IStartAudioController>(mname, m_startAudioController);
98 0 : }
99 :
100 0 : void AudioModule::resolveImports()
101 : {
102 0 : m_engineGlobalSetup->resolveImports();
103 :
104 0 : auto cr = globalIoc()->resolve<muse::rcommand::ICommandsRegister>(mname);
105 0 : if (cr) {
106 0 : cr->reg(std::make_shared<AudioCommandsRegister>());
107 : }
108 0 : }
109 :
110 0 : void AudioModule::onInit(const IApplication::RunMode& mode)
111 : {
112 0 : m_configuration->init();
113 :
114 0 : if (mode == IApplication::RunMode::AudioPluginRegistration) {
115 0 : return;
116 : }
117 :
118 : // rpc
119 0 : m_rpcChannel->setupOnMain();
120 : #ifndef Q_OS_WASM
121 0 : m_rpcTicker.start(1, [this]() {
122 0 : m_rpcChannel->process();
123 0 : }, Ticker::Mode::Repeat);
124 : #endif
125 :
126 0 : m_startAudioController->init();
127 :
128 : #ifndef Q_OS_WASM
129 0 : m_startAudioController->startAudioProcessing(mode);
130 : #endif
131 :
132 : //! --- Diagnostics ---
133 0 : auto pr = globalIoc()->resolve<muse::diagnostics::IDiagnosticsPathsRegister>(mname);
134 0 : if (pr) {
135 0 : std::vector<io::path_t> paths = m_configuration->soundFontDirectories();
136 0 : for (const io::path_t& p : paths) {
137 0 : pr->reg("soundfonts", p);
138 : }
139 0 : }
140 0 : }
141 :
142 0 : void AudioModule::onDeinit()
143 : {
144 0 : m_rpcTicker.stop();
145 0 : m_startAudioController->stopAudioProcessing();
146 0 : m_engineGlobalSetup->onDeinit();
147 0 : }
148 :
149 0 : modularity::IContextSetup* AudioModule::newContext(const muse::modularity::ContextPtr& ctx) const
150 : {
151 0 : return new AudioContext(ctx);
152 : }
153 :
154 : // Context
155 :
156 0 : void AudioContext::registerExports()
157 : {
158 0 : m_actionsController = std::make_shared<AudioActionsController>(iocContext());
159 0 : m_transportEventsController = std::make_shared<TransportEventsController>(iocContext());
160 :
161 : #ifndef Q_OS_WASM
162 0 : ioc()->registerExport<ISoundFontInstallScenario>(mname, new GeneralSoundFontInstallScenario(iocContext()));
163 : #endif
164 :
165 0 : ioc()->registerExport<IPlayback>(mname, new Playback(iocContext()));
166 0 : }
167 :
168 0 : void AudioContext::resolveImports()
169 : {
170 0 : auto cs = ioc()->resolve<muse::rcommand::ICommandsState>(mname);
171 0 : if (cs) {
172 0 : cs->reg(std::make_shared<AudioCommandsState>(iocContext(), m_actionsController));
173 : }
174 0 : }
175 :
176 0 : void AudioContext::onInit(const IApplication::RunMode& mode)
177 : {
178 0 : if (mode == IApplication::RunMode::AudioPluginRegistration) {
179 0 : return;
180 : }
181 :
182 0 : m_audioInited = true;
183 :
184 0 : m_actionsController->init();
185 0 : m_transportEventsController->init();
186 : }
187 :
188 0 : void AudioContext::onDeinit()
189 : {
190 0 : m_transportEventsController->deinit();
191 0 : }
|