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, 2006 Christian Schoenebeck * |
* Copyright (C) 2005 - 2008 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 * |
66 |
} |
} |
67 |
|
|
68 |
std::vector<String> AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::PossibilitiesAsString() { |
std::vector<String> AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::PossibilitiesAsString() { |
69 |
const char** pPortNames = jack_get_ports(pChannel->pDevice->hJackClient, NULL, NULL, JackPortIsInput); |
const char** pPortNames = jack_get_ports(pChannel->pDevice->hJackClient, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput); |
70 |
if (!pPortNames) return std::vector<String>(); |
if (!pPortNames) return std::vector<String>(); |
71 |
std::vector<String> result; |
std::vector<String> result; |
72 |
for (int i = 0; pPortNames[i]; i++) result.push_back(pPortNames[i]); |
for (int i = 0; pPortNames[i]; i++) result.push_back(pPortNames[i]); |
73 |
//free(pPortNames); FIXME: pPortNames should be freed here |
free(pPortNames); |
74 |
return result; |
return result; |
75 |
} |
} |
76 |
|
|
178 |
* @see AcquireChannels() |
* @see AcquireChannels() |
179 |
*/ |
*/ |
180 |
AudioOutputDeviceJack::AudioOutputDeviceJack(std::map<String,DeviceCreationParameter*> Parameters) : AudioOutputDevice(Parameters) { |
AudioOutputDeviceJack::AudioOutputDeviceJack(std::map<String,DeviceCreationParameter*> Parameters) : AudioOutputDevice(Parameters) { |
181 |
if (((DeviceCreationParameterString*)Parameters["NAME"])->ValueAsString().size() >= jack_client_name_size()) |
JackClient* pJackClient = JackClient::CreateAudio(((DeviceCreationParameterString*)Parameters["NAME"])->ValueAsString()); |
|
throw Exception("JACK client name too long"); |
|
|
|
|
|
if ((hJackClient = jack_client_new(((DeviceCreationParameterString*)Parameters["NAME"])->ValueAsString().c_str())) == 0) |
|
|
throw AudioOutputException("Seems Jack server not running."); |
|
|
|
|
182 |
existingJackDevices++; |
existingJackDevices++; |
183 |
|
pJackClient->SetAudioOutputDevice(this); |
184 |
jack_set_process_callback(hJackClient, __libjack_process_callback, this); |
hJackClient = pJackClient->hJackClient; |
|
jack_on_shutdown(hJackClient, __libjack_shutdown_callback, this); |
|
|
if (jack_activate(hJackClient)) |
|
|
throw AudioOutputException("Jack: Cannot activate Jack client."); |
|
|
|
|
185 |
uiMaxSamplesPerCycle = jack_get_buffer_size(hJackClient); |
uiMaxSamplesPerCycle = jack_get_buffer_size(hJackClient); |
186 |
|
|
187 |
// create audio channels |
// create audio channels |
192 |
} |
} |
193 |
|
|
194 |
AudioOutputDeviceJack::~AudioOutputDeviceJack() { |
AudioOutputDeviceJack::~AudioOutputDeviceJack() { |
195 |
// destroy jack client |
// destroy jack client if there is no midi device associated with it |
196 |
jack_client_close(hJackClient); |
JackClient::ReleaseAudio(((DeviceCreationParameterString*)Parameters["NAME"])->ValueAsString()); |
197 |
existingJackDevices--; |
existingJackDevices--; |
198 |
} |
} |
199 |
|
|
249 |
} |
} |
250 |
|
|
251 |
String AudioOutputDeviceJack::Version() { |
String AudioOutputDeviceJack::Version() { |
252 |
String s = "$Revision: 1.21 $"; |
String s = "$Revision: 1.22 $"; |
253 |
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 |
254 |
} |
} |
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
// *************** JackClient *************** |
259 |
|
// * |
260 |
|
|
261 |
// libjack callback functions |
// libjack callback functions |
262 |
|
|
263 |
int __libjack_process_callback(jack_nframes_t nframes, void* arg) { |
int linuxsampler_libjack_process_callback(jack_nframes_t nframes, void* arg) { |
264 |
AudioOutputDeviceJack* pAudioOutputDeviceJack = (AudioOutputDeviceJack*) arg; |
return static_cast<JackClient*>(arg)->Process(nframes); |
|
return pAudioOutputDeviceJack->Process(nframes); |
|
265 |
} |
} |
266 |
|
|
267 |
void __libjack_shutdown_callback(void* arg) { |
void linuxsampler_libjack_shutdown_callback(void* arg) { |
268 |
AudioOutputDeviceJack* pAudioOutputDeviceJack = (AudioOutputDeviceJack*) arg; |
static_cast<JackClient*>(arg)->Stop(); |
|
pAudioOutputDeviceJack->Stop(); |
|
269 |
fprintf(stderr, "Jack: Jack server shutdown, exiting.\n"); |
fprintf(stderr, "Jack: Jack server shutdown, exiting.\n"); |
270 |
} |
} |
271 |
|
|
272 |
|
std::map<String, JackClient*> JackClient::Clients; |
273 |
|
|
274 |
|
int JackClient::Process(uint Samples) { |
275 |
|
const config_t& config = ConfigReader.Lock(); |
276 |
|
#if HAVE_JACK_MIDI |
277 |
|
if (config.MidiDevice) config.MidiDevice->Process(Samples); |
278 |
|
#endif |
279 |
|
int res = config.AudioDevice ? config.AudioDevice->Process(Samples) : 0; |
280 |
|
ConfigReader.Unlock(); |
281 |
|
return res; |
282 |
|
} |
283 |
|
|
284 |
|
void JackClient::Stop() { |
285 |
|
const config_t& config = ConfigReader.Lock(); |
286 |
|
if (config.AudioDevice) config.AudioDevice->Stop(); |
287 |
|
ConfigReader.Unlock(); |
288 |
|
} |
289 |
|
|
290 |
|
JackClient::JackClient(String Name) : ConfigReader(Config) { |
291 |
|
{ |
292 |
|
config_t& config = Config.GetConfigForUpdate(); |
293 |
|
config.AudioDevice = 0; |
294 |
|
config.MidiDevice = 0; |
295 |
|
} |
296 |
|
{ |
297 |
|
config_t& config = Config.SwitchConfig(); |
298 |
|
config.AudioDevice = 0; |
299 |
|
config.MidiDevice = 0; |
300 |
|
} |
301 |
|
audio = midi = false; |
302 |
|
if (Name.size() >= jack_client_name_size()) |
303 |
|
throw Exception("JACK client name too long"); |
304 |
|
if ((hJackClient = jack_client_new(Name.c_str())) == 0) |
305 |
|
throw Exception("Seems Jack server is not running."); |
306 |
|
jack_set_process_callback(hJackClient, linuxsampler_libjack_process_callback, this); |
307 |
|
jack_on_shutdown(hJackClient, linuxsampler_libjack_shutdown_callback, this); |
308 |
|
if (jack_activate(hJackClient)) |
309 |
|
throw Exception("Jack: Cannot activate Jack client."); |
310 |
|
} |
311 |
|
|
312 |
|
JackClient::~JackClient() { |
313 |
|
jack_client_close(hJackClient); |
314 |
|
} |
315 |
|
|
316 |
|
void JackClient::SetAudioOutputDevice(AudioOutputDeviceJack* device) { |
317 |
|
Config.GetConfigForUpdate().AudioDevice = device; |
318 |
|
Config.SwitchConfig().AudioDevice = device; |
319 |
|
} |
320 |
|
|
321 |
|
void JackClient::SetMidiInputDevice(MidiInputDeviceJack* device) { |
322 |
|
Config.GetConfigForUpdate().MidiDevice = device; |
323 |
|
Config.SwitchConfig().MidiDevice = device; |
324 |
|
} |
325 |
|
|
326 |
|
JackClient* JackClient::CreateAudio(String Name) { // static |
327 |
|
JackClient* client; |
328 |
|
std::map<String, JackClient*>::const_iterator it = Clients.find(Name); |
329 |
|
if (it == Clients.end()) { |
330 |
|
client = new JackClient(Name); |
331 |
|
Clients[Name] = client; |
332 |
|
} else { |
333 |
|
client = it->second; |
334 |
|
if (client->audio) throw Exception("Jack audio device '" + Name + "' already exists"); |
335 |
|
} |
336 |
|
client->audio = true; |
337 |
|
return client; |
338 |
|
} |
339 |
|
|
340 |
|
JackClient* JackClient::CreateMidi(String Name) { // static |
341 |
|
JackClient* client; |
342 |
|
std::map<String, JackClient*>::const_iterator it = Clients.find(Name); |
343 |
|
if (it == Clients.end()) { |
344 |
|
client = new JackClient(Name); |
345 |
|
Clients[Name] = client; |
346 |
|
} else { |
347 |
|
client = it->second; |
348 |
|
if (client->midi) throw Exception("Jack MIDI device '" + Name + "' already exists"); |
349 |
|
} |
350 |
|
client->midi = true; |
351 |
|
return client; |
352 |
|
} |
353 |
|
|
354 |
|
void JackClient::ReleaseAudio(String Name) { // static |
355 |
|
JackClient* client = Clients[Name]; |
356 |
|
client->SetAudioOutputDevice(0); |
357 |
|
client->audio = false; |
358 |
|
if (!client->midi) { |
359 |
|
Clients.erase(Name); |
360 |
|
delete client; |
361 |
|
} |
362 |
|
} |
363 |
|
|
364 |
|
void JackClient::ReleaseMidi(String Name) { // static |
365 |
|
JackClient* client = Clients[Name]; |
366 |
|
client->SetMidiInputDevice(0); |
367 |
|
client->midi = false; |
368 |
|
if (!client->audio) { |
369 |
|
Clients.erase(Name); |
370 |
|
delete client; |
371 |
|
} |
372 |
|
} |
373 |
|
|
374 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |
375 |
|
|
376 |
#endif // HAVE_JACK |
#endif // HAVE_JACK |