From 9b01cf0480bf12cd39ffa57bdf5d4a0fa634fbdb Mon Sep 17 00:00:00 2001 From: s13bby Date: Sun, 15 Nov 2026 12:35:20 +0600 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D1=83=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20Pitch=20Bend=20=D0=B8=20Vibrato=20=D1=87=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B7=20MIDI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit реализовано: 1. обработка MIDI Pitch Bend 2. управление глубиной вибрато через Mod Wheel (CC1) 3. реализован LFO на основе синусоидальной волны 4. добавлена частотная модуляция голосов с помощью LFO 5. реализовано совместное применение Pitch Bend и Vibrato без взаимного перезаписывания эффектов 6. добавлен пересчет частоты осциллятора с учетом всех множителей --- CMakeLists.txt | 1 + include/command.hpp | 4 ++- include/lfo.hpp | 28 ++++++++++++++++++ include/midi_input.hpp | 2 ++ include/synth.hpp | 12 ++++++-- include/voice.hpp | 8 ++++-- src/lfo.cpp | 12 ++++++++ src/midi_input.cpp | 4 +++ src/synth.cpp | 64 +++++++++++++++++++++++++++++------------- src/voice.cpp | 15 ++++++++-- 10 files changed, 121 insertions(+), 29 deletions(-) create mode 100644 include/lfo.hpp create mode 100644 src/lfo.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e903271..bf5f91f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(SOURCES src/synth.cpp src/midi_input.cpp src/voice.cpp + src/lfo.cpp ) add_executable(${PROJECT_NAME} ${SOURCES}) diff --git a/include/command.hpp b/include/command.hpp index c04ca24..f1b92cb 100644 --- a/include/command.hpp +++ b/include/command.hpp @@ -7,7 +7,8 @@ enum class CommandType{ None, NoteOn, NoteOff, - PitchBend + PitchBend, + ModWheel }; struct Command{ @@ -15,6 +16,7 @@ struct Command{ uint8_t note = 0; uint8_t velocity = 0; uint16_t pitch_bend = 8192; + uint8_t mod_wheel = 0; }; #endif diff --git a/include/lfo.hpp b/include/lfo.hpp new file mode 100644 index 0000000..959729f --- /dev/null +++ b/include/lfo.hpp @@ -0,0 +1,28 @@ +#ifndef LFO +#define LFO + +#include +#include +#include + +class Lfo{ + public: + Lfo(){ + sample = 0.0; + frequency = 5.0; + phase = 0.0; + sample_rate = 48000; + } + + double generate(); + + void set_frequency(double value); + + private: + double sample; + double frequency; + double phase; + uint32_t sample_rate; +}; + +#endif diff --git a/include/midi_input.hpp b/include/midi_input.hpp index 03a2ae4..4b0d2b7 100644 --- a/include/midi_input.hpp +++ b/include/midi_input.hpp @@ -14,6 +14,7 @@ class MidiInput{ public: MidiInput(Synth& synth) : synth_(synth){ pitch = 8192; + mod_wheel = 0; } void list_ports(); @@ -25,6 +26,7 @@ class MidiInput{ Synth& synth_; uint16_t pitch; + uint8_t mod_wheel; }; diff --git a/include/synth.hpp b/include/synth.hpp index 116bb9d..8ed11fe 100644 --- a/include/synth.hpp +++ b/include/synth.hpp @@ -9,6 +9,7 @@ #include "../include/command.hpp" #include "../include/voice.hpp" #include "../include/stereo_sample.hpp" +#include "../include/lfo.hpp" class Synth { public: @@ -17,19 +18,21 @@ class Synth { pitch_bend_range = 2.0; pitch_semitones = 0.0; pitch_multiplier = 1.0; + vibrato_depth_ = 0.0; + + mod_wheel = 0; stolen_voices = 0; } StereoSample generate(); void generate_buffer(double* array, int size); - void set_frequency(double value); - void set_amplitude(double value); - void set_midi_note(uint8_t note); + void set_mod_wheel(uint8_t value); void pitch_bend(uint16_t value); double pitch_to_semitones(uint16_t value); double semitones_to_multiplier(double semitones); + double mod_wheel_to_depth(uint8_t value); bool note_on(uint8_t note, uint8_t velocity); bool note_off(uint8_t note); @@ -39,11 +42,14 @@ class Synth { Voice* find_quietest_voice(); SpscQueue queue; + Lfo lfo; double pitch_bend_range; double pitch_semitones; double pitch_multiplier; + double vibrato_depth_; + uint8_t mod_wheel; uint64_t stolen_voices; std::array voices; diff --git a/include/voice.hpp b/include/voice.hpp index 5770a43..4def4ff 100644 --- a/include/voice.hpp +++ b/include/voice.hpp @@ -20,7 +20,8 @@ class Voice{ envelope_value = 0.0; pan = 0.0; base_frequency = 440.0; - multiplier_ = 1.0; + pitch_bend = 1.0; + vibrato = 1.0; active = false; } @@ -29,6 +30,8 @@ class Voice{ void note_off(); void set_pan(double value); void set_pitch_bend(double multiplier); + void set_vibrato(double multiplier); + void update_frequency(); StereoSample generate(); @@ -47,7 +50,8 @@ class Voice{ double envelope_value; double pan; double base_frequency; - double multiplier_; + double pitch_bend; + double vibrato; bool active; }; diff --git a/src/lfo.cpp b/src/lfo.cpp new file mode 100644 index 0000000..9cca22b --- /dev/null +++ b/src/lfo.cpp @@ -0,0 +1,12 @@ +#include "../include/lfo.hpp" + +double Lfo::generate(){ + sample = std::sin(phase); + phase += (2*std::numbers::pi*frequency)/sample_rate; + phase = std::fmod(phase, (2*std::numbers::pi)); + return sample; +} + +void Lfo::set_frequency(double value){ + if (value > 0.0 && value < 25.0) frequency = value; +} diff --git a/src/midi_input.cpp b/src/midi_input.cpp index b6a5b10..0d44395 100644 --- a/src/midi_input.cpp +++ b/src/midi_input.cpp @@ -49,6 +49,10 @@ void MidiInput::read_message(){ pitch = ((velocity & 0x7F)<<7) | (note & 0x7F); synth_.pitch_bend(pitch); } + else if ( (status & 0xF0) == 0xB0 && note == 1){ + mod_wheel = static_cast(velocity); + synth_.set_mod_wheel(mod_wheel); + } } } } diff --git a/src/synth.cpp b/src/synth.cpp index bc3f8ff..f81063d 100644 --- a/src/synth.cpp +++ b/src/synth.cpp @@ -4,6 +4,9 @@ StereoSample Synth::generate(){ int processed = 0; int active_voices = 0; + double mod_lfo = 0.0; + double delta_c = 0.0; + double delta_s = 0.0; StereoSample generated = {0.0, 0.0}; StereoSample result = {0.0, 0.0}; Command command; @@ -52,9 +55,19 @@ StereoSample Synth::generate(){ } break; } + + case CommandType::ModWheel:{ + mod_wheel = command.mod_wheel; + vibrato_depth_ = mod_wheel_to_depth(mod_wheel); + break; + } } } + mod_lfo = lfo.generate(); + delta_c = vibrato_depth_ * mod_lfo; + delta_s = semitones_to_multiplier(delta_c/100.0); for (auto& voice : voices){ + voice.set_vibrato(delta_s); generated = voice.generate(); result.left += generated.left; result.right += generated.right; @@ -66,6 +79,37 @@ StereoSample Synth::generate(){ return result; } +void Synth::set_mod_wheel(uint8_t value){ + Command command; + command.type = CommandType::ModWheel; + command.mod_wheel = value; + queue.push(command); +} + +void Synth::pitch_bend(uint16_t value){ + Command command; + command.type = CommandType::PitchBend; + command.pitch_bend = value; + queue.push(command); +} + +double Synth::pitch_to_semitones(uint16_t value){ + double n = 0.0; + double casted_value = static_cast(value); + if (value < 8192) n = (casted_value - 8192)/8192.0; + else n = (casted_value - 8192)/8191.0; + + return n * pitch_bend_range; +} + +double Synth::semitones_to_multiplier(double semitones){ + return std::pow(2.0, (semitones/12)); +} + +double Synth::mod_wheel_to_depth(uint8_t value){ + return (static_cast(value)/127.0)*100; +} + bool Synth::note_on(uint8_t note, uint8_t velocity){ return queue.push({CommandType::NoteOn, note, velocity}); } @@ -111,23 +155,3 @@ Voice* Synth::find_quietest_voice(){ } return quietest; } - -void Synth::pitch_bend(uint16_t value){ - Command command; - command.type = CommandType::PitchBend; - command.pitch_bend = value; - queue.push(command); -} - -double Synth::pitch_to_semitones(uint16_t value){ - double n = 0.0; - double casted_value = static_cast(value); - if (value < 8192) n = (casted_value - 8192)/8192; - else n = (casted_value - 8192)/8191; - - return n * pitch_bend_range; -} - -double Synth::semitones_to_multiplier(double semitones){ - return std::pow(2.0, (semitones/12)); -} diff --git a/src/voice.cpp b/src/voice.cpp index ad8f6cb..5fa10ea 100644 --- a/src/voice.cpp +++ b/src/voice.cpp @@ -7,7 +7,7 @@ void Voice::note_on(uint8_t note_, uint8_t velocity){ note = note_; std::uniform_real_distribution pan_distribution(-1.0, 1.0); base_frequency = 440.0*std::pow(2.0,( (static_cast(note)-69)/12.0 )); - oscillator.frequency = base_frequency * multiplier_; + update_frequency(); oscillator.amplitude = (static_cast(velocity) / 127.0); set_pan(pan_distribution(gen)); envelope.note_on(); @@ -26,8 +26,17 @@ void Voice::set_pan(double value){ } void Voice::set_pitch_bend(double multiplier){ - multiplier_ = multiplier; - oscillator.frequency = base_frequency * multiplier_; + pitch_bend = multiplier; + update_frequency(); +} + +void Voice::set_vibrato(double multiplier){ + vibrato = multiplier; + update_frequency(); +} + +void Voice::update_frequency(){ + oscillator.frequency = base_frequency * pitch_bend * vibrato; } StereoSample Voice::generate(){