NAP
process.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 <vector>
9 
10 // RTTI includes
11 #include <rtti/rtti.h>
12 
13 // Nap includes
14 #include <utility/threading.h>
15 
16 // Audio includes
17 #include <audio/utility/asyncobserver.h>
18 #include <audio/utility/audiotypes.h>
19 #include <audio/utility/safeptr.h>
20 
21 namespace nap
22 {
23  namespace audio
24  {
25 
26  // Forward declarations
27  class NodeManager;
28  class Node;
29  class ParentProcess;
30 
36  class NAPAPI Process
37  {
38  RTTI_ENABLE()
39 
40  friend class NodeManager;
41 
42  public:
47  Process(NodeManager& nodeManager);
48 
52  Process(ParentProcess& parent);
53 
54  /*
55  * We need to delete these so that the compiler doesn't try to use them. Otherwise we get compile errors on vector<unique_ptr>.
56  */
57  Process(const Process&) = delete;
58  Process& operator=(const Process&) = delete;
59 
60  virtual ~Process();
61 
62  /*
63  * Invoked by OutputPin::pull() methods and by parent processes.
64  * Makes sure all the output pins of this node are filled with a sample buffer up to the current time stamp.
65  * In practice calls process() call if the process is not up to date.
66  */
67  void update();
68 
72  NodeManager& getNodeManager() const { return *mNodeManager; }
73 
79  int getBufferSize() const;
80 
84  float getSampleRate() const;
85 
89  DiscreteTimeValue getSampleTime() const;
90 
91  protected:
97  virtual void sampleRateChanged(float sampleRate) { }
98 
104  virtual void bufferSizeChanged(int bufferSize) { }
105 
109  bool isRegisteredWithNodeManager() const { return mRegisteredWithNodeManager.load(); }
110 
111  private:
115  virtual void process() = 0;
116 
117  /*
118  * Used by the node manager to notify the node that the buffer size has changed.
119  * @param bufferSize the new value
120  */
121  virtual void setBufferSize(int bufferSize) { bufferSizeChanged(bufferSize); }
122 
123  /*
124  * Used by the node manager to notify the node that the sample rate has changed.
125  * @param sampleRate: the new value
126  */
127  void setSampleRate(float sampleRate) { sampleRateChanged(sampleRate); }
128 
129  NodeManager* mNodeManager = nullptr; // The node manager that this process is processed on
130  DiscreteTimeValue mLastCalculatedSample = 0; // The time stamp of the latest calculated sample by this node
131 
132  // Set to true when the node has made itself known with the node manager. This registration is deferred to the audio thread so it has to be tracked by this boolean.
133  std::atomic<bool> mRegisteredWithNodeManager = { false };
134  };
135 
136 
143  class NAPAPI ParentProcess : public Process
144  {
145  RTTI_ENABLE(Process)
146 
147  public:
148  enum class Mode
149  {
150  Sequential, Parallel
151  };
152 
153  public:
160  ParentProcess(NodeManager& nodeManager, ThreadPool& threadPool, AsyncObserver& observer) : Process(
161  nodeManager), mThreadPool(threadPool), mAsyncObserver(observer)
162  {}
163 
167  ParentProcess(ParentProcess& parent) : Process(parent), mThreadPool(parent.mThreadPool),
168  mAsyncObserver(parent.mAsyncObserver)
169  { }
170 
174  void addChild(Process& child);
175 
179  void removeChild(Process& child);
180 
185  void processParallel();
186 
190  void processSequential();
191 
195  void process() override;
196 
202  void setMode(Mode mode) { mMode.store(mode); }
203 
207  Mode getMode() const { return mMode.load(); }
208 
209  private:
210  ThreadPool& mThreadPool;
211  AsyncObserver& mAsyncObserver;
212  std::vector<Process*> mChildren;
213  std::atomic<Mode> mMode = {Mode::Sequential};
214  };
215 
216 
217  }
218 }
nap::audio::ParentProcess::ParentProcess
ParentProcess(NodeManager &nodeManager, ThreadPool &threadPool, AsyncObserver &observer)
Definition: process.h:160
nap::audio::AsyncObserver
Definition: asyncobserver.h:24
nap::audio::ParentProcess::setMode
void setMode(Mode mode)
Definition: process.h:202
nap::audio::Process
Definition: process.h:36
nap::audio::ParentProcess
Definition: process.h:143
nap::audio::Process::isRegisteredWithNodeManager
bool isRegisteredWithNodeManager() const
Definition: process.h:109
nap::audio::ParentProcess::ParentProcess
ParentProcess(ParentProcess &parent)
Definition: process.h:167
nap::audio::NodeManager
Definition: audionodemanager.h:33
nap::audio::Process::bufferSizeChanged
virtual void bufferSizeChanged(int bufferSize)
Definition: process.h:104
nap::audio::DiscreteTimeValue
nap::uint64 DiscreteTimeValue
Definition: audiotypes.h:125
nap::audio::Process::sampleRateChanged
virtual void sampleRateChanged(float sampleRate)
Definition: process.h:97
nap
Definition: templateapp.h:17
nap::ThreadPool
Definition: threading.h:109
nap::audio::ParentProcess::getMode
Mode getMode() const
Definition: process.h:207
nap::audio::ParentProcess::Mode
Mode
Definition: process.h:148