author | František Kučera <franta-hg@frantovo.cz> |
Wed, 30 Sep 2020 00:34:41 +0200 | |
branch | v_0 |
changeset 3 | baa8055c5b10 |
parent 2 | 01553c2ffa1d |
child 4 | 65dfbf0494a3 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) |
|
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> |
22 |
#include <vector> |
|
23 |
#include <iostream> |
|
24 |
#include <sstream> |
|
25 |
#include <locale> |
|
26 |
#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
|
27 |
#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
|
28 |
#include <signal.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
|
29 |
|
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
30 |
#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
|
31 |
#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
|
32 |
#include <jack/ringbuffer.h> |
0 | 33 |
|
34 |
#include <relpipe/common/type/typedefs.h> |
|
35 |
#include <relpipe/reader/TypeId.h> |
|
36 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
37 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
38 |
||
39 |
#include "Configuration.h" |
|
40 |
#include "JackException.h" |
|
41 |
||
42 |
namespace relpipe { |
|
43 |
namespace out { |
|
44 |
namespace jack { |
|
45 |
||
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
46 |
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
|
47 |
|
0 | 48 |
class JackHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
49 |
private: |
|
50 |
Configuration& configuration; |
|
51 |
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
|
52 |
|
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
53 |
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
|
54 |
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
|
55 |
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
|
56 |
|
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
57 |
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
|
58 |
|
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
59 |
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
|
60 |
|
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 |
|
0 | 67 |
public: |
68 |
||
69 |
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
|
70 |
// Initialize JACK connection: |
2
01553c2ffa1d
configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
71 |
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
|
72 |
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
|
73 |
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
|
74 |
|
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
75 |
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
|
76 |
|
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
77 |
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
|
78 |
// 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
|
79 |
|
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
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
|
81 |
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
|
82 |
|
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
83 |
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
|
84 |
|
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
85 |
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
|
86 |
if (jackError) throw JackException(L"Could not activate client."); |
0 | 87 |
} |
88 |
||
89 |
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
|
90 |
// 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
|
91 |
|
0 | 92 |
} |
93 |
||
94 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
95 |
// 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
|
96 |
// TODO: if (continueProcessing) {} ? |
0 | 97 |
} |
98 |
||
99 |
void endOfPipe() { |
|
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
100 |
// TODO: send optional (configurable) MIDI events |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
101 |
// TODO: wait until the ring buffer is empty |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
102 |
} |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
103 |
|
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
104 |
int dequeueMessages(jack_nframes_t frames) { |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
105 |
|
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
106 |
|
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
107 |
// TODO: send events from the ring buffer to JACK + correct timing |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
108 |
std::cout << "dequeueMessages(" << frames << ")" << std::endl; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
109 |
|
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
110 |
|
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
111 |
return 0; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
112 |
} |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
113 |
|
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
114 |
void finish(int sig) { |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
115 |
continueProcessing = false; |
0 | 116 |
} |
117 |
||
1
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
118 |
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
|
119 |
// 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
|
120 |
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
|
121 |
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
|
122 |
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
|
123 |
} |
7f3ab657dc50
link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
124 |
|
0 | 125 |
}; |
126 |
||
3
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
127 |
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
|
128 |
JackHandler* instance = (JackHandler*) arg; |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
129 |
return instance->dequeueMessages(frames); |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
130 |
} |
baa8055c5b10
ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
131 |
|
0 | 132 |
} |
133 |
} |
|
134 |
} |