From de210b571d7bf3d14278efbc430ba5c94ec04090 Mon Sep 17 00:00:00 2001 From: Robert Bendun Date: Sun, 13 Nov 2022 23:26:06 +0100 Subject: [PATCH] By default connect to existing MIDI port in repl --- musique/main.cc | 8 ++++++-- musique/midi/midi.hh | 3 +++ musique/midi/rt_midi.cc | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/musique/main.cc b/musique/main.cc index 2686ca3..3ae5fc7 100644 --- a/musique/main.cc +++ b/musique/main.cc @@ -178,9 +178,13 @@ struct Runner std::cout << "Connected MIDI output to port " << *output_port << ". Ready to play!" << std::endl; } } else { - midi.connect_output(); + bool connected_to_existing_port = midi.connect_or_create_output(); if (!quiet_mode) { - std::cout << "Created new MIDI output port 'Musique'. Ready to play!" << std::endl; + 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; + } } } diff --git a/musique/midi/midi.hh b/musique/midi/midi.hh index bdfc18e..04a2624 100644 --- a/musique/midi/midi.hh +++ b/musique/midi/midi.hh @@ -26,6 +26,8 @@ namespace midi { ~Rt_Midi() override = default; + bool connect_or_create_output(); + /// Connect with MIDI virtual port void connect_output(); @@ -35,6 +37,7 @@ namespace midi /// List available ports void list_ports(std::ostream &out) const; + bool supports_output() const override; void send_note_on (uint8_t channel, uint8_t note_number, uint8_t velocity) override; diff --git a/musique/midi/rt_midi.cc b/musique/midi/rt_midi.cc index 0307e19..0e21ae6 100644 --- a/musique/midi/rt_midi.cc +++ b/musique/midi/rt_midi.cc @@ -34,6 +34,22 @@ try { 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() try { ensure(not output.has_value(), "Reconeccting is not supported yet");