builtin port fix

This commit is contained in:
Robert Bendun 2023-02-15 00:23:50 +01:00
parent 25b446a5cc
commit f46a866613
7 changed files with 64 additions and 65 deletions

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- `port` function have proper semantics now
## [0.4.0]
### Added

View File

@ -1637,8 +1637,8 @@ static Result<Value> builtin_port(Interpreter &interpreter, std::vector<Value> a
for (auto const& [key, port] : Context::established_connections) {
if (port == interpreter.current_context->port) {
return std::visit(Overloaded {
[](midi::connections::Virtual_Port) { return Value(Symbol("virtual")); },
[](midi::connections::Established_Port port) { return Value(Number(port)); },
[](midi::Virtual_Port) { return Value(Symbol("virtual")); },
[](midi::Established_Port port) { return Value(Number(port)); },
}, key);
}
}
@ -1647,14 +1647,14 @@ static Result<Value> builtin_port(Interpreter &interpreter, std::vector<Value> a
if (auto a = match<Number>(args)) {
auto [port_number] = *a;
Try(interpreter.current_context->connect(port_number.floor().as_int()));
Try(interpreter.current_context->connect(midi::Established_Port(port_number.floor().as_int())));
return {};
}
if (auto a = match<Symbol>(args)) {
auto [port_type] = *a;
if (port_type == "virtual") {
Try(interpreter.current_context->connect(std::nullopt));
Try(interpreter.current_context->connect(midi::Virtual_Port{}));
return {};
}

View File

@ -14,11 +14,11 @@ std::chrono::duration<float> Context::length_to_duration(std::optional<Number> l
}
template<>
struct std::hash<midi::connections::Key>
struct std::hash<midi::Port>
{
std::size_t operator()(midi::connections::Key const& value) const
std::size_t operator()(midi::Port const& value) const
{
using namespace midi::connections;
using namespace midi;
return hash_combine(value.index(), std::visit(Overloaded {
[](Virtual_Port) { return 0u; },
[](Established_Port port) { return port; },
@ -26,35 +26,33 @@ struct std::hash<midi::connections::Key>
}
};
std::unordered_map<midi::connections::Key, std::shared_ptr<midi::Connection>> Context::established_connections;
std::unordered_map<midi::Port, std::shared_ptr<midi::Connection>> Context::established_connections;
/// Establish connection to given port
std::optional<Error> Context::connect(std::optional<Port_Number> port_number)
std::optional<Error> Context::connect(std::optional<midi::Port> desired_port)
{
// FIXME This function doesn't support creating virtual ports when established ports are available
using namespace midi::connections;
auto const key = port_number ? Key(*port_number) : Key(Virtual_Port{});
if (auto it = established_connections.find(key); it != established_connections.end()) {
port = it->second;
return std::nullopt;
}
if (port_number) {
if (!desired_port) {
if (not established_connections.empty()) {
port = established_connections.begin()->second;
return std::nullopt;
}
auto connection = std::make_shared<midi::Rt_Midi>();
connection->connect_output(*port_number);
established_connections[*port_number] = connection;
established_connections[connection->establish_any_connection()] = connection;
port = connection;
return std::nullopt;
}
auto connection = std::make_shared<midi::Rt_Midi>();
if (connection->connect_or_create_output()) {
established_connections[0u] = connection;
} else {
established_connections[Virtual_Port{}] = connection;
if (auto it = established_connections.find(*desired_port); it != established_connections.end()) {
port = it->second;
return std::nullopt;
}
auto connection = std::make_shared<midi::Rt_Midi>();
connection->connect(*desired_port);
established_connections[*desired_port] = connection;
port = connection;
return std::nullopt;
}

View File

@ -9,17 +9,6 @@
#include <musique/value/note.hh>
#include <musique/value/number.hh>
namespace midi::connections
{
using Established_Port = unsigned int;
struct Virtual_Port
{
bool operator==(Virtual_Port const&) const = default;
};
using Key = std::variant<Established_Port, Virtual_Port>;
}
/// Context holds default values for music related actions
struct Context
@ -39,12 +28,12 @@ struct Context
using Port_Number = unsigned int;
/// Connections that have been established so far
static std::unordered_map<midi::connections::Key, std::shared_ptr<midi::Connection>> established_connections;
static std::unordered_map<midi::Port, std::shared_ptr<midi::Connection>> established_connections;
/// Establish connection to given port
///
/// If port number wasn't provided connect to first existing one or create one
std::optional<Error> connect(std::optional<Port_Number>);
std::optional<Error> connect(std::optional<midi::Port>);
/// Fills empty places in Note like octave and length with default values from context
Note fill(Note) const;

View File

@ -169,7 +169,7 @@ struct Runner
ensure(the == nullptr, "Only one instance of runner is supported");
the = this;
interpreter.current_context->connect(std::nullopt);
// interpreter.current_context->connect(std::nullopt);
Env::global->force_define("say", +[](Interpreter &interpreter, std::vector<Value> args) -> Result<Value> {
for (auto it = args.begin(); it != args.end(); ++it) {

View File

@ -4,10 +4,20 @@
#include <functional>
#include <optional>
#include <string>
#include <variant>
// Documentation of midi messages available at http://midi.teragonaudio.com/tech/midispec.htm
namespace midi
{
using Established_Port = unsigned int;
struct Virtual_Port
{
bool operator==(Virtual_Port const&) const = default;
};
using Port = std::variant<Established_Port, Virtual_Port>;
struct Connection
{
virtual ~Connection() = default;
@ -26,18 +36,15 @@ namespace midi
{
~Rt_Midi() override = default;
bool connect_or_create_output();
/// Connect to existing MIDI output or create virtual port
Port establish_any_connection();
/// Connect with MIDI virtual port
void connect_output();
/// Connect with specific MIDI port for outputing MIDI messages
void connect_output(unsigned target);
/// Connect to given port
void connect(Port port);
/// 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;
@ -45,6 +52,7 @@ namespace midi
void send_program_change(uint8_t channel, uint8_t program) override;
void send_controller_change(uint8_t channel, uint8_t controller_number, uint8_t value) override;
std::optional<RtMidiOut> output;
};

View File

@ -34,15 +34,16 @@ try {
std::exit(33);
}
bool midi::Rt_Midi::connect_or_create_output()
midi::Port midi::Rt_Midi::establish_any_connection()
try {
output.emplace();
if (output->getPortCount()) {
output->openPort(0);
return true;
output->openPort(0, "Musique");
return Established_Port { 0 };
}
output->openVirtualPort("Musique");
return false;
return Virtual_Port{};
}
catch (RtMidiError &error) {
// TODO(error)
@ -50,23 +51,22 @@ catch (RtMidiError &error) {
std::exit(33);
}
void midi::Rt_Midi::connect_output()
void midi::Rt_Midi::connect(midi::Port port)
try {
ensure(not output.has_value(), "Reconeccting is not supported yet");
output.emplace();
output->openVirtualPort("Musique");
} catch (RtMidiError &error) {
// TODO(error)
std::cerr << "Failed to use MIDI connection: " << error.getMessage() << std::endl;
std::exit(33);
std::visit(Overloaded{
[this](midi::Virtual_Port) {
output.emplace();
output->openVirtualPort("Musique");
},
[this](midi::Established_Port port) {
output.emplace();
output->openPort(port, "Musique");
},
}, port);
output->setClientName("Musique");
output->setPortName("Musique");
}
void midi::Rt_Midi::connect_output(unsigned target)
try {
ensure(not output.has_value(), "Reconeccting is not supported yet");
output.emplace();
output->openPort(target);
} catch (RtMidiError &error) {
catch (RtMidiError &error) {
// TODO(error)
std::cerr << "Failed to use MIDI connection: " << error.getMessage() << std::endl;
std::exit(33);