1 |
schoenebeck |
200 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
|
|
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
schoenebeck |
880 |
* Copyright (C) 2005, 2006 Christian Schoenebeck * |
7 |
schoenebeck |
200 |
* * |
8 |
|
|
* 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 * |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
11 |
|
|
* (at your option) any later version. * |
12 |
|
|
* * |
13 |
|
|
* This program is distributed in the hope that it will be useful, * |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
|
|
* GNU General Public License for more details. * |
17 |
|
|
* * |
18 |
|
|
* You should have received a copy of the GNU General Public License * |
19 |
|
|
* along with this program; if not, write to the Free Software * |
20 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
|
|
* MA 02111-1307 USA * |
22 |
|
|
***************************************************************************/ |
23 |
|
|
|
24 |
|
|
#include "AudioOutputDeviceJack.h" |
25 |
|
|
#include "AudioOutputDeviceFactory.h" |
26 |
|
|
|
27 |
capela |
268 |
#include <errno.h> |
28 |
|
|
|
29 |
schoenebeck |
200 |
#if HAVE_JACK |
30 |
|
|
|
31 |
persson |
379 |
#ifndef HAVE_JACK_CLIENT_NAME_SIZE |
32 |
|
|
#define jack_client_name_size() 33 |
33 |
|
|
#endif |
34 |
|
|
|
35 |
schoenebeck |
200 |
namespace LinuxSampler { |
36 |
|
|
|
37 |
schoenebeck |
374 |
/// number of currently existing JACK audio output devices in LinuxSampler |
38 |
|
|
static int existingJackDevices = 0; |
39 |
|
|
|
40 |
|
|
// *************** AudioChannelJack::ParameterName *************** |
41 |
schoenebeck |
226 |
// * |
42 |
|
|
|
43 |
|
|
AudioOutputDeviceJack::AudioChannelJack::ParameterName::ParameterName(AudioChannelJack* pChannel) : AudioChannel::ParameterName(ToString(pChannel->ChannelNr)) { |
44 |
|
|
this->pChannel = pChannel; |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
void AudioOutputDeviceJack::AudioChannelJack::ParameterName::OnSetValue(String s) { |
48 |
schoenebeck |
227 |
if (jack_port_set_name(pChannel->hJackPort, s.c_str())) throw AudioOutputException("Failed to rename JACK port"); |
49 |
schoenebeck |
226 |
} |
50 |
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
schoenebeck |
374 |
// *************** AudioChannelJack::ParameterJackBindings *************** |
54 |
schoenebeck |
226 |
// * |
55 |
|
|
|
56 |
|
|
AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::ParameterJackBindings(AudioChannelJack* pChannel) : DeviceRuntimeParameterStrings(std::vector<String>()) { |
57 |
|
|
this->pChannel = pChannel; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
String AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::Description() { |
61 |
|
|
return "Bindings to other JACK clients"; |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
bool AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::Fix() { |
65 |
|
|
return false; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
std::vector<String> AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::PossibilitiesAsString() { |
69 |
schoenebeck |
227 |
const char** pPortNames = jack_get_ports(pChannel->pDevice->hJackClient, NULL, NULL, JackPortIsInput); |
70 |
schoenebeck |
226 |
if (!pPortNames) return std::vector<String>(); |
71 |
|
|
std::vector<String> result; |
72 |
|
|
for (int i = 0; pPortNames[i]; i++) result.push_back(pPortNames[i]); |
73 |
|
|
//free(pPortNames); FIXME: pPortNames should be freed here |
74 |
|
|
return result; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
void AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::OnSetValue(std::vector<String> vS) { |
78 |
schoenebeck |
403 |
String src_name = ((DeviceCreationParameterString*)pChannel->pDevice->Parameters["NAME"])->ValueAsString() + ":" + |
79 |
|
|
((DeviceRuntimeParameterString*)pChannel->Parameters["NAME"])->ValueAsString(); |
80 |
schoenebeck |
483 |
// 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 |
schoenebeck |
226 |
for (int i = 0; i < vS.size(); i++) { |
87 |
|
|
String dst_name = vS[i]; |
88 |
schoenebeck |
227 |
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"); |
90 |
|
|
else if (res) throw AudioOutputException("Jack: Cannot connect port '" + src_name + "' to port '" + dst_name + "'"); |
91 |
schoenebeck |
226 |
} |
92 |
schoenebeck |
483 |
// remember bindings |
93 |
|
|
Bindings = vS; |
94 |
schoenebeck |
226 |
} |
95 |
|
|
|
96 |
|
|
String AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::Name() { |
97 |
|
|
return "JACK_BINDINGS"; |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
// *************** AudioChannelJack *************** |
103 |
|
|
// * |
104 |
|
|
|
105 |
|
|
AudioOutputDeviceJack::AudioChannelJack::AudioChannelJack(uint ChannelNr, AudioOutputDeviceJack* pDevice) throw (AudioOutputException) : AudioChannel(ChannelNr, CreateJackPort(ChannelNr, pDevice), pDevice->uiMaxSamplesPerCycle) { |
106 |
|
|
this->pDevice = pDevice; |
107 |
|
|
this->ChannelNr = ChannelNr; |
108 |
persson |
1248 |
delete Parameters["NAME"]; |
109 |
schoenebeck |
226 |
Parameters["NAME"] = new ParameterName(this); |
110 |
|
|
Parameters["JACK_BINDINGS"] = new ParameterJackBindings(this); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
AudioOutputDeviceJack::AudioChannelJack::~AudioChannelJack() { |
114 |
|
|
//TODO: delete JACK port |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
float* AudioOutputDeviceJack::AudioChannelJack::CreateJackPort(uint ChannelNr, AudioOutputDeviceJack* pDevice) throw (AudioOutputException) { |
118 |
schoenebeck |
227 |
String port_id = ToString(ChannelNr); |
119 |
schoenebeck |
226 |
hJackPort = jack_port_register(pDevice->hJackClient, port_id.c_str(), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0); |
120 |
|
|
if (!hJackPort) throw AudioOutputException("Jack: Cannot register Jack output port."); |
121 |
|
|
return (float*) jack_port_get_buffer(hJackPort, pDevice->uiMaxSamplesPerCycle); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
|
125 |
|
|
|
126 |
schoenebeck |
374 |
// *************** AudioOutputDeviceJack::ParameterName *************** |
127 |
|
|
// * |
128 |
|
|
|
129 |
|
|
AudioOutputDeviceJack::ParameterName::ParameterName() : DeviceCreationParameterString() { |
130 |
|
|
InitWithDefault(); // use default name |
131 |
|
|
} |
132 |
|
|
|
133 |
schoenebeck |
880 |
AudioOutputDeviceJack::ParameterName::ParameterName(String s) throw (Exception) : DeviceCreationParameterString(s) { |
134 |
schoenebeck |
374 |
} |
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 |
schoenebeck |
880 |
void AudioOutputDeviceJack::ParameterName::OnSetValue(String s) throw (Exception) { |
161 |
schoenebeck |
374 |
// not possible, as parameter is fix |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
String AudioOutputDeviceJack::ParameterName::Name() { |
165 |
|
|
return "NAME"; |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
|
169 |
|
|
|
170 |
schoenebeck |
226 |
// *************** AudioOutputDeviceJack *************** |
171 |
|
|
// * |
172 |
|
|
|
173 |
schoenebeck |
200 |
/** |
174 |
|
|
* Open and initialize connection to the JACK system. |
175 |
|
|
* |
176 |
|
|
* @param Parameters - optional parameters |
177 |
|
|
* @throws AudioOutputException on error |
178 |
|
|
* @see AcquireChannels() |
179 |
|
|
*/ |
180 |
schoenebeck |
226 |
AudioOutputDeviceJack::AudioOutputDeviceJack(std::map<String,DeviceCreationParameter*> Parameters) : AudioOutputDevice(Parameters) { |
181 |
schoenebeck |
374 |
if (((DeviceCreationParameterString*)Parameters["NAME"])->ValueAsString().size() >= jack_client_name_size()) |
182 |
schoenebeck |
880 |
throw Exception("JACK client name too long"); |
183 |
schoenebeck |
374 |
|
184 |
|
|
if ((hJackClient = jack_client_new(((DeviceCreationParameterString*)Parameters["NAME"])->ValueAsString().c_str())) == 0) |
185 |
schoenebeck |
200 |
throw AudioOutputException("Seems Jack server not running."); |
186 |
|
|
|
187 |
schoenebeck |
374 |
existingJackDevices++; |
188 |
|
|
|
189 |
schoenebeck |
200 |
jack_set_process_callback(hJackClient, __libjack_process_callback, this); |
190 |
|
|
jack_on_shutdown(hJackClient, __libjack_shutdown_callback, this); |
191 |
|
|
if (jack_activate(hJackClient)) |
192 |
|
|
throw AudioOutputException("Jack: Cannot activate Jack client."); |
193 |
|
|
|
194 |
|
|
uiMaxSamplesPerCycle = jack_get_buffer_size(hJackClient); |
195 |
|
|
|
196 |
schoenebeck |
226 |
// create audio channels |
197 |
|
|
AcquireChannels(((DeviceCreationParameterInt*)Parameters["CHANNELS"])->ValueAsInt()); |
198 |
schoenebeck |
227 |
|
199 |
|
|
// finally activate device if desired |
200 |
|
|
if (((DeviceCreationParameterBool*)Parameters["ACTIVE"])->ValueAsBool()) Play(); |
201 |
schoenebeck |
200 |
} |
202 |
|
|
|
203 |
|
|
AudioOutputDeviceJack::~AudioOutputDeviceJack() { |
204 |
|
|
// destroy jack client |
205 |
|
|
jack_client_close(hJackClient); |
206 |
schoenebeck |
374 |
existingJackDevices--; |
207 |
schoenebeck |
200 |
} |
208 |
|
|
|
209 |
|
|
/** |
210 |
|
|
* This method should not be called directly! It will be called by |
211 |
|
|
* libjack to demand transmission of further sample points. |
212 |
|
|
*/ |
213 |
|
|
int AudioOutputDeviceJack::Process(uint Samples) { |
214 |
|
|
if (csIsPlaying.Pop()) { |
215 |
|
|
// let all connected engines render 'Samples' sample points |
216 |
|
|
return RenderAudio(Samples); |
217 |
|
|
} |
218 |
|
|
else { |
219 |
|
|
// playback stop by zeroing output buffer(s) and not calling connected sampler engines to render audio |
220 |
|
|
return RenderSilence(Samples); |
221 |
|
|
} |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
void AudioOutputDeviceJack::Play() { |
225 |
|
|
csIsPlaying.PushAndUnlock(true); |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
bool AudioOutputDeviceJack::IsPlaying() { |
229 |
persson |
497 |
return csIsPlaying.GetUnsafe(); |
230 |
schoenebeck |
200 |
} |
231 |
|
|
|
232 |
|
|
void AudioOutputDeviceJack::Stop() { |
233 |
|
|
csIsPlaying.PushAndUnlock(false); |
234 |
|
|
} |
235 |
|
|
|
236 |
schoenebeck |
226 |
AudioChannel* AudioOutputDeviceJack::CreateChannel(uint ChannelNr) { |
237 |
|
|
return new AudioChannelJack(ChannelNr, this); |
238 |
schoenebeck |
200 |
} |
239 |
|
|
|
240 |
|
|
uint AudioOutputDeviceJack::MaxSamplesPerCycle() { |
241 |
|
|
return jack_get_buffer_size(hJackClient); |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
uint AudioOutputDeviceJack::SampleRate() { |
245 |
|
|
return jack_get_sample_rate(hJackClient); |
246 |
|
|
} |
247 |
|
|
|
248 |
|
|
String AudioOutputDeviceJack::Name() { |
249 |
schoenebeck |
221 |
return "JACK"; |
250 |
schoenebeck |
200 |
} |
251 |
|
|
|
252 |
|
|
String AudioOutputDeviceJack::Driver() { |
253 |
|
|
return Name(); |
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
String AudioOutputDeviceJack::Description() { |
257 |
|
|
return "JACK Audio Connection Kit"; |
258 |
|
|
} |
259 |
|
|
|
260 |
|
|
String AudioOutputDeviceJack::Version() { |
261 |
persson |
1248 |
String s = "$Revision: 1.21 $"; |
262 |
schoenebeck |
200 |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
263 |
|
|
} |
264 |
|
|
|
265 |
|
|
// libjack callback functions |
266 |
|
|
|
267 |
|
|
int __libjack_process_callback(jack_nframes_t nframes, void* arg) { |
268 |
|
|
AudioOutputDeviceJack* pAudioOutputDeviceJack = (AudioOutputDeviceJack*) arg; |
269 |
|
|
return pAudioOutputDeviceJack->Process(nframes); |
270 |
|
|
} |
271 |
|
|
|
272 |
|
|
void __libjack_shutdown_callback(void* arg) { |
273 |
|
|
AudioOutputDeviceJack* pAudioOutputDeviceJack = (AudioOutputDeviceJack*) arg; |
274 |
|
|
pAudioOutputDeviceJack->Stop(); |
275 |
|
|
fprintf(stderr, "Jack: Jack server shutdown, exiting.\n"); |
276 |
|
|
} |
277 |
|
|
|
278 |
|
|
} // namespace LinuxSampler |
279 |
|
|
|
280 |
|
|
#endif // HAVE_JACK |