Vital
Loading...
Searching...
No Matches
decimator.cpp
Go to the documentation of this file.
1#include "decimator.h"
2
4
5namespace vital {
6
14 Decimator::Decimator(int max_stages) : ProcessorRouter(kNumInputs, 1), max_stages_(max_stages) {
15 num_stages_ = -1;
16 for (int i = 0; i < max_stages_; ++i) {
18 stage->setOversampleAmount(1 << (max_stages_ - i - 1)); // e.g., 4x, 2x, etc.
19 addProcessor(stage);
20 stages_.push_back(stage);
21 }
22 }
23
28
36 stages_[0]->useInput(input(kAudio));
37 stages_[0]->useOutput(output());
38 for (int i = 1; i < max_stages_; ++i) {
39 stages_[i]->plug(stages_[i - 1], IirHalfbandDecimator::kAudio);
40 stages_[i]->useOutput(output());
41 }
42 }
43
49 void Decimator::reset(poly_mask reset_mask) {
50 for (int i = 0; i < max_stages_; ++i)
51 stages_[i]->reset(reset_mask);
52 }
53
63 void Decimator::process(int num_samples) {
64 int num_stages = 0;
65
66 // Determine how many decimation stages are needed based on sample rates
67 if (input(kAudio)->source->owner) {
68 int input_sample_rate = input(kAudio)->source->owner->getSampleRate();
69 int output_sample_rate = getSampleRate();
70 while(input_sample_rate > output_sample_rate) {
71 num_stages++;
72 input_sample_rate /= 2;
73 }
74
75 VITAL_ASSERT(num_stages <= max_stages_);
76 VITAL_ASSERT(input_sample_rate == output_sample_rate);
77 }
78
79 // If no decimation is needed, pass input directly to output
80 if (num_stages == 0) {
81 utils::copyBuffer(output()->buffer, input(kAudio)->source->buffer, num_samples);
82 return;
83 }
84
85 // If the number of required stages has changed, reset and reconfigure them
86 if (num_stages != num_stages_) {
87 for (int i = 0; i < num_stages; ++i)
88 stages_[i]->reset(constants::kFullMask);
89
90 num_stages_ = num_stages;
91
92 // Enable or disable each stage accordingly
93 for (int i = 0; i < max_stages_; ++i) {
94 IirHalfbandDecimator* stage = stages_[i];
95 bool should_enable = i < num_stages;
96 stage->enable(should_enable);
97 stage->setSharpCutoff(i == num_stages - 1);
98
99 if (should_enable) {
100 int oversample_amount = 1 << (num_stages - i - 1);
101 stage->setOversampleAmount(oversample_amount);
102 }
103 }
104 }
105
106 // Execute standard ProcessorRouter processing, which processes all sub-processors in order
107 ProcessorRouter::process(num_samples);
108 }
109} // namespace vital
Decimator(int max_stages=1)
Constructs a Decimator with a specified maximum number of halfband stages.
Definition decimator.cpp:14
virtual void process(int num_samples) override
Main audio processing routine that checks required decimation stages and processes them.
Definition decimator.cpp:63
void reset(poly_mask reset_mask) override
Resets all decimator stages for specified voices.
Definition decimator.cpp:49
@ kAudio
The main audio input to be decimated.
Definition decimator.h:26
virtual ~Decimator()
Destructor. Cleans up decimator stages.
Definition decimator.cpp:27
void init() override
Initializes the Decimator, hooking up audio connections for each stage.
Definition decimator.cpp:35
An IIR-based half-band decimator for downsampling audio by a factor of 2.
Definition iir_halfband_decimator.h:17
force_inline void setSharpCutoff(bool sharp_cutoff)
Enables or disables the sharper 25-tap cutoff mode.
Definition iir_halfband_decimator.h:83
@ kAudio
Main audio input for decimation.
Definition iir_halfband_decimator.h:44
virtual void setOversampleAmount(int oversample)
Sets the oversampling amount and updates the effective sample rate.
Definition processor.h:293
force_inline Input * input(unsigned int index=0) const
Retrieves the Input pointer at a given index.
Definition processor.h:587
force_inline int getSampleRate() const
Retrieves the current (effective) sample rate.
Definition processor.h:326
force_inline Output * output(unsigned int index=0) const
Retrieves the Output pointer at a given index.
Definition processor.h:616
virtual void enable(bool enable)
Enables or disables this Processor.
Definition processor.h:318
A specialized Processor that manages a directed graph of Processors and ensures correct processing or...
Definition processor_router.h:34
virtual void process(int num_samples) override
Processes audio through all Processors managed by this router.
Definition processor_router.cpp:57
virtual void addProcessor(Processor *processor)
Adds a Processor to be managed by this router.
Definition processor_router.cpp:121
#define VITAL_ASSERT(x)
Definition common.h:11
const poly_mask kFullMask
A mask covering all lanes of a poly_float vector.
Definition synth_constants.h:257
force_inline void copyBuffer(mono_float *dest, const mono_float *source, int size)
Copies data from a source mono buffer to a destination mono buffer.
Definition poly_utils.h:586
Contains classes and functions used within the Vital synthesizer framework.
const Output * source
The output from which this input reads samples.
Definition processor.h:134
Processor * owner
Owning processor.
Definition processor.h:112
Represents a vector of integer values using SIMD instructions.
Definition poly_values.h:56