-
Notifications
You must be signed in to change notification settings - Fork 4
/
jackthread.cpp
123 lines (98 loc) · 3.31 KB
/
jackthread.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
#include "jackthread.h"
// Implemented in main.cpp
int process_wrapper(jack_nframes_t nframes, void *arg);
JackThread::JackThread() :
QObject()
{
}
JackThread::~JackThread()
{
if (client != 0)
{
jack_set_process_callback(client, 0, 0);
jack_client_close(client);
}
}
void JackThread::setup() {
ready = false;
if((client = jack_client_open ("RegieControl", JackNullOption, NULL)) == 0)
{
emit error("Jack server not running?");
}
jack_set_process_callback (client, process_wrapper, 0);
input_port = jack_port_register (client, "in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput , 0);
feedback_port = jack_port_register (client, "feedback", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
if (jack_activate(client))
{
emit error("Cannot activate jack client");
}
qDebug() << "Trying to connect ports";
const char **ports;
// Connecting our IN port => looking for outputs
ports = jack_get_ports (client, "nanoKONTROL2", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput);
if (ports == NULL) {
qDebug() << "Cannot find any MIDI output ports";
} else {
qDebug() << "Trying to connect our" << jack_port_name(input_port) << "to" << ports[0];
if (jack_connect (client, ports[0], jack_port_name (input_port))) {
qDebug() << "failed";
} else {
qDebug() << "success";
}
free (ports);
}
// Connecting our feedack (=OUT) port => looking for inputs
ports = jack_get_ports (client, "nanoKONTROL2", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput);
if (ports == NULL) {
qDebug() << "Cannot find any MIDI input ports";
} else {
qDebug() << "Trying to connect our" << jack_port_name(feedback_port) << "to" << ports[0];
if (jack_connect (client, jack_port_name (feedback_port), ports[0])) {
qDebug() << "failed";
} else {
qDebug() << "success";
}
free (ports);
}
ready = true;
}
int JackThread::process(jack_nframes_t nframes, void *arg) {
Q_UNUSED(arg);
uint32_t i = 0;
if (!ready)
{
return 0;
}
void* feedback_port_buf = jack_port_get_buffer(feedback_port, 1);
jack_midi_clear_buffer(feedback_port_buf);
while (midiEventsToBeSent.length() > 0)
{
QByteArray* data = midiEventsToBeSent.dequeue();
unsigned char* out;
out = jack_midi_event_reserve(feedback_port_buf, 0, data->size());
memcpy(out, data->data(), data->size());
free(data);
}
void* in_port_buf = jack_port_get_buffer(input_port , nframes);
if (in_port_buf == 0)
{
return 0;
}
jack_midi_event_t in_event;
jack_nframes_t event_count = jack_midi_get_event_count(in_port_buf);
//printf ("We have %d events to handle\n", event_count);
if (!event_count) return 0;
for (i = 0; i < event_count; i++) {
jack_midi_event_get(&in_event, in_port_buf, i);
emit midiEvent((char)in_event.buffer[0], (char)in_event.buffer[1], (char)in_event.buffer[2]);
}
return 0;
}
void JackThread::set_led(unsigned char num, unsigned char value)
{
QByteArray* array = new QByteArray();
array->append(0xb0); // MIDI ControlChange
array->append(num);
array->append(value);
midiEventsToBeSent.enqueue(array);
}