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