From 5818405474291f7f7cae084c7ba7794abd297e25 Mon Sep 17 00:00:00 2001 From: s13bby Date: Sat, 14 Nov 2026 13:01:54 +0600 Subject: [PATCH] =?UTF-8?q?midi=20=D0=B7=D0=B0=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BB!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit теперь некст полифония --- include/midi_input.hpp | 8 +++++ src/main.cpp | 75 +++++++++++++++++++++--------------------- src/midi_input.cpp | 19 +++++++++-- 3 files changed, 62 insertions(+), 40 deletions(-) diff --git a/include/midi_input.hpp b/include/midi_input.hpp index ebe5034..e7f3ac0 100644 --- a/include/midi_input.hpp +++ b/include/midi_input.hpp @@ -4,17 +4,25 @@ #include #include #include +#include + +#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_; }; diff --git a/src/main.cpp b/src/main.cpp index 4223aa5..d7d617d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; diff --git a/src/midi_input.cpp b/src/midi_input.cpp index d3ef158..1010164 100644 --- a/src/midi_input.cpp +++ b/src/midi_input.cpp @@ -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 message; midiIn.getMessage(&message); if (!message.empty()){ - for (const auto& elem : message){ - std::cout << static_cast(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(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); } + else if (((status & 0xF0) == 0x80)) synth_.note_off(note); } }