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  public:
36  InputPinBase(Node* node);
37 
38  virtual ~InputPinBase();
39 
43  virtual void connect(OutputPin& input) = 0;
44 
48  virtual void disconnect(OutputPin& input) = 0;
49 
53  virtual void disconnectAll() = 0;
54 
58  virtual bool isConnected() const = 0;
59 
64  void enqueueConnect(OutputPin& pin);
65 
70  void enqueueDisconnect(OutputPin& pin);
71 
75  Node& getNode() { return *mNode; }
76 
77  private:
78  // The node that owns this input
79  Node* mNode = nullptr;
80  };
81 
82 
87  class NAPAPI InputPin final : public InputPinBase
88  {
89  RTTI_ENABLE(InputPinBase)
90 
91  friend class OutputPin;
92 
93  public:
94  InputPin(Node* node) : InputPinBase(node) { }
95 
99  virtual ~InputPin() override;
100 
105  SampleBuffer* pull();
106 
112  void connect(OutputPin& input) override;
113 
114 
118  void disconnect(OutputPin& input) override;
119 
120 
124  void disconnectAll() override;
125 
129  bool isConnected() const override { return mInput != nullptr; }
130 
131  private:
132  /*
133  * The audio output connected to this input.
134  * When it is a nullptr this input is not connected.
135  */
136  OutputPin* mInput = nullptr;
137  };
138 
139 
145  class NAPAPI MultiInputPin final : public InputPinBase
146  {
147  RTTI_ENABLE(InputPinBase)
148 
149  public:
150  MultiInputPin(Node* node, unsigned int reservedInputCount = 2);
151 
152  virtual ~MultiInputPin() override;
153 
162  void pull(std::vector<SampleBuffer*>& result);
163 
168  void connect(OutputPin& input) override;
169 
174  void disconnect(OutputPin& input) override;
175 
179  void disconnectAll() override;
180 
184  bool isConnected() const override { return !mInputs.empty(); }
185 
190  void reserveInputs(unsigned int inputCount);
191 
192  private:
193  std::vector<OutputPin*> mInputs;
194  std::vector<OutputPin*> mInputsCache;
195  };
196 
197 
204  class NAPAPI OutputPin final
205  {
206  RTTI_ENABLE()
207 
208  friend class Node;
209  friend class InputPinBase;
210  friend class InputPin;
211  friend class MultiInputPin;
212 
213  public:
217  OutputPin(Node* node);
218 
219  ~OutputPin();
220 
224  void disconnectAll();
225 
229  bool isConnected() const
230  { return !mOutputs.empty(); }
231 
236  SampleBuffer* pull();
237 
241  Node& getNode() { return *mNode; }
242 
243  protected:
245 
246  private:
247  // Used by the NodeManager to resize the internal buffers when necessary
248  void setBufferSize(int bufferSize);
249 
250  // The node that owns this output
251  Node* mNode = nullptr;
252 
253  // The inputs that this output is connected to
254  // This list is kept so the connections can be broken on destruction.
255  std::set<InputPinBase*> mOutputs;
256  };
257 
258  }
259 
260 }
nap::audio::InputPinBase::getNode
Node & getNode()
Definition: audiopin.h:75
nap::audio::MultiInputPin
Definition: audiopin.h:145
nap::audio::InputPinBase
Definition: audiopin.h:32
nap::audio::OutputPin
Definition: audiopin.h:204
nap::audio::Node
Definition: audionode.h:33
nap::audio::InputPin
Definition: audiopin.h:87
nap
Definition: templateapp.h:17
nap::audio::OutputPin::mBuffer
SampleBuffer mBuffer
The buffer containing the latest output.
Definition: audiopin.h:244
nap::audio::OutputPin::getNode
Node & getNode()
Definition: audiopin.h:241
nap::audio::MultiInputPin::isConnected
bool isConnected() const override
Definition: audiopin.h:184
nap::audio::SampleBuffer
std::vector< SampleValue > SampleBuffer
Definition: audiotypes.h:34
nap::audio::InputPin::isConnected
bool isConnected() const override
Definition: audiopin.h:129