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 * |
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 * |
24 |
#include "AudioOutputDeviceJack.h" |
#include "AudioOutputDeviceJack.h" |
25 |
#include "AudioOutputDeviceFactory.h" |
#include "AudioOutputDeviceFactory.h" |
26 |
|
|
27 |
#if HAVE_JACK |
#include <errno.h> |
|
|
|
|
namespace LinuxSampler { |
|
28 |
|
|
29 |
REGISTER_AUDIO_OUTPUT_DRIVER(AudioOutputDeviceJack); |
#if HAVE_JACK |
30 |
|
|
31 |
/* Common parameters for now they'll have to be registered here. */ |
#ifndef HAVE_JACK_CLIENT_NAME_SIZE |
32 |
REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceJack, ParameterActive); |
#define jack_client_name_size() 33 |
33 |
REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceJack, ParameterChannels); |
#endif |
34 |
|
|
35 |
|
namespace LinuxSampler { |
36 |
|
|
37 |
|
/// number of currently existing JACK audio output devices in LinuxSampler |
38 |
|
static int existingJackDevices = 0; |
39 |
|
|
40 |
// *************** ParameterName *************** |
// *************** AudioChannelJack::ParameterName *************** |
41 |
// * |
// * |
42 |
|
|
43 |
AudioOutputDeviceJack::AudioChannelJack::ParameterName::ParameterName(AudioChannelJack* pChannel) : AudioChannel::ParameterName(ToString(pChannel->ChannelNr)) { |
AudioOutputDeviceJack::AudioChannelJack::ParameterName::ParameterName(AudioChannelJack* pChannel) : AudioChannel::ParameterName(ToString(pChannel->ChannelNr)) { |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
// *************** ParameterJackBindings *************** |
// *************** AudioChannelJack::ParameterJackBindings *************** |
54 |
// * |
// * |
55 |
|
|
56 |
AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::ParameterJackBindings(AudioChannelJack* pChannel) : DeviceRuntimeParameterStrings(std::vector<String>()) { |
AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::ParameterJackBindings(AudioChannelJack* pChannel) : DeviceRuntimeParameterStrings(std::vector<String>()) { |
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 |
|
|
77 |
void AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::OnSetValue(std::vector<String> vS) { |
void AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::OnSetValue(std::vector<String> vS) { |
78 |
// TODO: we should remove all existing bindings before we connect new ones here |
String src_name = ((DeviceCreationParameterString*)pChannel->pDevice->Parameters["NAME"])->ValueAsString() + ":" + |
79 |
String src_name = "LinuxSampler:" + ((DeviceRuntimeParameterString*)pChannel->Parameters["NAME"])->ValueAsString(); |
((DeviceRuntimeParameterString*)pChannel->Parameters["NAME"])->ValueAsString(); |
80 |
|
// disconnect all current bindings first |
81 |
|
for (int i = 0; i < Bindings.size(); i++) { |
82 |
|
String dst_name = Bindings[i]; |
83 |
|
int res = jack_disconnect(pChannel->pDevice->hJackClient, src_name.c_str(), dst_name.c_str()); |
84 |
|
} |
85 |
|
// connect new bindings |
86 |
for (int i = 0; i < vS.size(); i++) { |
for (int i = 0; i < vS.size(); i++) { |
87 |
String dst_name = vS[i]; |
String dst_name = vS[i]; |
88 |
int res = jack_connect(pChannel->pDevice->hJackClient, src_name.c_str(), dst_name.c_str()); |
int res = jack_connect(pChannel->pDevice->hJackClient, src_name.c_str(), dst_name.c_str()); |
89 |
if (res == EEXIST) throw AudioOutputException("Jack: Connection to port '" + dst_name + "' already established"); |
if (res == EEXIST) throw AudioOutputException("Jack: Connection to port '" + dst_name + "' already established"); |
90 |
else if (res) throw AudioOutputException("Jack: Cannot connect port '" + src_name + "' to port '" + dst_name + "'"); |
else if (res) throw AudioOutputException("Jack: Cannot connect port '" + src_name + "' to port '" + dst_name + "'"); |
91 |
} |
} |
92 |
|
// remember bindings |
93 |
|
Bindings = vS; |
94 |
} |
} |
95 |
|
|
96 |
String AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::Name() { |
String AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::Name() { |
105 |
AudioOutputDeviceJack::AudioChannelJack::AudioChannelJack(uint ChannelNr, AudioOutputDeviceJack* pDevice) throw (AudioOutputException) : AudioChannel(ChannelNr, CreateJackPort(ChannelNr, pDevice), pDevice->uiMaxSamplesPerCycle) { |
AudioOutputDeviceJack::AudioChannelJack::AudioChannelJack(uint ChannelNr, AudioOutputDeviceJack* pDevice) throw (AudioOutputException) : AudioChannel(ChannelNr, CreateJackPort(ChannelNr, pDevice), pDevice->uiMaxSamplesPerCycle) { |
106 |
this->pDevice = pDevice; |
this->pDevice = pDevice; |
107 |
this->ChannelNr = ChannelNr; |
this->ChannelNr = ChannelNr; |
108 |
|
delete Parameters["NAME"]; |
109 |
Parameters["NAME"] = new ParameterName(this); |
Parameters["NAME"] = new ParameterName(this); |
110 |
Parameters["JACK_BINDINGS"] = new ParameterJackBindings(this); |
Parameters["JACK_BINDINGS"] = new ParameterJackBindings(this); |
111 |
} |
} |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
// *************** AudioOutputDeviceJack::ParameterName *************** |
127 |
|
// * |
128 |
|
|
129 |
|
AudioOutputDeviceJack::ParameterName::ParameterName() : DeviceCreationParameterString() { |
130 |
|
InitWithDefault(); // use default name |
131 |
|
} |
132 |
|
|
133 |
|
AudioOutputDeviceJack::ParameterName::ParameterName(String s) throw (Exception) : DeviceCreationParameterString(s) { |
134 |
|
} |
135 |
|
|
136 |
|
String AudioOutputDeviceJack::ParameterName::Description() { |
137 |
|
return "Arbitrary JACK client name"; |
138 |
|
} |
139 |
|
|
140 |
|
bool AudioOutputDeviceJack::ParameterName::Fix() { |
141 |
|
return true; |
142 |
|
} |
143 |
|
|
144 |
|
bool AudioOutputDeviceJack::ParameterName::Mandatory() { |
145 |
|
return false; |
146 |
|
} |
147 |
|
|
148 |
|
std::map<String,DeviceCreationParameter*> AudioOutputDeviceJack::ParameterName::DependsAsParameters() { |
149 |
|
return std::map<String,DeviceCreationParameter*>(); // no dependencies |
150 |
|
} |
151 |
|
|
152 |
|
std::vector<String> AudioOutputDeviceJack::ParameterName::PossibilitiesAsString(std::map<String,String> Parameters) { |
153 |
|
return std::vector<String>(); |
154 |
|
} |
155 |
|
|
156 |
|
optional<String> AudioOutputDeviceJack::ParameterName::DefaultAsString(std::map<String,String> Parameters) { |
157 |
|
return (existingJackDevices) ? "LinuxSampler" + ToString(existingJackDevices) : "LinuxSampler"; |
158 |
|
} |
159 |
|
|
160 |
|
void AudioOutputDeviceJack::ParameterName::OnSetValue(String s) throw (Exception) { |
161 |
|
// not possible, as parameter is fix |
162 |
|
} |
163 |
|
|
164 |
|
String AudioOutputDeviceJack::ParameterName::Name() { |
165 |
|
return "NAME"; |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
|
|
170 |
// *************** AudioOutputDeviceJack *************** |
// *************** AudioOutputDeviceJack *************** |
171 |
// * |
// * |
172 |
|
|
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 ((hJackClient = jack_client_new("LinuxSampler")) == 0) |
JackClient* pJackClient = JackClient::CreateAudio(((DeviceCreationParameterString*)Parameters["NAME"])->ValueAsString()); |
182 |
throw AudioOutputException("Seems Jack server not running."); |
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--; |
198 |
} |
} |
199 |
|
|
200 |
/** |
/** |
217 |
} |
} |
218 |
|
|
219 |
bool AudioOutputDeviceJack::IsPlaying() { |
bool AudioOutputDeviceJack::IsPlaying() { |
220 |
csIsPlaying.GetUnsafe(); |
return csIsPlaying.GetUnsafe(); |
221 |
} |
} |
222 |
|
|
223 |
void AudioOutputDeviceJack::Stop() { |
void AudioOutputDeviceJack::Stop() { |
249 |
} |
} |
250 |
|
|
251 |
String AudioOutputDeviceJack::Version() { |
String AudioOutputDeviceJack::Version() { |
252 |
String s = "$Revision: 1.12 $"; |
String s = "$Revision: 1.24 $"; |
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 |
|
#if HAVE_JACK_MIDI |
295 |
|
config.MidiDevice = 0; |
296 |
|
#endif |
297 |
|
} |
298 |
|
{ |
299 |
|
config_t& config = Config.SwitchConfig(); |
300 |
|
config.AudioDevice = 0; |
301 |
|
#if HAVE_JACK_MIDI |
302 |
|
config.MidiDevice = 0; |
303 |
|
#endif |
304 |
|
} |
305 |
|
audio = midi = false; |
306 |
|
if (Name.size() >= jack_client_name_size()) |
307 |
|
throw Exception("JACK client name too long"); |
308 |
|
#if HAVE_JACK_CLIENT_OPEN |
309 |
|
hJackClient = jack_client_open(Name.c_str(), JackNullOption, NULL); |
310 |
|
#else |
311 |
|
hJackClient = jack_client_new(Name.c_str()); |
312 |
|
#endif |
313 |
|
if (!hJackClient) |
314 |
|
throw Exception("Seems Jack server is not running."); |
315 |
|
jack_set_process_callback(hJackClient, linuxsampler_libjack_process_callback, this); |
316 |
|
jack_on_shutdown(hJackClient, linuxsampler_libjack_shutdown_callback, this); |
317 |
|
if (jack_activate(hJackClient)) |
318 |
|
throw Exception("Jack: Cannot activate Jack client."); |
319 |
|
} |
320 |
|
|
321 |
|
JackClient::~JackClient() { |
322 |
|
jack_client_close(hJackClient); |
323 |
|
} |
324 |
|
|
325 |
|
void JackClient::SetAudioOutputDevice(AudioOutputDeviceJack* device) { |
326 |
|
Config.GetConfigForUpdate().AudioDevice = device; |
327 |
|
Config.SwitchConfig().AudioDevice = device; |
328 |
|
} |
329 |
|
|
330 |
|
#if HAVE_JACK_MIDI |
331 |
|
void JackClient::SetMidiInputDevice(MidiInputDeviceJack* device) { |
332 |
|
Config.GetConfigForUpdate().MidiDevice = device; |
333 |
|
Config.SwitchConfig().MidiDevice = device; |
334 |
|
} |
335 |
|
#endif |
336 |
|
|
337 |
|
JackClient* JackClient::CreateAudio(String Name) { // static |
338 |
|
JackClient* client; |
339 |
|
std::map<String, JackClient*>::const_iterator it = Clients.find(Name); |
340 |
|
if (it == Clients.end()) { |
341 |
|
client = new JackClient(Name); |
342 |
|
Clients[Name] = client; |
343 |
|
} else { |
344 |
|
client = it->second; |
345 |
|
if (client->audio) throw Exception("Jack audio device '" + Name + "' already exists"); |
346 |
|
} |
347 |
|
client->audio = true; |
348 |
|
return client; |
349 |
|
} |
350 |
|
|
351 |
|
JackClient* JackClient::CreateMidi(String Name) { // static |
352 |
|
JackClient* client; |
353 |
|
std::map<String, JackClient*>::const_iterator it = Clients.find(Name); |
354 |
|
if (it == Clients.end()) { |
355 |
|
client = new JackClient(Name); |
356 |
|
Clients[Name] = client; |
357 |
|
} else { |
358 |
|
client = it->second; |
359 |
|
if (client->midi) throw Exception("Jack MIDI device '" + Name + "' already exists"); |
360 |
|
} |
361 |
|
client->midi = true; |
362 |
|
return client; |
363 |
|
} |
364 |
|
|
365 |
|
void JackClient::ReleaseAudio(String Name) { // static |
366 |
|
JackClient* client = Clients[Name]; |
367 |
|
client->SetAudioOutputDevice(0); |
368 |
|
client->audio = false; |
369 |
|
if (!client->midi) { |
370 |
|
Clients.erase(Name); |
371 |
|
delete client; |
372 |
|
} |
373 |
|
} |
374 |
|
|
375 |
|
void JackClient::ReleaseMidi(String Name) { // static |
376 |
|
JackClient* client = Clients[Name]; |
377 |
|
#if HAVE_JACK_MIDI |
378 |
|
client->SetMidiInputDevice(0); |
379 |
|
#endif |
380 |
|
client->midi = false; |
381 |
|
if (!client->audio) { |
382 |
|
Clients.erase(Name); |
383 |
|
delete client; |
384 |
|
} |
385 |
|
} |
386 |
|
|
387 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |
388 |
|
|
389 |
#endif // HAVE_JACK |
#endif // HAVE_JACK |