From 1c75f418e23b83214ae40de1d8c0c5dfe8104731 Mon Sep 17 00:00:00 2001 From: s13bby Date: Sat, 14 Nov 2026 10:23:02 +0600 Subject: [PATCH] .. --- CMakeLists.txt | 7 ++++++ include/midi_input.hpp | 21 ++++++++++++++++ include/synth.hpp | 2 ++ src/main.cpp | 54 +++++++++++++++++++++++++++--------------- src/midi_input.cpp | 34 ++++++++++++++++++++++++++ src/synth.cpp | 17 +++++++++++-- 6 files changed, 114 insertions(+), 21 deletions(-) create mode 100644 include/midi_input.hpp create mode 100644 src/midi_input.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bc191ac..7cfce10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) find_package(Threads REQUIRED) +find_package(PkgConfig REQUIRED) +pkg_check_modules(RTMIDI REQUIRED IMPORTED_TARGET rtmidi) set(SOURCES src/main.cpp @@ -15,6 +17,7 @@ set(SOURCES src/miniaudio.cpp src/spsc_queue.cpp src/synth.cpp + src/midi_input.cpp ) add_executable(${PROJECT_NAME} ${SOURCES}) @@ -24,6 +27,10 @@ target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/external ) +target_include_directories(osc PRIVATE ${RTMIDI_INCLUDE_DIRS}) +target_link_libraries(osc PRIVATE ${RTMIDI_LIBRARIES}) +target_link_libraries(osc PRIVATE PkgConfig::RTMIDI) + target_compile_options(osc PRIVATE -fsanitize=thread -g -O1) target_link_options(osc PRIVATE -fsanitize=thread) diff --git a/include/midi_input.hpp b/include/midi_input.hpp new file mode 100644 index 0000000..ebe5034 --- /dev/null +++ b/include/midi_input.hpp @@ -0,0 +1,21 @@ +#ifndef MIDIINPUT +#define MIDIINPUT + +#include +#include +#include + +class MidiInput{ + + + public: + void list_ports(); + void open_port(unsigned int port); + void read_message(); + + private: + RtMidiIn midiIn; + +}; + +#endif diff --git a/include/synth.hpp b/include/synth.hpp index 10d0671..c8be0fd 100644 --- a/include/synth.hpp +++ b/include/synth.hpp @@ -17,6 +17,7 @@ class Synth { amplitude = 0.5; sample_rate = 48000; phase = 0.0; + current_note = 0; }; double generate(); @@ -43,6 +44,7 @@ class Synth { double phase; uint32_t sample_rate; + uint8_t current_note; }; #endif diff --git a/src/main.cpp b/src/main.cpp index 3417a6b..4223aa5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include "../include/audio_engine.hpp" #include "../include/spsc_queue.hpp" #include "../include/command.hpp" +#include "../include/midi_input.hpp" // void producer(SpscQueue& queue, std::atomic& producer_done){ // for (int i = 0; i < 32; ++i){ @@ -60,22 +61,27 @@ int main(){ - // std::cout << "440.гц\n"; - // std::string a = ""; - // Synth synth; - - Command command; + std::string a = ""; + Synth synth; + // Command command; SpscQueue queue; + MidiInput midi; - for (int i = 0; i < 33; ++i){ - std::cout << queue.push({CommandType::NoteOn, 60, 100}) << "\n"; - std::cout << queue.push({CommandType::NoteOn, 20, 80}) << "\n"; - } - std::cout << "выгрузка\n"; - for (int i = 0; i < 65; ++i){ - std::cout << queue.pop(command) << "\n"; + midi.list_ports(); + midi.open_port(2); + while (true) midi.read_message(); - } + + + // for (int i = 0; i < 33; ++i){ + // std::cout << queue.push({CommandType::NoteOn, 60, 100}) << "\n"; + // std::cout << queue.push({CommandType::NoteOn, 20, 80}) << "\n"; + // } + // std::cout << "выгрузка\n"; + // for (int i = 0; i < 65; ++i){ + // std::cout << queue.pop(command) << "\n"; + // + // } // std::cout << static_cast(command.type) << "\n"; // std::cout << static_cast(command.note)<< "\n"; // std::cout << static_cast(command.velocity) << "\n"; @@ -93,6 +99,7 @@ int main(){ // thread2.join(); // synth.set_frequency(440); + // synth.set_amplitude(0.5); // // try{ @@ -101,14 +108,23 @@ int main(){ // while (true){ // std::cin >> a; // if (a == "on"){ - // if (!synth.note_on()){ - // throw std::runtime_error("ошибка: не удалось воспроизвести ноту"); - // } + // 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"){ - // if (!synth.note_off()){ - // throw std::runtime_error("ошибка: не удалось воспроизвести ноту"); - // } + // synth.note_off(60); + // // if (!synth.note_off()){ + // // throw std::runtime_error("ошибка: не удалось воспроизвести ноту"); + // // } // } // else if (a == "exit"){ // audio_engine.stop(); diff --git a/src/midi_input.cpp b/src/midi_input.cpp new file mode 100644 index 0000000..d3ef158 --- /dev/null +++ b/src/midi_input.cpp @@ -0,0 +1,34 @@ +#include "../include/midi_input.hpp" + +void MidiInput::list_ports(){ + unsigned int nPort = midiIn.getPortCount(); + for (unsigned int i = 0; i < nPort; ++i){ + + std::string namePort = midiIn.getPortName(i); + std::cout<< i << " : " << namePort <<"\n"; + } +} + +void MidiInput::open_port(unsigned int port){ + unsigned int nPort = midiIn.getPortCount(); + try{ + if (nPort > port){ + midiIn.openPort(port); + std::cout << port << ": порт открыт\n"; + } + } + catch(RtMidiError& error){ + error.printMessage(); + std::runtime_error("ошибка"); + } +} + +void MidiInput::read_message(){ + std::vector message; + midiIn.getMessage(&message); + if (!message.empty()){ + for (const auto& elem : message){ + std::cout << static_cast(elem) << "\n"; + } + } +} diff --git a/src/synth.cpp b/src/synth.cpp index 51774c6..d80a068 100644 --- a/src/synth.cpp +++ b/src/synth.cpp @@ -6,6 +6,7 @@ double Synth::generate(){ Command command; while (processed < 16 && queue.pop(command)){ + switch (command.type){ default: break; @@ -14,12 +15,24 @@ double Synth::generate(){ break; case CommandType::NoteOn: - envelope.note_on(); + if (command.velocity == 0){ + if (command.note == current_note){ + envelope.note_off(); + } + } + else{ + current_note = command.note; + set_midi_note(command.note); + amplitude = (static_cast(command.velocity) / 127); + envelope.note_on(); + } ++processed; break; case CommandType::NoteOff: - envelope.note_off(); + if (command.note == current_note){ + envelope.note_off(); + } ++processed; break; }