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 "mixernode.h"
23 :
24 : using namespace muse::audio::engine;
25 :
26 0 : void MixerNode::onOutputSpecChanged(const OutputSpec& spec)
27 : {
28 0 : const size_t bufferSize = m_outputSpec.samplesPerChannel * m_outputSpec.audioChannelCount;
29 0 : m_buffer.resize(bufferSize);
30 :
31 0 : for (auto& input : m_inputs) {
32 0 : input->setOutputSpec(spec);
33 : }
34 0 : }
35 :
36 0 : void MixerNode::onModeChanged(const ProcessMode mode)
37 : {
38 0 : for (auto& input : m_inputs) {
39 0 : input->setMode(mode);
40 : }
41 0 : }
42 :
43 0 : bool MixerNode::doAddNode(std::shared_ptr<IAudioNode> other)
44 : {
45 0 : m_inputs.emplace_back(other);
46 0 : return true;
47 : }
48 :
49 0 : bool MixerNode::doRemoveNode(std::shared_ptr<IAudioNode> other)
50 : {
51 0 : muse::remove(m_inputs, other);
52 0 : return true;
53 : }
54 :
55 0 : void MixerNode::process(float* buffer, samples_t samplesPerChannel)
56 : {
57 0 : if (!m_enabled) {
58 0 : return;
59 : }
60 :
61 0 : const audioch_t audioChannelCount = m_outputSpec.audioChannelCount;
62 0 : const size_t outBufferSize = samplesPerChannel * audioChannelCount;
63 :
64 0 : for (auto& input : m_inputs) {
65 : // prepare input buffer
66 0 : if (m_buffer.size() < outBufferSize) {
67 0 : m_buffer.resize(outBufferSize);
68 : }
69 :
70 : // clear previous data
71 0 : std::fill(m_buffer.begin(), m_buffer.begin() + outBufferSize, 0.0f);
72 :
73 : // process input
74 0 : input->process(m_buffer.data(), samplesPerChannel);
75 :
76 : // mix input into output
77 0 : for (samples_t s = 0; s < samplesPerChannel; ++s) {
78 0 : size_t samplePos = s * audioChannelCount;
79 0 : for (audioch_t audioChNum = 0; audioChNum < audioChannelCount; ++audioChNum) {
80 0 : size_t idx = samplePos + audioChNum;
81 0 : buffer[idx] += m_buffer[idx];
82 : }
83 : }
84 : }
85 : }
86 :
87 0 : void MixerNode::doSelfProcess(float*, samples_t)
88 : {
89 : // noop
90 0 : }
|