RavEngine
Loading...
Searching...
No Matches
delay_filter.h
1/*
2Copyright 2018 Google Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS-IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef RESONANCE_AUDIO_DSP_DELAY_FILTER_H_
18#define RESONANCE_AUDIO_DSP_DELAY_FILTER_H_
19
20#include <algorithm>
21#include <memory>
22
23#include "base/audio_buffer.h"
24
25namespace vraudio {
26
27// Single channel delay line class. Delays input buffer data by a non-negative
28// integer number of samples.
29//
30// This implementation is not thread safe. The ClearBuffer() and InsertData()
31// functions should not be called by a seperate thread during GetDelayedData().
33 public:
34 // Constructs a DelayFilter.
35 //
36 // @param max_delay_length Maximum number of samples the input should be
37 // delayed by.
38 // @param frames_per_buffer Number of frames in each processed buffer.
39 DelayFilter(size_t max_delay_length, size_t frames_per_buffer);
40
41 // Sets the maximum delay length. It will allocate more space in the
42 // |delay_line_| if the new |max_delay_length| is more than doubled.
43 //
44 // @param max_delay_length New maximum delay in samples.
45 void SetMaximumDelay(size_t max_delay_length);
46
47 // Returns the current length of the |delay_line_|.
48 size_t GetDelayBufferLength() const { return delay_line_->num_frames(); }
49
50 // Returns the current maximum delay applicable to input buffers.
51 size_t GetMaximumDelayLength() const { return max_delay_length_; }
52
53 // Sets all of the |delay_line_| samples to zero.
54 void ClearBuffer() { delay_line_->Clear(); }
55
56 // Copies an |AudioBuffer::Channel| of data to the delay line.
57 //
58 // @param input Input data.
59 void InsertData(const AudioBuffer::Channel& input);
60
61 // Fills an |AudioBuffer::Channel| with data delayed by a specified amount
62 // less than or equal to the delay line's set |max_delay_length_|.
63 //
64 // @param delay_samples Requested delay to the data extraced from the delay
65 // line. Must be less than or equal to |max_delay_length_|.
66 // @param buffer Pointer to the output data, i.e., delayed input data.
67 void GetDelayedData(size_t delay_samples, AudioBuffer::Channel* buffer);
68
69 private:
70 // Maximum length of the delay to be applied (in samples).
71 size_t max_delay_length_;
72
73 // Number of frames in each AudioBuffer input/output.
74 size_t frames_per_buffer_;
75
76 // The delay line holding all of the delayed samples.
77 std::unique_ptr<AudioBuffer> delay_line_;
78
79 // Position in the delay line to begin writing to.
80 size_t write_cursor_;
81};
82
83} // namespace vraudio
84
85#endif // RESONANCE_AUDIO_DSP_DELAY_FILTER_H_
Definition channel_view.h:30
Definition delay_filter.h:32