-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroundaboutthread.cpp
293 lines (271 loc) · 11.8 KB
/
roundaboutthread.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
Copyright 2011 Arne Jacobs <[email protected]>
This file is part of Roundabout.
Roundabout is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Roundabout is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Roundabout. If not, see <http://www.gnu.org/licenses/>.
*/
#include "roundaboutthread.h"
#include "roundaboutsequencer.h"
RoundaboutThread::RoundaboutThread(QObject *parent) :
QThread(parent),
shutdown(false),
client(0),
sequencer(0),
activeSequencer(0),
stepsPerBeat(4),
stepExpectedAtNextBufferBegin(true)
{
midiInput.reserve(4096);
midiOutput.reserve(4096);
inboundEventsInterfaces.reserve(1024);
// connect to the jack server:
client = jack_client_open("Roundabout", JackNullOption, 0);
if (client) {
bool success = true;
sampleRate = jack_get_sample_rate(client);
// register process callback:
success = success && (jack_set_process_callback(client, process, this) == 0);
// register ports:
midiInputPort = jack_port_register(client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
midiOutputPort = jack_port_register(client, "midi out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
audioOutputPort = jack_port_register(client, "audio out", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
success = success && midiInputPort && midiOutputPort && audioOutputPort;
// start the jack client:
success = success && (jack_activate(client) == 0);
if (!success) {
jack_client_close(client);
client = 0;
}
}
// start the QThread:
if (isValid()) {
start();
}
}
RoundaboutThread::~RoundaboutThread()
{
if (isValid()) {
// close the jack client:
jack_client_close(client);
// send a shutdown event to the QThread:
RoundaboutThreadOutboundEvent event;
event.eventType = RoundaboutThreadOutboundEvent::SHUTDOWN;
writeOutboundEvent(event);
outboundCondition.wakeAll();
// wait for the thread to finish:
wait();
}
}
bool RoundaboutThread::isValid() const
{
return client;
}
void RoundaboutThread::processInboundEvents()
{
// process our events:
InboundEventsHelper<RoundaboutThreadInboundEvent>::processInboundEvents();
// process other events:
for (int i = 0; i < inboundEventsInterfaces.size(); i++) {
inboundEventsInterfaces[i]->processInboundEvents();
}
}
void RoundaboutThread::processOutboundEvents()
{
// process our events:
OutboundEventsHelper<RoundaboutThreadOutboundEvent>::processOutboundEvents();
// process other events:
for (int i = 0; i < outboundEventsInterfaces.size(); i++) {
outboundEventsInterfaces[i]->processOutboundEvents();
}
}
QString RoundaboutThread::getJackClientName() const
{
return QString(jack_get_client_name(client));
}
void RoundaboutThread::createSequencer()
{
RoundaboutSequencer *sequencer = new RoundaboutSequencer(this);
RoundaboutThreadInboundEvent inboundEvent;
inboundEvent.eventType = RoundaboutThreadInboundEvent::CREATE_SEQUENCER;
inboundEvent.sequencer = sequencer;
writeInboundEvent(inboundEvent);
}
void RoundaboutThread::setStepsPerBeat(double stepsPerBeat)
{
RoundaboutThreadInboundEvent inboundEvent;
inboundEvent.eventType = RoundaboutThreadInboundEvent::CHANGE_STEPS_PER_BEAT;
inboundEvent.stepsPerBeat = stepsPerBeat;
writeInboundEvent(inboundEvent);
}
void RoundaboutThread::setInputChannel(int channel)
{
RoundaboutThreadInboundEvent inboundEvent;
inboundEvent.eventType = RoundaboutThreadInboundEvent::CHANGE_INPUT_CHANNEL;
inboundEvent.channel = qBound(0, channel, 15);
writeInboundEvent(inboundEvent);
}
void RoundaboutThread::setOutputChannel(int channel)
{
RoundaboutThreadInboundEvent inboundEvent;
inboundEvent.eventType = RoundaboutThreadInboundEvent::CHANGE_OUTPUT_CHANNEL;
inboundEvent.channel = qBound(0, channel, 15);
writeInboundEvent(inboundEvent);
}
void RoundaboutThread::run()
{
for (; !shutdown; ) {
// wait for outbound events:
QMutexLocker locker(&outboundMutex);
outboundCondition.wait(&outboundMutex);
processOutboundEvents();
}
}
void RoundaboutThread::processInboundEvent(RoundaboutThreadInboundEvent &inboundEvent)
{
if (inboundEvent.eventType == RoundaboutThreadInboundEvent::CREATE_SEQUENCER) {
inboundEventsInterfaces.append(inboundEvent.sequencer);
if (sequencer == 0) {
sequencer = inboundEvent.sequencer;
}
sequencers.append(inboundEvent.sequencer);
RoundaboutThreadOutboundEvent outboundEvent;
outboundEvent.eventType = RoundaboutThreadOutboundEvent::CREATED_SEQUENCER;
outboundEvent.sequencer = inboundEvent.sequencer;
writeOutboundEvent(outboundEvent);
} else if (inboundEvent.eventType == RoundaboutThreadInboundEvent::CHANGE_STEPS_PER_BEAT) {
stepsPerBeat = inboundEvent.stepsPerBeat;
} else if (inboundEvent.eventType == RoundaboutThreadInboundEvent::CHANGE_INPUT_CHANNEL) {
for (int i = 0; i < sequencers.size(); i++) {
sequencers[i]->processChangeInputChannel(inboundEvent.channel);
}
} else if (inboundEvent.eventType == RoundaboutThreadInboundEvent::CHANGE_OUTPUT_CHANNEL) {
for (int i = 0; i < sequencers.size(); i++) {
sequencers[i]->processChangeOutputChannel(inboundEvent.channel);
}
}
}
void RoundaboutThread::processOutboundEvent(RoundaboutThreadOutboundEvent &event)
{
if (event.eventType == RoundaboutThreadOutboundEvent::CREATED_SEQUENCER) {
outboundEventsInterfaces.append(event.sequencer);
createdSequencer(event.sequencer);
} else if (event.eventType == RoundaboutThreadOutboundEvent::SHUTDOWN) {
shutdown = true;
}
}
int RoundaboutThread::process(jack_nframes_t nframes)
{
// get transport state:
jack_position_t currentPos;
jack_transport_state_t currentState = jack_transport_query(client, ¤tPos);
// get midi input buffer:
void *midiInputBuffer = jack_port_get_buffer(midiInputPort, nframes);
// copy input midi events:
midiInput.resize(0);
jack_nframes_t midiInputEventCount = jack_midi_get_event_count(midiInputBuffer);
jack_nframes_t midiInputEventIndex = 0;
// get midi output buffer:
void *midiOutputBuffer = jack_port_get_buffer(midiOutputPort, nframes);
jack_midi_clear_buffer(midiOutputBuffer);
processInboundEvents();
if (sequencer) {
if ((currentPos.valid & JackPositionBBT) && (currentState == JackTransportRolling)) {
jack_nframes_t bbt_offset = (currentPos.valid & JackBBTFrameOffset ? currentPos.bbt_offset : 0);
double framesPerMinute = 60.0 * currentPos.frame_rate;
double ticksPerMinute = (double)currentPos.ticks_per_beat * (double)currentPos.beats_per_minute;
double ticksPerFrame = ticksPerMinute / framesPerMinute;
// current tick is bbt_offset frames before the first frame
double currentTick = (double)bbt_offset * ticksPerFrame + (double)currentPos.tick;
// beat position is current tick / ticks per beat:
double currentBeat = currentTick / (double)currentPos.ticks_per_beat + (double)(currentPos.beat - 1);
// current step is position in beat * steps per beat:
double currentStep = currentBeat * stepsPerBeat;
double stepPosition = currentStep - (int)currentStep;
double framesPerStep = framesPerMinute / ((double)currentPos.beats_per_minute * stepsPerBeat);
jack_nframes_t nextStep = (jack_nframes_t)(framesPerStep - stepPosition * framesPerStep);
if (stepExpectedAtNextBufferBegin && (nextStep > nframes / 2)) {
nextStep = 0;
stepPosition += 1.0;
}
for (; nextStep < nframes; ) {
// process all midi input events up to nextStep:
midiInput.resize(0);
for (; midiInputEventIndex < midiInputEventCount; ) {
jack_midi_event_t jackMidiEvent;
jack_midi_event_get(&jackMidiEvent, midiInputBuffer, midiInputEventIndex);
if (jackMidiEvent.time <= nextStep) {
MidiEvent midiEvent(jackMidiEvent.size);
memcpy(midiEvent.buffer, jackMidiEvent.buffer, jackMidiEvent.size * sizeof(jack_midi_data_t));
midiInput.append(midiEvent);
midiInputEventIndex++;
} else {
break;
}
}
for (int i = 0; i < sequencers.size(); i++) {
sequencers[i]->processMidiEvents(midiInput);
}
if (activeSequencer) {
// leave the current step and output the corresponding midi (note off) events:
midiOutput.resize(0);
activeSequencer->processStepEnd(midiOutput);
for (int i = 0; i < midiOutput.size(); i++) {
const MidiEvent &event = midiOutput[i];
jack_midi_event_write(midiOutputBuffer, nextStep, event.buffer, event.size);
}
}
activeSequencer = sequencer;
// enter the next step and output the corresponding midi (note on) events:
midiOutput.resize(0);
sequencer = sequencer->processStepBegin(midiOutput);
for (int i = 0; i < midiOutput.size(); i++) {
const MidiEvent &event = midiOutput[i];
jack_midi_event_write(midiOutputBuffer, nextStep, event.buffer, event.size);
}
stepPosition -= 1.0;
nextStep = (jack_nframes_t)(framesPerStep - stepPosition * framesPerStep);
}
// process all midi input events that are left:
midiInput.resize(0);
for (; midiInputEventIndex < midiInputEventCount; ) {
jack_midi_event_t jackMidiEvent;
jack_midi_event_get(&jackMidiEvent, midiInputBuffer, midiInputEventIndex);
MidiEvent midiEvent(jackMidiEvent.size);
memcpy(midiEvent.buffer, jackMidiEvent.buffer, jackMidiEvent.size * sizeof(jack_midi_data_t));
midiInput.append(midiEvent);
midiInputEventIndex++;
}
for (int i = 0; i < sequencers.size(); i++) {
sequencers[i]->processMidiEvents(midiInput);
}
stepExpectedAtNextBufferBegin = (nextStep == nframes);
} else if (activeSequencer) {
// leave the current step and output the corresponding midi (note off) events:
midiOutput.resize(0);
for (int i = 0; i < sequencers.size(); i++) {
sequencers[i]->processStop(midiOutput);
}
for (int i = 0; i < midiOutput.size(); i++) {
const MidiEvent &event = midiOutput[i];
jack_midi_event_write(midiOutputBuffer, 0, event.buffer, event.size);
}
activeSequencer = 0;
sequencer = sequencers.first();
stepExpectedAtNextBufferBegin = true;
}
}
outboundCondition.wakeAll();
return 0;
}
int RoundaboutThread::process(jack_nframes_t nframes, void *arg)
{
return ((RoundaboutThread*)arg)->process(nframes);
}