-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjacktransportclient.cpp
305 lines (270 loc) · 12.4 KB
/
jacktransportclient.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
294
295
296
297
298
299
300
301
302
303
304
305
/*
Copyright 2011 Arne Jacobs <[email protected]>
This file is part of elektrocillin.
Elektrocillin 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.
Elektrocillin 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 Elektrocillin. If not, see <http://www.gnu.org/licenses/>.
*/
#include "jacktransportclient.h"
#include "graphicsclientitem.h"
#include "graphicsdiscretecontrolitem.h"
#include <QDebug>
JackTransportClient::JackTransportClient(const QString &clientName, size_t ringBufferSize) :
JackThreadEventProcessorClient(new JackTransportThread(this), clientName, QStringList(), QStringList("Beat") + QStringList("Bar"), QStringList(), QStringList(), ringBufferSize),
ringBufferToThread(ringBufferSize),
lastTransportFrameTime(0),
currentBarTime(0),
beatsPerMinute(120),
beatsPerBar(4),
beatType(4),
ticksPerBeat(1920)
{
JackTransportThread *thread = (JackTransportThread*)getJackThread();
thread->setRingBufferFromClient(&ringBufferToThread);
}
JackTransportClient::~JackTransportClient()
{
// calling close will stop the Jack client and also stop the associated thread:
close();
// deleting the associated thread is now safe, as it has been stopped:
delete getJackThread();
}
void JackTransportClient::saveState(QDataStream &stream)
{
JackThreadEventProcessorClient::saveState(stream);
stream << beatsPerMinute << beatsPerBar << beatType << ticksPerBeat;
}
void JackTransportClient::loadState(QDataStream &stream)
{
JackThreadEventProcessorClient::loadState(stream);
stream >> beatsPerMinute >> beatsPerBar >> beatType >> ticksPerBeat;
}
QGraphicsItem * JackTransportClient::createGraphicsItem()
{
JackTransportGraphicsItem *graphicsItem = new JackTransportGraphicsItem(this);
JackTransportThread *thread = (JackTransportThread*)getJackThread();
QObject::connect(thread, SIGNAL(changedPosition(QString)), graphicsItem, SLOT(changePosition(QString)));
return graphicsItem;
}
void JackTransportClient::changeBeatsPerMinute(double bpm)
{
BeatsPerMinuteEvent *event = new BeatsPerMinuteEvent(bpm);
postEvent(event);
}
void JackTransportClient::changeBeatsPerBar(int beatsPerBar)
{
BeatsPerBarEvent *event = new BeatsPerBarEvent(beatsPerBar);
postEvent(event);
}
void JackTransportClient::changeBeatType(int beatType)
{
BeatTypeEvent *event = new BeatTypeEvent(beatType);
postEvent(event);
}
bool JackTransportClient::init()
{
// register a jack timebase callback:
jack_set_timebase_callback(getClient(), 0, timebase, this);
return JackThreadEventProcessorClient::init();
}
bool JackTransportClient::process(jack_nframes_t nframes)
{
// get the current transport state and send it to the associated thread:
currentState = jack_transport_query(getClient(), ¤tPos);
ringBufferToThread.write(currentPos);
wakeJackThread();
// compute a few values which are used in processAudio():
bbt_offset = (currentPos.valid & JackBBTFrameOffset ? currentPos.bbt_offset : 0);
framesPerMinute = 60.0 * currentPos.frame_rate;
ticksPerMinute = (double)currentPos.ticks_per_beat * (double)currentPos.beats_per_minute;
ticksPerFrame = ticksPerMinute / framesPerMinute;
// do the standard processing:
JackThreadEventProcessorClient::process(nframes);
return true;
}
void JackTransportClient::processAudio(const double *, double *outputs, jack_nframes_t time)
{
if ((currentPos.valid & JackPositionBBT) && (currentState == JackTransportRolling)) {
// current tick is bbt_offset frames before the first frame
double currentTick = (double)(time + 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);
double beatPosition = currentBeat - (int)currentBeat;
// bar position is current beat / beats per bar:
double barPosition = currentBeat / (double)currentPos.beats_per_bar;
barPosition = barPosition - (int)barPosition;
// stretch these value (which lie in [0:1]) to [-1:1]:
outputs[0] = beatPosition * 2.0 - 1.0;
outputs[1] = barPosition * 2.0 - 1.0;
} else {
outputs[0] = 0;
outputs[1] = 0;
}
}
void JackTransportClient::processNoteOn(unsigned char channel, unsigned char noteNumber, unsigned char velocity, jack_nframes_t time)
{
// set the bpm from the midi note number:
beatsPerMinute = noteNumber + 60;
}
void JackTransportClient::processNoteOff(unsigned char channel, unsigned char noteNumber, unsigned char velocity, jack_nframes_t time)
{
}
void JackTransportClient::processAfterTouch(unsigned char channel, unsigned char noteNumber, unsigned char pressure, jack_nframes_t time)
{
}
void JackTransportClient::processController(unsigned char channel, unsigned char controller, unsigned char value, jack_nframes_t time)
{
}
void JackTransportClient::processPitchBend(unsigned char channel, unsigned int value, jack_nframes_t time)
{
}
void JackTransportClient::processChannelPressure(unsigned char channel, unsigned char pressure, jack_nframes_t time)
{
}
bool JackTransportClient::processEvent(const RingBufferEvent *event, jack_nframes_t time)
{
if (const BeatsPerMinuteEvent *event_ = dynamic_cast<const BeatsPerMinuteEvent*>(event)) {
beatsPerMinute = event_->beatsPerMinute;
return true;
} else if (const BeatsPerBarEvent *event_ = dynamic_cast<const BeatsPerBarEvent*>(event)) {
beatsPerBar = event_->beatsPerBar;
return true;
} else if (const BeatTypeEvent *event_ = dynamic_cast<const BeatTypeEvent*>(event)) {
beatType = event_->beatType;
return true;
} else {
return false;
}
}
void JackTransportClient::timebase(jack_transport_state_t state, jack_nframes_t nframes, jack_position_t *pos, int new_pos)
{
// we provide the JackPositionBBT fields (and the recommended frame offset):
pos->valid = (jack_position_bits_t)(JackPositionBBT | JackBBTFrameOffset);
pos->beats_per_bar = beatsPerBar;
pos->beat_type = beatType;
pos->ticks_per_beat = ticksPerBeat;
pos->beats_per_minute = beatsPerMinute;
if (pos->frame != 0) {
// compute the current beat time based on the previous beat time and the current tempo:
double incrementInMinutes = ((double)pos->frame - lastTransportFrameTime) / ((double)pos->frame_rate * 60.0);
currentBarTime += incrementInMinutes * beatsPerMinute / beatsPerBar;
} else {
currentBarTime = 0;
}
double currentBeatTime = currentBarTime * beatsPerBar;
lastTransportFrameTime = pos->frame;
double tick = currentBeatTime * (double)ticksPerBeat;
double bar = currentBeatTime / (double)beatsPerBar;
pos->tick = (int)tick;
// save the difference between rounded-down tick and floating-point tick in frames in the frame offset field:
// 1 tick = 1 minute / beatsPerMinute / ticksPerBeat
// 1 frame = 1 minute / 60 / frame_rate => 1 minute = 1 frame * 60 * frame_rate
// => 1 tick = 1 frame * 60 * frame_rate / beatsPerMinute / ticksPerBeat
double tickDifference = tick - (double)pos->tick;
pos->bbt_offset = (jack_nframes_t)(tickDifference * 60.0 * (double)pos->frame_rate / ((double)beatsPerMinute * (double)ticksPerBeat));
pos->tick = pos->tick % ticksPerBeat;
pos->beat = (int)currentBeatTime % beatsPerBar + 1;
pos->bar = (int)bar + 1;
pos->bar_start_tick = (double)(pos->bar - 1) * (double)beatsPerBar * (double)ticksPerBeat;
}
void JackTransportClient::timebase(jack_transport_state_t state, jack_nframes_t nframes, jack_position_t *pos, int new_pos, void *arg)
{
JackTransportClient *client = (JackTransportClient*)arg;
client->timebase(state, nframes, pos, new_pos);
}
JackTransportThread::JackTransportThread(JackTransportClient *client, QObject *parent) :
JackThread(client, parent),
ringBufferFromClient(0)
{
}
void JackTransportThread::setRingBufferFromClient(JackRingBuffer<jack_position_t> *ringBufferFromClient)
{
this->ringBufferFromClient = ringBufferFromClient;
}
void JackTransportThread::processDeferred()
{
if (ringBufferFromClient && ringBufferFromClient->readSpace()) {
ringBufferFromClient->readAdvance(ringBufferFromClient->readSpace() - 1);
jack_position_t pos = ringBufferFromClient->read();
// create a string describing the current position:
// (ignore unique_1 and usecs because these are rather unnerving)
QString posText = QString("frame_rate: %1\nframe: %2")
.arg(pos.frame_rate)
.arg(pos.frame);
if (pos.valid & JackPositionBBT) {
posText += QString("\nbar: %1\nbeat: %2\ntick: %3\nbar_start_tick: %4\nbeats_per_bar: %5\nbeat_type: %6\nticks_per_beat: %7\nbeats_per_minute: %8")
.arg(pos.bar)
.arg(pos.beat)
.arg(pos.tick)
.arg(pos.bar_start_tick)
.arg(pos.beats_per_bar)
.arg(pos.beat_type)
.arg(pos.ticks_per_beat)
.arg(pos.beats_per_minute);
}
if (pos.valid & JackPositionTimecode) {
posText += QString("\nframe_time: %1\nnext_time: %2")
.arg(pos.frame_time)
.arg(pos.next_time);
}
if (pos.valid & JackBBTFrameOffset) {
posText += QString("\nbbt_offset: %1")
.arg(pos.bbt_offset);
}
changedPosition(posText);
}
}
JackTransportGraphicsItem::JackTransportGraphicsItem(JackTransportClient *client, QGraphicsItem *parent) :
QGraphicsRectItem(parent),
padding(4)
{
setPen(QPen(QBrush(Qt::black), 1));
setBrush(Qt::white);
labelItem = new GraphicsLabelItem(this);
GraphicsContinuousControlItem *beatsPerMinuteControl = new GraphicsContinuousControlItem("Beats per minute", 1, 300, 120, 600, GraphicsContinuousControlItem::HORIZONTAL, 'g', -1, 1, this);
GraphicsDiscreteControlItem *beatsPerBarControl = new GraphicsDiscreteControlItem("Beats per bar", 1, 32, 4, 320, GraphicsContinuousControlItem::HORIZONTAL, this);
GraphicsDiscreteControlItem *beatTypeControl = new GraphicsDiscreteControlItem("Beat type", 1, 32, 4, 320, GraphicsContinuousControlItem::HORIZONTAL, this);
beatsPerMinuteControl->setPos(padding, padding + 0 * (padding + beatsPerMinuteControl->rect().height()));
beatsPerBarControl->setPos(padding, padding + 1 * (padding + beatsPerMinuteControl->rect().height()));
beatTypeControl->setPos(padding, padding + 2 * (padding + beatsPerMinuteControl->rect().height()));
controlsRect = (beatsPerMinuteControl->rect().translated(beatsPerMinuteControl->pos()) | beatsPerBarControl->rect().translated(beatsPerBarControl->pos()) | beatTypeControl->rect().translated(beatTypeControl->pos()));
labelItem->setPos(controlsRect.bottomLeft() + QPointF(0, padding));
QObject::connect(beatsPerMinuteControl, SIGNAL(valueChanged(double)), client, SLOT(changeBeatsPerMinute(double)));
QObject::connect(beatsPerBarControl, SIGNAL(valueChanged(int)), client, SLOT(changeBeatsPerBar(int)));
QObject::connect(beatTypeControl, SIGNAL(valueChanged(int)), client, SLOT(changeBeatType(int)));
changePosition("");
}
void JackTransportGraphicsItem::changePosition(const QString &pos)
{
labelItem->setText(pos);
setRect((controlsRect | labelItem->rect().translated(labelItem->pos())).adjusted(-padding, -padding, padding, padding));
}
class JackTransportClientFactory : public JackClientFactory
{
public:
JackTransportClientFactory()
{
JackClientSerializer::getInstance()->registerFactory(this);
}
QString getName()
{
return "Transport";
}
JackClient * createClient(const QString &clientName)
{
return new JackTransportClient(clientName);
}
static JackTransportClientFactory factory;
};
JackTransportClientFactory JackTransportClientFactory::factory;
JackClientFactory * JackTransportClient::getFactory()
{
return &JackTransportClientFactory::factory;
}