-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevice.cpp
executable file
·250 lines (212 loc) · 6.64 KB
/
Device.cpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "Device.h"
#include "Alsa.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <alsa/asoundlib.h>
using namespace atom;
using namespace std;
using namespace ps;
namespace c = std::chrono;
namespace
{
bool toOnOff(snd_seq_event_type_t type)
{
return type == SND_SEQ_EVENT_NOTEON;
}
snd_seq_addr_t findPort(snd_seq_t& seq, const string& portName)
{
snd_seq_client_info_t* cinfo = nullptr;
snd_seq_client_info_alloca(&cinfo);
snd_seq_port_info_t* pinfo = nullptr;
snd_seq_port_info_alloca(&pinfo);
snd_seq_client_info_set_client(cinfo, -1);
while (snd_seq_query_next_client(&seq, cinfo) >= 0) {
int client = snd_seq_client_info_get_client(cinfo);
snd_seq_port_info_set_client(pinfo, client);
snd_seq_port_info_set_port(pinfo, -1);
while (snd_seq_query_next_port(&seq, pinfo) >= 0) {
if (snd_seq_port_info_get_name(pinfo) == portName) {
return snd_seq_addr_t{
(uint8_t)snd_seq_port_info_get_client(pinfo),
(uint8_t)snd_seq_port_info_get_port(pinfo)
};
}
}
}
throw Exception("Could not find midi port {}", portName);
}
}
void ps::convertNote(atom::Note note, snd_seq_event& out)
{
out.type = note.OnOff ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
out.data.note.channel = note.Channel;
out.data.note.note = static_cast<uint8_t>(note.Note);
out.data.note.velocity = note.Velocity;
}
Device::Device(const char* devicePortName)
{
int err = snd_seq_open(&_seq, "default", SND_SEQ_OPEN_DUPLEX, 0);
if (err < 0) {
throw DeviceInitError("Failed to initialize ALSA (open sequencer)");
}
err = snd_seq_set_client_name(_seq, "pisample");
if (err < 0) {
throw DeviceInitError("Failed to initialize ALSA (set client name)");
}
err = snd_seq_create_simple_port(_seq, "host-atom",
SND_SEQ_PORT_CAP_READ |
SND_SEQ_PORT_CAP_SUBS_READ |
SND_SEQ_PORT_CAP_WRITE |
SND_SEQ_PORT_CAP_SUBS_WRITE,
SND_SEQ_PORT_TYPE_MIDI_GENERIC |
SND_SEQ_PORT_TYPE_APPLICATION);
if (err < 0) {
throw DeviceInitError("Failed to initialize ALSA (create simple port)");
}
_hostPort = err;
cerr << "Our port: [app]: " << _hostPort << '\n';
_deviceAddress = findPort(*_seq, devicePortName);
err = snd_seq_connect_from(_seq, _hostPort,
_deviceAddress.client, _deviceAddress.port);
if (err < 0) {
throw DeviceInitError("Failed to connect to input '{}'", devicePortName);
}
err = snd_seq_connect_to(_seq, _hostPort,
_deviceAddress.client, _deviceAddress.port);
if (err < 0) {
throw DeviceInitError("Failed to connect to output '{},", devicePortName);
}
sendNotes(atom::initSequence());
forAllButtons([this](Buttons b) { sendControl(Control{ (uint8_t)b, 0x00 }); });
_nfds = snd_seq_poll_descriptors_count(_seq, POLLIN);
_fds = make_unique<pollfd[]>(_nfds);
// TODO: may need to be moved inside the loop in case we change it
// dynamically ?
snd_seq_poll_descriptors(_seq, _fds.get(), _nfds, POLLIN);
// Drain any input sent before we started.
while (true) {
int nActive = ::poll(_fds.get(), _nfds, 0);
if (nActive > 0) {
snd_seq_event_t* event = nullptr;
snd_seq_event_input(_seq, &event);
}
else {
break;
}
}
fmt::print("Connected to device:port : {}:{} ({})\n",
(int)_deviceAddress.client,
(int)_deviceAddress.port,
devicePortName);
}
Device::~Device()
{
forAllPads([this](Pad p){ sendNotes( changePadMode(p, PadMode::Off) ); });
forAllButtons([this](Buttons b) { sendControl(Control{ (uint8_t)b, 0x00 }); });
snd_seq_close(_seq);
}
void Device::sendNotes(const vector<Note>& notes)
{
snd_seq_event note;
for (auto& n: notes) {
memset(¬e, 0, sizeof(note));
snd_seq_ev_set_direct(¬e);
note.type = n.OnOff ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
note.source.port = _hostPort;
note.dest = _deviceAddress;
note.data.note.channel = n.Channel;
note.data.note.note = n.Note;
note.data.note.velocity = n.Velocity;
int err = snd_seq_event_output_direct(_seq, ¬e);
if (err < 0) {
throw DeviceInitError("Failed to send note to device: {}", err);
}
}
}
void Device::sendControl(Control c)
{
snd_seq_event ctl;
memset(&ctl, 0, sizeof(ctl));
snd_seq_ev_set_direct(&ctl);
ctl.type = SND_SEQ_EVENT_CONTROLLER;
ctl.source.port = _hostPort;
ctl.dest = _deviceAddress;
ctl.data.control.channel = 0;
ctl.data.control.param = c.Param;
ctl.data.control.value = c.Value;
int err = snd_seq_event_output_direct(_seq, &ctl);
if (err < 0) {
throw DeviceInitError("Failed to send control to device: {}", err);
}
}
bool Device::poll()
{
int nActive = ::poll(_fds.get(), _nfds, 0);
if (nActive == 0) {
return false;
}
if (nActive < 0) {
throw DeviceFailure("Failed to get next event in poll");
}
snd_seq_event_t* event = nullptr;
do {
// Note : should always have something since poll succeeded.
// We are in blocking mode though blocking is not wished.
int err = snd_seq_event_input(_seq, &event);
if (err < 0) {
throw DeviceFailure("snd_seq_event_input had an error");
}
if (event == nullptr) {
break; // should have happened really, but defensive
}
auto printType = [&] {
cout << "Source: " << (int) event->source.client << ":"
<< (int) event->source.port
<< ", type: " << eventToString(event->type);
};
switch (event->type) {
case SND_SEQ_EVENT_NOTEON:
case SND_SEQ_EVENT_NOTEOFF:
if (_synth) {
_synth->event(Note{
.OnOff = toOnOff(event->type),
.Channel = event->data.note.channel,
.Note = event->data.note.note,
.Velocity = event->data.note.velocity
});
}
else {
printType();
cout << '\n';
}
break;
case SND_SEQ_EVENT_CONTROLLER:
if (event->data.control.channel != 0) {
cerr << "UNKNOWN CHANNEL IN CONTROL: " <<
(int)event->data.control.channel << ", dropping\n";
break;
}
if (_synth) {
_synth->event(Control{
.Param = (uint8_t)event->data.control.param,
.Value = (uint8_t)event->data.control.value
});
}
else {
printType();
cout << '\n';
}
break;
case SND_SEQ_EVENT_CHANPRESS:
printType();
cout << '\n';
break;
case SND_SEQ_EVENT_PORT_UNSUBSCRIBED:
printType();
cerr << ", Device disconnected, exiting\n";
return EXIT_FAILURE;
}
} while (snd_seq_event_input_pending(_seq, false) > 0);
return true;
}