This commit is contained in:
2026-11-14 10:23:02 +06:00
parent 5731475015
commit 1c75f418e2
6 changed files with 114 additions and 21 deletions
+7
View File
@@ -7,6 +7,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(RTMIDI REQUIRED IMPORTED_TARGET rtmidi)
set(SOURCES set(SOURCES
src/main.cpp src/main.cpp
@@ -15,6 +17,7 @@ set(SOURCES
src/miniaudio.cpp src/miniaudio.cpp
src/spsc_queue.cpp src/spsc_queue.cpp
src/synth.cpp src/synth.cpp
src/midi_input.cpp
) )
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} ${SOURCES})
@@ -24,6 +27,10 @@ target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/external ${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_compile_options(osc PRIVATE -fsanitize=thread -g -O1)
target_link_options(osc PRIVATE -fsanitize=thread) target_link_options(osc PRIVATE -fsanitize=thread)
+21
View File
@@ -0,0 +1,21 @@
#ifndef MIDIINPUT
#define MIDIINPUT
#include <rtmidi/RtMidi.h>
#include <iostream>
#include <stdexcept>
class MidiInput{
public:
void list_ports();
void open_port(unsigned int port);
void read_message();
private:
RtMidiIn midiIn;
};
#endif
+2
View File
@@ -17,6 +17,7 @@ class Synth {
amplitude = 0.5; amplitude = 0.5;
sample_rate = 48000; sample_rate = 48000;
phase = 0.0; phase = 0.0;
current_note = 0;
}; };
double generate(); double generate();
@@ -43,6 +44,7 @@ class Synth {
double phase; double phase;
uint32_t sample_rate; uint32_t sample_rate;
uint8_t current_note;
}; };
#endif #endif
+34 -18
View File
@@ -7,6 +7,7 @@
#include "../include/audio_engine.hpp" #include "../include/audio_engine.hpp"
#include "../include/spsc_queue.hpp" #include "../include/spsc_queue.hpp"
#include "../include/command.hpp" #include "../include/command.hpp"
#include "../include/midi_input.hpp"
// void producer(SpscQueue& queue, std::atomic<bool>& producer_done){ // void producer(SpscQueue& queue, std::atomic<bool>& producer_done){
// for (int i = 0; i < 32; ++i){ // for (int i = 0; i < 32; ++i){
@@ -60,22 +61,27 @@
int main(){ int main(){
// std::cout << "440.гц\n"; std::string a = "";
// std::string a = ""; Synth synth;
// Synth synth; // Command command;
Command command;
SpscQueue queue; SpscQueue queue;
MidiInput midi;
for (int i = 0; i < 33; ++i){ midi.list_ports();
std::cout << queue.push({CommandType::NoteOn, 60, 100}) << "\n"; midi.open_port(2);
std::cout << queue.push({CommandType::NoteOn, 20, 80}) << "\n"; while (true) midi.read_message();
}
std::cout << "выгрузка\n";
for (int i = 0; i < 65; ++i){
std::cout << queue.pop(command) << "\n";
}
// 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<int>(command.type) << "\n"; // std::cout << static_cast<int>(command.type) << "\n";
// std::cout << static_cast<int>(command.note)<< "\n"; // std::cout << static_cast<int>(command.note)<< "\n";
// std::cout << static_cast<int>(command.velocity) << "\n"; // std::cout << static_cast<int>(command.velocity) << "\n";
@@ -93,6 +99,7 @@ int main(){
// thread2.join(); // thread2.join();
// synth.set_frequency(440); // synth.set_frequency(440);
// synth.set_amplitude(0.5); // synth.set_amplitude(0.5);
// //
// try{ // try{
@@ -101,14 +108,23 @@ int main(){
// while (true){ // while (true){
// std::cin >> a; // std::cin >> a;
// if (a == "on"){ // if (a == "on"){
// if (!synth.note_on()){ // synth.note_on(69, 127);
// throw std::runtime_error("ошибка: не удалось воспроизвести ноту"); // 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"){ // else if (a == "off"){
// if (!synth.note_off()){ // synth.note_off(60);
// throw std::runtime_error("ошибка: не удалось воспроизвести ноту"); // // if (!synth.note_off()){
// } // // throw std::runtime_error("ошибка: не удалось воспроизвести ноту");
// // }
// } // }
// else if (a == "exit"){ // else if (a == "exit"){
// audio_engine.stop(); // audio_engine.stop();
+34
View File
@@ -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<unsigned char> message;
midiIn.getMessage(&message);
if (!message.empty()){
for (const auto& elem : message){
std::cout << static_cast<int>(elem) << "\n";
}
}
}
+13
View File
@@ -6,6 +6,7 @@ double Synth::generate(){
Command command; Command command;
while (processed < 16 && queue.pop(command)){ while (processed < 16 && queue.pop(command)){
switch (command.type){ switch (command.type){
default: default:
break; break;
@@ -14,12 +15,24 @@ double Synth::generate(){
break; break;
case CommandType::NoteOn: case CommandType::NoteOn:
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<double>(command.velocity) / 127);
envelope.note_on(); envelope.note_on();
}
++processed; ++processed;
break; break;
case CommandType::NoteOff: case CommandType::NoteOff:
if (command.note == current_note){
envelope.note_off(); envelope.note_off();
}
++processed; ++processed;
break; break;
} }