author | František Kučera <franta-hg@frantovo.cz> |
Sat, 03 Oct 2020 11:37:07 +0200 | |
branch | v_0 |
changeset 11 | 1c0b9981eb6d |
parent 10 | 6aa881c10efd |
child 12 | f56620b95180 |
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 |
|
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
55 |
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
|
56 |
|
10 | 57 |
/** |
58 |
* Is passed through the ring buffer |
|
59 |
* from the relpipe-reading thread to the jack-writing thread (callback). |
|
60 |
*/ |
|
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
61 |
struct MidiMessage { |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
62 |
uint8_t buffer[4096]; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
63 |
uint32_t size; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
64 |
uint32_t time; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
65 |
}; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
66 |
|
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
|
67 |
/** |
11
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
68 |
* JACK callbacks (called from the real-time thread) |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
69 |
*/ |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
70 |
class RealTimeContext { |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
71 |
public: |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
72 |
jack_client_t* jackClient = nullptr; |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
73 |
jack_port_t* jackPort = nullptr; |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
74 |
jack_ringbuffer_t* ringBuffer = nullptr; |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
75 |
|
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
76 |
const int RING_BUFFER_SIZE = 100; |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
77 |
|
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
78 |
int processCallback(jack_nframes_t frames) { |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
79 |
const size_t queuedMessages = jack_ringbuffer_read_space(ringBuffer) / sizeof (MidiMessage); |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
80 |
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 |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
81 |
jack_midi_clear_buffer(jackPortBuffer); // TODO: clean buffer? |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
82 |
for (size_t i = 0; i < queuedMessages && i < frames; i++) { |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
83 |
// TODO: correct timing? |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
84 |
MidiMessage m; |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
85 |
jack_ringbuffer_read(ringBuffer, (char*) &m, sizeof (MidiMessage)); |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
86 |
jack_midi_data_t* midiData = jack_midi_event_reserve(jackPortBuffer, m.time, m.size); |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
87 |
memcpy(midiData, m.buffer, m.size); |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
88 |
} |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
89 |
return 0; |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
90 |
} |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
91 |
|
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
92 |
static int processCallback(jack_nframes_t frames, void* arg) { |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
93 |
return static_cast<RealTimeContext*> (arg)->processCallback(frames); |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
94 |
} |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
95 |
|
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
96 |
} realTimeContext; |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
97 |
|
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
98 |
/** |
10 | 99 |
* 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
|
100 |
*/ |
10 | 101 |
class RelationContext { |
102 |
public: |
|
103 |
size_t indexOfEvent = -1; |
|
104 |
size_t indexOfChannel = -1; |
|
105 |
size_t indexOfNoteOn = -1; |
|
106 |
size_t indexOfNotePitch = -1; |
|
107 |
size_t indexOfNoteVelocity = -1; |
|
108 |
size_t indexOfControllerId = -1; |
|
109 |
size_t indexOfControllerValue = -1; |
|
110 |
size_t indexOfRaw = -1; |
|
111 |
||
112 |
size_t attributeCount = 0; |
|
113 |
||
114 |
class RecordContext { |
|
115 |
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
|
116 |
|
10 | 117 |
enum class Event { |
118 |
NOTE, |
|
119 |
CONTROL, |
|
120 |
SYSEX, |
|
121 |
UNKNOWN |
|
122 |
}; |
|
123 |
||
124 |
static Event parseEventType(const relpipe::common::type::StringX name) { |
|
125 |
if (name == L"note") return Event::NOTE; |
|
126 |
else if (name == L"control") return Event::CONTROL; |
|
127 |
else if (name == L"sysex") return Event::SYSEX; |
|
128 |
else return Event::UNKNOWN; |
|
129 |
} |
|
130 |
||
131 |
Event event; |
|
132 |
relpipe::common::type::Integer channel; |
|
133 |
relpipe::common::type::Boolean noteOn; |
|
134 |
relpipe::common::type::Integer notePitch; |
|
135 |
relpipe::common::type::Integer noteVelocity; |
|
136 |
relpipe::common::type::Integer controllerId; |
|
137 |
relpipe::common::type::Integer controllerValue; |
|
138 |
relpipe::common::type::StringX raw; |
|
139 |
||
140 |
size_t attributeIndex = 0; |
|
141 |
||
142 |
} recordContext; |
|
143 |
||
144 |
} 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
|
145 |
|
0 | 146 |
public: |
147 |
||
148 |
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
|
149 |
// Initialize JACK connection: |
2
01553c2ffa1d
configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
150 |
std::string clientName = convertor.to_bytes(configuration.jackClientName); |
11
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
151 |
realTimeContext.jackClient = jack_client_open(clientName.c_str(), JackNullOption, nullptr); |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
152 |
if (realTimeContext.jackClient == nullptr) throw JackException(L"Could not create JACK client."); |
1
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
153 |
|
11
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
154 |
realTimeContext.ringBuffer = jack_ringbuffer_create(realTimeContext.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
|
155 |
|
11
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
156 |
jack_set_process_callback(realTimeContext.jackClient, RealTimeContext::processCallback, &realTimeContext); |
1
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
157 |
// 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
|
158 |
|
11
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
159 |
realTimeContext.jackPort = jack_port_register(realTimeContext.jackClient, "output", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0); |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
160 |
if (realTimeContext.jackPort == nullptr) throw JackException(L"Could not register port."); |
1
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
161 |
|
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
162 |
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
|
163 |
|
11
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
164 |
int jackError = jack_activate(realTimeContext.jackClient); |
1
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
165 |
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
|
166 |
|
6be3464ccb2b
wait for a port connection
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
167 |
// Wait for a port connection, because it does not make much sense to send MIDI events nowhere: |
11
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
168 |
while (jack_port_connected(realTimeContext.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
|
169 |
|
27579c5cdc51
send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
170 |
// TODO: configurable auto-connection to another client/port |
0 | 171 |
} |
172 |
||
173 |
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
|
174 |
// 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
|
175 |
|
10 | 176 |
relationContext = RelationContext(); |
177 |
relationContext.attributeCount = attributes.size(); |
|
178 |
||
179 |
for (int i = 0; i < relationContext.attributeCount; i++) { |
|
180 |
const relpipe::common::type::StringX attributeName = attributes[i].getAttributeName(); |
|
181 |
if (attributeName == L"event") relationContext.indexOfEvent = i; |
|
182 |
else if (attributeName == L"channel") relationContext.indexOfChannel = i; |
|
183 |
else if (attributeName == L"controller_id") relationContext.indexOfControllerId = i; |
|
184 |
else if (attributeName == L"controller_value") relationContext.indexOfControllerValue = i; |
|
185 |
else if (attributeName == L"note_on") relationContext.indexOfNoteOn = i; |
|
186 |
else if (attributeName == L"note_pitch") relationContext.indexOfNotePitch = i; |
|
187 |
else if (attributeName == L"note_velocity") relationContext.indexOfNoteVelocity = i; |
|
188 |
else if (attributeName == L"raw") relationContext.indexOfRaw = i; |
|
189 |
} |
|
11
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
190 |
|
10 | 191 |
// TODO: check also data types and skipt relation if important attributes are missing |
192 |
// TODO: configurable relation name |
|
0 | 193 |
} |
194 |
||
195 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
10 | 196 |
// 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
|
197 |
// 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
|
198 |
// 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
|
199 |
|
8
27579c5cdc51
send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
200 |
// 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
|
201 |
// currentMidiMessage.size = …; |
27579c5cdc51
send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
202 |
// 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
|
203 |
|
8
27579c5cdc51
send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
204 |
// 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
|
205 |
// TODO: real data |
10 | 206 |
|
207 |
{ |
|
208 |
RelationContext& rel = relationContext; |
|
209 |
RelationContext::RecordContext& rec = rel.recordContext; |
|
210 |
||
211 |
if (rel.indexOfEvent == rec.attributeIndex) rec.event = rec.parseEventType(value); |
|
212 |
else if (rel.indexOfChannel == rec.attributeIndex) rec.channel = std::stoi(value); |
|
213 |
else if (rel.indexOfControllerId == rec.attributeIndex) rec.controllerId = std::stoi(value); |
|
214 |
else if (rel.indexOfControllerValue == rec.attributeIndex) rec.controllerValue = std::stoi(value); |
|
215 |
else if (rel.indexOfNoteOn == rec.attributeIndex) rec.noteOn = value == L"true"; |
|
216 |
else if (rel.indexOfNotePitch == rec.attributeIndex) rec.notePitch = std::stoi(value); |
|
217 |
else if (rel.indexOfNoteVelocity == rec.attributeIndex) rec.noteVelocity = std::stoi(value); |
|
218 |
else if (rel.indexOfRaw == rec.attributeIndex) rec.raw = value; |
|
219 |
} |
|
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
|
220 |
|
10 | 221 |
relationContext.recordContext.attributeIndex++; |
222 |
||
223 |
if (relationContext.recordContext.attributeIndex == relationContext.attributeCount) { |
|
11
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
224 |
if (jack_ringbuffer_write_space(realTimeContext.ringBuffer) >= sizeof (MidiMessage)) { |
10 | 225 |
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
|
226 |
|
10 | 227 |
// TODO: convert relationContext.recordContext to m |
228 |
m.time = 0; |
|
229 |
m.size = 3; |
|
230 |
m.buffer[0] = 0x90; |
|
231 |
m.buffer[1] = 0x24; |
|
232 |
m.buffer[2] = 0x40; |
|
233 |
||
11
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
234 |
jack_ringbuffer_write(realTimeContext.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
|
235 |
} 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
|
236 |
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
|
237 |
} |
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
|
238 |
|
10 | 239 |
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
|
240 |
} |
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
|
241 |
|
0 | 242 |
} |
243 |
||
244 |
void endOfPipe() { |
|
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
245 |
// 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
|
246 |
|
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
|
247 |
// Wait until the ring buffer is empty |
11
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
248 |
while (continueProcessing && jack_ringbuffer_read_space(realTimeContext.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
|
249 |
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
|
250 |
// 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
|
251 |
// 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
|
252 |
} |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
253 |
|
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
254 |
void finish(int sig) { |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
255 |
continueProcessing = false; |
0 | 256 |
} |
257 |
||
1
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
258 |
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
|
259 |
// Close JACK connection: |
11
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
260 |
jack_deactivate(realTimeContext.jackClient); |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
261 |
jack_client_close(realTimeContext.jackClient); |
1c0b9981eb6d
move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
262 |
jack_ringbuffer_free(realTimeContext.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
|
263 |
} |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
264 |
|
0 | 265 |
}; |
266 |
||
267 |
} |
|
268 |
} |
|
269 |
} |