добавлено управление Pitch Bend и Vibrato через MIDI
реализовано: 1. обработка MIDI Pitch Bend 2. управление глубиной вибрато через Mod Wheel (CC1) 3. реализован LFO на основе синусоидальной волны 4. добавлена частотная модуляция голосов с помощью LFO 5. реализовано совместное применение Pitch Bend и Vibrato без взаимного перезаписывания эффектов 6. добавлен пересчет частоты осциллятора с учетом всех множителей
This commit is contained in:
@@ -19,6 +19,7 @@ set(SOURCES
|
|||||||
src/synth.cpp
|
src/synth.cpp
|
||||||
src/midi_input.cpp
|
src/midi_input.cpp
|
||||||
src/voice.cpp
|
src/voice.cpp
|
||||||
|
src/lfo.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||||
|
|||||||
+3
-1
@@ -7,7 +7,8 @@ enum class CommandType{
|
|||||||
None,
|
None,
|
||||||
NoteOn,
|
NoteOn,
|
||||||
NoteOff,
|
NoteOff,
|
||||||
PitchBend
|
PitchBend,
|
||||||
|
ModWheel
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Command{
|
struct Command{
|
||||||
@@ -15,6 +16,7 @@ struct Command{
|
|||||||
uint8_t note = 0;
|
uint8_t note = 0;
|
||||||
uint8_t velocity = 0;
|
uint8_t velocity = 0;
|
||||||
uint16_t pitch_bend = 8192;
|
uint16_t pitch_bend = 8192;
|
||||||
|
uint8_t mod_wheel = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef LFO
|
||||||
|
#define LFO
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <numbers>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
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
|
||||||
@@ -14,6 +14,7 @@ class MidiInput{
|
|||||||
public:
|
public:
|
||||||
MidiInput(Synth& synth) : synth_(synth){
|
MidiInput(Synth& synth) : synth_(synth){
|
||||||
pitch = 8192;
|
pitch = 8192;
|
||||||
|
mod_wheel = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_ports();
|
void list_ports();
|
||||||
@@ -25,6 +26,7 @@ class MidiInput{
|
|||||||
Synth& synth_;
|
Synth& synth_;
|
||||||
|
|
||||||
uint16_t pitch;
|
uint16_t pitch;
|
||||||
|
uint8_t mod_wheel;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+9
-3
@@ -9,6 +9,7 @@
|
|||||||
#include "../include/command.hpp"
|
#include "../include/command.hpp"
|
||||||
#include "../include/voice.hpp"
|
#include "../include/voice.hpp"
|
||||||
#include "../include/stereo_sample.hpp"
|
#include "../include/stereo_sample.hpp"
|
||||||
|
#include "../include/lfo.hpp"
|
||||||
|
|
||||||
class Synth {
|
class Synth {
|
||||||
public:
|
public:
|
||||||
@@ -17,19 +18,21 @@ class Synth {
|
|||||||
pitch_bend_range = 2.0;
|
pitch_bend_range = 2.0;
|
||||||
pitch_semitones = 0.0;
|
pitch_semitones = 0.0;
|
||||||
pitch_multiplier = 1.0;
|
pitch_multiplier = 1.0;
|
||||||
|
vibrato_depth_ = 0.0;
|
||||||
|
|
||||||
|
mod_wheel = 0;
|
||||||
stolen_voices = 0;
|
stolen_voices = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
StereoSample generate();
|
StereoSample generate();
|
||||||
|
|
||||||
void generate_buffer(double* array, int size);
|
void generate_buffer(double* array, int size);
|
||||||
void set_frequency(double value);
|
void set_mod_wheel(uint8_t value);
|
||||||
void set_amplitude(double value);
|
|
||||||
void set_midi_note(uint8_t note);
|
|
||||||
void pitch_bend(uint16_t value);
|
void pitch_bend(uint16_t value);
|
||||||
|
|
||||||
double pitch_to_semitones(uint16_t value);
|
double pitch_to_semitones(uint16_t value);
|
||||||
double semitones_to_multiplier(double semitones);
|
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_on(uint8_t note, uint8_t velocity);
|
||||||
bool note_off(uint8_t note);
|
bool note_off(uint8_t note);
|
||||||
@@ -39,11 +42,14 @@ class Synth {
|
|||||||
Voice* find_quietest_voice();
|
Voice* find_quietest_voice();
|
||||||
|
|
||||||
SpscQueue queue;
|
SpscQueue queue;
|
||||||
|
Lfo lfo;
|
||||||
|
|
||||||
double pitch_bend_range;
|
double pitch_bend_range;
|
||||||
double pitch_semitones;
|
double pitch_semitones;
|
||||||
double pitch_multiplier;
|
double pitch_multiplier;
|
||||||
|
double vibrato_depth_;
|
||||||
|
|
||||||
|
uint8_t mod_wheel;
|
||||||
uint64_t stolen_voices;
|
uint64_t stolen_voices;
|
||||||
|
|
||||||
std::array<Voice, 8> voices;
|
std::array<Voice, 8> voices;
|
||||||
|
|||||||
+6
-2
@@ -20,7 +20,8 @@ class Voice{
|
|||||||
envelope_value = 0.0;
|
envelope_value = 0.0;
|
||||||
pan = 0.0;
|
pan = 0.0;
|
||||||
base_frequency = 440.0;
|
base_frequency = 440.0;
|
||||||
multiplier_ = 1.0;
|
pitch_bend = 1.0;
|
||||||
|
vibrato = 1.0;
|
||||||
|
|
||||||
active = false;
|
active = false;
|
||||||
}
|
}
|
||||||
@@ -29,6 +30,8 @@ class Voice{
|
|||||||
void note_off();
|
void note_off();
|
||||||
void set_pan(double value);
|
void set_pan(double value);
|
||||||
void set_pitch_bend(double multiplier);
|
void set_pitch_bend(double multiplier);
|
||||||
|
void set_vibrato(double multiplier);
|
||||||
|
void update_frequency();
|
||||||
|
|
||||||
StereoSample generate();
|
StereoSample generate();
|
||||||
|
|
||||||
@@ -47,7 +50,8 @@ class Voice{
|
|||||||
double envelope_value;
|
double envelope_value;
|
||||||
double pan;
|
double pan;
|
||||||
double base_frequency;
|
double base_frequency;
|
||||||
double multiplier_;
|
double pitch_bend;
|
||||||
|
double vibrato;
|
||||||
|
|
||||||
bool active;
|
bool active;
|
||||||
};
|
};
|
||||||
|
|||||||
+12
@@ -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;
|
||||||
|
}
|
||||||
@@ -49,6 +49,10 @@ void MidiInput::read_message(){
|
|||||||
pitch = ((velocity & 0x7F)<<7) | (note & 0x7F);
|
pitch = ((velocity & 0x7F)<<7) | (note & 0x7F);
|
||||||
synth_.pitch_bend(pitch);
|
synth_.pitch_bend(pitch);
|
||||||
}
|
}
|
||||||
|
else if ( (status & 0xF0) == 0xB0 && note == 1){
|
||||||
|
mod_wheel = static_cast<uint8_t>(velocity);
|
||||||
|
synth_.set_mod_wheel(mod_wheel);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+44
-20
@@ -4,6 +4,9 @@
|
|||||||
StereoSample Synth::generate(){
|
StereoSample Synth::generate(){
|
||||||
int processed = 0;
|
int processed = 0;
|
||||||
int active_voices = 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 generated = {0.0, 0.0};
|
||||||
StereoSample result = {0.0, 0.0};
|
StereoSample result = {0.0, 0.0};
|
||||||
Command command;
|
Command command;
|
||||||
@@ -52,9 +55,19 @@ StereoSample Synth::generate(){
|
|||||||
}
|
}
|
||||||
break;
|
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){
|
for (auto& voice : voices){
|
||||||
|
voice.set_vibrato(delta_s);
|
||||||
generated = voice.generate();
|
generated = voice.generate();
|
||||||
result.left += generated.left;
|
result.left += generated.left;
|
||||||
result.right += generated.right;
|
result.right += generated.right;
|
||||||
@@ -66,6 +79,37 @@ StereoSample Synth::generate(){
|
|||||||
return result;
|
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<double>(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<double>(value)/127.0)*100;
|
||||||
|
}
|
||||||
|
|
||||||
bool Synth::note_on(uint8_t note, uint8_t velocity){
|
bool Synth::note_on(uint8_t note, uint8_t velocity){
|
||||||
return queue.push({CommandType::NoteOn, note, velocity});
|
return queue.push({CommandType::NoteOn, note, velocity});
|
||||||
}
|
}
|
||||||
@@ -111,23 +155,3 @@ Voice* Synth::find_quietest_voice(){
|
|||||||
}
|
}
|
||||||
return quietest;
|
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<double>(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));
|
|
||||||
}
|
|
||||||
|
|||||||
+12
-3
@@ -7,7 +7,7 @@ void Voice::note_on(uint8_t note_, uint8_t velocity){
|
|||||||
note = note_;
|
note = note_;
|
||||||
std::uniform_real_distribution<double> pan_distribution(-1.0, 1.0);
|
std::uniform_real_distribution<double> pan_distribution(-1.0, 1.0);
|
||||||
base_frequency = 440.0*std::pow(2.0,( (static_cast<int>(note)-69)/12.0 ));
|
base_frequency = 440.0*std::pow(2.0,( (static_cast<int>(note)-69)/12.0 ));
|
||||||
oscillator.frequency = base_frequency * multiplier_;
|
update_frequency();
|
||||||
oscillator.amplitude = (static_cast<double>(velocity) / 127.0);
|
oscillator.amplitude = (static_cast<double>(velocity) / 127.0);
|
||||||
set_pan(pan_distribution(gen));
|
set_pan(pan_distribution(gen));
|
||||||
envelope.note_on();
|
envelope.note_on();
|
||||||
@@ -26,8 +26,17 @@ void Voice::set_pan(double value){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Voice::set_pitch_bend(double multiplier){
|
void Voice::set_pitch_bend(double multiplier){
|
||||||
multiplier_ = multiplier;
|
pitch_bend = multiplier;
|
||||||
oscillator.frequency = base_frequency * 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(){
|
StereoSample Voice::generate(){
|
||||||
|
|||||||
Reference in New Issue
Block a user