4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * Copyright 2009-2011 Jörg Hermann Müller
8 * This file is part of AudaSpace.
10 * Audaspace is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * AudaSpace is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Audaspace; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file audaspace/jack/AUD_JackDevice.cpp
31 #include "AUD_JackDevice.h"
32 #include "AUD_IReader.h"
37 void* AUD_JackDevice::runMixingThread(void* device)
39 ((AUD_JackDevice*)device)->updateRingBuffers();
43 void AUD_JackDevice::updateRingBuffers()
46 unsigned int samplesize = AUD_SAMPLE_SIZE(m_specs);
48 unsigned int channels = m_specs.channels;
49 sample_t* buffer = m_buffer.getBuffer();
50 float* deinterleave = m_deinterleavebuf.getBuffer();
51 jack_transport_state_t state;
52 jack_position_t position;
54 pthread_mutex_lock(&m_mixingLock);
61 state = jack_transport_query(m_client, &position);
62 m_syncFunc(m_syncFuncData, state != JackTransportStopped, position.frame / (float) m_specs.rate);
65 for(i = 0; i < channels; i++)
66 jack_ringbuffer_reset(m_ringbuffers[i]);
69 size = jack_ringbuffer_write_space(m_ringbuffers[0]);
70 for(i = 1; i < channels; i++)
71 if((temp = jack_ringbuffer_write_space(m_ringbuffers[i])) < size)
74 while(size > samplesize)
77 mix((data_t*)buffer, size);
78 for(i = 0; i < channels; i++)
80 for(j = 0; j < size; j++)
81 deinterleave[i * size + j] = buffer[i + j * channels];
82 jack_ringbuffer_write(m_ringbuffers[i], (char*)(deinterleave + i * size), size * sizeof(float));
85 size = jack_ringbuffer_write_space(m_ringbuffers[0]);
86 for(i = 1; i < channels; i++)
87 if((temp = jack_ringbuffer_write_space(m_ringbuffers[i])) < size)
96 pthread_cond_wait(&m_mixingCondition, &m_mixingLock);
98 pthread_mutex_unlock(&m_mixingLock);
101 int AUD_JackDevice::jack_mix(jack_nframes_t length, void *data)
103 AUD_JackDevice* device = (AUD_JackDevice*)data;
105 int count = device->m_specs.channels;
110 // play silence while syncing
111 for(unsigned int i = 0; i < count; i++)
112 memset(jack_port_get_buffer(device->m_ports[i], length), 0, length * sizeof(float));
117 size_t readsamples = jack_ringbuffer_read_space(device->m_ringbuffers[0]);
118 for(i = 1; i < count; i++)
119 if((temp = jack_ringbuffer_read_space(device->m_ringbuffers[i])) < readsamples)
122 readsamples = AUD_MIN(readsamples / sizeof(float), length);
124 for(unsigned int i = 0; i < count; i++)
126 buffer = (char*)jack_port_get_buffer(device->m_ports[i], length);
127 jack_ringbuffer_read(device->m_ringbuffers[i], buffer, readsamples * sizeof(float));
128 if(readsamples < length)
129 memset(buffer + readsamples * sizeof(float), 0, (length - readsamples) * sizeof(float));
132 if(pthread_mutex_trylock(&(device->m_mixingLock)) == 0)
134 pthread_cond_signal(&(device->m_mixingCondition));
135 pthread_mutex_unlock(&(device->m_mixingLock));
142 int AUD_JackDevice::jack_sync(jack_transport_state_t state, jack_position_t* pos, void* data)
144 AUD_JackDevice* device = (AUD_JackDevice*)data;
146 if(state == JackTransportStopped)
149 if(pthread_mutex_trylock(&(device->m_mixingLock)) == 0)
151 if(device->m_sync > 2)
153 if(device->m_sync == 3)
156 pthread_mutex_unlock(&(device->m_mixingLock));
163 pthread_cond_signal(&(device->m_mixingCondition));
165 pthread_mutex_unlock(&(device->m_mixingLock));
167 else if(!device->m_sync)
173 void AUD_JackDevice::jack_shutdown(void *data)
175 AUD_JackDevice* device = (AUD_JackDevice*)data;
176 device->m_valid = false;
179 static const char* clientopen_error = "AUD_JackDevice: Couldn't connect to "
181 static const char* port_error = "AUD_JackDevice: Couldn't create output port.";
182 static const char* activate_error = "AUD_JackDevice: Couldn't activate the "
185 AUD_JackDevice::AUD_JackDevice(std::string name, AUD_DeviceSpecs specs, int buffersize)
187 if(specs.channels == AUD_CHANNELS_INVALID)
188 specs.channels = AUD_CHANNELS_STEREO;
192 m_specs.format = AUD_FORMAT_FLOAT32;
194 jack_options_t options = JackNullOption;
195 jack_status_t status;
198 m_client = jack_client_open(name.c_str(), options, &status);
200 AUD_THROW(AUD_ERROR_JACK, clientopen_error);
203 jack_set_process_callback(m_client, AUD_JackDevice::jack_mix, this);
204 jack_on_shutdown(m_client, AUD_JackDevice::jack_shutdown, this);
205 jack_set_sync_callback(m_client, AUD_JackDevice::jack_sync, this);
207 // register our output channels which are called ports in jack
208 m_ports = new jack_port_t*[m_specs.channels];
213 for(int i = 0; i < m_specs.channels; i++)
215 sprintf(portname, "out %d", i+1);
216 m_ports[i] = jack_port_register(m_client, portname,
217 JACK_DEFAULT_AUDIO_TYPE,
218 JackPortIsOutput, 0);
219 if(m_ports[i] == NULL)
220 AUD_THROW(AUD_ERROR_JACK, port_error);
223 catch(AUD_Exception&)
225 jack_client_close(m_client);
230 m_specs.rate = (AUD_SampleRate)jack_get_sample_rate(m_client);
232 buffersize *= sizeof(sample_t);
233 m_ringbuffers = new jack_ringbuffer_t*[specs.channels];
234 for(unsigned int i = 0; i < specs.channels; i++)
235 m_ringbuffers[i] = jack_ringbuffer_create(buffersize);
236 buffersize *= specs.channels;
237 m_deinterleavebuf.resize(buffersize);
238 m_buffer.resize(buffersize);
245 m_nextState = m_state = jack_transport_query(m_client, NULL);
247 pthread_mutex_init(&m_mixingLock, NULL);
248 pthread_cond_init(&m_mixingCondition, NULL);
250 // activate the client
251 if(jack_activate(m_client))
253 jack_client_close(m_client);
255 for(unsigned int i = 0; i < specs.channels; i++)
256 jack_ringbuffer_free(m_ringbuffers[i]);
257 delete[] m_ringbuffers;
258 pthread_mutex_destroy(&m_mixingLock);
259 pthread_cond_destroy(&m_mixingCondition);
262 AUD_THROW(AUD_ERROR_JACK, activate_error);
265 const char** ports = jack_get_ports(m_client, NULL, NULL,
266 JackPortIsPhysical | JackPortIsInput);
269 for(int i = 0; i < m_specs.channels && ports[i]; i++)
270 jack_connect(m_client, jack_port_name(m_ports[i]), ports[i]);
276 pthread_attr_init(&attr);
277 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
279 pthread_create(&m_mixingThread, &attr, runMixingThread, this);
281 pthread_attr_destroy(&attr);
284 AUD_JackDevice::~AUD_JackDevice()
287 jack_client_close(m_client);
292 pthread_mutex_lock(&m_mixingLock);
293 pthread_cond_signal(&m_mixingCondition);
294 pthread_mutex_unlock(&m_mixingLock);
295 pthread_join(m_mixingThread, NULL);
297 pthread_cond_destroy(&m_mixingCondition);
298 pthread_mutex_destroy(&m_mixingLock);
299 for(unsigned int i = 0; i < m_specs.channels; i++)
300 jack_ringbuffer_free(m_ringbuffers[i]);
301 delete[] m_ringbuffers;
306 void AUD_JackDevice::playing(bool playing)
311 void AUD_JackDevice::startPlayback()
313 jack_transport_start(m_client);
314 m_nextState = JackTransportRolling;
317 void AUD_JackDevice::stopPlayback()
319 jack_transport_stop(m_client);
320 m_nextState = JackTransportStopped;
323 void AUD_JackDevice::seekPlayback(float time)
326 jack_transport_locate(m_client, time * m_specs.rate);
329 void AUD_JackDevice::setSyncCallback(AUD_syncFunction sync, void* data)
332 m_syncFuncData = data;
335 float AUD_JackDevice::getPlaybackPosition()
337 jack_position_t position;
338 jack_transport_query(m_client, &position);
339 return position.frame / (float) m_specs.rate;
342 bool AUD_JackDevice::doesPlayback()
344 jack_transport_state_t state = jack_transport_query(m_client, NULL);
347 m_nextState = m_state = state;
349 return m_nextState != JackTransportStopped;