3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2008 Christian Schoenebeck * |
* Copyright (C) 2005 - 2013 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
120 |
if (!hJackPort) throw AudioOutputException("Jack: Cannot register Jack output port."); |
if (!hJackPort) throw AudioOutputException("Jack: Cannot register Jack output port."); |
121 |
return (float*) jack_port_get_buffer(hJackPort, pDevice->uiMaxSamplesPerCycle); |
return (float*) jack_port_get_buffer(hJackPort, pDevice->uiMaxSamplesPerCycle); |
122 |
} |
} |
123 |
|
|
124 |
|
void AudioOutputDeviceJack::AudioChannelJack::UpdateJackBuffer(uint size) { |
125 |
|
SetBuffer( |
126 |
|
(float*)jack_port_get_buffer(hJackPort, size) |
127 |
|
); |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
|
|
208 |
* libjack to demand transmission of further sample points. |
* libjack to demand transmission of further sample points. |
209 |
*/ |
*/ |
210 |
int AudioOutputDeviceJack::Process(uint Samples) { |
int AudioOutputDeviceJack::Process(uint Samples) { |
211 |
|
int res; |
212 |
|
|
213 |
|
// in recent versions of JACK2 and JACk1, we are forced to |
214 |
|
// re-retrieve the audio buffer pointers in each process period |
215 |
|
UpdateJackBuffers(Samples); |
216 |
|
|
217 |
if (csIsPlaying.Pop()) { |
if (csIsPlaying.Pop()) { |
218 |
// let all connected engines render 'Samples' sample points |
// let all connected engines render 'Samples' sample points |
219 |
return RenderAudio(Samples); |
res = RenderAudio(Samples); |
220 |
} |
} |
221 |
else { |
else { |
222 |
// playback stop by zeroing output buffer(s) and not calling connected sampler engines to render audio |
// playback stop by zeroing output buffer(s) and not calling connected sampler engines to render audio |
223 |
return RenderSilence(Samples); |
res = RenderSilence(Samples); |
224 |
} |
} |
225 |
|
csIsPlaying.RttDone(); |
226 |
|
return res; |
227 |
|
} |
228 |
|
|
229 |
|
void AudioOutputDeviceJack::UpdateJackBuffers(uint size) { |
230 |
|
for (int i = 0; i < Channels.size(); ++i) |
231 |
|
static_cast<AudioChannelJack*>(Channels[i])->UpdateJackBuffer(size); |
232 |
|
} |
233 |
|
|
234 |
|
float AudioOutputDeviceJack::latency() { |
235 |
|
if (!hJackClient) return -1; |
236 |
|
const float size = jack_get_buffer_size(hJackClient); |
237 |
|
const float rate = jack_get_sample_rate(hJackClient); |
238 |
|
return size / rate; |
239 |
} |
} |
240 |
|
|
241 |
void AudioOutputDeviceJack::Play() { |
void AudioOutputDeviceJack::Play() { |
275 |
} |
} |
276 |
|
|
277 |
String AudioOutputDeviceJack::Version() { |
String AudioOutputDeviceJack::Version() { |
278 |
String s = "$Revision: 1.23 $"; |
String s = "$Revision: 1.25 $"; |
279 |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
280 |
} |
} |
281 |
|
|
294 |
static_cast<JackClient*>(arg)->Stop(); |
static_cast<JackClient*>(arg)->Stop(); |
295 |
fprintf(stderr, "Jack: Jack server shutdown, exiting.\n"); |
fprintf(stderr, "Jack: Jack server shutdown, exiting.\n"); |
296 |
} |
} |
297 |
|
|
298 |
|
int JackClient::libjackSampleRateCallback(jack_nframes_t nframes, void *arg) { |
299 |
|
JackClient* client = static_cast<JackClient*>(arg); |
300 |
|
const config_t& config = client->ConfigReader.Lock(); |
301 |
|
if (config.AudioDevice) |
302 |
|
config.AudioDevice->ReconnectAll(); |
303 |
|
client->ConfigReader.Unlock(); |
304 |
|
return 0; |
305 |
|
} |
306 |
|
|
307 |
|
int JackClient::libjackBufferSizeCallback(jack_nframes_t nframes, void *arg) { |
308 |
|
dmsg(1,("libjackBufferSizeCallback(%d)\n",nframes)); |
309 |
|
JackClient* client = static_cast<JackClient*>(arg); |
310 |
|
const config_t& config = client->ConfigReader.Lock(); |
311 |
|
if (config.AudioDevice) { |
312 |
|
config.AudioDevice->UpdateJackBuffers(nframes); |
313 |
|
config.AudioDevice->ReconnectAll(); |
314 |
|
} |
315 |
|
client->ConfigReader.Unlock(); |
316 |
|
return 0; |
317 |
|
} |
318 |
|
|
319 |
std::map<String, JackClient*> JackClient::Clients; |
std::map<String, JackClient*> JackClient::Clients; |
320 |
|
|
352 |
audio = midi = false; |
audio = midi = false; |
353 |
if (Name.size() >= jack_client_name_size()) |
if (Name.size() >= jack_client_name_size()) |
354 |
throw Exception("JACK client name too long"); |
throw Exception("JACK client name too long"); |
355 |
if ((hJackClient = jack_client_new(Name.c_str())) == 0) |
#if HAVE_JACK_CLIENT_OPEN |
356 |
|
hJackClient = jack_client_open(Name.c_str(), JackNullOption, NULL); |
357 |
|
#else |
358 |
|
hJackClient = jack_client_new(Name.c_str()); |
359 |
|
#endif |
360 |
|
if (!hJackClient) |
361 |
throw Exception("Seems Jack server is not running."); |
throw Exception("Seems Jack server is not running."); |
362 |
jack_set_process_callback(hJackClient, linuxsampler_libjack_process_callback, this); |
jack_set_process_callback(hJackClient, linuxsampler_libjack_process_callback, this); |
363 |
jack_on_shutdown(hJackClient, linuxsampler_libjack_shutdown_callback, this); |
jack_on_shutdown(hJackClient, linuxsampler_libjack_shutdown_callback, this); |
364 |
|
jack_set_buffer_size_callback(hJackClient, libjackBufferSizeCallback, this); |
365 |
|
jack_set_sample_rate_callback(hJackClient, libjackSampleRateCallback, this); |
366 |
|
|
367 |
if (jack_activate(hJackClient)) |
if (jack_activate(hJackClient)) |
368 |
throw Exception("Jack: Cannot activate Jack client."); |
throw Exception("Jack: Cannot activate Jack client."); |
369 |
} |
} |