AME
ame_Delay.hpp
Go to the documentation of this file.
1
10#pragma once
11
12#include "ame_AudioBuffer.hpp"
13#include "ame_Math.hpp"
14#include "ame_Util.hpp"
15
16#include <array>
17#include <cassert>
18#include <cmath>
19
20namespace ame::dsp
21{
26template <size_t MaximumChannels, size_t MaximumDelayInSamples>
27class Delay
28{
29public:
30 Delay()
31 {
32 readPos.changeLength (MaximumChannels * MaximumDelayInSamples);
33 writePos.changeLength (MaximumChannels * MaximumDelayInSamples);
34 }
35 ~Delay() = default;
36
41 void setDelay (const float delayInSamples)
42 {
43 assert (delayInSamples <= MaximumDelayInSamples);
44 assert (0 <= delayInSamples);
45
46 fractional = delayInSamples - static_cast<uint_fast32_t> (delayInSamples);
47 readPos.set (writePos.get (-delayInSamples));
48 }
49
51 template <typename SampleType, size_t N>
53 {
54 assert (block.getNumChannels() <= MaximumChannels);
55
56 uint_fast32_t i = 0;
57 for (uint_fast32_t samp = 0; samp < block.getNumSamplesPerChannel(); ++samp)
58 {
59 for (uint_fast32_t ch = 0; ch < block.getNumChannels(); ++ch)
60 {
61 const float input = block.view[i];
62 delayLine[ch][writePos.get()] = input;
63 block.view[i] = std::lerp (delayLine[ch][readPos.get()], delayLine[ch][readPos.get (-1)], fractional);
64 ++i;
65 }
66 ++readPos;
67 ++writePos;
68 }
69 }
70
71private:
72 std::array<std::array<float, MaximumDelayInSamples + 1>, MaximumChannels> delayLine {};
73 Wrap<int32_t> readPos {};
74 Wrap<int32_t> writePos {};
75 float fractional = 0.0f;
76};
77} // namespace ame::dsp
Audio buffer.
Math functions.
Some utilities functions.
A lightweight data structure that stores a pointer to an audio buffer.
Definition: ame_AudioBuffer.hpp:32
uint_fast32_t getNumChannels() const noexcept
Returns the number of channels.
Definition: ame_AudioBuffer.hpp:49
uint_fast32_t getNumSamplesPerChannel() const noexcept
Returns the number of samples per channel.
Definition: ame_AudioBuffer.hpp:55
A number to wrap between 0~length.
Definition: ame_Util.hpp:254
Fractional delay.
Definition: ame_Delay.hpp:28
void process(AudioBlockView< SampleType, N > &block)
Process audio effect.
Definition: ame_Delay.hpp:52
void setDelay(const float delayInSamples)
Set delay time.
Definition: ame_Delay.hpp:41