NAP
audiopin.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Std includes
8 #include <set>
9 #include <mutex>
10 
11 // RTTI includes
12 #include <rtti/rtti.h>
13 
14 // Audio includes
15 #include <audio/utility/audiotypes.h>
16 
17 
18 namespace nap
19 {
20  namespace audio
21  {
22 
23  // Forward declarations
24  class Node;
25  class InputPin;
26  class OutputPin;
27 
28 
32  class NAPAPI InputPinBase
33  {
34  RTTI_ENABLE()
35 
36  friend class OutputPin;
37 
38  public:
39  InputPinBase(Node* node);
40 
41  virtual ~InputPinBase();
42 
47  void connect(OutputPin& input);
48 
53  void disconnect(OutputPin& input);
54 
59  void disconnectAll();
60 
64  Node& getNode() { return *mNode; }
65 
66  private:
70  virtual void connectNow(OutputPin& input) = 0;
71 
75  virtual void disconnectNow(OutputPin& input) = 0;
76 
80  virtual void disconnectAllNow() = 0;
81 
82  // The node that owns this input
83  Node* mNode = nullptr;
84  };
85 
86 
91  class NAPAPI InputPin final : public InputPinBase
92  {
93  RTTI_ENABLE(InputPinBase)
94 
95  friend class OutputPin;
96 
97  public:
98  InputPin(Node* node) : InputPinBase(node) { }
99 
103  virtual ~InputPin() override;
104 
109  SampleBuffer* pull();
110 
111  private:
117  void connectNow(OutputPin& input) override;
118 
122  void disconnectNow(OutputPin& input) override;
123 
127  void disconnectAllNow() override;
128 
133  OutputPin* mInput = nullptr;
134  };
135 
136 
142  class NAPAPI MultiInputPin final : public InputPinBase
143  {
144  RTTI_ENABLE(InputPinBase)
145 
146  public:
147  MultiInputPin(Node* node, unsigned int reservedInputCount = 2);
148 
149  virtual ~MultiInputPin() override;
150 
159  void pull(std::vector<SampleBuffer*>& result);
160 
161 
166  void reserveInputs(unsigned int inputCount);
167 
168  private:
173  void connectNow(OutputPin& input) override;
174 
179  void disconnectNow(OutputPin& input) override;
180 
184  void disconnectAllNow() override;
185 
186  std::vector<OutputPin*> mInputs;
187  std::vector<OutputPin*> mInputsCache;
188  };
189 
190 
197  class NAPAPI OutputPin final
198  {
199  RTTI_ENABLE()
200 
201  friend class Node;
202  friend class InputPinBase;
203  friend class InputPin;
204  friend class MultiInputPin;
205 
206  public:
210  OutputPin(Node* node);
211 
212  ~OutputPin();
213 
218  void disconnectAll();
219 
224  SampleBuffer* pull();
225 
229  Node& getNode() { return *mNode; }
230 
231  protected:
233 
234  private:
238  void disconnectAllNow();
239 
240  // Used by the NodeManager to resize the internal buffers when necessary
241  void setBufferSize(int bufferSize);
242 
243  // The node that owns this output
244  Node* mNode = nullptr;
245 
246  // The inputs that this output is connected to
247  // This list is kept so the connections can be broken on destruction.
248  std::set<InputPinBase*> mOutputs;
249  };
250 
251  }
252 
253 }
nap::audio::MultiInputPin
Definition: audiopin.h:142
nap::audio::InputPinBase
Definition: audiopin.h:32
nap::audio::OutputPin
Definition: audiopin.h:197
nap::audio::Node
Definition: audionode.h:33
nap::audio::InputPin
Definition: audiopin.h:91
nap
Definition: templateapp.h:17
nap::audio::OutputPin::mBuffer
SampleBuffer mBuffer
The buffer containing the latest output.
Definition: audiopin.h:232
nap::audio::SampleBuffer
std::vector< SampleValue > SampleBuffer
Definition: audiotypes.h:34