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 :
23 : #pragma once
24 :
25 : #include "audionode.h"
26 :
27 : #include <vector>
28 :
29 : #include "global/containers.h"
30 :
31 : namespace muse::audio {
32 : //! NOTE Just combines several nodes into one entity
33 : template<typename T>
34 : class ChainNode : public AudioNode<T>
35 : {
36 : public:
37 :
38 : void add(IAudioNodePtr node);
39 : void remove(IAudioNodePtr node);
40 : void clear();
41 :
42 : protected:
43 :
44 0 : void doSelfProcess(float*, samples_t) override {}
45 :
46 : void doAdd(IAudioNodePtr node);
47 : virtual void rebuild();
48 :
49 : std::vector<IAudioNodePtr> m_nodes;
50 : };
51 :
52 : template<typename T>
53 0 : void ChainNode<T>::rebuild()
54 : {
55 0 : IAudioNodePtr next = this->shared_from_this();
56 0 : for (auto& node : m_nodes) {
57 0 : node->disconnectAll();
58 0 : node->connect(next);
59 0 : next = node;
60 : }
61 0 : }
62 :
63 : template<typename T>
64 : void ChainNode<T>::add(IAudioNodePtr node)
65 : {
66 : IF_ASSERT_FAILED(node) {
67 : return;
68 : }
69 :
70 : doAdd(node);
71 : rebuild();
72 : }
73 :
74 : template<typename T>
75 0 : void ChainNode<T>::doAdd(IAudioNodePtr node)
76 : {
77 0 : IF_ASSERT_FAILED(node) {
78 0 : return;
79 : }
80 :
81 0 : node->setOutputSpec(AudioNode<T>::outputSpec());
82 0 : node->setMode(AudioNode<T>::mode());
83 0 : m_nodes.push_back(node);
84 : }
85 :
86 : template<typename T>
87 : void ChainNode<T>::remove(IAudioNodePtr node)
88 : {
89 : IF_ASSERT_FAILED(node) {
90 : return;
91 : }
92 :
93 : bool removed = muse::remove(m_nodes, node);
94 : if (removed) {
95 : node->disconnectAll();
96 : rebuild();
97 : }
98 : }
99 :
100 : template<typename T>
101 0 : void ChainNode<T>::clear()
102 : {
103 0 : for (auto& node : m_nodes) {
104 0 : node->disconnectAll();
105 : }
106 0 : m_nodes.clear();
107 0 : }
108 : }
|