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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/*
* Copyright 2009-2011 Jörg Hermann Müller
*
* This file is part of AudaSpace.
*
* Audaspace 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 2 of the License, or
* (at your option) any later version.
*
* AudaSpace 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 Audaspace; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file audaspace/jack/AUD_JackDevice.cpp
* \ingroup audjack
*/
#include "AUD_JackDevice.h"
#include "AUD_IReader.h"
#include <stdio.h>
#include <stdlib.h>
void* AUD_JackDevice::runMixingThread(void* device)
{
((AUD_JackDevice*)device)->updateRingBuffers();
return NULL;
}
void AUD_JackDevice::updateRingBuffers()
{
size_t size, temp;
unsigned int samplesize = AUD_SAMPLE_SIZE(m_specs);
unsigned int i, j;
unsigned int channels = m_specs.channels;
sample_t* buffer = m_buffer.getBuffer();
float* deinterleave = m_deinterleavebuf.getBuffer();
jack_transport_state_t state;
jack_position_t position;
pthread_mutex_lock(&m_mixingLock);
while(m_valid)
{
if(m_sync > 1)
{
if(m_syncFunc)
{
state = AUD_jack_transport_query(m_client, &position);
m_syncFunc(m_syncFuncData, state != JackTransportStopped, position.frame / (float) m_specs.rate);
}
for(i = 0; i < channels; i++)
AUD_jack_ringbuffer_reset(m_ringbuffers[i]);
}
size = AUD_jack_ringbuffer_write_space(m_ringbuffers[0]);
for(i = 1; i < channels; i++)
if((temp = AUD_jack_ringbuffer_write_space(m_ringbuffers[i])) < size)
size = temp;
while(size > samplesize)
{
size /= samplesize;
mix((data_t*)buffer, size);
for(i = 0; i < channels; i++)
{
for(j = 0; j < size; j++)
deinterleave[i * size + j] = buffer[i + j * channels];
AUD_jack_ringbuffer_write(m_ringbuffers[i], (char*)(deinterleave + i * size), size * sizeof(float));
}
size = AUD_jack_ringbuffer_write_space(m_ringbuffers[0]);
for(i = 1; i < channels; i++)
if((temp = AUD_jack_ringbuffer_write_space(m_ringbuffers[i])) < size)
size = temp;
}
if(m_sync > 1)
{
m_sync = 3;
}
pthread_cond_wait(&m_mixingCondition, &m_mixingLock);
}
pthread_mutex_unlock(&m_mixingLock);
}
int AUD_JackDevice::jack_mix(jack_nframes_t length, void *data)
{
AUD_JackDevice* device = (AUD_JackDevice*)data;
unsigned int i;
int count = device->m_specs.channels;
char* buffer;
if(device->m_sync)
{
// play silence while syncing
for(unsigned int i = 0; i < count; i++)
memset(AUD_jack_port_get_buffer(device->m_ports[i], length), 0, length * sizeof(float));
}
else
{
size_t temp;
size_t readsamples = AUD_jack_ringbuffer_read_space(device->m_ringbuffers[0]);
for(i = 1; i < count; i++)
if((temp = AUD_jack_ringbuffer_read_space(device->m_ringbuffers[i])) < readsamples)
readsamples = temp;
readsamples = AUD_MIN(readsamples / sizeof(float), length);
for(unsigned int i = 0; i < count; i++)
{
buffer = (char*)AUD_jack_port_get_buffer(device->m_ports[i], length);
AUD_jack_ringbuffer_read(device->m_ringbuffers[i], buffer, readsamples * sizeof(float));
if(readsamples < length)
memset(buffer + readsamples * sizeof(float), 0, (length - readsamples) * sizeof(float));
}
if(pthread_mutex_trylock(&(device->m_mixingLock)) == 0)
{
pthread_cond_signal(&(device->m_mixingCondition));
pthread_mutex_unlock(&(device->m_mixingLock));
}
}
return 0;
}
int AUD_JackDevice::jack_sync(jack_transport_state_t state, jack_position_t* pos, void* data)
{
AUD_JackDevice* device = (AUD_JackDevice*)data;
if(state == JackTransportStopped)
return 1;
if(pthread_mutex_trylock(&(device->m_mixingLock)) == 0)
{
if(device->m_sync > 2)
{
if(device->m_sync == 3)
{
device->m_sync = 0;
pthread_mutex_unlock(&(device->m_mixingLock));
return 1;
}
}
else
{
device->m_sync = 2;
pthread_cond_signal(&(device->m_mixingCondition));
}
pthread_mutex_unlock(&(device->m_mixingLock));
}
else if(!device->m_sync)
device->m_sync = 1;
return 0;
}
void AUD_JackDevice::jack_shutdown(void *data)
{
AUD_JackDevice* device = (AUD_JackDevice*)data;
device->m_valid = false;
}
static const char* clientopen_error = "AUD_JackDevice: Couldn't connect to "
"jack server.";
static const char* port_error = "AUD_JackDevice: Couldn't create output port.";
static const char* activate_error = "AUD_JackDevice: Couldn't activate the "
"client.";
AUD_JackDevice::AUD_JackDevice(std::string name, AUD_DeviceSpecs specs, int buffersize)
{
if(specs.channels == AUD_CHANNELS_INVALID)
specs.channels = AUD_CHANNELS_STEREO;
// jack uses floats
m_specs = specs;
m_specs.format = AUD_FORMAT_FLOAT32;
jack_options_t options = JackNullOption;
jack_status_t status;
// open client
m_client = AUD_jack_client_open(name.c_str(), options, &status);
if(m_client == NULL)
AUD_THROW(AUD_ERROR_JACK, clientopen_error);
// set callbacks
AUD_jack_set_process_callback(m_client, AUD_JackDevice::jack_mix, this);
AUD_jack_on_shutdown(m_client, AUD_JackDevice::jack_shutdown, this);
AUD_jack_set_sync_callback(m_client, AUD_JackDevice::jack_sync, this);
// register our output channels which are called ports in jack
m_ports = new jack_port_t*[m_specs.channels];
try
{
char portname[64];
for(int i = 0; i < m_specs.channels; i++)
{
sprintf(portname, "out %d", i+1);
m_ports[i] = AUD_jack_port_register(m_client, portname,
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput, 0);
if(m_ports[i] == NULL)
AUD_THROW(AUD_ERROR_JACK, port_error);
}
}
catch(AUD_Exception&)
{
AUD_jack_client_close(m_client);
delete[] m_ports;
throw;
}
m_specs.rate = (AUD_SampleRate)AUD_jack_get_sample_rate(m_client);
buffersize *= sizeof(sample_t);
m_ringbuffers = new jack_ringbuffer_t*[specs.channels];
for(unsigned int i = 0; i < specs.channels; i++)
m_ringbuffers[i] = AUD_jack_ringbuffer_create(buffersize);
buffersize *= specs.channels;
m_deinterleavebuf.resize(buffersize);
m_buffer.resize(buffersize);
create();
m_valid = true;
m_sync = 0;
m_syncFunc = NULL;
m_nextState = m_state = AUD_jack_transport_query(m_client, NULL);
pthread_mutex_init(&m_mixingLock, NULL);
pthread_cond_init(&m_mixingCondition, NULL);
// activate the client
if(AUD_jack_activate(m_client))
{
AUD_jack_client_close(m_client);
delete[] m_ports;
for(unsigned int i = 0; i < specs.channels; i++)
AUD_jack_ringbuffer_free(m_ringbuffers[i]);
delete[] m_ringbuffers;
pthread_mutex_destroy(&m_mixingLock);
pthread_cond_destroy(&m_mixingCondition);
destroy();
AUD_THROW(AUD_ERROR_JACK, activate_error);
}
const char** ports = AUD_jack_get_ports(m_client, NULL, NULL,
JackPortIsPhysical | JackPortIsInput);
if(ports != NULL)
{
for(int i = 0; i < m_specs.channels && ports[i]; i++)
AUD_jack_connect(m_client, AUD_jack_port_name(m_ports[i]), ports[i]);
free(ports);
}
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&m_mixingThread, &attr, runMixingThread, this);
pthread_attr_destroy(&attr);
}
AUD_JackDevice::~AUD_JackDevice()
{
if(m_valid)
AUD_jack_client_close(m_client);
m_valid = false;
delete[] m_ports;
pthread_mutex_lock(&m_mixingLock);
pthread_cond_signal(&m_mixingCondition);
pthread_mutex_unlock(&m_mixingLock);
pthread_join(m_mixingThread, NULL);
pthread_cond_destroy(&m_mixingCondition);
pthread_mutex_destroy(&m_mixingLock);
for(unsigned int i = 0; i < m_specs.channels; i++)
AUD_jack_ringbuffer_free(m_ringbuffers[i]);
delete[] m_ringbuffers;
destroy();
}
void AUD_JackDevice::playing(bool playing)
{
// Do nothing.
}
void AUD_JackDevice::startPlayback()
{
AUD_jack_transport_start(m_client);
m_nextState = JackTransportRolling;
}
void AUD_JackDevice::stopPlayback()
{
AUD_jack_transport_stop(m_client);
m_nextState = JackTransportStopped;
}
void AUD_JackDevice::seekPlayback(float time)
{
if(time >= 0.0f)
AUD_jack_transport_locate(m_client, time * m_specs.rate);
}
void AUD_JackDevice::setSyncCallback(AUD_syncFunction sync, void* data)
{
m_syncFunc = sync;
m_syncFuncData = data;
}
float AUD_JackDevice::getPlaybackPosition()
{
jack_position_t position;
AUD_jack_transport_query(m_client, &position);
return position.frame / (float) m_specs.rate;
}
bool AUD_JackDevice::doesPlayback()
{
jack_transport_state_t state = AUD_jack_transport_query(m_client, NULL);
if(state != m_state)
m_nextState = m_state = state;
return m_nextState != JackTransportStopped;
}
|