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) 2022 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 : #pragma once
24 :
25 : #include <memory>
26 :
27 : #include "audio/engine/internal/synthesizers/abstractsynthesizer.h"
28 : #include "async/channel.h"
29 :
30 : #include "libhandler.h"
31 : #include "musesamplersequencer.h"
32 :
33 : #include "imusesamplertracks.h"
34 :
35 : #include "timer.h"
36 :
37 : namespace muse::musesampler {
38 : class MuseSamplerWrapper : public audio::synth::AbstractSynthesizer, public IMuseSamplerTracks
39 : {
40 : public:
41 : MuseSamplerWrapper(MuseSamplerLibHandlerPtr samplerLib, const InstrumentInfo& instrument, const muse::audio::AudioSourceParams& params);
42 : ~MuseSamplerWrapper() override;
43 :
44 : void setMode(const muse::audio::ProcessMode mode) override;
45 : void setOutputSpec(const audio::OutputSpec& spec) override;
46 : muse::audio::samples_t process(float* buffer, muse::audio::samples_t samplesPerChannel) override;
47 :
48 : std::string name() const override;
49 : muse::audio::AudioSourceType type() const override;
50 : void flushSound() override;
51 : bool isValid() const override;
52 :
53 : void prepareToPlay() override;
54 : bool readyToPlay() const override;
55 :
56 : void processInput() override;
57 : void clearCache() override;
58 :
59 : private:
60 : void setupSound(const mpe::PlaybackSetupData& setupData) override;
61 : void setupEvents(const mpe::PlaybackData& playbackData) override;
62 : const mpe::PlaybackData& playbackData() const override;
63 :
64 : // IMuseSamplerTracks
65 : const TrackList& allTracks() const override;
66 : ms_Track addTrack() override;
67 :
68 : muse::audio::TimePosition playbackPosition() const override;
69 : void setPlaybackPosition(const muse::audio::TimePosition& position) override;
70 : bool isActive() const;
71 : void setIsActive(bool active);
72 :
73 : bool initSampler(const muse::audio::sample_rate_t sampleRate, const muse::audio::samples_t blockSize);
74 :
75 : void setupOnlineSound();
76 : void updateRenderingProgress(ms_RenderingRangeList list, int size);
77 :
78 : InstrumentInfo resolveInstrument(const mpe::PlaybackSetupData& setupData) const;
79 : std::string resolveDefaultPresetCode(const InstrumentInfo& instrument) const;
80 :
81 : void prepareOutputBuffer(const muse::audio::samples_t samples);
82 : void handleAuditionEvents(const MuseSamplerSequencer::EventType& event);
83 : void doCurrentSetPosition();
84 : void extractOutputSamples(muse::audio::samples_t samples, float* output);
85 :
86 : struct RenderingInfo {
87 : long long maxChunksDurationUs = 0;
88 : int errorCode = 0;
89 : std::string errorText;
90 : std::string errorData;
91 : int64_t percentage = 0;
92 : audio::InputProcessingProgress::ChunkInfoList lastReceivedChunks;
93 :
94 0 : void clear()
95 : {
96 0 : *this = RenderingInfo();
97 0 : }
98 : };
99 :
100 : MuseSamplerLibHandlerPtr m_samplerLib = nullptr;
101 : ms_MuseSampler m_sampler = nullptr;
102 : InstrumentInfo m_instrument;
103 : TrackList m_tracks;
104 : ms_OutputBuffer m_bus;
105 :
106 : RenderingInfo m_renderingInfo;
107 :
108 : using RenderingStateChangedChannel = async::Channel<ms_RenderingRangeList, int>;
109 : RenderingStateChangedChannel m_renderingStateChanged;
110 :
111 : muse::audio::TimePosition m_currentPosition;
112 :
113 : audio::OutputSpec m_outputSpec;
114 :
115 : std::vector<float> m_leftChannel;
116 : std::vector<float> m_rightChannel;
117 :
118 : std::array<float*, 2> m_internalBuffer;
119 :
120 : bool m_offlineModeStarted = false;
121 : audio::sample_rate_t m_offlineSampleRate = 0;
122 : bool m_allNotesOffRequested = false;
123 : bool m_pendingSetPosition = false;
124 :
125 : MuseSamplerSequencer m_sequencer;
126 :
127 : std::unique_ptr<Timer> m_checkReadyToPlayTimer;
128 : };
129 :
130 : using MuseSamplerWrapperPtr = std::shared_ptr<MuseSamplerWrapper>;
131 : }
|