добавлены унисон, дополнительные формы волны и фильтр SVF

реализовано:

1. добавлен второй осциллятор
2. реализован Unison с поддержкой до 7 голосов на один осциллятор
3. добавлено управление количеством Unison Voices
4. реализованы Detune и Stereo Spread для расширения стереообраза
5. добавлено управление уровнем дополнительных голосов Unison (Unison Gain)
6. реализованы 4 формы волны: Sine, Saw, Square и Triangle
7. реализован State Variable Filter (SVF) с режимами Low-Pass, Band-Pass и High-Pass
8. добавлено управление частотой среза (Cutoff) и резонансом (Resonance)
9. реализован пересчёт коэффициентов фильтра при изменении параметров
10. добавлена независимая стереообработка с отдельными экземплярами фильтра для левого и правого каналов
11. интегрирован фильтр в обработку звука каждого Voice и подключён к основному Synth
This commit is contained in:
2026-11-16 00:49:29 +06:00
parent 9b01cf0480
commit 89953541c3
17 changed files with 824 additions and 167 deletions
+5 -2
View File
@@ -2,7 +2,9 @@
#define AUDIO_ENGINE
#include <stdexcept>
#include <array>
#include <iostream>
#include "../external/miniaudio.h"
#include "../include/synth.hpp"
#include "../include/stereo_sample.hpp"
@@ -10,12 +12,13 @@
class AudioEngine{
public:
AudioEngine(Synth& synth) : synth_(synth){
AudioEngine(Synth& synth, Synth& synth2) : synths{&synth, &synth2}{
config = ma_device_config_init(ma_device_type_playback);
config.playback.format = ma_format_f32;
config.playback.channels = 2;
config.sampleRate = 48000;
config.pUserData = this;
config.periodSizeInFrames = 512;
config.dataCallback = data_callback;
result_init = ma_device_init(NULL, &config, &device);
@@ -34,7 +37,7 @@ class AudioEngine{
int stop();
private:
Synth& synth_;
std::array<Synth*, 2> synths;
bool device_is_init = false;
bool device_is_started = false;
+16 -1
View File
@@ -8,7 +8,21 @@ enum class CommandType{
NoteOn,
NoteOff,
PitchBend,
ModWheel
ModWheel,
SetUnisonVoices,
SetStereoSpread,
SetDetuneAmount,
SetUnisonGain,
SetWavetableSin,
SetWavetableSaw,
SetWavetableSquare,
SetWavetableTriangle,
SetFilterCutOff,
SetFilterResonance,
SetFilterOff,
SetLowPassFilter,
SetBandPassFilter,
SetHighPassFilter
};
struct Command{
@@ -17,6 +31,7 @@ struct Command{
uint8_t velocity = 0;
uint16_t pitch_bend = 8192;
uint8_t mod_wheel = 0;
double value = 0.0;
};
#endif
+50
View File
@@ -0,0 +1,50 @@
#ifndef FILTER
#define FILTER
#include <cstdint>
#include <cmath>
#include <numbers>
#include "../include/command.hpp"
class Filter{
public:
Filter(){
mode = CommandType::SetFilterOff;
cutoff = 1000.0;
resonance = 0.707;
frequency_coefficient = 0.0;
low = 0.0;
band = 0.0;
high = 0.0;
damping = 0.0;
sample_rate = 48000;
}
void set_cutoff(double value);
void set_resonance(double value);
void set_filter_off();
void set_low_pass_filter();
void set_band_pass_filter();
void set_high_pass_filter();
void update_coefficients();
double process(double input);
private:
CommandType mode;
double cutoff;
double resonance;
double frequency_coefficient;
double low;
double band;
double high;
double damping;
uint32_t sample_rate;
};
#endif
+3 -2
View File
@@ -5,6 +5,7 @@
#include <iostream>
#include <stdexcept>
#include <cstring>
#include <array>
#include "../include/synth.hpp"
@@ -12,7 +13,7 @@ class MidiInput{
public:
MidiInput(Synth& synth) : synth_(synth){
MidiInput(Synth& synth, Synth& synth2) : synths{&synth, &synth2} {
pitch = 8192;
mod_wheel = 0;
}
@@ -23,7 +24,7 @@ class MidiInput{
private:
RtMidiIn midiIn;
Synth& synth_;
std::array<Synth*, 2> synths;
uint16_t pitch;
uint8_t mod_wheel;
+41
View File
@@ -2,8 +2,40 @@
#define OSCILLATOR
#include <cstdint>
#include <cmath>
#include <cstddef>
#include <numbers>
#include <array>
#include "../include/wavetable.hpp"
struct Oscillator{
double generate(){
if (wavetable != nullptr){
const std::size_t table_size = wavetable->wavetable.size();
double position = (phase / (2.0 * std::numbers::pi)) * table_size;
std::size_t index = static_cast<std::size_t>(position);
std::size_t next_index = (index + 1) % table_size;
double fraction = position - index;
double y0 = wavetable->wavetable[index];
double y1 = wavetable->wavetable[next_index];
double value = y0 + (y1 - y0) * fraction;
phase += (2 * std::numbers::pi * frequency)/(sample_rate);
phase = std::fmod(phase, (2 * std::numbers::pi));
return value;
}
else return 0.0;
}
void set_wavetable(Wavetable* wavetable_){
wavetable = wavetable_;
}
Wavetable* wavetable = nullptr;
double frequency = 440.0;
double amplitude = 0.5;
double phase = 0.0;
@@ -11,4 +43,13 @@ struct Oscillator{
uint32_t sample_rate = 48000;
};
struct UnisonOscillator{
Oscillator oscillator;
double pan = 0.0;
double base_pan = 0.0;
double base_detune = 0.0;
double amplitude = 1.0;
double detune = 0.0;
};
#endif
+1 -1
View File
@@ -13,7 +13,7 @@ class SpscQueue{
SpscQueue(){
head.store(0, std::memory_order_relaxed);
tail.store(0, std::memory_order_relaxed);
buffer.fill({CommandType::None, 0, 0});
buffer.fill(Command{});
}
bool push(const Command&);
+31 -1
View File
@@ -15,6 +15,12 @@ class Synth {
public:
Synth(){
sin_wavetable.generate_sin(32);
saw_wavetable.generate_saw(32);
square_wavetable.generate_square(28);
triangle_wavetable.generate_triangle(40);
pitch_bend_range = 2.0;
pitch_semitones = 0.0;
pitch_multiplier = 1.0;
@@ -28,6 +34,21 @@ class Synth {
void generate_buffer(double* array, int size);
void set_mod_wheel(uint8_t value);
void set_unison_voices(uint8_t value);
void set_unison_gain(double value);
void set_stereo_spread(double value);
void set_detune_amount(double value);
void set_wavetable(Wavetable* wavetable);
void set_wavetable_sin();
void set_wavetable_saw();
void set_wavetable_square();
void set_wavetable_triangle();
void set_filter_cutoff(double value);
void set_filter_resonance(double value);
void set_filter_off();
void set_hpf();
void set_bpf();
void set_lpf();
void pitch_bend(uint16_t value);
double pitch_to_semitones(uint16_t value);
@@ -41,9 +62,18 @@ class Synth {
Voice* find_free_voice();
Voice* find_quietest_voice();
SpscQueue queue;
SpscQueue midi_queue;
SpscQueue gui_queue;
Lfo lfo;
Tables tables;
Wavetable sin_wavetable;
Wavetable saw_wavetable;
Wavetable square_wavetable;
Wavetable triangle_wavetable;
double pitch_bend_range;
double pitch_semitones;
double pitch_multiplier;
+44 -6
View File
@@ -2,35 +2,68 @@
#define VOICE
#include <cstdint>
#include <cstddef>
#include <numbers>
#include <cmath>
#include <algorithm>
#include <random>
#include <array>
#include "../include/envelope.hpp"
#include "../include/oscillator.hpp"
#include "../include/stereo_sample.hpp"
#include "../include/filter.hpp"
class Voice{
public:
Voice(){
oscillators[1].base_detune = -10.0;
oscillators[1].base_pan = -1.0;
oscillators[2].base_detune = 10.0;
oscillators[2].base_pan = 1.0;
oscillators[3].base_detune = -20.0;
oscillators[3].base_pan = -0.6;
oscillators[4].base_detune = 20.0;
oscillators[4].base_pan = 0.6;
oscillators[5].base_detune = -30.0;
oscillators[5].base_pan = -0.3;
oscillators[6].base_detune = 30.0;
oscillators[6].base_pan = 0.3;
note = 0;
unison_voices = 7;
envelope_value = 0.0;
pan = 0.0;
base_frequency = 440.0;
pitch_bend = 1.0;
vibrato = 1.0;
stereo_spread = 1.0;
detune_amount = 1.0;
unison_gain = 1.0;
active = false;
}
void note_on(uint8_t note, uint8_t velocity);
void note_off();
void set_pan(double value);
void set_pitch_bend(double multiplier);
void set_vibrato(double multiplier);
void set_unison_voices(uint8_t value);
void set_stereo_spread(double value);
void set_detune_amount(double value);
void set_unison_gain(double value);
void set_wavetable(Wavetable* Wavetable);
void set_filter_cutoff(double value);
void set_filter_resonance(double value);
void set_filter_off();
void set_hpf();
void set_bpf();
void set_lpf();
void update_frequency();
StereoSample generate();
@@ -43,15 +76,20 @@ class Voice{
private:
Envelope envelope;
Oscillator oscillator;
Filter filter_left;
Filter filter_right;
std::array<UnisonOscillator, 7> oscillators{};
uint8_t note;
uint8_t unison_voices;
double envelope_value;
double pan;
double base_frequency;
double pitch_bend;
double vibrato;
double stereo_spread;
double detune_amount;
double unison_gain;
bool active;
};
+26
View File
@@ -0,0 +1,26 @@
#ifndef WAVETABLE
#define WAVETABLE
#include <array>
#include <cstdint>
#include <numbers>
#include <cmath>
struct Wavetable{
void generate_sin(uint16_t harmonics_);
void generate_saw(uint16_t harmonics_);
void generate_square(uint16_t harmonics_);
void generate_triangle(uint16_t harmonics_);
std::array<double, 2048> wavetable{};
uint16_t harmonics = 0;
};
struct Tables{
Wavetable* select_saw_tables(double frequency);
void generate_saw_tables();
std::array<Wavetable, 6> tables;
};
#endif