midi заработал!

теперь некст полифония
This commit is contained in:
2026-11-14 13:01:54 +06:00
parent 1c75f418e2
commit 5818405474
3 changed files with 62 additions and 40 deletions
+8
View File
@@ -4,17 +4,25 @@
#include <rtmidi/RtMidi.h> #include <rtmidi/RtMidi.h>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <cstring>
#include "../include/synth.hpp"
class MidiInput{ class MidiInput{
public: public:
MidiInput(Synth& synth) : synth_(synth){
}
void list_ports(); void list_ports();
void open_port(unsigned int port); void open_port(unsigned int port);
void read_message(); void read_message();
private: private:
RtMidiIn midiIn; RtMidiIn midiIn;
Synth& synth_;
}; };
+14 -13
View File
@@ -65,11 +65,11 @@ int main(){
Synth synth; Synth synth;
// Command command; // Command command;
SpscQueue queue; SpscQueue queue;
MidiInput midi; MidiInput midi(synth);
midi.list_ports(); midi.list_ports();
midi.open_port(2); midi.open_port(1);
while (true) midi.read_message(); // while (true) midi.read_message();
@@ -102,10 +102,10 @@ int main(){
// synth.set_amplitude(0.5); // synth.set_amplitude(0.5);
// //
// try{ try{
// AudioEngine audio_engine(synth); AudioEngine audio_engine(synth);
// audio_engine.start(); audio_engine.start();
// while (true){ while (true){
// std::cin >> a; // std::cin >> a;
// if (a == "on"){ // if (a == "on"){
// synth.note_on(69, 127); // synth.note_on(69, 127);
@@ -130,12 +130,13 @@ int main(){
// audio_engine.stop(); // audio_engine.stop();
// return 0; // return 0;
// } // }
// } while (true) midi.read_message();
// } }
// catch (const std::exception& error){ }
// std::cerr << error.what() << "\n"; catch (const std::exception& error){
// return 1; std::cerr << error.what() << "\n";
// } return 1;
}
return 0; return 0;
+16 -3
View File
@@ -19,7 +19,7 @@ void MidiInput::open_port(unsigned int port){
} }
catch(RtMidiError& error){ catch(RtMidiError& error){
error.printMessage(); error.printMessage();
std::runtime_error("ошибка"); throw std::runtime_error("ошибка");
} }
} }
@@ -27,8 +27,21 @@ 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()){
for (const auto& elem : message){ if (message.size() < 3) throw std::runtime_error("ошибка: сообщение меньше 3");
std::cout << static_cast<int>(elem) << "\n"; 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 ((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);
} }
} }