..
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
+35
-19
@@ -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<bool>& 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<int>(command.type) << "\n";
|
||||
// std::cout << static_cast<int>(command.note)<< "\n";
|
||||
// std::cout << static_cast<int>(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();
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
-2
@@ -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<double>(command.velocity) / 127);
|
||||
envelope.note_on();
|
||||
}
|
||||
++processed;
|
||||
break;
|
||||
|
||||
case CommandType::NoteOff:
|
||||
envelope.note_off();
|
||||
if (command.note == current_note){
|
||||
envelope.note_off();
|
||||
}
|
||||
++processed;
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user