author | František Kučera <franta-hg@frantovo.cz> |
Sat, 03 Oct 2020 00:05:43 +0200 | |
branch | v_0 |
changeset 10 | 6aa881c10efd |
parent 9 | 14cf28d7681c |
child 11 | 1c0b9981eb6d |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
10 | 3 |
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
0 | 4 |
* |
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
#pragma once |
|
18 |
||
19 |
#include <memory> |
|
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
20 |
#include <atomic> |
0 | 21 |
#include <string> |
6
4174fc0c2e7c
send some (constant, dummy) MIDI message to JACK
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
22 |
#include <cstring> |
0 | 23 |
#include <vector> |
24 |
#include <iostream> |
|
25 |
#include <sstream> |
|
26 |
#include <locale> |
|
27 |
#include <codecvt> |
|
1
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
28 |
#include <sys/mman.h> |
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
29 |
#include <signal.h> |
4
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
30 |
#include <unistd.h> |
1
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
31 |
|
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
32 |
#include <jack/jack.h> |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
33 |
#include <jack/midiport.h> |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
34 |
#include <jack/ringbuffer.h> |
0 | 35 |
|
36 |
#include <relpipe/common/type/typedefs.h> |
|
37 |
#include <relpipe/reader/TypeId.h> |
|
38 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
39 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
40 |
||
41 |
#include "Configuration.h" |
|
42 |
#include "JackException.h" |
|
43 |
||
44 |
namespace relpipe { |
|
45 |
namespace out { |
|
46 |
namespace jack { |
|
47 |
||
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
48 |
int dequeueMessages(jack_nframes_t frames, void* arg); |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
49 |
|
0 | 50 |
class JackHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
51 |
private: |
|
52 |
Configuration& configuration; |
|
53 |
std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding |
|
1
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
54 |
|
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
55 |
jack_client_t* jackClient = nullptr; |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
jack_port_t* jackPort = nullptr; |
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
57 |
jack_ringbuffer_t* ringBuffer = nullptr; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
58 |
|
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
59 |
std::atomic<bool> continueProcessing{true}; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
60 |
|
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
61 |
const int RING_BUFFER_SIZE = 100; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
62 |
|
10 | 63 |
/** |
64 |
* Is passed through the ring buffer |
|
65 |
* from the relpipe-reading thread to the jack-writing thread (callback). |
|
66 |
*/ |
|
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
67 |
struct MidiMessage { |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
68 |
uint8_t buffer[4096]; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
69 |
uint32_t size; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
70 |
uint32_t time; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
71 |
}; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
72 |
|
4
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
73 |
/** |
10 | 74 |
* Temporary storage of values read from relational input. |
4
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
75 |
*/ |
10 | 76 |
class RelationContext { |
77 |
public: |
|
78 |
size_t indexOfEvent = -1; |
|
79 |
size_t indexOfChannel = -1; |
|
80 |
size_t indexOfNoteOn = -1; |
|
81 |
size_t indexOfNotePitch = -1; |
|
82 |
size_t indexOfNoteVelocity = -1; |
|
83 |
size_t indexOfControllerId = -1; |
|
84 |
size_t indexOfControllerValue = -1; |
|
85 |
size_t indexOfRaw = -1; |
|
86 |
||
87 |
size_t attributeCount = 0; |
|
88 |
||
89 |
class RecordContext { |
|
90 |
public: |
|
4
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
91 |
|
10 | 92 |
enum class Event { |
93 |
NOTE, |
|
94 |
CONTROL, |
|
95 |
SYSEX, |
|
96 |
UNKNOWN |
|
97 |
}; |
|
98 |
||
99 |
static Event parseEventType(const relpipe::common::type::StringX name) { |
|
100 |
if (name == L"note") return Event::NOTE; |
|
101 |
else if (name == L"control") return Event::CONTROL; |
|
102 |
else if (name == L"sysex") return Event::SYSEX; |
|
103 |
else return Event::UNKNOWN; |
|
104 |
} |
|
105 |
||
106 |
Event event; |
|
107 |
relpipe::common::type::Integer channel; |
|
108 |
relpipe::common::type::Boolean noteOn; |
|
109 |
relpipe::common::type::Integer notePitch; |
|
110 |
relpipe::common::type::Integer noteVelocity; |
|
111 |
relpipe::common::type::Integer controllerId; |
|
112 |
relpipe::common::type::Integer controllerValue; |
|
113 |
relpipe::common::type::StringX raw; |
|
114 |
||
115 |
size_t attributeIndex = 0; |
|
116 |
||
117 |
} recordContext; |
|
118 |
||
119 |
} relationContext; |
|
4
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
120 |
|
0 | 121 |
public: |
122 |
||
123 |
JackHandler(Configuration& configuration) : configuration(configuration) { |
|
1
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
124 |
// Initialize JACK connection: |
2
01553c2ffa1d
configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
125 |
std::string clientName = convertor.to_bytes(configuration.jackClientName); |
1
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
126 |
jackClient = jack_client_open(clientName.c_str(), JackNullOption, nullptr); |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
127 |
if (jackClient == nullptr) throw JackException(L"Could not create JACK client."); |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
128 |
|
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
129 |
ringBuffer = jack_ringbuffer_create(RING_BUFFER_SIZE * sizeof (MidiMessage)); |
1
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
130 |
|
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
131 |
jack_set_process_callback(jackClient, relpipe::out::jack::dequeueMessages, this); |
1
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
132 |
// TODO: report also other events (connections etc.) |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
133 |
|
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
134 |
jackPort = jack_port_register(jackClient, "output", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0); |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
135 |
if (jackPort == nullptr) throw JackException(L"Could not register port."); |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
136 |
|
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
137 |
if (mlockall(MCL_CURRENT | MCL_FUTURE)) fwprintf(stderr, L"Warning: Can not lock memory.\n"); |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
138 |
|
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
139 |
int jackError = jack_activate(jackClient); |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
140 |
if (jackError) throw JackException(L"Could not activate client."); |
5
6be3464ccb2b
wait for a port connection
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
141 |
|
6be3464ccb2b
wait for a port connection
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
142 |
// Wait for a port connection, because it does not make much sense to send MIDI events nowhere: |
9
14cf28d7681c
option: --required-jack-connections (start after given number of port connections)
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
143 |
while (jack_port_connected(jackPort) < configuration.requiredJackConnections) usleep(10000); |
8
27579c5cdc51
send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
144 |
|
27579c5cdc51
send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
145 |
// TODO: configurable auto-connection to another client/port |
0 | 146 |
} |
147 |
||
148 |
void startRelation(const relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
149 |
// TODO: validate metadata and prepare attribute mappings (names and types are important, order does not matter) |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
150 |
|
10 | 151 |
relationContext = RelationContext(); |
152 |
relationContext.attributeCount = attributes.size(); |
|
153 |
||
154 |
for (int i = 0; i < relationContext.attributeCount; i++) { |
|
155 |
const relpipe::common::type::StringX attributeName = attributes[i].getAttributeName(); |
|
156 |
if (attributeName == L"event") relationContext.indexOfEvent = i; |
|
157 |
else if (attributeName == L"channel") relationContext.indexOfChannel = i; |
|
158 |
else if (attributeName == L"controller_id") relationContext.indexOfControllerId = i; |
|
159 |
else if (attributeName == L"controller_value") relationContext.indexOfControllerValue = i; |
|
160 |
else if (attributeName == L"note_on") relationContext.indexOfNoteOn = i; |
|
161 |
else if (attributeName == L"note_pitch") relationContext.indexOfNotePitch = i; |
|
162 |
else if (attributeName == L"note_velocity") relationContext.indexOfNoteVelocity = i; |
|
163 |
else if (attributeName == L"raw") relationContext.indexOfRaw = i; |
|
164 |
} |
|
165 |
||
166 |
// TODO: check also data types and skipt relation if important attributes are missing |
|
167 |
// TODO: configurable relation name |
|
0 | 168 |
} |
169 |
||
170 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
10 | 171 |
// TODO: switch to RelationalReaderStringHandler |
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
172 |
// TODO: append to current message + if this is last attribute, put whole message to the ring buffer |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
173 |
// TODO: if (continueProcessing) {} ? |
4
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
174 |
|
8
27579c5cdc51
send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
175 |
// memcpy(currentMidiMessage.buffer, ….buffer, ….size); |
27579c5cdc51
send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
176 |
// currentMidiMessage.size = …; |
27579c5cdc51
send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
177 |
// currentMidiMessage.time = …; |
7
b2fc99295546
send correct number of messages to JACK + wait in order to be sure that all events were sent
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
178 |
|
8
27579c5cdc51
send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
179 |
// TODO: correct timing? |
27579c5cdc51
send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
180 |
// TODO: real data |
10 | 181 |
|
182 |
{ |
|
183 |
RelationContext& rel = relationContext; |
|
184 |
RelationContext::RecordContext& rec = rel.recordContext; |
|
185 |
||
186 |
if (rel.indexOfEvent == rec.attributeIndex) rec.event = rec.parseEventType(value); |
|
187 |
else if (rel.indexOfChannel == rec.attributeIndex) rec.channel = std::stoi(value); |
|
188 |
else if (rel.indexOfControllerId == rec.attributeIndex) rec.controllerId = std::stoi(value); |
|
189 |
else if (rel.indexOfControllerValue == rec.attributeIndex) rec.controllerValue = std::stoi(value); |
|
190 |
else if (rel.indexOfNoteOn == rec.attributeIndex) rec.noteOn = value == L"true"; |
|
191 |
else if (rel.indexOfNotePitch == rec.attributeIndex) rec.notePitch = std::stoi(value); |
|
192 |
else if (rel.indexOfNoteVelocity == rec.attributeIndex) rec.noteVelocity = std::stoi(value); |
|
193 |
else if (rel.indexOfRaw == rec.attributeIndex) rec.raw = value; |
|
194 |
} |
|
7
b2fc99295546
send correct number of messages to JACK + wait in order to be sure that all events were sent
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
195 |
|
10 | 196 |
relationContext.recordContext.attributeIndex++; |
197 |
||
198 |
if (relationContext.recordContext.attributeIndex == relationContext.attributeCount) { |
|
199 |
if (jack_ringbuffer_write_space(ringBuffer) >= sizeof (MidiMessage)) { |
|
200 |
MidiMessage m; |
|
8
27579c5cdc51
send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
201 |
|
10 | 202 |
// TODO: convert relationContext.recordContext to m |
203 |
m.time = 0; |
|
204 |
m.size = 3; |
|
205 |
m.buffer[0] = 0x90; |
|
206 |
m.buffer[1] = 0x24; |
|
207 |
m.buffer[2] = 0x40; |
|
208 |
||
209 |
jack_ringbuffer_write(ringBuffer, (const char *) &m, sizeof (m)); |
|
4
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
210 |
} else { |
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
211 |
fwprintf(stderr, L"Error: ring buffer is full → skipping event.\n"); |
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
212 |
} |
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
213 |
|
10 | 214 |
relationContext.recordContext = RelationContext::RecordContext(); |
4
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
215 |
} |
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
216 |
|
0 | 217 |
} |
218 |
||
219 |
void endOfPipe() { |
|
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
220 |
// TODO: send optional (configurable) MIDI events |
4
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
221 |
|
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
222 |
// Wait until the ring buffer is empty |
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
223 |
while (continueProcessing && jack_ringbuffer_read_space(ringBuffer)) usleep(1000); |
7
b2fc99295546
send correct number of messages to JACK + wait in order to be sure that all events were sent
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
224 |
usleep(1000000); |
b2fc99295546
send correct number of messages to JACK + wait in order to be sure that all events were sent
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
225 |
// TODO: better waiting (ringBuffer might be empty, but events have not been sent to JACK yet) |
8
27579c5cdc51
send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
226 |
// TODO: optionally mute all; probably enabled by default |
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
227 |
} |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
228 |
|
10 | 229 |
/** |
230 |
* TODO: use separate class/instance for JACK callbacks to minimize scope and prevent mistakes. |
|
231 |
*/ |
|
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
232 |
int dequeueMessages(jack_nframes_t frames) { |
6
4174fc0c2e7c
send some (constant, dummy) MIDI message to JACK
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
233 |
const size_t queuedMessages = jack_ringbuffer_read_space(ringBuffer) / sizeof (MidiMessage); |
7
b2fc99295546
send correct number of messages to JACK + wait in order to be sure that all events were sent
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
234 |
void* jackPortBuffer = jack_port_get_buffer(jackPort, frames); // jack_port_get_buffer() must be called outside the loop, otherwise it will multiply the MIDI events |
b2fc99295546
send correct number of messages to JACK + wait in order to be sure that all events were sent
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
235 |
jack_midi_clear_buffer(jackPortBuffer); // TODO: clean buffer? |
6
4174fc0c2e7c
send some (constant, dummy) MIDI message to JACK
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
236 |
for (size_t i = 0; i < queuedMessages && i < frames; i++) { |
4174fc0c2e7c
send some (constant, dummy) MIDI message to JACK
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
237 |
// TODO: correct timing? |
4174fc0c2e7c
send some (constant, dummy) MIDI message to JACK
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
238 |
MidiMessage m; |
4174fc0c2e7c
send some (constant, dummy) MIDI message to JACK
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
239 |
jack_ringbuffer_read(ringBuffer, (char*) &m, sizeof (MidiMessage)); |
4174fc0c2e7c
send some (constant, dummy) MIDI message to JACK
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
240 |
jack_midi_data_t* midiData = jack_midi_event_reserve(jackPortBuffer, m.time, m.size); |
4174fc0c2e7c
send some (constant, dummy) MIDI message to JACK
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
241 |
memcpy(midiData, m.buffer, m.size); |
4
65dfbf0494a3
pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
242 |
} |
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
243 |
return 0; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
244 |
} |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
245 |
|
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
246 |
void finish(int sig) { |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
247 |
continueProcessing = false; |
0 | 248 |
} |
249 |
||
1
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
250 |
virtual ~JackHandler() { |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
251 |
// Close JACK connection: |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
252 |
jack_deactivate(jackClient); |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
253 |
jack_client_close(jackClient); |
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
254 |
jack_ringbuffer_free(ringBuffer); |
1
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
255 |
} |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
256 |
|
0 | 257 |
}; |
258 |
||
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
259 |
int dequeueMessages(jack_nframes_t frames, void* arg) { |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
260 |
JackHandler* instance = (JackHandler*) arg; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
261 |
return instance->dequeueMessages(frames); |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
262 |
} |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
263 |
|
0 | 264 |
} |
265 |
} |
|
266 |
} |