blob: 17e354d8254639e45f0af7289b9285535fdc1c91 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
#pragma once
#include "types.h"
#include "submitter.h"
#include "sender.h"
#include <rtmidi/RtMidi.h>
#include <string>
namespace midi_router
{
struct Device_Connection : public Sender
{
Device_Connection(Device_Id const & source_id, std::string const & device_name, Submitter & submitter, RtMidiIn::RtMidiCallback callback):
source_id(source_id),
device_name(device_name),
submitter(submitter)
{
midi_in.setCallback(callback, this);
midi_in.ignoreTypes(true, false, true);
}
Device_Id const & source_id;
std::string const & device_name;
Submitter & submitter;
RtMidiIn midi_in {};
RtMidiOut midi_out {};
void
send(std::vector<std::uint8_t> const & payload) override
{
midi_out.sendMessage(&payload);
}
std::string
get_id() const override
{
return source_id;
}
void
open()
{
if (!open_port(midi_in, device_name))
{
std::cerr << "Input port not found for device " << device_name << "\n";
}
if (!open_port(midi_out, device_name))
{
std::cerr << "Output port not found for device " << device_name << "\n";
}
}
void
close()
{
midi_in.closePort();
midi_out.closePort();
}
private:
bool
open_port(RtMidi & midi, std::string const & name)
{
for (std::size_t i = 0; i < midi.getPortCount(); ++i)
{
if (midi.getPortName(i).contains(name))
{
midi.openPort(i);
return true;
}
}
return false;
}
};
} // namespace midi_router
|