Skip to content

Commit

Permalink
Implemented InputNode
Browse files Browse the repository at this point in the history
  • Loading branch information
mfep committed Mar 28, 2024
1 parent 19a7f1c commit 051ac96
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/midi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_library(midi
add_library(midi STATIC
InputNode.cpp
InputObserver.cpp
MidiEngine.cpp
MidiGraph.cpp
Expand Down
34 changes: 34 additions & 0 deletions src/midi/InputNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "InputNode.hpp"

#include "MidiProbe.hpp"

#include <spdlog/spdlog.h>

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<unsigned char>* message,
void* user_data)
{
auto* instance = static_cast<InputNode*>(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<int>(error_code), message);
}
32 changes: 32 additions & 0 deletions src/midi/InputNode.hpp
Original file line number Diff line number Diff line change
@@ -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<unsigned char>* 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

0 comments on commit 051ac96

Please sign in to comment.