Vital
Loading...
Searching...
No Matches
shepard_tone_source.cpp
Go to the documentation of this file.
1/*
2Summary:
3ShepardToneSource uses a single keyframe and a special looping technique to produce a continuously rising (or falling) tone—an auditory illusion known as a Shepard tone. By rearranging frequency components into a loop frame and interpolating between these frames, this source can create a stable, looping sonic texture that simulates infinite pitch movement.
4 */
5
8
10 // Create a loop frame to facilitate continuous looping of the frequency domain spectrum.
11 loop_frame_ = std::make_unique<WaveSourceKeyframe>();
12}
13
15
16void ShepardToneSource::render(vital::WaveFrame* wave_frame, float position) {
17 if (numFrames() == 0)
18 return;
19
20 // Retrieve the single keyframe that holds the base spectrum.
21 WaveSourceKeyframe* keyframe = getKeyframe(0);
22 vital::WaveFrame* key_wave_frame = keyframe->wave_frame();
23 vital::WaveFrame* loop_wave_frame = loop_frame_->wave_frame();
24
25 // Interleave frequency components in a pattern (e.g., placing them at every even index)
26 // to produce a continuous looping effect in the frequency domain.
27 for (int i = 0; i < vital::WaveFrame::kWaveformSize / 2; ++i) {
28 loop_wave_frame->frequency_domain[i * 2] = key_wave_frame->frequency_domain[i];
29 loop_wave_frame->frequency_domain[i * 2 + 1] = 0.0f;
30 }
31
32 loop_wave_frame->toTimeDomain();
33
34 // Use compute_frame_ (inherited from WaveSource) to interpolate between keyframe and loop_frame_
35 // based on the given position, producing a stable Shepard tone-like result.
36 compute_frame_->setInterpolationMode(interpolation_mode_);
37 compute_frame_->interpolate(keyframe, loop_frame_.get(), position / (vital::kNumOscillatorWaveFrames - 1.0f));
38 wave_frame->copy(compute_frame_->wave_frame());
39}
40
virtual WavetableComponentFactory::ComponentType getType() override
Returns the component type associated with this source.
Definition shepard_tone_source.cpp:41
ShepardToneSource()
Constructs a ShepardToneSource with a loop frame for seamless frequency content.
Definition shepard_tone_source.cpp:9
virtual ~ShepardToneSource()
Definition shepard_tone_source.cpp:14
std::unique_ptr< WaveSourceKeyframe > loop_frame_
A loop frame used to create the repetitive Shepard effect.
Definition shepard_tone_source.h:54
virtual void render(vital::WaveFrame *wave_frame, float position) override
Renders a frame of the Shepard tone wavetable at a given position.
Definition shepard_tone_source.cpp:16
std::unique_ptr< WaveSourceKeyframe > compute_frame_
A keyframe for intermediate interpolation computations.
Definition wave_source.h:79
InterpolationMode interpolation_mode_
The mode of interpolation.
Definition wave_source.h:80
WaveSourceKeyframe * getKeyframe(int index)
Retrieves a WaveSourceKeyframe by index.
Definition wave_source.cpp:51
A keyframe that holds a single WaveFrame and supports various interpolation methods.
Definition wave_source.h:92
vital::WaveFrame * wave_frame()
Provides direct access to the stored WaveFrame.
Definition wave_source.h:107
ComponentType
Enumerates all known WavetableComponents, including sources and modifiers.
Definition wavetable_component_factory.h:28
@ kShepardToneSource
Definition wavetable_component_factory.h:33
int numFrames() const
Gets the number of keyframes.
Definition wavetable_component.h:155
Represents a single frame of a wavetable, containing both time-domain and frequency-domain data.
Definition wave_frame.h:16
void copy(const WaveFrame *other)
Copies another WaveFrame's time and frequency domain data into this one.
Definition wave_frame.cpp:57
std::complex< float > frequency_domain[kWaveformSize]
The frequency-domain representation (complex spectrum).
Definition wave_frame.h:125
static constexpr int kWaveformSize
The size of the waveform (number of samples per frame).
Definition wave_frame.h:21
void toTimeDomain()
Converts the currently loaded frequency-domain data into time-domain representation.
Definition wave_frame.cpp:77
constexpr int kNumOscillatorWaveFrames
Number of wave frames in each oscillator’s wavetable.
Definition synth_constants.h:19