теперь работает MIDI Pitch Bend с диапазоном +- 2 полутона
This commit is contained in:
+6
-4
@@ -6,13 +6,15 @@
|
|||||||
enum class CommandType{
|
enum class CommandType{
|
||||||
None,
|
None,
|
||||||
NoteOn,
|
NoteOn,
|
||||||
NoteOff
|
NoteOff,
|
||||||
|
PitchBend
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Command{
|
struct Command{
|
||||||
CommandType type;
|
CommandType type = CommandType::None;
|
||||||
uint8_t note;
|
uint8_t note = 0;
|
||||||
uint8_t velocity;
|
uint8_t velocity = 0;
|
||||||
|
uint16_t pitch_bend = 8192;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -40,9 +40,11 @@ class Envelope{
|
|||||||
|
|
||||||
void note_on();
|
void note_on();
|
||||||
void note_off();
|
void note_off();
|
||||||
|
|
||||||
double process();
|
double process();
|
||||||
|
|
||||||
bool is_idle() const;
|
bool is_idle() const;
|
||||||
|
bool is_release() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Condition condition;
|
Condition condition;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class MidiInput{
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
MidiInput(Synth& synth) : synth_(synth){
|
MidiInput(Synth& synth) : synth_(synth){
|
||||||
|
pitch = 8192;
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_ports();
|
void list_ports();
|
||||||
@@ -24,6 +24,8 @@ class MidiInput{
|
|||||||
RtMidiIn midiIn;
|
RtMidiIn midiIn;
|
||||||
Synth& synth_;
|
Synth& synth_;
|
||||||
|
|
||||||
|
uint16_t pitch;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
#include "../include/spsc_queue.hpp"
|
#include "../include/spsc_queue.hpp"
|
||||||
#include "../include/command.hpp"
|
#include "../include/command.hpp"
|
||||||
@@ -11,21 +12,40 @@
|
|||||||
|
|
||||||
class Synth {
|
class Synth {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
Synth(){
|
||||||
|
pitch_bend_range = 2.0;
|
||||||
|
pitch_semitones = 0.0;
|
||||||
|
pitch_multiplier = 1.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_frequency(double value);
|
||||||
void set_amplitude(double value);
|
void set_amplitude(double value);
|
||||||
void set_midi_note(uint8_t note);
|
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_on(uint8_t note, uint8_t velocity);
|
||||||
bool note_off(uint8_t note);
|
bool note_off(uint8_t note);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Voice* find_free_voice();
|
Voice* find_free_voice();
|
||||||
|
Voice* find_quietest_voice();
|
||||||
|
|
||||||
SpscQueue queue;
|
SpscQueue queue;
|
||||||
|
|
||||||
|
double pitch_bend_range;
|
||||||
|
double pitch_semitones;
|
||||||
|
double pitch_multiplier;
|
||||||
|
|
||||||
|
uint64_t stolen_voices;
|
||||||
|
|
||||||
std::array<Voice, 8> voices;
|
std::array<Voice, 8> voices;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+10
-3
@@ -16,7 +16,11 @@ class Voice{
|
|||||||
public:
|
public:
|
||||||
Voice(){
|
Voice(){
|
||||||
note = 0;
|
note = 0;
|
||||||
|
|
||||||
|
envelope_value = 0.0;
|
||||||
pan = 0.0;
|
pan = 0.0;
|
||||||
|
base_frequency = 440.0;
|
||||||
|
multiplier_ = 1.0;
|
||||||
|
|
||||||
active = false;
|
active = false;
|
||||||
}
|
}
|
||||||
@@ -24,11 +28,15 @@ class Voice{
|
|||||||
void note_on(uint8_t note, uint8_t velocity);
|
void note_on(uint8_t note, uint8_t velocity);
|
||||||
void note_off();
|
void note_off();
|
||||||
void set_pan(double value);
|
void set_pan(double value);
|
||||||
|
void set_pitch_bend(double multiplier);
|
||||||
|
|
||||||
StereoSample generate();
|
StereoSample generate();
|
||||||
|
|
||||||
|
double get_volume() const;
|
||||||
|
|
||||||
bool is_active() const;
|
bool is_active() const;
|
||||||
bool is_playing(uint8_t note_) const;
|
bool is_playing(uint8_t note_) const;
|
||||||
|
bool is_released() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Envelope envelope;
|
Envelope envelope;
|
||||||
@@ -38,11 +46,10 @@ class Voice{
|
|||||||
|
|
||||||
double envelope_value;
|
double envelope_value;
|
||||||
double pan;
|
double pan;
|
||||||
|
double base_frequency;
|
||||||
|
double multiplier_;
|
||||||
|
|
||||||
bool active;
|
bool active;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -70,3 +70,7 @@ double Envelope::process(){
|
|||||||
bool Envelope::is_idle() const{
|
bool Envelope::is_idle() const{
|
||||||
return condition == Condition::Idle;
|
return condition == Condition::Idle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Envelope::is_release() const{
|
||||||
|
return condition == Condition::Release;
|
||||||
|
}
|
||||||
|
|||||||
+9
-2
@@ -27,21 +27,28 @@ void MidiInput::read_message(){
|
|||||||
std::vector<unsigned char> message;
|
std::vector<unsigned char> message;
|
||||||
midiIn.getMessage(&message);
|
midiIn.getMessage(&message);
|
||||||
if (!message.empty()){
|
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 status;
|
||||||
unsigned char note;
|
unsigned char note;
|
||||||
unsigned char velocity;
|
unsigned char velocity;
|
||||||
unsigned char channel;
|
unsigned char channel;
|
||||||
|
|
||||||
status = message[0];
|
status = message[0];
|
||||||
note = message[1];
|
note = message[1];
|
||||||
velocity = message[2];
|
velocity = message[2];
|
||||||
channel = (status & 0x0F) + 1;
|
channel = (status & 0x0F) + 1;
|
||||||
|
|
||||||
std::cout << static_cast<int>(status) << " | " << static_cast<int>(note) << " | " << static_cast<int>(velocity) << " | " << static_cast<int>(channel)<< "\n";
|
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 ((status & 0xF0) == 0x90 ) {
|
||||||
if (velocity > 0) synth_.note_on(note, velocity);
|
if (velocity > 0) synth_.note_on(note, velocity);
|
||||||
else synth_.note_off(note);
|
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
@@ -20,7 +20,13 @@ StereoSample Synth::generate(){
|
|||||||
|
|
||||||
case CommandType::NoteOn:{
|
case CommandType::NoteOn:{
|
||||||
Voice* voice = find_free_voice();
|
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);
|
voice->note_on(command.note, command.velocity);
|
||||||
}
|
}
|
||||||
++processed;
|
++processed;
|
||||||
@@ -31,11 +37,21 @@ StereoSample Synth::generate(){
|
|||||||
for (auto& voice : voices){
|
for (auto& voice : voices){
|
||||||
if (voice.is_playing(command.note)){
|
if (voice.is_playing(command.note)){
|
||||||
voice.note_off();
|
voice.note_off();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
++processed;
|
++processed;
|
||||||
break;
|
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){
|
for (auto& voice : voices){
|
||||||
@@ -50,28 +66,6 @@ StereoSample Synth::generate(){
|
|||||||
return result;
|
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){
|
bool Synth::note_on(uint8_t note, uint8_t velocity){
|
||||||
return queue.push({CommandType::NoteOn, note, velocity});
|
return queue.push({CommandType::NoteOn, note, velocity});
|
||||||
}
|
}
|
||||||
@@ -88,3 +82,52 @@ Voice* Synth::find_free_voice(){
|
|||||||
}
|
}
|
||||||
return nullptr;
|
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
@@ -5,9 +5,9 @@ std::mt19937 gen(rd());
|
|||||||
|
|
||||||
void Voice::note_on(uint8_t note_, uint8_t velocity){
|
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);
|
||||||
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);
|
oscillator.amplitude = (static_cast<double>(velocity) / 127.0);
|
||||||
set_pan(pan_distribution(gen));
|
set_pan(pan_distribution(gen));
|
||||||
envelope.note_on();
|
envelope.note_on();
|
||||||
@@ -25,6 +25,11 @@ void Voice::set_pan(double value){
|
|||||||
pan = std::clamp(value, -1.0, 1.0);
|
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(){
|
StereoSample Voice::generate(){
|
||||||
envelope_value = envelope.process();
|
envelope_value = envelope.process();
|
||||||
if (envelope.is_idle()){
|
if (envelope.is_idle()){
|
||||||
@@ -40,10 +45,18 @@ StereoSample Voice::generate(){
|
|||||||
return {left*envelope_value, right*envelope_value};
|
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;
|
return active;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Voice::is_playing(uint8_t note_) const {
|
bool Voice::is_playing(uint8_t note_) const {
|
||||||
return is_active() && note == note_;
|
return is_active() && note == note_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Voice::is_released() const{
|
||||||
|
return envelope.is_release();
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user