Vital
Loading...
Searching...
No Matches
wave_frame.h
Go to the documentation of this file.
1#pragma once
2
3#include "common.h"
4
5#include <complex>
6
7namespace vital {
8
16 class WaveFrame {
17 public:
19 static constexpr int kWaveformBits = 11;
21 static constexpr int kWaveformSize = 1 << kWaveformBits;
23 static constexpr int kNumRealComplex = kWaveformSize / 2 + 1;
27 static constexpr float kDefaultFrequencyRatio = 1.0f;
29 static constexpr float kDefaultSampleRate = 44100.0f;
30
38
45
52 void normalize(bool allow_positive_gain = false);
53
59 void clear();
60
66 void setFrequencyRatio(float ratio) { frequency_ratio = ratio; }
67
73 void setSampleRate(float rate) { sample_rate = rate; }
74
80 void multiply(mono_float value);
81
87 void loadTimeDomain(float* buffer);
88
94 void addFrom(WaveFrame* source);
95
101 void copy(const WaveFrame* other);
102
106 void toFrequencyDomain();
107
111 void toTimeDomain();
112
119 void removedDc();
120
121 int index;
125 std::complex<float> frequency_domain[kWaveformSize];
126
132 float* getFrequencyData() { return reinterpret_cast<float*>(frequency_domain); }
133
134 JUCE_LEAK_DETECTOR(WaveFrame)
135 };
136
144 public:
155
160
167 static const WaveFrame* getWaveFrame(Shape shape) { return &instance()->wave_frames_[shape]; }
168
169 private:
175 static const PredefinedWaveFrames* instance() {
176 static const PredefinedWaveFrames wave_frames;
177 return &wave_frames;
178 }
179
180 void createSin(WaveFrame& wave_frame);
181 void createSaturatedSin(WaveFrame& wave_frame);
182 void createTriangle(WaveFrame& wave_frame);
183 void createSquare(WaveFrame& wave_frame);
184 void createPulse(WaveFrame& wave_frame);
185 void createSaw(WaveFrame& wave_frame);
186
187 WaveFrame wave_frames_[kNumShapes];
188 };
189} // namespace vital
Holds a set of predefined WaveFrame shapes that can be used as basic building blocks.
Definition wave_frame.h:143
PredefinedWaveFrames()
Constructs the PredefinedWaveFrames, initializing all predefined shapes.
Definition wave_frame.cpp:103
static const WaveFrame * getWaveFrame(Shape shape)
Retrieves a pointer to a WaveFrame representing a predefined shape.
Definition wave_frame.h:167
Shape
Supported predefined shapes.
Definition wave_frame.h:146
@ kSaturatedSin
Definition wave_frame.h:148
@ kTriangle
Definition wave_frame.h:149
@ kSquare
Definition wave_frame.h:150
@ kSaw
Definition wave_frame.h:152
@ kPulse
Definition wave_frame.h:151
@ kNumShapes
Definition wave_frame.h:153
@ kSin
Definition wave_frame.h:147
Represents a single frame of a wavetable, containing both time-domain and frequency-domain data.
Definition wave_frame.h:16
static constexpr int kWaveformBits
The number of bits that define the size of the waveform (2^kWaveformBits = kWaveformSize).
Definition wave_frame.h:19
void copy(const WaveFrame *other)
Copies another WaveFrame's time and frequency domain data into this one.
Definition wave_frame.cpp:57
static constexpr float kDefaultSampleRate
The default sample rate for a WaveFrame.
Definition wave_frame.h:29
void setSampleRate(float rate)
Sets the sample rate for this wave frame.
Definition wave_frame.h:73
float sample_rate
The sample rate associated with this frame.
Definition wave_frame.h:123
void setFrequencyRatio(float ratio)
Sets the frequency ratio for this wave frame.
Definition wave_frame.h:66
void addFrom(WaveFrame *source)
Adds another WaveFrame's data to this one, sample-by-sample, in both time and frequency domains.
Definition wave_frame.cpp:50
float frequency_ratio
The frequency ratio for this frame (e.g., for pitch scaling).
Definition wave_frame.h:122
std::complex< float > frequency_domain[kWaveformSize]
The frequency-domain representation (complex spectrum).
Definition wave_frame.h:125
void toFrequencyDomain()
Converts the currently loaded time-domain data into frequency-domain representation.
Definition wave_frame.cpp:64
static constexpr int kWaveformSize
The size of the waveform (number of samples per frame).
Definition wave_frame.h:21
static constexpr float kDefaultFrequencyRatio
The default frequency ratio for a WaveFrame (usually 1.0f).
Definition wave_frame.h:27
mono_float time_domain[2 *kWaveformSize]
The time-domain data, extended buffer size for FFT alignment.
Definition wave_frame.h:124
static constexpr int kNumExtraComplex
The number of "extra" complex bins to pad after the real frequency components.
Definition wave_frame.h:25
int index
The index of this frame in a wavetable.
Definition wave_frame.h:121
void toTimeDomain()
Converts the currently loaded frequency-domain data into time-domain representation.
Definition wave_frame.cpp:77
WaveFrame()
Constructs a WaveFrame with default frequency ratio and sample rate.
Definition wave_frame.h:36
float * getFrequencyData()
Gets a pointer to the frequency-domain data interpreted as floats.
Definition wave_frame.h:132
void removedDc()
Removes the DC offset from the waveform.
Definition wave_frame.cpp:90
static constexpr int kNumRealComplex
The number of real-valued frequency components (half the size + 1).
Definition wave_frame.h:23
mono_float getMaxZeroOffset() const
Retrieves the maximum absolute amplitude in the time-domain waveform.
Definition wave_frame.cpp:30
void multiply(mono_float value)
Multiplies all samples in both time and frequency domains by a given value.
Definition wave_frame.cpp:16
void loadTimeDomain(float *buffer)
Loads time-domain data from a given buffer and updates the frequency domain accordingly.
Definition wave_frame.cpp:23
void normalize(bool allow_positive_gain=false)
Normalizes the time-domain waveform samples to have a maximum absolute value of 1....
Definition wave_frame.cpp:38
void clear()
Clears the waveform data, resetting it to default states.
Definition wave_frame.cpp:7
Contains classes and functions used within the Vital synthesizer framework.
float mono_float
Definition common.h:33