By default connect to existing MIDI port in repl

This commit is contained in:
Robert Bendun 2022-11-13 23:26:06 +01:00
parent e101d362b9
commit de210b571d
3 changed files with 25 additions and 2 deletions

View File

@ -178,11 +178,15 @@ struct Runner
std::cout << "Connected MIDI output to port " << *output_port << ". Ready to play!" << std::endl; std::cout << "Connected MIDI output to port " << *output_port << ". Ready to play!" << std::endl;
} }
} else { } else {
midi.connect_output(); bool connected_to_existing_port = midi.connect_or_create_output();
if (!quiet_mode) { if (!quiet_mode) {
if (connected_to_existing_port) {
std::cout << "Connected MIDI output to port " << midi.output->getPortName(0) << std::endl;
} else {
std::cout << "Created new MIDI output port 'Musique'. Ready to play!" << std::endl; std::cout << "Created new MIDI output port 'Musique'. Ready to play!" << std::endl;
} }
} }
}
Env::global->force_define("say", +[](Interpreter &interpreter, std::vector<Value> args) -> Result<Value> { Env::global->force_define("say", +[](Interpreter &interpreter, std::vector<Value> args) -> Result<Value> {
for (auto it = args.begin(); it != args.end(); ++it) { for (auto it = args.begin(); it != args.end(); ++it) {

View File

@ -26,6 +26,8 @@ namespace midi
{ {
~Rt_Midi() override = default; ~Rt_Midi() override = default;
bool connect_or_create_output();
/// Connect with MIDI virtual port /// Connect with MIDI virtual port
void connect_output(); void connect_output();
@ -35,6 +37,7 @@ namespace midi
/// List available ports /// List available ports
void list_ports(std::ostream &out) const; void list_ports(std::ostream &out) const;
bool supports_output() const override; bool supports_output() const override;
void send_note_on (uint8_t channel, uint8_t note_number, uint8_t velocity) override; void send_note_on (uint8_t channel, uint8_t note_number, uint8_t velocity) override;

View File

@ -34,6 +34,22 @@ try {
std::exit(33); std::exit(33);
} }
bool midi::Rt_Midi::connect_or_create_output()
try {
output.emplace();
if (auto pc = output->getPortCount()) {
output->openPort(0);
return true;
}
output->openVirtualPort("Musique");
return false;
}
catch (RtMidiError &error) {
// TODO(error)
std::cerr << "Failed to use MIDI connection: " << error.getMessage() << std::endl;
std::exit(33);
}
void midi::Rt_Midi::connect_output() void midi::Rt_Midi::connect_output()
try { try {
ensure(not output.has_value(), "Reconeccting is not supported yet"); ensure(not output.has_value(), "Reconeccting is not supported yet");