| 1 | /* |
| 2 | Copyright 2018 Google Inc. All Rights Reserved. |
| 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 | #ifndef RESONANCE_AUDIO_NODE_PROCESSING_NODE_H_ |
| 18 | #define RESONANCE_AUDIO_NODE_PROCESSING_NODE_H_ |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "base/audio_buffer.h" |
| 24 | #include "node/publisher_node.h" |
| 25 | #include "node/subscriber_node.h" |
| 26 | |
| 27 | namespace vraudio { |
| 28 | |
| 29 | // Audio processing node that reads from multiple inputs, processes the |
| 30 | // received data and outputs its result. |
| 31 | class ProcessingNode : public Node, |
| 32 | public SubscriberNode<const AudioBuffer*>, |
| 33 | public PublisherNode<const AudioBuffer*> { |
| 34 | public: |
| 35 | typedef SubscriberNode<const AudioBuffer*> SubscriberNodeType; |
| 36 | typedef PublisherNode<const AudioBuffer*> PublisherNodeType; |
| 37 | |
| 38 | // Helper class to manage incoming |AudioBuffer|s. |
| 39 | class NodeInput { |
| 40 | public: |
| 41 | // Constructor. |
| 42 | // |
| 43 | // @param input_vector Vector containing pointers to incoming |
| 44 | // |AudioBuffer|s. |
| 45 | explicit NodeInput(const std::vector<const AudioBuffer*>& input_vector); |
| 46 | |
| 47 | // Returns a nullptr if zero or more than one input buffers are available. |
| 48 | // Otherwise a pointer to the single input |AudioBuffer| is returned. This |
| 49 | // method should be used if only a single input |AudioBuffer| is expected. |
| 50 | // |
| 51 | // @return Pointer to single input |AudioBuffer|. |
| 52 | const AudioBuffer* GetSingleInput() const; |
| 53 | |
| 54 | // Returns vector with input |AudioBuffer|s. |
| 55 | // |
| 56 | // @return Pointer to single input |AudioBuffer|. |
| 57 | const std::vector<const AudioBuffer*>& GetInputBuffers() const; |
| 58 | |
| 59 | // Delete copy constructor. |
| 60 | NodeInput(const NodeInput& that) = delete; |
| 61 | |
| 62 | private: |
| 63 | // Const reference to vector of input |AudioBuffer|s. |
| 64 | const std::vector<const AudioBuffer*>& input_vector_; |
| 65 | }; |
| 66 | |
| 67 | ProcessingNode(); |
| 68 | |
| 69 | // SubscriberNode<InputType> implementation. |
| 70 | void Connect( |
| 71 | const std::shared_ptr<PublisherNodeType>& publisher_node) override; |
| 72 | |
| 73 | // Node implementation. |
| 74 | void Process() final; |
| 75 | bool CleanUp() override; |
| 76 | |
| 77 | // By default, calls to AudioProcess() are skipped in case of empty input |
| 78 | // buffers. This enables this node to process audio buffers in the absence of |
| 79 | // input data (which is needed for instance for a reverberation effect). |
| 80 | void EnableProcessOnEmptyInput(bool enable); |
| 81 | |
| 82 | // Disable copy constructor. |
| 83 | ProcessingNode(const ProcessingNode& that) = delete; |
| 84 | |
| 85 | protected: |
| 86 | // Calls |CleanUp| on all connected input nodes. |
| 87 | void CallCleanUpOnInputNodes(); |
| 88 | |
| 89 | // Pure virtual method to implement the audio processing method. This method |
| 90 | // receives a vector of all input arguments to be processed and requires to |
| 91 | // output a single output buffer. |
| 92 | // |
| 93 | // @param input Input instance to receive pointers to input |AudioBuffer|s. |
| 94 | // @return Returns output data. |
| 95 | virtual const AudioBuffer* AudioProcess(const NodeInput& input) = 0; |
| 96 | |
| 97 | private: |
| 98 | // PublisherNode<OutputType> implementation. |
| 99 | std::shared_ptr<Node> GetSharedNodePtr() final; |
| 100 | Node::Output<const AudioBuffer*>* GetOutput() final; |
| 101 | |
| 102 | // Input stream to poll for incoming data. |
| 103 | Node::Input<const AudioBuffer*> input_stream_; |
| 104 | |
| 105 | // Output stream to write processed data to. |
| 106 | Node::Output<const AudioBuffer*> output_stream_; |
| 107 | |
| 108 | // Flag that indicates if |AudioProcess| should be called in case no input |
| 109 | // data is available. |
| 110 | bool process_on_no_input_; |
| 111 | }; |
| 112 | |
| 113 | } // namespace vraudio |
| 114 | |
| 115 | #endif // RESONANCE_AUDIO_NODE_PROCESSING_NODE_H_ |
| 116 | |