Vital
Loading...
Searching...
No Matches
synth_oscillator.h
Go to the documentation of this file.
1#pragma once
2
3#include "spectral_morph.h"
4#include "synth_constants.h"
5#include "utils.h"
6#include "wave_frame.h"
7#include "wavetable.h"
8
9namespace vital {
10
11 class FourierTransform;
12 class Wavetable;
13
22
30 public:
32 static constexpr int kSeed = 0x4;
33
39 int size = (kRandomAmplitudeStages + 1) * (Wavetable::kNumHarmonics + 1) / poly_float::kSize;
40 static RandomValues instance(size);
41 return &instance;
42 }
43
48 const poly_float* buffer() { return data_.get(); }
49
50 private:
55 RandomValues(int num_poly_floats) {
56 data_ = std::make_unique<poly_float[]>(num_poly_floats);
57 utils::RandomGenerator generator(-1.0f, 1.0f);
58 generator.seed(kSeed);
59 for (int i = 0; i < num_poly_floats; ++i)
60 data_[i] = generator.polyNext();
61 }
62
64 std::unique_ptr<poly_float[]> data_;
65 };
66
74 class SynthOscillator : public Processor {
75 public:
76
80 enum {
112 };
113
117 enum {
121 };
122
141
161
179
185 static bool isFirstModulation(int type) {
186 return type == kFmOscillatorA || type == kRmOscillatorA;
187 }
188
194 static bool isSecondModulation(int type) {
195 return type == kFmOscillatorB || type == kRmOscillatorB;
196 }
197
199 static constexpr int kMaxUnison = 16;
201 static constexpr int kPolyPhasePerVoice = kMaxUnison / poly_float::kSize;
203 static constexpr int kNumPolyPhase = kMaxUnison / 2;
205 static constexpr int kNumBuffers = kNumPolyPhase * poly_float::kSize;
207 static constexpr int kSpectralBufferSize = Wavetable::kWaveformSize * 2 / poly_float::kSize + poly_float::kSize;
208
211
259
267 static void setDistortionValues(DistortionType distortion_type,
268 poly_float* values, int num_values, bool spread);
269
277 static void setSpectralMorphValues(SpectralMorph spectral_morph,
278 poly_float* values, int num_values, bool spread);
279
289 static void runSpectralMorph(SpectralMorph morph_type, float morph_amount,
290 const Wavetable::WavetableData* wavetable_data,
291 int wavetable_index, poly_float* dest, FourierTransform* transform);
292
301 static vital::poly_int adjustPhase(DistortionType distortion_type, poly_int phase,
302 poly_float distortion_amount, poly_int distortion_phase);
303
311 static vital::poly_float getPhaseWindow(DistortionType distortion_type, poly_int phase,
312 poly_int distorted_phase);
313
320 static poly_float interpolate(const mono_float* buffer, const poly_int indices);
321
327 static bool usesDistortionPhase(DistortionType distortion_type);
328
333 SynthOscillator(Wavetable* wavetable);
334
340 void reset(poly_mask reset_mask, poly_int sample);
341
346 void reset(poly_mask reset_mask) override;
347
352 void setSpectralMorphValues(SpectralMorph spectral_morph);
353
358 void setDistortionValues(DistortionType distortion_type);
359
364 void process(int num_samples) override;
365
370 Processor* clone() const override { return new SynthOscillator(*this); }
371
376 void setFirstOscillatorOutput(Output* oscillator) { first_mod_oscillator_ = oscillator; }
377
382 void setSecondOscillatorOutput(Output* oscillator) { second_mod_oscillator_ = oscillator; }
383
388 void setSampleOutput(Output* sample) { sample_ = sample; }
389
394 void setOversampleAmount(int oversample) override {
396 phase_inc_buffer_->ensureBufferSize(oversample * kMaxBufferSize);
397 }
398
399 private:
407 template<poly_int(*phaseDistort)(poly_int, poly_float, poly_int, const poly_float*, int),
408 poly_float(*window)(poly_int, poly_int, poly_float, const poly_float*, int)>
409 void processOscillators(int num_samples, DistortionType distortion_type);
410
419 template<poly_int(*phaseDistort)(poly_int, poly_float, poly_int, const poly_float*, int),
420 poly_float(*window)(poly_int, poly_int, poly_float, const poly_float*, int)>
421 void processChunk(poly_float current_center_amplitude, poly_float current_detuned_amplitude);
422
428 void processBlend(int num_samples, poly_mask reset_mask);
429
436 void loadVoiceBlock(VoiceBlock& voice_block, int index, poly_mask active_mask);
437
441 void resetWavetableBuffers();
442
447 void setActiveOscillators(int new_active_oscillators);
448
458 template<poly_float(*snapTranspose)(poly_float, poly_float, float*)>
459 void setPhaseIncBufferSnap(int num_samples, poly_mask reset_mask,
460 poly_int trigger_sample, poly_mask active_mask, float* snap_buffer);
461
469 void setPhaseIncBuffer(int num_samples, poly_mask reset_mask,
470 poly_int trigger_sample, poly_mask active_mask);
471
475 void setPhaseIncMults();
476
480 void setupShepardWrap();
481
485 void clearShepardWrap();
486
492 void doShepardWrap(poly_mask new_buffer_mask, int quantize);
493
497 void setAmplitude();
498
504 void setWaveBuffers(poly_float phase_inc, int index);
505
517 template<void(*spectralMorph)(const Wavetable::WavetableData*, int, poly_float*,
518 FourierTransform*, float, int, const poly_float*)>
519 void computeSpectralWaveBufferPair(int phase_update, int index, bool formant_shift,
520 float phase_adjustment, poly_int wave_index,
521 poly_float voice_increment, poly_float morph_amount);
522
530 template<void(*spectralMorph)(const Wavetable::WavetableData*, int, poly_float*,
531 FourierTransform*, float, int, const poly_float*)>
532 void setFourierWaveBuffers(poly_float phase_inc, int index, bool formant_shift);
533
540 void stereoBlend(poly_float* audio_out, int num_samples, poly_mask reset_mask);
541
549 void levelOutput(poly_float* audio_out, const poly_float* raw_out, int num_samples, poly_mask reset_mask);
550
557 void convertVoiceChannels(int num_samples, poly_float* audio_out, poly_mask active_mask);
558
564 force_inline float getPhaseIncAdjustment() {
565 static constexpr int kBaseSampleRate = 44100;
566
567 float adjustment = 1.0f;
568 int sample_rate_mult = getSampleRate() / kBaseSampleRate;
569 while (sample_rate_mult > 1) {
570 sample_rate_mult >>= 1;
571 adjustment *= 2.0f;
572 }
573 return adjustment;
574 }
575
577 poly_int phases_[kNumPolyPhase];
579 poly_float detunings_[kNumPolyPhase];
581 poly_float phase_inc_mults_[kNumPolyPhase];
583 poly_float from_phase_inc_mults_[kNumPolyPhase];
585 poly_int shepard_double_masks_[kNumPolyPhase];
587 poly_int shepard_half_masks_[kNumPolyPhase];
589 poly_int waiting_shepard_double_masks_[kNumPolyPhase];
591 poly_int waiting_shepard_half_masks_[kNumPolyPhase];
592
594 poly_float pan_amplitude_;
595 poly_float center_amplitude_;
596 poly_float detuned_amplitude_;
597 poly_float midi_note_;
598 poly_float distortion_phase_;
599 poly_float blend_stereo_multiply_;
600 poly_float blend_center_multiply_;
601
603 const mono_float* next_buffers_[kNumBuffers];
604 const mono_float* wave_buffers_[kNumBuffers];
605 const mono_float* last_buffers_[kNumBuffers];
606
608 poly_float spectral_morph_values_[kNumPolyPhase];
609 poly_float last_spectral_morph_values_[kNumPolyPhase];
610 poly_float distortion_values_[kNumPolyPhase];
611 poly_float last_distortion_values_[kNumPolyPhase];
612
614 VoiceBlock voice_block_;
615
617 utils::RandomGenerator random_generator_;
618
620 int transpose_quantize_;
622 poly_float last_quantized_transpose_;
624 poly_float last_quantize_ratio_;
626 int unison_;
628 int active_oscillators_;
629
631 Wavetable* wavetable_;
633 int wavetable_version_;
634
636 Output* first_mod_oscillator_;
637 Output* second_mod_oscillator_;
638 Output* sample_;
639
641 poly_float fourier_frames1_[kNumBuffers + 1][kSpectralBufferSize];
642 poly_float fourier_frames2_[kNumBuffers + 1][kSpectralBufferSize];
643
645 std::shared_ptr<FourierTransform> fourier_transform_;
647 std::shared_ptr<Output> phase_inc_buffer_;
649 std::shared_ptr<PhaseBuffer> phase_buffer_;
650
651 JUCE_LEAK_DETECTOR(SynthOscillator)
652 };
653} // namespace vital
A Fourier transform implementation using KissFFT for platforms where other accelerations are unavaila...
Definition fourier_transform.h:210
Base class for all signal-processing units in Vital.
Definition processor.h:212
virtual void setOversampleAmount(int oversample)
Sets the oversampling amount and updates the effective sample rate.
Definition processor.h:293
force_inline int getSampleRate() const
Retrieves the current (effective) sample rate.
Definition processor.h:326
A singleton class that generates and stores random poly_float values.
Definition synth_oscillator.h:29
static constexpr int kSeed
Seed value for internal random generator.
Definition synth_oscillator.h:32
const poly_float * buffer()
Get the internal random data buffer.
Definition synth_oscillator.h:48
static RandomValues * instance()
Retrieve the singleton instance of RandomValues.
Definition synth_oscillator.h:38
A core oscillator processor that generates audio by reading wavetable data with various effects.
Definition synth_oscillator.h:74
void process(int num_samples) override
Processes the oscillator for a given number of samples, writing to the output buffers.
Definition synth_oscillator.cpp:1194
SynthOscillator(Wavetable *wavetable)
Constructs a SynthOscillator with the specified Wavetable.
Definition synth_oscillator.cpp:519
static bool isFirstModulation(int type)
Checks if distortion type uses the first modulation oscillator.
Definition synth_oscillator.h:185
DistortionType
Types of distortion/waveshaping used by the oscillator.
Definition synth_oscillator.h:145
@ kRmOscillatorB
RM using oscillator B.
Definition synth_oscillator.h:157
@ kFmOscillatorA
FM using oscillator A.
Definition synth_oscillator.h:153
@ kFmSample
FM using sample.
Definition synth_oscillator.h:155
@ kFmOscillatorB
FM using oscillator B.
Definition synth_oscillator.h:154
@ kNone
No distortion.
Definition synth_oscillator.h:146
@ kSqueeze
Squeeze distortion.
Definition synth_oscillator.h:151
@ kRmSample
RM using sample.
Definition synth_oscillator.h:158
@ kFormant
Formant shifting.
Definition synth_oscillator.h:148
@ kPulseWidth
Pulse width distortion.
Definition synth_oscillator.h:152
@ kRmOscillatorA
RM using oscillator A.
Definition synth_oscillator.h:156
@ kQuantize
Quantization.
Definition synth_oscillator.h:149
@ kBend
Bend distortion.
Definition synth_oscillator.h:150
@ kNumDistortionTypes
Definition synth_oscillator.h:159
@ kSync
Sync distortion.
Definition synth_oscillator.h:147
static void setDistortionValues(DistortionType distortion_type, poly_float *values, int num_values, bool spread)
Sets distortion values for an array of poly_float, handling unison spread if necessary.
Definition synth_oscillator.cpp:1014
static constexpr int kPolyPhasePerVoice
Number of poly phases used per voice.
Definition synth_oscillator.h:201
static bool usesDistortionPhase(DistortionType distortion_type)
Checks if a given distortion type uses a separate distortion_phase (kSync, kQuantize,...
Definition synth_oscillator.cpp:1189
@ kMidiTrack
MIDI tracking toggle.
Definition synth_oscillator.h:83
@ kPhase
Phase offset.
Definition synth_oscillator.h:92
@ kDistortionAmount
Distortion amount.
Definition synth_oscillator.h:107
@ kRandomPhase
Random phase amount.
Definition synth_oscillator.h:94
@ kStereoSpread
Stereo spread amount.
Definition synth_oscillator.h:96
@ kReset
Manual reset (phase reset)
Definition synth_oscillator.h:109
@ kMidiNote
MIDI note (for pitch)
Definition synth_oscillator.h:82
@ kBlend
Blend between center voice and detuned voices.
Definition synth_oscillator.h:95
@ kSpectralMorphType
Spectral morph type.
Definition synth_oscillator.h:103
@ kDistortionPhase
Phase distortion offset.
Definition synth_oscillator.h:93
@ kTune
Fine tune.
Definition synth_oscillator.h:87
@ kUnisonVoices
Number of unison voices.
Definition synth_oscillator.h:90
@ kDetuneRange
Detune range.
Definition synth_oscillator.h:99
@ kRetrigger
Retrigger mask.
Definition synth_oscillator.h:110
@ kUnisonDistortionSpread
Unison distortion spread.
Definition synth_oscillator.h:101
@ kSpectralUnison
Toggle for spectral unison.
Definition synth_oscillator.h:105
@ kPan
Stereo panning.
Definition synth_oscillator.h:89
@ kTranspose
Transpose amount.
Definition synth_oscillator.h:85
@ kSmoothlyInterpolate
Not used directly in this file, but part of the Vital architecture.
Definition synth_oscillator.h:84
@ kDistortionType
Distortion type.
Definition synth_oscillator.h:106
@ kUnisonDetune
Unison detune amount.
Definition synth_oscillator.h:91
@ kSpectralMorphAmount
Spectral morph amount.
Definition synth_oscillator.h:104
@ kDetunePower
Detune power exponent.
Definition synth_oscillator.h:98
@ kAmplitude
Output amplitude.
Definition synth_oscillator.h:88
@ kUnisonSpectralMorphSpread
Unison spectral morph spread.
Definition synth_oscillator.h:102
@ kUnisonFrameSpread
Unison wavetable frame spread.
Definition synth_oscillator.h:100
@ kStackStyle
Unison stack style.
Definition synth_oscillator.h:97
@ kWaveFrame
Waveform frame selection.
Definition synth_oscillator.h:81
@ kNumInputs
Number of inputs.
Definition synth_oscillator.h:111
@ kActiveVoices
Active voice mask.
Definition synth_oscillator.h:108
@ kTransposeQuantize
Transpose quantize setting.
Definition synth_oscillator.h:86
static vital::poly_float getPhaseWindow(DistortionType distortion_type, poly_int phase, poly_int distorted_phase)
Retrieves a window multiplier (for example, half-sin window in formant mode).
Definition synth_oscillator.cpp:1173
void setSecondOscillatorOutput(Output *oscillator)
Assigns an oscillator Output pointer for FM/RM modulation (second mod oscillator).
Definition synth_oscillator.h:382
void setSampleOutput(Output *sample)
Assigns a sample Output pointer for FM/RM modulation using a sample.
Definition synth_oscillator.h:388
void setOversampleAmount(int oversample) override
Overrides the base Processor method to set oversampling. Ensures internal buffers are large enough.
Definition synth_oscillator.h:394
SpectralMorph
Types of spectral morph effects that can be applied to the wavetable.
Definition synth_oscillator.h:126
@ kSmear
Smear morph.
Definition synth_oscillator.h:132
@ kRandomAmplitudes
Random amplitudes morph.
Definition synth_oscillator.h:133
@ kSkew
Skew morph.
Definition synth_oscillator.h:138
@ kFormScale
Formant scaling.
Definition synth_oscillator.h:129
@ kHarmonicScale
Harmonic scaling.
Definition synth_oscillator.h:130
@ kHighPass
Highpass morph.
Definition synth_oscillator.h:135
@ kInharmonicScale
Inharmonic scaling.
Definition synth_oscillator.h:131
@ kNoSpectralMorph
No spectral morph effect.
Definition synth_oscillator.h:127
@ kLowPass
Lowpass morph.
Definition synth_oscillator.h:134
@ kNumSpectralMorphTypes
Definition synth_oscillator.h:139
@ kShepardTone
Shepard tone morph.
Definition synth_oscillator.h:137
@ kPhaseDisperse
Phase dispersion.
Definition synth_oscillator.h:136
@ kVocode
Vocode morph.
Definition synth_oscillator.h:128
static constexpr int kNumBuffers
Number of buffers to store waveforms, based on the number of poly phases.
Definition synth_oscillator.h:205
Processor * clone() const override
Clones this oscillator, returning a new instance with the same parameters.
Definition synth_oscillator.h:370
void setFirstOscillatorOutput(Output *oscillator)
Assigns an oscillator Output pointer for FM/RM modulation (first mod oscillator).
Definition synth_oscillator.h:376
static void runSpectralMorph(SpectralMorph morph_type, float morph_amount, const Wavetable::WavetableData *wavetable_data, int wavetable_index, poly_float *dest, FourierTransform *transform)
Applies a spectral morph operation (e.g., vocode, smear) directly on a buffer.
Definition synth_oscillator.cpp:1112
static constexpr int kSpectralBufferSize
Size of spectral buffer for Fourier transforms.
Definition synth_oscillator.h:207
UnisonStackType
Ways to stack unison voices for chord or harmonic effects.
Definition synth_oscillator.h:165
@ kNormal
Standard unison.
Definition synth_oscillator.h:166
@ kMinorChord
Minor chord intervals.
Definition synth_oscillator.h:174
@ kNumUnisonStackTypes
Definition synth_oscillator.h:177
@ kOctave
Alternate between the base pitch and +1 octave.
Definition synth_oscillator.h:169
@ kOddHarmonicSeries
Odd harmonic series.
Definition synth_oscillator.h:176
@ kMajorChord
Major chord intervals.
Definition synth_oscillator.h:173
@ kHarmonicSeries
Harmonic series.
Definition synth_oscillator.h:175
@ kCenterDropOctave2
Shift center voice down two octaves.
Definition synth_oscillator.h:168
@ kOctave2
Alternate among the base pitch, +1 octave, +2 octaves.
Definition synth_oscillator.h:170
@ kPowerChord2
Extended power chord intervals.
Definition synth_oscillator.h:172
@ kCenterDropOctave
Shift center voice down an octave.
Definition synth_oscillator.h:167
@ kPowerChord
Power chord intervals.
Definition synth_oscillator.h:171
static void setSpectralMorphValues(SpectralMorph spectral_morph, poly_float *values, int num_values, bool spread)
Sets spectral morph values for an array of poly_float, handling unison spread if necessary.
Definition synth_oscillator.cpp:1070
@ kNumOutputs
Number of outputs.
Definition synth_oscillator.h:120
@ kLevelled
Output after amplitude leveling/panning.
Definition synth_oscillator.h:119
@ kRaw
Unleveled or "raw" output.
Definition synth_oscillator.h:118
static poly_float interpolate(const mono_float *buffer, const poly_int indices)
Performs linear interpolation on a single wave buffer.
Definition synth_oscillator.cpp:1185
static vital::poly_int adjustPhase(DistortionType distortion_type, poly_int phase, poly_float distortion_amount, poly_int distortion_phase)
Adjusts phase for sync, formant, quantize, etc.
Definition synth_oscillator.cpp:1154
void reset(poly_mask reset_mask, poly_int sample)
Resets oscillator state with an offset sample count.
Definition synth_oscillator.cpp:558
static constexpr int kMaxUnison
Maximum number of unison voices.
Definition synth_oscillator.h:199
static const mono_float kStackMultipliers[kNumUnisonStackTypes][kNumPolyPhase]
Precomputed multipliers used for stacking unison voices into intervals.
Definition synth_oscillator.h:210
static constexpr int kNumPolyPhase
Number of poly phases total.
Definition synth_oscillator.h:203
static bool isSecondModulation(int type)
Checks if distortion type uses the second modulation oscillator.
Definition synth_oscillator.h:194
A class representing a wavetable, holding multiple frames of waveforms and their frequency-domain rep...
Definition wavetable.h:20
static constexpr int kNumHarmonics
Number of harmonics in the waveform (half the size plus one).
Definition wavetable.h:29
static constexpr int kWaveformSize
Size of each waveform frame.
Definition wavetable.h:25
A basic random number generator for producing uniform distributions of floats.
Definition utils.h:61
#define force_inline
Definition common.h:23
Contains classes and functions used within the Vital synthesizer framework.
constexpr int kMaxBufferSize
Maximum buffer size for processing.
Definition common.h:39
constexpr int kMaxOversample
Maximum allowed oversampling factor.
Definition common.h:40
float mono_float
Definition common.h:33
Holds and manages a buffer of samples (poly_float) for a Processor's output.
Definition processor.h:35
Holds a buffer of poly_int values for phase information.
Definition synth_oscillator.h:18
poly_int buffer[kMaxBufferSize *kMaxOversample]
Phase buffer array.
Definition synth_oscillator.h:20
A helper struct for loading per-voice data during audio processing.
Definition synth_oscillator.h:216
int total_samples
Total samples processed for this chunk.
Definition synth_oscillator.h:232
const mono_float * to_buffers[poly_float::kSize]
Definition synth_oscillator.h:257
const poly_float * modulation_buffer
Buffer for FM/RM modulation values.
Definition synth_oscillator.h:251
int start_sample
Sample offset to start processing for this voice.
Definition synth_oscillator.h:230
bool smoothing_enabled
Whether wave blending is smoothly interpolated.
Definition synth_oscillator.h:249
poly_float distortion
Current distortion amount.
Definition synth_oscillator.h:243
int end_sample
Sample offset to end processing for this voice.
Definition synth_oscillator.h:231
poly_float from_phase_inc_mult
Previous chunk's phase increment multiplier.
Definition synth_oscillator.h:236
const mono_float * from_buffers[poly_float::kSize]
Buffers used for crossfading from one wave to another.
Definition synth_oscillator.h:256
const poly_float * phase_inc_buffer
Phase increment buffer.
Definition synth_oscillator.h:252
poly_mask shepard_half_mask
Mask indicating voices that should shift by /2.
Definition synth_oscillator.h:239
bool isStatic() const
Checks if the from_buffers and to_buffers are identical (i.e., no crossfade needed).
Definition synth_oscillator.h:226
poly_int last_distortion_phase
Previous chunk's distortion phase.
Definition synth_oscillator.h:242
poly_int phase
Current oscillator phase.
Definition synth_oscillator.h:234
const poly_int * phase_buffer
Phase buffer to add to 'phase' each sample.
Definition synth_oscillator.h:253
int num_buffer_samples
Number of samples to fade between wave buffers.
Definition synth_oscillator.h:246
poly_int distortion_phase
Current distortion phase.
Definition synth_oscillator.h:241
poly_float phase_inc_mult
Current phase increment multiplier.
Definition synth_oscillator.h:235
poly_float last_distortion
Previous chunk's distortion amount.
Definition synth_oscillator.h:244
poly_int current_buffer_sample
Keeps track of fade progress.
Definition synth_oscillator.h:247
poly_mask shepard_double_mask
Mask indicating voices that should shift by *2.
Definition synth_oscillator.h:238
VoiceBlock()
Default constructor. Initializes all values to a safe default.
Definition synth_oscillator.cpp:504
SpectralMorph spectral_morph
The current spectral morph type.
Definition synth_oscillator.h:250
Struct holding all necessary data for the Wavetable, including multiple frames.
Definition wavetable.h:41
Represents a vector of floating-point values using SIMD instructions.
Definition poly_values.h:600
Represents a vector of integer values using SIMD instructions.
Definition poly_values.h:56
Provides various utility functions, classes, and constants for audio, math, and general-purpose opera...