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 <iostream>
#include <stdexcept>
#include <cstring>
#include "../include/synth.hpp"
class MidiInput{
public:
MidiInput(Synth& synth) : synth_(synth){
}
void list_ports();
void open_port(unsigned int port);
void read_message();
private:
RtMidiIn midiIn;
Synth& synth_;
};
+38 -37
View File
@@ -65,11 +65,11 @@ int main(){
Synth synth;
// Command command;
SpscQueue queue;
MidiInput midi;
MidiInput midi(synth);
midi.list_ports();
midi.open_port(2);
while (true) midi.read_message();
midi.open_port(1);
// while (true) midi.read_message();
@@ -102,40 +102,41 @@ int main(){
// synth.set_amplitude(0.5);
//
// try{
// AudioEngine audio_engine(synth);
// audio_engine.start();
// while (true){
// std::cin >> a;
// if (a == "on"){
// synth.note_on(69, 127);
// synth.note_on(60, 127);
// synth.note_on(48, 127);
// }
// if (a == "1"){
// synth.note_on(69, 127);
// // if (!synth.note_on()){
// // throw std::runtime_error("ошибка: не удалось воспроизвести ноту");
// // }
// }
// else if (a == "2") synth.note_on(60, 127);
// else if (a == "3") synth.note_on(48, 127);
// else if (a == "off"){
// synth.note_off(60);
// // if (!synth.note_off()){
// // throw std::runtime_error("ошибка: не удалось воспроизвести ноту");
// // }
// }
// else if (a == "exit"){
// audio_engine.stop();
// return 0;
// }
// }
// }
// catch (const std::exception& error){
// std::cerr << error.what() << "\n";
// return 1;
// }
try{
AudioEngine audio_engine(synth);
audio_engine.start();
while (true){
// std::cin >> a;
// if (a == "on"){
// synth.note_on(69, 127);
// synth.note_on(60, 127);
// synth.note_on(48, 127);
// }
// if (a == "1"){
// synth.note_on(69, 127);
// // if (!synth.note_on()){
// // throw std::runtime_error("ошибка: не удалось воспроизвести ноту");
// // }
// }
// else if (a == "2") synth.note_on(60, 127);
// else if (a == "3") synth.note_on(48, 127);
// else if (a == "off"){
// synth.note_off(60);
// // if (!synth.note_off()){
// // throw std::runtime_error("ошибка: не удалось воспроизвести ноту");
// // }
// }
// else if (a == "exit"){
// audio_engine.stop();
// return 0;
// }
while (true) midi.read_message();
}
}
catch (const std::exception& error){
std::cerr << error.what() << "\n";
return 1;
}
return 0;
+16 -3
View File
@@ -19,7 +19,7 @@ void MidiInput::open_port(unsigned int port){
}
catch(RtMidiError& error){
error.printMessage();
std::runtime_error("ошибка");
throw std::runtime_error("ошибка");
}
}
@@ -27,8 +27,21 @@ void MidiInput::read_message(){
std::vector<unsigned char> message;
midiIn.getMessage(&message);
if (!message.empty()){
for (const auto& elem : message){
std::cout << static_cast<int>(elem) << "\n";
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 ((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);
}
}