diff --git a/include/command.hpp b/include/command.hpp index 1102698..c04ca24 100644 --- a/include/command.hpp +++ b/include/command.hpp @@ -6,13 +6,15 @@ enum class CommandType{ None, NoteOn, - NoteOff + NoteOff, + PitchBend }; struct Command{ - CommandType type; - uint8_t note; - uint8_t velocity; + CommandType type = CommandType::None; + uint8_t note = 0; + uint8_t velocity = 0; + uint16_t pitch_bend = 8192; }; #endif diff --git a/include/envelope.hpp b/include/envelope.hpp index 79d0d23..4a6c38b 100644 --- a/include/envelope.hpp +++ b/include/envelope.hpp @@ -40,9 +40,11 @@ class Envelope{ void note_on(); void note_off(); + double process(); bool is_idle() const; + bool is_release() const; private: Condition condition; diff --git a/include/midi_input.hpp b/include/midi_input.hpp index e7f3ac0..03a2ae4 100644 --- a/include/midi_input.hpp +++ b/include/midi_input.hpp @@ -13,7 +13,7 @@ class MidiInput{ public: MidiInput(Synth& synth) : synth_(synth){ - + pitch = 8192; } void list_ports(); @@ -24,6 +24,8 @@ class MidiInput{ RtMidiIn midiIn; Synth& synth_; + uint16_t pitch; + }; #endif diff --git a/include/synth.hpp b/include/synth.hpp index 3055bce..116bb9d 100644 --- a/include/synth.hpp +++ b/include/synth.hpp @@ -3,6 +3,7 @@ #include #include +#include #include "../include/spsc_queue.hpp" #include "../include/command.hpp" @@ -11,21 +12,40 @@ class Synth { public: + + Synth(){ + pitch_bend_range = 2.0; + pitch_semitones = 0.0; + pitch_multiplier = 1.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 pitch_bend(uint16_t value); + + double pitch_to_semitones(uint16_t value); + double semitones_to_multiplier(double semitones); bool note_on(uint8_t note, uint8_t velocity); bool note_off(uint8_t note); private: Voice* find_free_voice(); + Voice* find_quietest_voice(); SpscQueue queue; + double pitch_bend_range; + double pitch_semitones; + double pitch_multiplier; + + uint64_t stolen_voices; + std::array voices; }; diff --git a/include/voice.hpp b/include/voice.hpp index 935f21f..5770a43 100644 --- a/include/voice.hpp +++ b/include/voice.hpp @@ -16,7 +16,11 @@ class Voice{ public: Voice(){ note = 0; + + envelope_value = 0.0; pan = 0.0; + base_frequency = 440.0; + multiplier_ = 1.0; active = false; } @@ -24,11 +28,15 @@ class Voice{ void note_on(uint8_t note, uint8_t velocity); void note_off(); void set_pan(double value); + void set_pitch_bend(double multiplier); StereoSample generate(); + double get_volume() const; + bool is_active() const; bool is_playing(uint8_t note_) const; + bool is_released() const; private: Envelope envelope; @@ -38,11 +46,10 @@ class Voice{ double envelope_value; double pan; + double base_frequency; + double multiplier_; bool active; - - - }; #endif diff --git a/src/envelope.cpp b/src/envelope.cpp index d80910f..209c0c2 100644 --- a/src/envelope.cpp +++ b/src/envelope.cpp @@ -70,3 +70,7 @@ double Envelope::process(){ bool Envelope::is_idle() const{ return condition == Condition::Idle; } + +bool Envelope::is_release() const{ + return condition == Condition::Release; +} diff --git a/src/midi_input.cpp b/src/midi_input.cpp index 1010164..b6a5b10 100644 --- a/src/midi_input.cpp +++ b/src/midi_input.cpp @@ -27,21 +27,28 @@ void MidiInput::read_message(){ std::vector message; midiIn.getMessage(&message); if (!message.empty()){ - if (message.size() < 3) throw std::runtime_error("ошибка: сообщение меньше 3"); + // if (message.size() < 3) throw std::runtime_error("ошибка: сообщение меньше 3"); unsigned char status; unsigned char note; unsigned char velocity; unsigned char channel; + status = message[0]; note = message[1]; velocity = message[2]; channel = (status & 0x0F) + 1; std::cout << static_cast(status) << " | " << static_cast(note) << " | " << static_cast(velocity) << " | " << static_cast(channel)<< "\n"; - if ((status & 0xF0) == 0x90 ) { - if (velocity > 0) synth_.note_on(note, velocity); - else synth_.note_off(note); + if (channel == 1){ + if ((status & 0xF0) == 0x90 ) { + if (velocity > 0) synth_.note_on(note, velocity); + else synth_.note_off(note); + } + else if ( (status & 0xF0) == 0x80 ) synth_.note_off(note); + else if ( (status & 0xF0) == 0xE0 ){ + pitch = ((velocity & 0x7F)<<7) | (note & 0x7F); + synth_.pitch_bend(pitch); + } } - else if (((status & 0xF0) == 0x80)) synth_.note_off(note); } } diff --git a/src/synth.cpp b/src/synth.cpp index f20b145..bc3f8ff 100644 --- a/src/synth.cpp +++ b/src/synth.cpp @@ -20,7 +20,13 @@ StereoSample Synth::generate(){ case CommandType::NoteOn:{ Voice* voice = find_free_voice(); - if (voice){ + if (voice == nullptr){ + voice = find_quietest_voice(); + if (voice != nullptr){ + ++stolen_voices; + } + } + if (voice != nullptr){ voice->note_on(command.note, command.velocity); } ++processed; @@ -31,11 +37,21 @@ StereoSample Synth::generate(){ for (auto& voice : voices){ if (voice.is_playing(command.note)){ voice.note_off(); + break; } } ++processed; break; } + + case CommandType::PitchBend:{ + pitch_semitones = pitch_to_semitones(command.pitch_bend); + pitch_multiplier = semitones_to_multiplier(pitch_semitones); + for (auto& voice : voices){ + voice.set_pitch_bend(pitch_multiplier); + } + break; + } } } for (auto& voice : voices){ @@ -50,28 +66,6 @@ StereoSample Synth::generate(){ return result; } -// void Synth::generate_buffer(double* array, int size){ -// for (int i = 0; i < size; ++i){ -// array[i] = generate(); -// } -// } - -// void Synth::set_frequency(double value){ -// if (value < 0.0) oscillator.frequency = 0.0; -// else if (value >(oscillator.sample_rate/2)) oscillator.frequency = oscillator.sample_rate/2; -// else oscillator.frequency = value; -// } - -// void Synth::set_amplitude(double value){ -// if (value < 0.0) oscillator.amplitude = 0.0; -// else if (value > 1.0) oscillator.amplitude = 1.0; -// else oscillator.amplitude = value; -// } -/* -void Synth::set_midi_note(uint8_t note){ - set_frequency(440.0*std::pow(2.0,( (static_cast(note)-69)/12.0 ))); -}*/ - bool Synth::note_on(uint8_t note, uint8_t velocity){ return queue.push({CommandType::NoteOn, note, velocity}); } @@ -88,3 +82,52 @@ Voice* Synth::find_free_voice(){ } return nullptr; } + +Voice* Synth::find_quietest_voice(){ + Voice* quietest = nullptr; + + for (auto& voice : voices){ + if (!voice.is_active()){ + continue; + } + if (quietest == nullptr){ + quietest = &voice; + continue; + } + + if (!quietest->is_released() && voice.is_released()){ + quietest = &voice; + } + else if (quietest->is_released() && voice.is_released()){ + if (quietest->get_volume() > voice.get_volume()){ + quietest = &voice; + } + } + else if (!quietest->is_released() && !voice.is_released()){ + if (quietest->get_volume() > voice.get_volume()){ + 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 291c5f8..ad8f6cb 100644 --- a/src/voice.cpp +++ b/src/voice.cpp @@ -5,9 +5,9 @@ std::mt19937 gen(rd()); void Voice::note_on(uint8_t note_, uint8_t velocity){ note = note_; - std::uniform_real_distribution pan_distribution(-1.0, 1.0); - oscillator.frequency = 440.0*std::pow(2.0,( (static_cast(note)-69)/12.0 )); + base_frequency = 440.0*std::pow(2.0,( (static_cast(note)-69)/12.0 )); + oscillator.frequency = base_frequency * multiplier_; oscillator.amplitude = (static_cast(velocity) / 127.0); set_pan(pan_distribution(gen)); envelope.note_on(); @@ -25,6 +25,11 @@ void Voice::set_pan(double value){ pan = std::clamp(value, -1.0, 1.0); } +void Voice::set_pitch_bend(double multiplier){ + multiplier_ = multiplier; + oscillator.frequency = base_frequency * multiplier_; +} + StereoSample Voice::generate(){ envelope_value = envelope.process(); if (envelope.is_idle()){ @@ -40,10 +45,18 @@ StereoSample Voice::generate(){ return {left*envelope_value, right*envelope_value}; } -bool Voice::is_active() const { +double Voice::get_volume() const{ + return envelope_value; +} + +bool Voice::is_active() const{ return active; } bool Voice::is_playing(uint8_t note_) const { return is_active() && note == note_; } + +bool Voice::is_released() const{ + return envelope.is_release(); +}