4 * ***** BEGIN LGPL LICENSE BLOCK *****
6 * Copyright 2009 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 Lesser General Public License as published by
12 * the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with AudaSpace. If not, see <http://www.gnu.org/licenses/>.
23 * ***** END LGPL LICENSE BLOCK *****
26 #include "AUD_Mixer.h"
27 #include "AUD_JackDevice.h"
28 #include "AUD_IReader.h"
29 #include "AUD_Buffer.h"
34 void* AUD_JackDevice::runMixingThread(void* device)
36 ((AUD_JackDevice*)device)->updateRingBuffers();
40 void AUD_JackDevice::updateRingBuffers()
43 unsigned int samplesize = AUD_SAMPLE_SIZE(m_specs);
45 unsigned int channels = m_specs.channels;
46 sample_t* buffer = m_buffer->getBuffer();
47 float* deinterleave = m_deinterleavebuf->getBuffer();
48 jack_transport_state_t state;
49 jack_position_t position;
51 pthread_mutex_lock(&m_mixingLock);
58 state = jack_transport_query(m_client, &position);
59 m_syncFunc(m_syncFuncData, state != JackTransportStopped, position.frame / (float) m_specs.rate);
62 for(i = 0; i < channels; i++)
63 jack_ringbuffer_reset(m_ringbuffers[i]);
66 size = jack_ringbuffer_write_space(m_ringbuffers[0]);
67 for(i = 1; i < channels; i++)
68 if((temp = jack_ringbuffer_write_space(m_ringbuffers[i])) < size)
71 while(size > samplesize)
74 mix((data_t*)buffer, size);
75 for(i = 0; i < channels; i++)
77 for(j = 0; j < size; j++)
78 deinterleave[i * size + j] = buffer[i + j * channels];
79 jack_ringbuffer_write(m_ringbuffers[i], (char*)(deinterleave + i * size), size * sizeof(float));
82 size = jack_ringbuffer_write_space(m_ringbuffers[0]);
83 for(i = 1; i < channels; i++)
84 if((temp = jack_ringbuffer_write_space(m_ringbuffers[i])) < size)
93 pthread_cond_wait(&m_mixingCondition, &m_mixingLock);
95 pthread_mutex_unlock(&m_mixingLock);
98 int AUD_JackDevice::jack_mix(jack_nframes_t length, void *data)
100 AUD_JackDevice* device = (AUD_JackDevice*)data;
102 int count = device->m_specs.channels;
107 // play silence while syncing
108 for(unsigned int i = 0; i < count; i++)
109 memset(jack_port_get_buffer(device->m_ports[i], length), 0, length * sizeof(float));
114 size_t readsamples = jack_ringbuffer_read_space(device->m_ringbuffers[0]);
115 for(i = 1; i < count; i++)
116 if((temp = jack_ringbuffer_read_space(device->m_ringbuffers[i])) < readsamples)
119 readsamples = AUD_MIN(readsamples / sizeof(float), length);
121 for(unsigned int i = 0; i < count; i++)
123 buffer = (char*)jack_port_get_buffer(device->m_ports[i], length);
124 jack_ringbuffer_read(device->m_ringbuffers[i], buffer, readsamples * sizeof(float));
125 if(readsamples < length)
126 memset(buffer + readsamples * sizeof(float), 0, (length - readsamples) * sizeof(float));
129 if(pthread_mutex_trylock(&(device->m_mixingLock)) == 0)
131 pthread_cond_signal(&(device->m_mixingCondition));
132 pthread_mutex_unlock(&(device->m_mixingLock));
139 int AUD_JackDevice::jack_sync(jack_transport_state_t state, jack_position_t* pos, void* data)
141 AUD_JackDevice* device = (AUD_JackDevice*)data;
143 if(state == JackTransportStopped)
146 if(pthread_mutex_trylock(&(device->m_mixingLock)) == 0)
148 if(device->m_sync > 2)
150 if(device->m_sync == 3)
153 pthread_mutex_unlock(&(device->m_mixingLock));
160 pthread_cond_signal(&(device->m_mixingCondition));
162 pthread_mutex_unlock(&(device->m_mixingLock));
164 else if(!device->m_sync)
170 void AUD_JackDevice::jack_shutdown(void *data)
172 AUD_JackDevice* device = (AUD_JackDevice*)data;
173 device->m_valid = false;
176 AUD_JackDevice::AUD_JackDevice(AUD_DeviceSpecs specs, int buffersize)
178 if(specs.channels == AUD_CHANNELS_INVALID)
179 specs.channels = AUD_CHANNELS_STEREO;
183 m_specs.format = AUD_FORMAT_FLOAT32;
185 jack_options_t options = JackNullOption;
186 jack_status_t status;
189 m_client = jack_client_open("Blender", options, &status);
191 AUD_THROW(AUD_ERROR_JACK);
194 jack_set_process_callback(m_client, AUD_JackDevice::jack_mix, this);
195 jack_on_shutdown(m_client, AUD_JackDevice::jack_shutdown, this);
196 jack_set_sync_callback(m_client, AUD_JackDevice::jack_sync, this);
198 // register our output channels which are called ports in jack
199 m_ports = new jack_port_t*[m_specs.channels]; AUD_NEW("jack_port")
204 for(int i = 0; i < m_specs.channels; i++)
206 sprintf(portname, "out %d", i+1);
207 m_ports[i] = jack_port_register(m_client, portname,
208 JACK_DEFAULT_AUDIO_TYPE,
209 JackPortIsOutput, 0);
210 if(m_ports[i] == NULL)
211 AUD_THROW(AUD_ERROR_JACK);
216 jack_client_close(m_client);
217 delete[] m_ports; AUD_DELETE("jack_port")
221 m_specs.rate = (AUD_SampleRate)jack_get_sample_rate(m_client);
223 buffersize *= sizeof(sample_t);
224 m_ringbuffers = new jack_ringbuffer_t*[specs.channels]; AUD_NEW("jack_buffers")
225 for(unsigned int i = 0; i < specs.channels; i++)
226 m_ringbuffers[i] = jack_ringbuffer_create(buffersize);
227 buffersize *= specs.channels;
228 m_buffer = new AUD_Buffer(buffersize); AUD_NEW("buffer");
229 m_deinterleavebuf = new AUD_Buffer(buffersize); AUD_NEW("buffer");
238 pthread_mutex_init(&m_mixingLock, NULL);
239 pthread_cond_init(&m_mixingCondition, NULL);
243 // activate the client
244 if(jack_activate(m_client))
245 AUD_THROW(AUD_ERROR_JACK);
249 jack_client_close(m_client);
250 delete[] m_ports; AUD_DELETE("jack_port")
251 delete m_buffer; AUD_DELETE("buffer");
252 delete m_deinterleavebuf; AUD_DELETE("buffer");
253 for(unsigned int i = 0; i < specs.channels; i++)
254 jack_ringbuffer_free(m_ringbuffers[i]);
255 delete[] m_ringbuffers; AUD_DELETE("jack_buffers")
256 pthread_mutex_destroy(&m_mixingLock);
257 pthread_cond_destroy(&m_mixingCondition);
262 const char** ports = jack_get_ports(m_client, NULL, NULL,
263 JackPortIsPhysical | JackPortIsInput);
266 for(int i = 0; i < m_specs.channels && ports[i]; i++)
267 jack_connect(m_client, jack_port_name(m_ports[i]), ports[i]);
273 pthread_attr_init(&attr);
274 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
276 pthread_create(&m_mixingThread, &attr, runMixingThread, this);
278 pthread_attr_destroy(&attr);
281 AUD_JackDevice::~AUD_JackDevice()
284 jack_client_close(m_client);
287 delete[] m_ports; AUD_DELETE("jack_port")
289 pthread_mutex_lock(&m_mixingLock);
290 pthread_cond_signal(&m_mixingCondition);
291 pthread_mutex_unlock(&m_mixingLock);
292 pthread_join(m_mixingThread, NULL);
294 pthread_cond_destroy(&m_mixingCondition);
295 pthread_mutex_destroy(&m_mixingLock);
296 delete m_buffer; AUD_DELETE("buffer");
297 delete m_deinterleavebuf; AUD_DELETE("buffer");
298 for(unsigned int i = 0; i < m_specs.channels; i++)
299 jack_ringbuffer_free(m_ringbuffers[i]);
300 delete[] m_ringbuffers; AUD_DELETE("jack_buffers")
305 void AUD_JackDevice::playing(bool playing)
310 void AUD_JackDevice::startPlayback()
312 jack_transport_start(m_client);
315 void AUD_JackDevice::stopPlayback()
317 jack_transport_stop(m_client);
320 void AUD_JackDevice::seekPlayback(float time)
323 jack_transport_locate(m_client, time * m_specs.rate);
326 void AUD_JackDevice::setSyncCallback(AUD_syncFunction sync, void* data)
329 m_syncFuncData = data;
332 float AUD_JackDevice::getPlaybackPosition()
334 jack_position_t position;
335 jack_transport_query(m_client, &position);
336 return position.frame / (float) m_specs.rate;
339 bool AUD_JackDevice::doesPlayback()
341 return jack_transport_query(m_client, NULL) != JackTransportStopped;