Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 18 | * FlowGraph.h |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 19 | * |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 20 | * Processing node and ports that can be used in a simple data flow graph. |
| 21 | * This was designed to work with audio but could be used for other |
| 22 | * types of data. |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 23 | */ |
| 24 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 25 | #ifndef FLOWGRAPH_FLOW_GRAPH_NODE_H |
| 26 | #define FLOWGRAPH_FLOW_GRAPH_NODE_H |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 27 | |
| 28 | #include <cassert> |
| 29 | #include <cstring> |
| 30 | #include <math.h> |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 31 | #include <memory> |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 32 | #include <sys/types.h> |
| 33 | #include <time.h> |
| 34 | #include <unistd.h> |
Phil Burk | 22f2113 | 2019-03-08 12:43:19 -0800 | [diff] [blame] | 35 | #include <vector> |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 36 | |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 37 | // TODO Move these classes into separate files. |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 38 | // TODO Review use of raw pointers for connect(). Maybe use smart pointers but need to avoid |
| 39 | // run-time deallocation in audio thread. |
| 40 | |
Phil Burk | 51ca755 | 2019-01-04 10:59:13 -0800 | [diff] [blame] | 41 | // Set this to 1 if using it inside the Android framework. |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 42 | // This code is kept here so that it can be moved easily between Oboe and AAudio. |
Phil Burk | 9425ffd | 2020-07-09 13:21:09 -0700 | [diff] [blame] | 43 | #ifndef FLOWGRAPH_ANDROID_INTERNAL |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 44 | #define FLOWGRAPH_ANDROID_INTERNAL 0 |
Phil Burk | 9425ffd | 2020-07-09 13:21:09 -0700 | [diff] [blame] | 45 | #endif |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 46 | |
Phil Burk | 9425ffd | 2020-07-09 13:21:09 -0700 | [diff] [blame] | 47 | // Set this to a name that will prevent AAudio from calling into Oboe. |
Phil Burk | 82f422f | 2020-07-14 16:46:37 -0700 | [diff] [blame] | 48 | // AAudio and Oboe both use a version of this flowgraph package. |
| 49 | // There was a problem in the unit tests where AAudio would call a constructor |
| 50 | // in AAudio and then call a destructor in Oboe! That caused memory corruption. |
| 51 | // For more details, see Issue #930. |
Phil Burk | 9425ffd | 2020-07-09 13:21:09 -0700 | [diff] [blame] | 52 | #ifndef FLOWGRAPH_OUTER_NAMESPACE |
| 53 | #define FLOWGRAPH_OUTER_NAMESPACE oboe |
| 54 | #endif |
| 55 | |
| 56 | namespace FLOWGRAPH_OUTER_NAMESPACE { |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 57 | namespace flowgraph { |
| 58 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 59 | // Default block size that can be overridden when the FlowGraphPortFloat is created. |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 60 | // If it is too small then we will have too much overhead from switching between nodes. |
| 61 | // If it is too high then we will thrash the caches. |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 62 | constexpr int kDefaultBufferSize = 8; // arbitrary |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 63 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 64 | class FlowGraphPort; |
| 65 | class FlowGraphPortFloatInput; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 66 | |
| 67 | /***************************************************************************/ |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 68 | /** |
| 69 | * Base class for all nodes in the flowgraph. |
| 70 | */ |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 71 | class FlowGraphNode { |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 72 | public: |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 73 | FlowGraphNode() {} |
| 74 | virtual ~FlowGraphNode() = default; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 75 | |
| 76 | /** |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 77 | * Read from the input ports, |
| 78 | * generate multiple frames of data then write the results to the output ports. |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 79 | * |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 80 | * @param numFrames maximum number of frames requested for processing |
| 81 | * @return number of frames actually processed |
| 82 | */ |
Phil Burk | 22f2113 | 2019-03-08 12:43:19 -0800 | [diff] [blame] | 83 | virtual int32_t onProcess(int32_t numFrames) = 0; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 84 | |
| 85 | /** |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 86 | * If the callCount is at or after the previous callCount then call |
| 87 | * pullData on all of the upstreamNodes. |
| 88 | * Then call onProcess(). |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 89 | * This prevents infinite recursion in case of cyclic graphs. |
| 90 | * It also prevents nodes upstream from a branch from being executed twice. |
| 91 | * |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 92 | * @param callCount |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 93 | * @param numFrames |
| 94 | * @return number of frames valid |
| 95 | */ |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 96 | int32_t pullData(int32_t numFrames, int64_t callCount); |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 97 | |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Recursively reset all the nodes in the graph, starting from a Sink. |
Phil Burk | 41b868c | 2019-08-28 15:39:05 -0700 | [diff] [blame] | 100 | * |
| 101 | * This must not be called at the same time as pullData! |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 102 | */ |
| 103 | void pullReset(); |
| 104 | |
| 105 | /** |
| 106 | * Reset framePosition counters. |
| 107 | */ |
| 108 | virtual void reset(); |
| 109 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 110 | void addInputPort(FlowGraphPort &port) { |
Phil Burk | 22f2113 | 2019-03-08 12:43:19 -0800 | [diff] [blame] | 111 | mInputPorts.push_back(port); |
| 112 | } |
| 113 | |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 114 | bool isDataPulledAutomatically() const { |
| 115 | return mDataPulledAutomatically; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Set true if you want the data pulled through the graph automatically. |
| 120 | * This is the default. |
| 121 | * |
Phil Burk | 41b868c | 2019-08-28 15:39:05 -0700 | [diff] [blame] | 122 | * Set false if you want to pull the data from the input ports in the onProcess() method. |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 123 | * You might do this, for example, in a sample rate converting node. |
| 124 | * |
| 125 | * @param automatic |
| 126 | */ |
| 127 | void setDataPulledAutomatically(bool automatic) { |
| 128 | mDataPulledAutomatically = automatic; |
| 129 | } |
| 130 | |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 131 | virtual const char *getName() { |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 132 | return "FlowGraph"; |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 135 | int64_t getLastCallCount() { |
| 136 | return mLastCallCount; |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 139 | protected: |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 140 | |
| 141 | static constexpr int64_t kInitialCallCount = -1; |
| 142 | int64_t mLastCallCount = kInitialCallCount; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 143 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 144 | std::vector<std::reference_wrapper<FlowGraphPort>> mInputPorts; |
Phil Burk | 22f2113 | 2019-03-08 12:43:19 -0800 | [diff] [blame] | 145 | |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 146 | private: |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 147 | bool mDataPulledAutomatically = true; |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 148 | bool mBlockRecursion = false; |
| 149 | int32_t mLastFrameCount = 0; |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 150 | |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | /***************************************************************************/ |
| 154 | /** |
| 155 | * This is a connector that allows data to flow between modules. |
Phil Burk | 72356eb | 2019-01-09 10:35:51 -0800 | [diff] [blame] | 156 | * |
| 157 | * The ports are the primary means of interacting with a module. |
| 158 | * So they are generally declared as public. |
| 159 | * |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 160 | */ |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 161 | class FlowGraphPort { |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 162 | public: |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 163 | FlowGraphPort(FlowGraphNode &parent, int32_t samplesPerFrame) |
Phil Burk | 2da4a84 | 2019-08-16 12:15:23 -0700 | [diff] [blame] | 164 | : mContainingNode(parent) |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 165 | , mSamplesPerFrame(samplesPerFrame) { |
| 166 | } |
jimmytobin2425 | f66d162 | 2020-10-08 15:31:46 -0700 | [diff] [blame] | 167 | |
| 168 | virtual ~FlowGraphPort() = default; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 169 | |
| 170 | // Ports are often declared public. So let's make them non-copyable. |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 171 | FlowGraphPort(const FlowGraphPort&) = delete; |
| 172 | FlowGraphPort& operator=(const FlowGraphPort&) = delete; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 173 | |
| 174 | int32_t getSamplesPerFrame() const { |
| 175 | return mSamplesPerFrame; |
| 176 | } |
| 177 | |
Phil Burk | 22f2113 | 2019-03-08 12:43:19 -0800 | [diff] [blame] | 178 | virtual int32_t pullData(int64_t framePosition, int32_t numFrames) = 0; |
| 179 | |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 180 | virtual void pullReset() {} |
| 181 | |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 182 | protected: |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 183 | FlowGraphNode &mContainingNode; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 184 | |
| 185 | private: |
| 186 | const int32_t mSamplesPerFrame = 1; |
| 187 | }; |
| 188 | |
| 189 | /***************************************************************************/ |
| 190 | /** |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 191 | * This port contains a 32-bit float buffer that can contain several frames of data. |
| 192 | * Processing the data in a block improves performance. |
| 193 | * |
| 194 | * The size is framesPerBuffer * samplesPerFrame). |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 195 | */ |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 196 | class FlowGraphPortFloat : public FlowGraphPort { |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 197 | public: |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 198 | FlowGraphPortFloat(FlowGraphNode &parent, |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 199 | int32_t samplesPerFrame, |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 200 | int32_t framesPerBuffer = kDefaultBufferSize |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 201 | ); |
| 202 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 203 | virtual ~FlowGraphPortFloat() = default; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 204 | |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 205 | int32_t getFramesPerBuffer() const { |
| 206 | return mFramesPerBuffer; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | protected: |
| 210 | |
| 211 | /** |
| 212 | * @return buffer internal to the port or from a connected port |
| 213 | */ |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 214 | virtual float *getBuffer() { |
| 215 | return mBuffer.get(); |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 218 | private: |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 219 | const int32_t mFramesPerBuffer = 1; |
| 220 | std::unique_ptr<float[]> mBuffer; // allocated in constructor |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | /***************************************************************************/ |
| 224 | /** |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 225 | * The results of a node's processing are stored in the buffers of the output ports. |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 226 | */ |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 227 | class FlowGraphPortFloatOutput : public FlowGraphPortFloat { |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 228 | public: |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 229 | FlowGraphPortFloatOutput(FlowGraphNode &parent, int32_t samplesPerFrame) |
| 230 | : FlowGraphPortFloat(parent, samplesPerFrame) { |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 233 | virtual ~FlowGraphPortFloatOutput() = default; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 234 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 235 | using FlowGraphPortFloat::getBuffer; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 236 | |
| 237 | /** |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 238 | * Connect to the input of another module. |
| 239 | * An input port can only have one connection. |
| 240 | * An output port can have multiple connections. |
| 241 | * If you connect a second output port to an input port |
| 242 | * then it overwrites the previous connection. |
| 243 | * |
Phil Burk | 51ca755 | 2019-01-04 10:59:13 -0800 | [diff] [blame] | 244 | * This not thread safe. Do not modify the graph topology from another thread while running. |
| 245 | * Also do not delete a module while it is connected to another port if the graph is running. |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 246 | */ |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 247 | void connect(FlowGraphPortFloatInput *port); |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 248 | |
| 249 | /** |
| 250 | * Disconnect from the input of another module. |
| 251 | * This not thread safe. |
| 252 | */ |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 253 | void disconnect(FlowGraphPortFloatInput *port); |
Phil Burk | 22f2113 | 2019-03-08 12:43:19 -0800 | [diff] [blame] | 254 | |
| 255 | /** |
| 256 | * Call the parent module's onProcess() method. |
| 257 | * That may pull data from its inputs and recursively |
| 258 | * process the entire graph. |
| 259 | * @return number of frames actually pulled |
| 260 | */ |
| 261 | int32_t pullData(int64_t framePosition, int32_t numFrames) override; |
| 262 | |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 263 | |
| 264 | void pullReset() override; |
| 265 | |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | /***************************************************************************/ |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 269 | |
| 270 | /** |
| 271 | * An input port for streaming audio data. |
| 272 | * You can set a value that will be used for processing. |
| 273 | * If you connect an output port to this port then its value will be used instead. |
| 274 | */ |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 275 | class FlowGraphPortFloatInput : public FlowGraphPortFloat { |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 276 | public: |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 277 | FlowGraphPortFloatInput(FlowGraphNode &parent, int32_t samplesPerFrame) |
| 278 | : FlowGraphPortFloat(parent, samplesPerFrame) { |
Phil Burk | 22f2113 | 2019-03-08 12:43:19 -0800 | [diff] [blame] | 279 | // Add to parent so it can pull data from each input. |
| 280 | parent.addInputPort(*this); |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 283 | virtual ~FlowGraphPortFloatInput() = default; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 284 | |
| 285 | /** |
| 286 | * If connected to an output port then this will return |
| 287 | * that output ports buffers. |
| 288 | * If not connected then it returns the input ports own buffer |
| 289 | * which can be loaded using setValue(). |
| 290 | */ |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 291 | float *getBuffer() override; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 292 | |
| 293 | /** |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 294 | * Write every value of the float buffer. |
| 295 | * This value will be ignored if an output port is connected |
| 296 | * to this port. |
| 297 | */ |
| 298 | void setValue(float value) { |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 299 | int numFloats = kDefaultBufferSize * getSamplesPerFrame(); |
| 300 | float *buffer = getBuffer(); |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 301 | for (int i = 0; i < numFloats; i++) { |
| 302 | *buffer++ = value; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Connect to the output of another module. |
| 308 | * An input port can only have one connection. |
| 309 | * An output port can have multiple connections. |
| 310 | * This not thread safe. |
| 311 | */ |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 312 | void connect(FlowGraphPortFloatOutput *port) { |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 313 | assert(getSamplesPerFrame() == port->getSamplesPerFrame()); |
| 314 | mConnected = port; |
| 315 | } |
| 316 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 317 | void disconnect(FlowGraphPortFloatOutput *port) { |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 318 | assert(mConnected == port); |
| 319 | (void) port; |
| 320 | mConnected = nullptr; |
| 321 | } |
| 322 | |
| 323 | void disconnect() { |
| 324 | mConnected = nullptr; |
| 325 | } |
| 326 | |
Phil Burk | 22f2113 | 2019-03-08 12:43:19 -0800 | [diff] [blame] | 327 | /** |
| 328 | * Pull data from any output port that is connected. |
| 329 | */ |
| 330 | int32_t pullData(int64_t framePosition, int32_t numFrames) override; |
| 331 | |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 332 | void pullReset() override; |
| 333 | |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 334 | private: |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 335 | FlowGraphPortFloatOutput *mConnected = nullptr; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | /***************************************************************************/ |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 339 | |
| 340 | /** |
| 341 | * Base class for an edge node in a graph that has no upstream nodes. |
| 342 | * It outputs data but does not consume data. |
| 343 | * By default, it will read its data from an external buffer. |
| 344 | */ |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 345 | class FlowGraphSource : public FlowGraphNode { |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 346 | public: |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 347 | explicit FlowGraphSource(int32_t channelCount) |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 348 | : output(*this, channelCount) { |
| 349 | } |
| 350 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 351 | virtual ~FlowGraphSource() = default; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 352 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 353 | FlowGraphPortFloatOutput output; |
Phil Burk | 561cda3 | 2019-07-11 12:40:52 -0700 | [diff] [blame] | 354 | }; |
| 355 | |
| 356 | /***************************************************************************/ |
| 357 | |
| 358 | /** |
| 359 | * Base class for an edge node in a graph that has no upstream nodes. |
| 360 | * It outputs data but does not consume data. |
| 361 | * By default, it will read its data from an external buffer. |
| 362 | */ |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 363 | class FlowGraphSourceBuffered : public FlowGraphSource { |
Phil Burk | 561cda3 | 2019-07-11 12:40:52 -0700 | [diff] [blame] | 364 | public: |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 365 | explicit FlowGraphSourceBuffered(int32_t channelCount) |
| 366 | : FlowGraphSource(channelCount) {} |
Phil Burk | 561cda3 | 2019-07-11 12:40:52 -0700 | [diff] [blame] | 367 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 368 | virtual ~FlowGraphSourceBuffered() = default; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 369 | |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 370 | /** |
| 371 | * Specify buffer that the node will read from. |
| 372 | * |
| 373 | * @param data TODO Consider using std::shared_ptr. |
| 374 | * @param numFrames |
| 375 | */ |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 376 | void setData(const void *data, int32_t numFrames) { |
| 377 | mData = data; |
| 378 | mSizeInFrames = numFrames; |
| 379 | mFrameIndex = 0; |
| 380 | } |
| 381 | |
| 382 | protected: |
| 383 | const void *mData = nullptr; |
| 384 | int32_t mSizeInFrames = 0; // number of frames in mData |
| 385 | int32_t mFrameIndex = 0; // index of next frame to be processed |
| 386 | }; |
| 387 | |
| 388 | /***************************************************************************/ |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 389 | /** |
| 390 | * Base class for an edge node in a graph that has no downstream nodes. |
| 391 | * It consumes data but does not output data. |
| 392 | * This graph will be executed when data is read() from this node |
| 393 | * by pulling data from upstream nodes. |
| 394 | */ |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 395 | class FlowGraphSink : public FlowGraphNode { |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 396 | public: |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 397 | explicit FlowGraphSink(int32_t channelCount) |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 398 | : input(*this, channelCount) { |
| 399 | } |
| 400 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 401 | virtual ~FlowGraphSink() = default; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 402 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 403 | FlowGraphPortFloatInput input; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 404 | |
| 405 | /** |
| 406 | * Dummy processor. The work happens in the read() method. |
| 407 | * |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 408 | * @param numFrames |
| 409 | * @return number of frames actually processed |
| 410 | */ |
Phil Burk | 22f2113 | 2019-03-08 12:43:19 -0800 | [diff] [blame] | 411 | int32_t onProcess(int32_t numFrames) override { |
| 412 | return numFrames; |
Phil Burk | 84e7e65 | 2019-05-13 16:29:15 -0700 | [diff] [blame] | 413 | } |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 414 | |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 415 | virtual int32_t read(void *data, int32_t numFrames) = 0; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 416 | |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 417 | protected: |
| 418 | /** |
| 419 | * Pull data through the graph using this nodes last callCount. |
| 420 | * @param numFrames |
| 421 | * @return |
| 422 | */ |
| 423 | int32_t pullData(int32_t numFrames); |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 424 | }; |
| 425 | |
Phil Burk | f5b3b81 | 2019-06-04 07:51:07 -0700 | [diff] [blame] | 426 | /***************************************************************************/ |
| 427 | /** |
Phil Burk | 2da4a84 | 2019-08-16 12:15:23 -0700 | [diff] [blame] | 428 | * Base class for a node that has an input and an output with the same number of channels. |
Phil Burk | f5b3b81 | 2019-06-04 07:51:07 -0700 | [diff] [blame] | 429 | * This may include traditional filters, eg. FIR, but also include |
| 430 | * any processing node that converts input to output. |
| 431 | */ |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 432 | class FlowGraphFilter : public FlowGraphNode { |
Phil Burk | f5b3b81 | 2019-06-04 07:51:07 -0700 | [diff] [blame] | 433 | public: |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 434 | explicit FlowGraphFilter(int32_t channelCount) |
Phil Burk | f5b3b81 | 2019-06-04 07:51:07 -0700 | [diff] [blame] | 435 | : input(*this, channelCount) |
| 436 | , output(*this, channelCount) { |
| 437 | } |
| 438 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 439 | virtual ~FlowGraphFilter() = default; |
Phil Burk | f5b3b81 | 2019-06-04 07:51:07 -0700 | [diff] [blame] | 440 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 441 | FlowGraphPortFloatInput input; |
| 442 | FlowGraphPortFloatOutput output; |
Phil Burk | f5b3b81 | 2019-06-04 07:51:07 -0700 | [diff] [blame] | 443 | }; |
| 444 | |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 445 | } /* namespace flowgraph */ |
Phil Burk | 2c8f281 | 2020-07-13 10:55:04 -0700 | [diff] [blame] | 446 | } /* namespace FLOWGRAPH_OUTER_NAMESPACE */ |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 447 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 448 | #endif /* FLOWGRAPH_FLOW_GRAPH_NODE_H */ |