sfFDN
Loading...
Searching...
No Matches
audio_processor.h
1// Copyright (C) 2025 Alexandre St-Onge
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "attributes.h"
6#include "audio_buffer.h"
7
8#include <nlohmann/json.hpp>
9
10#include <cstdint>
11#include <memory>
12#include <vector>
13
14namespace sfFDN
15{
24{
25 public:
26 AudioProcessor() = default;
27 virtual ~AudioProcessor() = default;
28
33 virtual void Process(const AudioBuffer& input, AudioBuffer& output) noexcept SFFDN_NONBLOCKING = 0;
34
36 virtual uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING = 0;
37
39 virtual uint32_t OutputChannelCount() const noexcept SFFDN_NONBLOCKING = 0;
40
46 virtual void Clear() = 0;
47
51 virtual std::unique_ptr<AudioProcessor> Clone() const = 0;
52
53 protected:
54 // Polymorphic copying goes through Clone(). These are protected rather than public so that a
55 // derived class still gets its implicit copy and move operations, while slicing an
56 // AudioProcessor out of a derived object stays impossible. Declaring them here also stops the
57 // destructor above from suppressing the implicit move operations of derived classes.
58 AudioProcessor(const AudioProcessor&) = default;
59 AudioProcessor& operator=(const AudioProcessor&) = default;
60 AudioProcessor(AudioProcessor&&) noexcept = default;
61 AudioProcessor& operator=(AudioProcessor&&) noexcept = default;
62};
63
68{
69 public:
74 AudioProcessorChain(uint32_t block_size);
75
81 bool AddProcessor(std::unique_ptr<AudioProcessor>&& processor);
82
86 uint32_t GetProcessorCount() const;
87
92 AudioProcessor* GetProcessor(uint32_t index) const;
93
102 void Process(const AudioBuffer& input, AudioBuffer& output) noexcept SFFDN_NONBLOCKING override;
103
105 uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override;
106
108 uint32_t OutputChannelCount() const noexcept SFFDN_NONBLOCKING override;
109
111 void Clear() override;
112
116 std::unique_ptr<AudioProcessor> Clone() const override;
117
118 private:
119 uint32_t block_size_ = 0;
120 std::vector<std::unique_ptr<AudioProcessor>> processors_;
121
122 std::vector<float> work_buffer_a_;
123 std::vector<float> work_buffer_b_;
124 uint32_t max_work_buffer_size_ = 0;
125
126 void ProcessInternal(const AudioBuffer& input, AudioBuffer& output) noexcept SFFDN_NONBLOCKING;
127};
128} // namespace sfFDN
A class representing an audio buffer with multiple channels of non-interleaved audio data.
Definition audio_buffer.h:20
A chain of audio processors that processes audio sequentially.
Definition audio_processor.h:68
AudioProcessorChain(uint32_t block_size)
Constructs an AudioProcessorChain with a specified block size.
void Process(const AudioBuffer &input, AudioBuffer &output) noexcept SFFDN_NONBLOCKING override
Processes the audio buffers through the chain.
uint32_t GetProcessorCount() const
Returns the number of processors in the chain.
bool AddProcessor(std::unique_ptr< AudioProcessor > &&processor)
Adds an audio processor to the chain.
AudioProcessor * GetProcessor(uint32_t index) const
Returns a pointer to the processor at the specified index.
uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING override
Returns the number of input channels this processor expects.
Base class for audio processors.
Definition audio_processor.h:24
virtual uint32_t OutputChannelCount() const noexcept SFFDN_NONBLOCKING=0
Returns the number of output channels this processor produces.
virtual std::unique_ptr< AudioProcessor > Clone() const =0
Creates a copy of the audio processor.
virtual void Clear()=0
Clears the internal state of the processor.
virtual uint32_t InputChannelCount() const noexcept SFFDN_NONBLOCKING=0
Returns the number of input channels this processor expects.
virtual void Process(const AudioBuffer &input, AudioBuffer &output) noexcept SFFDN_NONBLOCKING=0
Process audio buffers.