/[svn]/linuxsampler/trunk/src/drivers/audio/AudioOutputDeviceJack.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/drivers/audio/AudioOutputDeviceJack.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2444 - (hide annotations) (download)
Tue Apr 23 12:19:57 2013 UTC (11 years ago) by schoenebeck
File size: 17049 byte(s)
* JACK driver: Use jack_on_info_shutdown() instead of the
  deprecated jack_on_shutdown() callback mechanism.

1 schoenebeck 200 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 2410 * Copyright (C) 2005 - 2013 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 persson 1651 const char** pPortNames = jack_get_ports(pChannel->pDevice->hJackClient, NULL, JACK_DEFAULT_AUDIO_TYPE, 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 persson 1651 free(pPortNames);
74 schoenebeck 226 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 schoenebeck 2412 /*int res =*/ jack_disconnect(pChannel->pDevice->hJackClient, src_name.c_str(), dst_name.c_str());
84 schoenebeck 483 }
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 schoenebeck 2410
124     void AudioOutputDeviceJack::AudioChannelJack::UpdateJackBuffer(uint size) {
125     SetBuffer(
126     (float*)jack_port_get_buffer(hJackPort, size)
127     );
128     }
129 schoenebeck 226
130    
131    
132 schoenebeck 374 // *************** AudioOutputDeviceJack::ParameterName ***************
133     // *
134    
135     AudioOutputDeviceJack::ParameterName::ParameterName() : DeviceCreationParameterString() {
136     InitWithDefault(); // use default name
137     }
138    
139 schoenebeck 880 AudioOutputDeviceJack::ParameterName::ParameterName(String s) throw (Exception) : DeviceCreationParameterString(s) {
140 schoenebeck 374 }
141    
142     String AudioOutputDeviceJack::ParameterName::Description() {
143     return "Arbitrary JACK client name";
144     }
145    
146     bool AudioOutputDeviceJack::ParameterName::Fix() {
147     return true;
148     }
149    
150     bool AudioOutputDeviceJack::ParameterName::Mandatory() {
151     return false;
152     }
153    
154     std::map<String,DeviceCreationParameter*> AudioOutputDeviceJack::ParameterName::DependsAsParameters() {
155     return std::map<String,DeviceCreationParameter*>(); // no dependencies
156     }
157    
158     std::vector<String> AudioOutputDeviceJack::ParameterName::PossibilitiesAsString(std::map<String,String> Parameters) {
159     return std::vector<String>();
160     }
161    
162     optional<String> AudioOutputDeviceJack::ParameterName::DefaultAsString(std::map<String,String> Parameters) {
163     return (existingJackDevices) ? "LinuxSampler" + ToString(existingJackDevices) : "LinuxSampler";
164     }
165    
166 schoenebeck 880 void AudioOutputDeviceJack::ParameterName::OnSetValue(String s) throw (Exception) {
167 schoenebeck 374 // not possible, as parameter is fix
168     }
169    
170     String AudioOutputDeviceJack::ParameterName::Name() {
171     return "NAME";
172     }
173    
174    
175    
176 schoenebeck 226 // *************** AudioOutputDeviceJack ***************
177     // *
178    
179 schoenebeck 200 /**
180     * Open and initialize connection to the JACK system.
181     *
182     * @param Parameters - optional parameters
183     * @throws AudioOutputException on error
184     * @see AcquireChannels()
185     */
186 schoenebeck 226 AudioOutputDeviceJack::AudioOutputDeviceJack(std::map<String,DeviceCreationParameter*> Parameters) : AudioOutputDevice(Parameters) {
187 schoenebeck 2412 pJackClient = JackClient::CreateAudio(((DeviceCreationParameterString*)Parameters["NAME"])->ValueAsString());
188 schoenebeck 374 existingJackDevices++;
189 persson 1651 pJackClient->SetAudioOutputDevice(this);
190     hJackClient = pJackClient->hJackClient;
191 schoenebeck 200 uiMaxSamplesPerCycle = jack_get_buffer_size(hJackClient);
192    
193 schoenebeck 226 // create audio channels
194     AcquireChannels(((DeviceCreationParameterInt*)Parameters["CHANNELS"])->ValueAsInt());
195 schoenebeck 227
196     // finally activate device if desired
197     if (((DeviceCreationParameterBool*)Parameters["ACTIVE"])->ValueAsBool()) Play();
198 schoenebeck 200 }
199    
200     AudioOutputDeviceJack::~AudioOutputDeviceJack() {
201 persson 1651 // destroy jack client if there is no midi device associated with it
202     JackClient::ReleaseAudio(((DeviceCreationParameterString*)Parameters["NAME"])->ValueAsString());
203 schoenebeck 374 existingJackDevices--;
204 schoenebeck 200 }
205    
206     /**
207     * This method should not be called directly! It will be called by
208     * libjack to demand transmission of further sample points.
209     */
210     int AudioOutputDeviceJack::Process(uint Samples) {
211 persson 1764 int res;
212 schoenebeck 2410
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 schoenebeck 200 if (csIsPlaying.Pop()) {
218     // let all connected engines render 'Samples' sample points
219 persson 1764 res = RenderAudio(Samples);
220 schoenebeck 200 }
221     else {
222     // playback stop by zeroing output buffer(s) and not calling connected sampler engines to render audio
223 persson 1764 res = RenderSilence(Samples);
224 schoenebeck 200 }
225 persson 1764 csIsPlaying.RttDone();
226     return res;
227 schoenebeck 200 }
228 schoenebeck 2410
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 schoenebeck 2412
241     jack_client_t* AudioOutputDeviceJack::jackClientHandle() {
242     return hJackClient;
243     }
244 schoenebeck 200
245     void AudioOutputDeviceJack::Play() {
246     csIsPlaying.PushAndUnlock(true);
247     }
248    
249     bool AudioOutputDeviceJack::IsPlaying() {
250 persson 497 return csIsPlaying.GetUnsafe();
251 schoenebeck 200 }
252    
253     void AudioOutputDeviceJack::Stop() {
254     csIsPlaying.PushAndUnlock(false);
255     }
256    
257 schoenebeck 226 AudioChannel* AudioOutputDeviceJack::CreateChannel(uint ChannelNr) {
258     return new AudioChannelJack(ChannelNr, this);
259 schoenebeck 200 }
260    
261     uint AudioOutputDeviceJack::MaxSamplesPerCycle() {
262     return jack_get_buffer_size(hJackClient);
263     }
264    
265     uint AudioOutputDeviceJack::SampleRate() {
266     return jack_get_sample_rate(hJackClient);
267     }
268 schoenebeck 2412
269     void AudioOutputDeviceJack::addListener(JackListener* listener) {
270     pJackClient->addListener(listener);
271     }
272 schoenebeck 200
273     String AudioOutputDeviceJack::Name() {
274 schoenebeck 221 return "JACK";
275 schoenebeck 200 }
276    
277     String AudioOutputDeviceJack::Driver() {
278     return Name();
279     }
280    
281     String AudioOutputDeviceJack::Description() {
282     return "JACK Audio Connection Kit";
283     }
284    
285     String AudioOutputDeviceJack::Version() {
286 persson 1764 String s = "$Revision: 1.25 $";
287 schoenebeck 200 return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
288     }
289    
290 persson 1651
291    
292     // *************** JackClient ***************
293     // *
294    
295 schoenebeck 200 // libjack callback functions
296    
297 persson 1651 int linuxsampler_libjack_process_callback(jack_nframes_t nframes, void* arg) {
298     return static_cast<JackClient*>(arg)->Process(nframes);
299 schoenebeck 200 }
300    
301 schoenebeck 2444 void JackClient::libjackShutdownCallback(jack_status_t code, const char* reason, void *arg) {
302 schoenebeck 2412 JackClient* jackClient = static_cast<JackClient*>(arg);
303     jackClient->Stop();
304 schoenebeck 200 fprintf(stderr, "Jack: Jack server shutdown, exiting.\n");
305 schoenebeck 2412 for (int i = 0; i < jackClient->jackListeners.size(); ++i) {
306     JackListener* listener = jackClient->jackListeners[i];
307 schoenebeck 2444 listener->onJackShutdown(code, reason);
308 schoenebeck 2412 }
309 schoenebeck 200 }
310 schoenebeck 2410
311     int JackClient::libjackSampleRateCallback(jack_nframes_t nframes, void *arg) {
312     JackClient* client = static_cast<JackClient*>(arg);
313     const config_t& config = client->ConfigReader.Lock();
314     if (config.AudioDevice)
315     config.AudioDevice->ReconnectAll();
316     client->ConfigReader.Unlock();
317     return 0;
318     }
319    
320     int JackClient::libjackBufferSizeCallback(jack_nframes_t nframes, void *arg) {
321     dmsg(1,("libjackBufferSizeCallback(%d)\n",nframes));
322     JackClient* client = static_cast<JackClient*>(arg);
323     const config_t& config = client->ConfigReader.Lock();
324     if (config.AudioDevice) {
325     config.AudioDevice->UpdateJackBuffers(nframes);
326     config.AudioDevice->ReconnectAll();
327     }
328     client->ConfigReader.Unlock();
329     return 0;
330     }
331 schoenebeck 2412
332     void JackClient::addListener(JackListener* listener) {
333     jackListeners.push_back(listener);
334     }
335 schoenebeck 200
336 persson 1651 std::map<String, JackClient*> JackClient::Clients;
337    
338     int JackClient::Process(uint Samples) {
339     const config_t& config = ConfigReader.Lock();
340     #if HAVE_JACK_MIDI
341     if (config.MidiDevice) config.MidiDevice->Process(Samples);
342     #endif
343     int res = config.AudioDevice ? config.AudioDevice->Process(Samples) : 0;
344     ConfigReader.Unlock();
345     return res;
346     }
347    
348     void JackClient::Stop() {
349     const config_t& config = ConfigReader.Lock();
350     if (config.AudioDevice) config.AudioDevice->Stop();
351     ConfigReader.Unlock();
352     }
353    
354     JackClient::JackClient(String Name) : ConfigReader(Config) {
355     {
356     config_t& config = Config.GetConfigForUpdate();
357     config.AudioDevice = 0;
358 schoenebeck 1682 #if HAVE_JACK_MIDI
359 persson 1651 config.MidiDevice = 0;
360 schoenebeck 1682 #endif
361 persson 1651 }
362     {
363     config_t& config = Config.SwitchConfig();
364     config.AudioDevice = 0;
365 schoenebeck 1682 #if HAVE_JACK_MIDI
366 persson 1651 config.MidiDevice = 0;
367 schoenebeck 1682 #endif
368 persson 1651 }
369     audio = midi = false;
370     if (Name.size() >= jack_client_name_size())
371     throw Exception("JACK client name too long");
372 persson 1748 #if HAVE_JACK_CLIENT_OPEN
373     hJackClient = jack_client_open(Name.c_str(), JackNullOption, NULL);
374     #else
375     hJackClient = jack_client_new(Name.c_str());
376     #endif
377     if (!hJackClient)
378 persson 1651 throw Exception("Seems Jack server is not running.");
379     jack_set_process_callback(hJackClient, linuxsampler_libjack_process_callback, this);
380 schoenebeck 2444 jack_on_info_shutdown(hJackClient, libjackShutdownCallback, this);
381 schoenebeck 2410 jack_set_buffer_size_callback(hJackClient, libjackBufferSizeCallback, this);
382     jack_set_sample_rate_callback(hJackClient, libjackSampleRateCallback, this);
383    
384 persson 1651 if (jack_activate(hJackClient))
385     throw Exception("Jack: Cannot activate Jack client.");
386     }
387    
388     JackClient::~JackClient() {
389     jack_client_close(hJackClient);
390     }
391    
392     void JackClient::SetAudioOutputDevice(AudioOutputDeviceJack* device) {
393     Config.GetConfigForUpdate().AudioDevice = device;
394     Config.SwitchConfig().AudioDevice = device;
395     }
396    
397 schoenebeck 1682 #if HAVE_JACK_MIDI
398 persson 1651 void JackClient::SetMidiInputDevice(MidiInputDeviceJack* device) {
399     Config.GetConfigForUpdate().MidiDevice = device;
400     Config.SwitchConfig().MidiDevice = device;
401     }
402 schoenebeck 1682 #endif
403 persson 1651
404     JackClient* JackClient::CreateAudio(String Name) { // static
405     JackClient* client;
406     std::map<String, JackClient*>::const_iterator it = Clients.find(Name);
407     if (it == Clients.end()) {
408     client = new JackClient(Name);
409     Clients[Name] = client;
410     } else {
411     client = it->second;
412     if (client->audio) throw Exception("Jack audio device '" + Name + "' already exists");
413     }
414     client->audio = true;
415     return client;
416     }
417    
418     JackClient* JackClient::CreateMidi(String Name) { // static
419     JackClient* client;
420     std::map<String, JackClient*>::const_iterator it = Clients.find(Name);
421     if (it == Clients.end()) {
422     client = new JackClient(Name);
423     Clients[Name] = client;
424     } else {
425     client = it->second;
426     if (client->midi) throw Exception("Jack MIDI device '" + Name + "' already exists");
427     }
428     client->midi = true;
429     return client;
430     }
431    
432     void JackClient::ReleaseAudio(String Name) { // static
433     JackClient* client = Clients[Name];
434     client->SetAudioOutputDevice(0);
435     client->audio = false;
436     if (!client->midi) {
437     Clients.erase(Name);
438     delete client;
439     }
440     }
441    
442     void JackClient::ReleaseMidi(String Name) { // static
443     JackClient* client = Clients[Name];
444 schoenebeck 1682 #if HAVE_JACK_MIDI
445 persson 1651 client->SetMidiInputDevice(0);
446 schoenebeck 1682 #endif
447 persson 1651 client->midi = false;
448     if (!client->audio) {
449     Clients.erase(Name);
450     delete client;
451     }
452     }
453    
454 schoenebeck 200 } // namespace LinuxSampler
455    
456     #endif // HAVE_JACK

  ViewVC Help
Powered by ViewVC