diff --git a/src/midi/CMakeLists.txt b/src/midi/CMakeLists.txt index 8d79a26..fba8ac2 100644 --- a/src/midi/CMakeLists.txt +++ b/src/midi/CMakeLists.txt @@ -1,4 +1,5 @@ -add_library(midi +add_library(midi STATIC + InputNode.cpp InputObserver.cpp MidiEngine.cpp MidiGraph.cpp diff --git a/src/midi/InputNode.cpp b/src/midi/InputNode.cpp new file mode 100644 index 0000000..efff112 --- /dev/null +++ b/src/midi/InputNode.cpp @@ -0,0 +1,34 @@ +#include "InputNode.hpp" + +#include "MidiProbe.hpp" + +#include + +mc::midi::InputNode::InputNode(const InputInfo& info) + : m_info(info), m_midi_in(RtMidi::UNSPECIFIED, MidiProbe::get_midi_client_name()) +{ + m_midi_in.setCallback(message_callback, this); + m_midi_in.setErrorCallback(error_callback, nullptr); + m_midi_in.openPort(m_info.m_id); +} + +void mc::midi::InputNode::enable_message_types(const MessageTypeMask& mask) +{ + m_midi_in.ignoreTypes(!mask.m_sysex_enabled, !mask.m_time_enabled, !mask.m_sensing_enabled); +} + +void mc::midi::InputNode::message_callback(double time_stamp, + std::vector* message, + void* user_data) +{ + auto* instance = static_cast(user_data); + instance->broadcast_data(*message); + instance->raise_message_received(instance->m_info.m_id, *message); +} + +void mc::midi::InputNode::error_callback(RtMidiError::Type error_code, + const std::string& message, + void* user_data) +{ + spdlog::error("Error {} occured in MIDI input: \"{}\"", static_cast(error_code), message); +} diff --git a/src/midi/InputNode.hpp b/src/midi/InputNode.hpp new file mode 100644 index 0000000..9a0cf8c --- /dev/null +++ b/src/midi/InputNode.hpp @@ -0,0 +1,32 @@ +#pragma once +#include "InputObserver.hpp" +#include "MidiEngine.hpp" +#include "MidiGraph.hpp" +#include "MidiInfo.hpp" + +#include "RtMidi.h" + +namespace mc::midi +{ + +class InputNode final : public Node, public InputObservable +{ +public: + explicit InputNode(const InputInfo& info); + + void enable_message_types(const MessageTypeMask& mask); + +private: + static void message_callback(double time_stamp, + std::vector* message, + void* user_data); + + static void error_callback(RtMidiError::Type error_code, + const std::string& message, + void* user_data); + + InputInfo m_info; + RtMidiIn m_midi_in; +}; + +} // namespace mc::midi