теперь работает MIDI Pitch Bend с диапазоном +- 2 полутона

This commit is contained in:
2026-11-15 00:47:07 +06:00
parent 2cff003e4b
commit 6773949803
9 changed files with 139 additions and 39 deletions
+6 -4
View File
@@ -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
+2
View File
@@ -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;
+3 -1
View File
@@ -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
+20
View File
@@ -3,6 +3,7 @@
#include <cstdint>
#include <array>
#include <cmath>
#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<Voice, 8> voices;
};
+10 -3
View File
@@ -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
+4
View File
@@ -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;
}
+9 -2
View File
@@ -27,21 +27,28 @@ void MidiInput::read_message(){
std::vector<unsigned char> 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<int>(status) << " | " << static_cast<int>(note) << " | " << static_cast<int>(velocity) << " | " << static_cast<int>(channel)<< "\n";
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) == 0x80 ) synth_.note_off(note);
else if ( (status & 0xF0) == 0xE0 ){
pitch = ((velocity & 0x7F)<<7) | (note & 0x7F);
synth_.pitch_bend(pitch);
}
}
}
}
+66 -23
View File
@@ -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<int>(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<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));
}
+16 -3
View File
@@ -5,9 +5,9 @@ std::mt19937 gen(rd());
void Voice::note_on(uint8_t note_, uint8_t velocity){
note = note_;
std::uniform_real_distribution<double> pan_distribution(-1.0, 1.0);
oscillator.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_;
oscillator.amplitude = (static_cast<double>(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();
}