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