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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 880 by schoenebeck, Tue Jun 27 22:57:37 2006 UTC revision 1682 by schoenebeck, Wed Feb 13 15:31:56 2008 UTC
# Line 3  Line 3 
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  *
# Line 66  namespace LinuxSampler { Line 66  namespace LinuxSampler {
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    
# Line 105  namespace LinuxSampler { Line 105  namespace LinuxSampler {
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      }      }
# Line 177  namespace LinuxSampler { Line 178  namespace LinuxSampler {
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
# Line 200  namespace LinuxSampler { Line 192  namespace LinuxSampler {
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    
# Line 257  namespace LinuxSampler { Line 249  namespace LinuxSampler {
249      }      }
250    
251      String AudioOutputDeviceJack::Version() {      String AudioOutputDeviceJack::Version() {
252         String s = "$Revision: 1.20 $";         String s = "$Revision: 1.23 $";
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 ((hJackClient = jack_client_new(Name.c_str())) == 0)
309                throw Exception("Seems Jack server is not running.");
310            jack_set_process_callback(hJackClient, linuxsampler_libjack_process_callback, this);
311            jack_on_shutdown(hJackClient, linuxsampler_libjack_shutdown_callback, this);
312            if (jack_activate(hJackClient))
313                throw Exception("Jack: Cannot activate Jack client.");
314        }
315    
316        JackClient::~JackClient() {
317            jack_client_close(hJackClient);
318        }
319    
320        void JackClient::SetAudioOutputDevice(AudioOutputDeviceJack* device) {
321            Config.GetConfigForUpdate().AudioDevice = device;
322            Config.SwitchConfig().AudioDevice = device;
323        }
324    
325    #if HAVE_JACK_MIDI
326        void JackClient::SetMidiInputDevice(MidiInputDeviceJack* device) {
327            Config.GetConfigForUpdate().MidiDevice = device;
328            Config.SwitchConfig().MidiDevice = device;
329        }
330    #endif
331    
332        JackClient* JackClient::CreateAudio(String Name) { // static
333            JackClient* client;
334            std::map<String, JackClient*>::const_iterator it = Clients.find(Name);
335            if (it == Clients.end()) {
336                client = new JackClient(Name);
337                Clients[Name] = client;
338            } else {
339                client = it->second;
340                if (client->audio) throw Exception("Jack audio device '" + Name + "' already exists");
341            }
342            client->audio = true;
343            return client;
344        }
345    
346        JackClient* JackClient::CreateMidi(String Name) { // static
347            JackClient* client;
348            std::map<String, JackClient*>::const_iterator it = Clients.find(Name);
349            if (it == Clients.end()) {
350                client = new JackClient(Name);
351                Clients[Name] = client;
352            } else {
353                client = it->second;
354                if (client->midi) throw Exception("Jack MIDI device '" + Name + "' already exists");
355            }
356            client->midi = true;
357            return client;
358        }
359    
360        void JackClient::ReleaseAudio(String Name) { // static
361            JackClient* client = Clients[Name];
362            client->SetAudioOutputDevice(0);
363            client->audio = false;
364            if (!client->midi) {
365                Clients.erase(Name);
366                delete client;
367            }
368        }
369    
370        void JackClient::ReleaseMidi(String Name) { // static
371            JackClient* client = Clients[Name];
372    #if HAVE_JACK_MIDI
373            client->SetMidiInputDevice(0);
374    #endif
375            client->midi = false;
376            if (!client->audio) {
377                Clients.erase(Name);
378                delete client;
379            }
380        }
381    
382  } // namespace LinuxSampler  } // namespace LinuxSampler
383    
384  #endif // HAVE_JACK  #endif // HAVE_JACK

Legend:
Removed from v.880  
changed lines
  Added in v.1682

  ViewVC Help
Powered by ViewVC