sfFDN
Loading...
Searching...
No Matches
oscillator.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
8namespace sfFDN
9{
12{
13 public:
14 Generator() = default;
15 virtual ~Generator() = default;
16
18 virtual void Generate(std::span<float> output) noexcept SFFDN_NONBLOCKING = 0;
19
20 protected:
21 // Protected so derived generators keep their implicit copy and move operations while slicing a
22 // Generator out of a derived object stays impossible.
23 Generator(const Generator&) = default;
24 Generator& operator=(const Generator&) = default;
25 Generator(Generator&&) noexcept = default;
26 Generator& operator=(Generator&&) noexcept = default;
27};
28
32class SineWave : public Generator
33{
34 public:
41 SineWave(float frequency = 0.0f, float initial_phase = 0.0f);
42
44 void ResetPhase();
45
49 void SetFrequency(float frequency);
50
54 void SetAmplitude(float amplitude);
55
59 float GetAmplitude() const;
60
62 float GetAmplitudeNonBlocking() const noexcept SFFDN_NONBLOCKING
63 {
64 return amplitude_;
65 }
66
68 float GetFrequency() const noexcept SFFDN_NONBLOCKING
69 {
70 return phase_increment_;
71 }
72
78 void SetOffset(float offset);
79
83 float GetOffset() const;
84
88 void SetPhaseOffset(float phase_offset);
89
91 float GetPhaseOffset() const noexcept SFFDN_NONBLOCKING
92 {
93 return phase_offset_;
94 }
95
97 float NextOut() const noexcept SFFDN_NONBLOCKING;
98
100 float Tick() noexcept SFFDN_NONBLOCKING;
101
103 void Generate(std::span<float> output) noexcept SFFDN_NONBLOCKING override;
104
110 void Multiply(std::span<const float> input, std::span<float> output) noexcept SFFDN_NONBLOCKING;
111
117 void MultiplyAccumulate(std::span<const float> input, std::span<float> output) noexcept SFFDN_NONBLOCKING;
118
119 private:
120 float phase_;
121 float phase_increment_;
122
123 float amplitude_;
124 float offset_;
125 float phase_offset_;
126};
127
128} // namespace sfFDN
Base class for oscillators and signal generators.
Definition oscillator.h:12
virtual void Generate(std::span< float > output) noexcept SFFDN_NONBLOCKING=0
Fills the output span with generated samples.
A sine wave oscillator.
Definition oscillator.h:33
void SetOffset(float offset)
Sets the DC offset of the sine wave oscillator.
float GetAmplitude() const
Returns the amplitude of the sine wave oscillator.
float GetFrequency() const noexcept SFFDN_NONBLOCKING
Returns the normalized frequency in cycles per sample.
Definition oscillator.h:68
float GetOffset() const
Returns the DC offset of the sine wave oscillator.
SineWave(float frequency=0.0f, float initial_phase=0.0f)
Constructs a sine wave oscillator.
void ResetPhase()
Resets the phase of the sine wave oscillator.
void SetPhaseOffset(float phase_offset)
Sets the phase offset of the sine wave oscillator.
void SetAmplitude(float amplitude)
Sets the amplitude of the sine wave oscillator.
float GetPhaseOffset() const noexcept SFFDN_NONBLOCKING
Returns the normalized phase offset in cycles.
Definition oscillator.h:91
void SetFrequency(float frequency)
Sets the frequency of the sine wave oscillator.
float GetAmplitudeNonBlocking() const noexcept SFFDN_NONBLOCKING
Returns the amplitude for use from a nonblocking processing path.
Definition oscillator.h:62
float NextOut() const noexcept SFFDN_NONBLOCKING
Returns the next output sample without advancing the phase.