NAP
audionodemanager.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 <mutex>
9 #include <set>
10 
11 // Nap includes
12 #include <utility/threading.h>
13 #include <nap/signalslot.h>
14 
15 // Audio includes
16 #include <audio/utility/audiotypes.h>
17 #include <audio/core/process.h>
18 
19 namespace nap
20 {
21  namespace audio
22  {
23 
24  // Forward declarations
25  class Node;
26 
33  class NAPAPI NodeManager final
34  {
35  friend class Process;
36  friend class OutputNode;
37  friend class InputNode;
38 
39  public:
40  using OutputMapping = std::vector<std::vector<SampleBuffer*>>;
41 
42  public:
43  NodeManager(DeletionQueue& deletionQueue) : mDeletionQueue(deletionQueue) { }
44 
45  ~NodeManager();
46 
54  void process(float** inputBuffer, float** outputBuffer, unsigned long framesPerBuffer);
55 
63  void process(std::vector<SampleBuffer*>& inputBuffer, std::vector<SampleBuffer*>& outputBuffer,
64  unsigned long framesPerBuffer);
65 
70 
76  void enqueueTask(nap::TaskQueue::Task task);
77 
81  int getInputChannelCount() const { return mInputChannelCount; }
82 
86  int getOutputChannelCount() const { return mOutputChannelCount; }
87 
91  float getSampleRate() const { return mSampleRate; }
92 
96  float getSamplesPerMillisecond() const { return mSamplesPerMillisecond; }
97 
104  int getInternalBufferSize() const { return mInternalBufferSize; }
105 
109  const DiscreteTimeValue& getSampleTime() const { return mSampleTime; }
110 
115  void setInputChannelCount(int inputChannelCount);
116 
121  void setOutputChannelCount(int outputChannelCount);
122 
127  void setSampleRate(float sampleRate);
128 
136  void setInternalBufferSize(int size);
137 
143  void registerRootProcess(Process& rootProcess);
144 
145 
151  void unregisterRootProcess(Process& rootProcess);
152 
160  template<typename T, typename... Args>
161  SafeOwner<T> makeSafe(Args&& ... args)
162  {
163  auto owner = SafeOwner<T>(mDeletionQueue, new T(std::forward<Args>(args)...));
164  return owner;
165  }
166 
174  template<typename T>
176  {
177  auto owner = SafeOwner<T>(mDeletionQueue, ptr);
178  return owner;
179  }
180 
184  DeletionQueue& getDeletionQueue() { return mDeletionQueue; }
185 
190 
191 
192  private:
193  // Used by the nodes and audio processes to register themselves on construction
194  void registerProcess(Process& process);
195 
196  // Used by the nodes and audio processes to unregister themselves on destruction
197  void unregisterProcess(Process& process);
198 
199  private:
200  /*
201  * Used by @OutputNode to provide new output for the node system
202  * Note: multiple output buffers can be provided for the same channel by different output nodes.
203  * @param buffer: a buffer of output
204  * @param channel: the channel that the output will be played on
205  */
206  void provideOutputBufferForChannel(SampleBuffer* buffer, int channel);
207 
208  /*
209  * Used by @InputNode to request audio input for the current buffer
210  * @param channel: the input channel that is being monitored
211  * @param index: the index of the requested sample within the buffer currently being calculated.
212  * TODO: optimize by returning a float array for the whole buffer
213  */
214  const SampleValue& getInputSample(int channel, int index) const
215  {
216  return mInputBuffer[channel][mInternalBufferOffset + index];
217  }
218 
219  int mInputChannelCount = 0; // Number of input channels this node manager processes
220  int mOutputChannelCount = 0; // Number of channel this node manager outputs
221  float mSampleRate = 0; // Current sample rate the node manager runs on.
222  float mSamplesPerMillisecond = 0; // cached here for optimization purpose
223  int mInternalBufferSize = 64; // The internal buffersize that the node manager runs on.
224 
225  DiscreteTimeValue mSampleTime = 0; // the actual sample time clock of the audio system
226  unsigned int mInternalBufferOffset = 0; // helper variable for the process method
227 
228  // for each output channel a vector of buffers that need to be played back on the corresponding channel
229  OutputMapping mOutputMapping;
230 
231  std::vector<float*> mInputBuffer; // Pointing to the audio input that this node manager has to process. The format is a non-interleaved array containing a float array for each channel.
232 
233  std::set<Process*> mProcesses; // all the audio processes managed by this node manager
234  std::set<Process*> mRootProcesses; // the nodes that will be processed directly by the manager on every audio callback
235 
236  nap::TaskQueue mTaskQueue = { 256 }; // Queue with lambda functions to be executed before processing the next internal buffer.
237  DeletionQueue& mDeletionQueue; // Deletion queue used to safely create and destruct nodes in a threadsafe manner.
238  };
239 
240  }
241 }
nap::audio::NodeManager::getOutputChannelCount
int getOutputChannelCount() const
Definition: audionodemanager.h:86
nap::audio::NodeManager::getSampleRate
float getSampleRate() const
Definition: audionodemanager.h:91
nap::audio::DeletionQueue
Definition: safeptr.h:74
nap::audio::NodeManager::makeSafe
SafeOwner< T > makeSafe(Args &&... args)
Definition: audionodemanager.h:161
nap::audio::NodeManager::getSampleTime
const DiscreteTimeValue & getSampleTime() const
Definition: audionodemanager.h:109
nap::audio::Process
Definition: process.h:36
nap::audio::OutputNode
Definition: outputnode.h:23
nap::audio::NodeManager::getDeletionQueue
DeletionQueue & getDeletionQueue()
Definition: audionodemanager.h:184
nap::audio::SampleValue
float SampleValue
Definition: audiotypes.h:28
nap::Signal< DiscreteTimeValue >
nap::audio::NodeManager::mChannelCountChangedSignal
Signal< NodeManager & > mChannelCountChangedSignal
Definition: audionodemanager.h:189
nap::audio::NodeManager
Definition: audionodemanager.h:33
nap::audio::SafeOwner
Definition: safeptr.h:25
nap::TaskQueue
Definition: threading.h:22
nap::audio::NodeManager::NodeManager
NodeManager(DeletionQueue &deletionQueue)
Definition: audionodemanager.h:43
nap::audio::NodeManager::getInternalBufferSize
int getInternalBufferSize() const
Definition: audionodemanager.h:104
nap::TaskQueue::Task
std::function< void()> Task
Definition: threading.h:26
nap::audio::DiscreteTimeValue
nap::uint64 DiscreteTimeValue
Definition: audiotypes.h:125
nap::audio::NodeManager::getInputChannelCount
int getInputChannelCount() const
Definition: audionodemanager.h:81
nap
Definition: templateapp.h:17
nap::audio::NodeManager::OutputMapping
std::vector< std::vector< SampleBuffer * > > OutputMapping
Definition: audionodemanager.h:40
nap::audio::NodeManager::getSamplesPerMillisecond
float getSamplesPerMillisecond() const
Definition: audionodemanager.h:96
nap::audio::InputNode
Definition: inputnode.h:23
nap::audio::NodeManager::mUpdateSignal
Signal< DiscreteTimeValue > mUpdateSignal
Definition: audionodemanager.h:69
nap::audio::SampleBuffer
std::vector< SampleValue > SampleBuffer
Definition: audiotypes.h:34
nap::audio::NodeManager::makeSafe
SafeOwner< T > makeSafe(T *ptr)
Definition: audionodemanager.h:175