2 Copyright (C) 2010 Paul Davis
3 Copyright (C) 2011 Tim Mayberry
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "libardour-config.h"
26 #include <alsa/asoundlib.h>
30 #include <CoreAudio/CoreAudio.h>
31 #include <CoreFoundation/CFString.h>
32 #include <sys/param.h>
33 #include <mach-o/dyld.h>
37 #include <portaudio.h>
40 #include <jack/jack.h>
44 #include <boost/scoped_ptr.hpp>
46 #include <glibmm/miscutils.h>
49 #include "pbd/error.h"
50 #include "pbd/convert.h"
51 #include "pbd/file_utils.h"
52 #include "pbd/search_path.h"
54 #include "ardour/jack_utils.h"
66 // The pretty driver names
67 const char * const portaudio_driver_name = X_("Portaudio");
68 const char * const coreaudio_driver_name = X_("CoreAudio");
69 const char * const alsa_driver_name = X_("ALSA");
70 const char * const oss_driver_name = X_("OSS");
71 const char * const freebob_driver_name = X_("FreeBoB");
72 const char * const ffado_driver_name = X_("FFADO");
73 const char * const netjack_driver_name = X_("NetJACK");
74 const char * const dummy_driver_name = X_("Dummy");
79 // The real driver names
80 const char * const portaudio_driver_command_line_name = X_("portaudio");
81 const char * const coreaudio_driver_command_line_name = X_("coreaudio");
82 const char * const alsa_driver_command_line_name = X_("alsa");
83 const char * const oss_driver_command_line_name = X_("oss");
84 const char * const freebob_driver_command_line_name = X_("freebob");
85 const char * const ffado_driver_command_line_name = X_("firewire");
86 const char * const netjack_driver_command_line_name = X_("netjack");
87 const char * const dummy_driver_command_line_name = X_("dummy");
89 // should we provide more "pretty" names like above?
90 const char * const alsaseq_midi_driver_name = X_("seq");
91 const char * const alsaraw_midi_driver_name = X_("raw");
92 const char * const winmme_midi_driver_name = X_("winmme");
93 const char * const coremidi_midi_driver_name = X_("coremidi");
95 // this should probably be translated
96 const char * const default_device_name = X_("Default");
106 ARDOUR::get_jack_audio_driver_names (vector<string>& audio_driver_names)
109 audio_driver_names.push_back (portaudio_driver_name);
111 audio_driver_names.push_back (coreaudio_driver_name);
114 audio_driver_names.push_back (alsa_driver_name);
116 audio_driver_names.push_back (oss_driver_name);
117 audio_driver_names.push_back (freebob_driver_name);
118 audio_driver_names.push_back (ffado_driver_name);
120 audio_driver_names.push_back (netjack_driver_name);
121 audio_driver_names.push_back (dummy_driver_name);
125 ARDOUR::get_jack_default_audio_driver_name (string& audio_driver_name)
127 vector<string> drivers;
128 get_jack_audio_driver_names (drivers);
129 audio_driver_name = drivers.front ();
133 ARDOUR::get_jack_sample_rate_strings (vector<string>& samplerates)
135 // do these really need to be translated?
136 samplerates.push_back (_("8000Hz"));
137 samplerates.push_back (_("22050Hz"));
138 samplerates.push_back (_("44100Hz"));
139 samplerates.push_back (_("48000Hz"));
140 samplerates.push_back (_("88200Hz"));
141 samplerates.push_back (_("96000Hz"));
142 samplerates.push_back (_("192000Hz"));
146 ARDOUR::get_jack_default_sample_rate ()
152 ARDOUR::get_jack_period_size_strings (std::vector<std::string>& period_sizes)
154 period_sizes.push_back ("32");
155 period_sizes.push_back ("64");
156 period_sizes.push_back ("128");
157 period_sizes.push_back ("256");
158 period_sizes.push_back ("512");
159 period_sizes.push_back ("1024");
160 period_sizes.push_back ("2048");
161 period_sizes.push_back ("4096");
162 period_sizes.push_back ("8192");
166 ARDOUR::get_jack_default_period_size ()
172 ARDOUR::get_jack_dither_mode_strings (const string& driver, vector<string>& dither_modes)
174 dither_modes.push_back (get_none_string ());
176 if (driver == alsa_driver_name ) {
177 dither_modes.push_back (_("Triangular"));
178 dither_modes.push_back (_("Rectangular"));
179 dither_modes.push_back (_("Shaped"));
184 ARDOUR::get_jack_default_dither_mode (const string& /*driver*/)
186 return get_none_string ();
190 ARDOUR::get_jack_latency_string (string samplerate, float periods, string period_size)
192 uint32_t rate = atoi (samplerate);
193 float psize = atof (period_size);
196 snprintf (buf, sizeof(buf), "%.1fmsec", (periods * psize) / (rate/1000.0));
202 get_jack_command_line_audio_driver_name (const string& driver_name, string& command_line_name)
204 using namespace ARDOUR;
205 if (driver_name == portaudio_driver_name) {
206 command_line_name = portaudio_driver_command_line_name;
208 } else if (driver_name == coreaudio_driver_name) {
209 command_line_name = coreaudio_driver_command_line_name;
211 } else if (driver_name == alsa_driver_name) {
212 command_line_name = alsa_driver_command_line_name;
214 } else if (driver_name == oss_driver_name) {
215 command_line_name = oss_driver_command_line_name;
217 } else if (driver_name == freebob_driver_name) {
218 command_line_name = freebob_driver_command_line_name;
220 } else if (driver_name == ffado_driver_name) {
221 command_line_name = ffado_driver_command_line_name;
223 } else if (driver_name == netjack_driver_name) {
224 command_line_name = netjack_driver_command_line_name;
226 } else if (driver_name == dummy_driver_name) {
227 command_line_name = dummy_driver_command_line_name;
234 get_jack_command_line_audio_device_name (const string& driver_name,
235 const string& device_name, string& command_line_device_name)
237 using namespace ARDOUR;
238 device_map_t devices;
240 get_jack_device_names_for_audio_driver (driver_name, devices);
242 for (device_map_t::const_iterator i = devices.begin (); i != devices.end(); ++i) {
243 if (i->first == device_name) {
244 command_line_device_name = i->second;
252 get_jack_command_line_dither_mode (const string& dither_mode, string& command_line_dither_mode)
254 using namespace ARDOUR;
256 if (dither_mode == _("Triangular")) {
257 command_line_dither_mode = "triangular";
259 } else if (dither_mode == _("Rectangular")) {
260 command_line_dither_mode = "rectangular";
262 } else if (dither_mode == _("Shaped")) {
263 command_line_dither_mode = "shaped";
271 ARDOUR::get_jack_alsa_device_names (device_map_t& devices)
275 snd_ctl_card_info_t *info;
276 snd_pcm_info_t *pcminfo;
277 snd_ctl_card_info_alloca(&info);
278 snd_pcm_info_alloca(&pcminfo);
283 while (snd_card_next (&cardnum) >= 0 && cardnum >= 0) {
286 devname += PBD::to_string (cardnum, std::dec);
288 if (snd_ctl_open (&handle, devname.c_str(), 0) >= 0 && snd_ctl_card_info (handle, info) >= 0) {
290 if (snd_ctl_card_info (handle, info) < 0) {
294 string card_name = snd_ctl_card_info_get_name (info);
296 /* change devname to use ID, not number */
299 devname += snd_ctl_card_info_get_id (info);
301 while (snd_ctl_pcm_next_device (handle, &device) >= 0 && device >= 0) {
303 /* only detect duplex devices here. more
304 * complex arrangements are beyond our scope
307 snd_pcm_info_set_device (pcminfo, device);
308 snd_pcm_info_set_subdevice (pcminfo, 0);
309 snd_pcm_info_set_stream (pcminfo, SND_PCM_STREAM_CAPTURE);
311 if (snd_ctl_pcm_info (handle, pcminfo) >= 0) {
313 snd_pcm_info_set_device (pcminfo, device);
314 snd_pcm_info_set_subdevice (pcminfo, 0);
315 snd_pcm_info_set_stream (pcminfo, SND_PCM_STREAM_PLAYBACK);
317 if (snd_ctl_pcm_info (handle, pcminfo) >= 0) {
319 devname += PBD::to_string (device, std::dec);
320 devices.insert (std::make_pair (card_name, devname));
325 snd_ctl_close(handle);
329 /* silence a compiler unused variable warning */
336 getDeviceUIDFromID( AudioDeviceID id, char *name, size_t nsize)
338 UInt32 size = sizeof(CFStringRef);
340 OSStatus res = AudioDeviceGetProperty(id, 0, false,
341 kAudioDevicePropertyDeviceUID, &size, &UI);
343 CFStringGetCString(UI,name,nsize,CFStringGetSystemEncoding());
350 ARDOUR::get_jack_coreaudio_device_names (device_map_t& devices)
353 // Find out how many Core Audio devices are there, if any...
354 // (code snippet gently "borrowed" from St?hane Letz jackdmp;)
357 UInt32 outSize = sizeof(isWritable);
359 err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices,
360 &outSize, &isWritable);
362 // Calculate the number of device available...
363 int numCoreDevices = outSize / sizeof(AudioDeviceID);
364 // Make space for the devices we are about to get...
365 AudioDeviceID *coreDeviceIDs = new AudioDeviceID [numCoreDevices];
366 err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices,
367 &outSize, (void *) coreDeviceIDs);
369 // Look for the CoreAudio device name...
370 char coreDeviceName[256];
373 for (int i = 0; i < numCoreDevices; i++) {
375 nameSize = sizeof (coreDeviceName);
377 /* enforce duplex devices only */
379 err = AudioDeviceGetPropertyInfo(coreDeviceIDs[i],
380 0, true, kAudioDevicePropertyStreams,
381 &outSize, &isWritable);
383 if (err != noErr || outSize == 0) {
387 err = AudioDeviceGetPropertyInfo(coreDeviceIDs[i],
388 0, false, kAudioDevicePropertyStreams,
389 &outSize, &isWritable);
391 if (err != noErr || outSize == 0) {
395 err = AudioDeviceGetPropertyInfo(coreDeviceIDs[i],
396 0, true, kAudioDevicePropertyDeviceName,
397 &outSize, &isWritable);
399 err = AudioDeviceGetProperty(coreDeviceIDs[i],
400 0, true, kAudioDevicePropertyDeviceName,
401 &nameSize, (void *) coreDeviceName);
403 char drivername[128];
405 // this returns the unique id for the device
406 // that must be used on the commandline for jack
408 if (getDeviceUIDFromID(coreDeviceIDs[i], drivername, sizeof (drivername)) == noErr) {
409 devices.insert (make_pair (coreDeviceName, drivername));
415 delete [] coreDeviceIDs;
418 /* silence a compiler unused variable warning */
424 ARDOUR::get_jack_portaudio_device_names (device_map_t& devices)
426 #ifdef HAVE_PORTAUDIO
427 if (Pa_Initialize() != paNoError) {
431 for (PaDeviceIndex i = 0; i < Pa_GetDeviceCount (); ++i) {
433 string readable_name;
434 string jack_device_name;
435 const PaDeviceInfo* device_info = Pa_GetDeviceInfo(i);
437 if (device_info != NULL) { // it should never be ?
438 api_name = Pa_GetHostApiInfo (device_info->hostApi)->name;
439 readable_name = api_name + " " + device_info->name;
440 jack_device_name = api_name + "::" + device_info->name;
441 devices.insert (make_pair (readable_name, jack_device_name));
446 /* silence a compiler unused variable warning */
452 ARDOUR::get_jack_oss_device_names (device_map_t& devices)
454 devices.insert (make_pair (default_device_name, default_device_name));
458 ARDOUR::get_jack_freebob_device_names (device_map_t& devices)
460 devices.insert (make_pair (default_device_name, default_device_name));
464 ARDOUR::get_jack_ffado_device_names (device_map_t& devices)
466 devices.insert (make_pair (default_device_name, default_device_name));
470 ARDOUR::get_jack_netjack_device_names (device_map_t& devices)
472 devices.insert (make_pair (default_device_name, default_device_name));
476 ARDOUR::get_jack_dummy_device_names (device_map_t& devices)
478 devices.insert (make_pair (default_device_name, default_device_name));
482 ARDOUR::get_jack_device_names_for_audio_driver (const string& driver_name, device_map_t& devices)
486 if (driver_name == portaudio_driver_name) {
487 get_jack_portaudio_device_names (devices);
488 } else if (driver_name == coreaudio_driver_name) {
489 get_jack_coreaudio_device_names (devices);
490 } else if (driver_name == alsa_driver_name) {
491 get_jack_alsa_device_names (devices);
492 } else if (driver_name == oss_driver_name) {
493 get_jack_oss_device_names (devices);
494 } else if (driver_name == freebob_driver_name) {
495 get_jack_freebob_device_names (devices);
496 } else if (driver_name == ffado_driver_name) {
497 get_jack_ffado_device_names (devices);
498 } else if (driver_name == netjack_driver_name) {
499 get_jack_netjack_device_names (devices);
500 } else if (driver_name == dummy_driver_name) {
501 get_jack_dummy_device_names (devices);
504 return !devices.empty();
508 std::vector<std::string>
509 ARDOUR::get_jack_device_names_for_audio_driver (const string& driver_name)
511 std::vector<std::string> readable_names;
512 device_map_t devices;
514 get_jack_device_names_for_audio_driver (driver_name, devices);
516 for (device_map_t::const_iterator i = devices.begin (); i != devices.end(); ++i) {
517 readable_names.push_back (i->first);
520 return readable_names;
524 ARDOUR::get_jack_audio_driver_supports_two_devices (const string& driver)
526 return (driver == alsa_driver_name || driver == oss_driver_name);
530 ARDOUR::get_jack_audio_driver_supports_latency_adjustment (const string& driver)
532 return (driver == alsa_driver_name || driver == coreaudio_driver_name ||
533 driver == ffado_driver_name || driver == portaudio_driver_name);
537 ARDOUR::get_jack_audio_driver_supports_setting_period_count (const string& driver)
539 return !(driver == dummy_driver_name || driver == coreaudio_driver_name ||
540 driver == portaudio_driver_name);
544 ARDOUR::get_jack_server_application_names (std::vector<std::string>& server_names)
547 server_names.push_back ("jackd.exe");
549 server_names.push_back ("jackd");
550 server_names.push_back ("jackdmp");
552 return !server_names.empty();
556 ARDOUR::set_path_env_for_jack_autostart (const vector<std::string>& dirs)
559 // push it back into the environment so that auto-started JACK can find it.
560 // XXX why can't we just expect OS X users to have PATH set correctly? we can't ...
561 setenv ("PATH", SearchPath(dirs).to_string().c_str(), 1);
563 /* silence a compiler unused variable warning */
569 ARDOUR::get_jack_server_dir_paths (vector<std::string>& server_dir_paths)
572 /* this magic lets us finds the path to the OSX bundle, and then
573 we infer JACK's location from there
576 char execpath[MAXPATHLEN+1];
577 uint32_t pathsz = sizeof (execpath);
579 _NSGetExecutablePath (execpath, &pathsz);
581 server_dir_paths.push_back (Glib::path_get_dirname (execpath));
584 SearchPath sp(string(g_getenv("PATH")));
587 gchar *install_dir = g_win32_get_package_installation_directory_of_module (NULL);
589 sp.push_back (install_dir);
590 g_free (install_dir);
592 // don't try and use a system wide JACK install yet.
595 sp.push_back ("/usr/bin");
596 sp.push_back ("/bin");
597 sp.push_back ("/usr/local/bin");
598 sp.push_back ("/opt/local/bin");
602 std::copy (sp.begin(), sp.end(), std::back_inserter(server_dir_paths));
604 return !server_dir_paths.empty();
608 ARDOUR::get_jack_server_paths (const vector<std::string>& server_dir_paths,
609 const vector<string>& server_names,
610 vector<std::string>& server_paths)
612 for (vector<string>::const_iterator i = server_names.begin(); i != server_names.end(); ++i) {
613 Glib::PatternSpec ps (*i);
614 find_matching_files_in_directories (server_dir_paths, ps, server_paths);
616 return !server_paths.empty();
620 ARDOUR::get_jack_server_paths (vector<std::string>& server_paths)
622 vector<std::string> server_dirs;
624 if (!get_jack_server_dir_paths (server_dirs)) {
628 vector<string> server_names;
630 if (!get_jack_server_application_names (server_names)) {
634 if (!get_jack_server_paths (server_dirs, server_names, server_paths)) {
638 return !server_paths.empty();
642 ARDOUR::get_jack_default_server_path (std::string& server_path)
644 vector<std::string> server_paths;
646 if (!get_jack_server_paths (server_paths)) {
650 server_path = server_paths.front ();
655 quote_string (const string& str)
657 return "\"" + str + "\"";
660 ARDOUR::JackCommandLineOptions::JackCommandLineOptions ()
667 , unlock_gui_libs(false)
678 , hardware_metering(false)
679 , hardware_monitoring(false)
689 ARDOUR::get_jack_command_line_string (const JackCommandLineOptions& options, string& command_line)
693 args.push_back (options.server_path);
696 // must use sync mode on windows
697 args.push_back ("-S");
699 // this needs to be added now on windows
700 if (!options.midi_driver.empty () && options.midi_driver != get_none_string ()) {
701 args.push_back ("-X");
702 args.push_back (options.midi_driver);
706 if (options.timeout) {
707 args.push_back ("-t");
708 args.push_back (to_string (options.timeout, std::dec));
711 if (options.no_mlock) {
712 args.push_back ("-m");
715 args.push_back ("-p");
716 args.push_back (to_string(options.ports_max, std::dec));
718 if (options.realtime) {
719 args.push_back ("-R");
720 if (options.priority != 0) {
721 args.push_back ("-P");
722 args.push_back (to_string(options.priority, std::dec));
725 args.push_back ("-r");
728 if (options.unlock_gui_libs) {
729 args.push_back ("-u");
732 if (options.verbose) {
733 args.push_back ("-v");
737 if (options.temporary) {
738 args.push_back ("-T");
742 string command_line_driver_name;
744 if (!get_jack_command_line_audio_driver_name (options.driver, command_line_driver_name)) {
748 args.push_back ("-d");
749 args.push_back (command_line_driver_name);
751 if (options.output_device.empty() && options.input_device.empty()) {
755 string command_line_input_device_name;
756 string command_line_output_device_name;
758 if (!get_jack_command_line_audio_device_name (options.driver,
759 options.input_device, command_line_input_device_name)) {
763 if (!get_jack_command_line_audio_device_name (options.driver,
764 options.output_device, command_line_output_device_name)) {
768 if (options.input_device.empty()) {
770 if (options.output_device.empty()) {
773 args.push_back ("-P");
774 } else if (options.output_device.empty()) {
776 if (options.input_device.empty()) {
779 args.push_back ("-C");
780 } else if (options.input_device != options.output_device) {
781 // capture and playback on two devices if supported
782 if (get_jack_audio_driver_supports_two_devices (options.driver)) {
783 args.push_back ("-C");
784 args.push_back (command_line_input_device_name);
785 args.push_back ("-P");
786 args.push_back (command_line_output_device_name);
792 if (options.input_channels) {
793 args.push_back ("-i");
794 args.push_back (to_string (options.input_channels, std::dec));
797 if (options.output_channels) {
798 args.push_back ("-o");
799 args.push_back (to_string (options.output_channels, std::dec));
802 if (get_jack_audio_driver_supports_setting_period_count (options.driver)) {
803 args.push_back ("-n");
804 args.push_back (to_string (options.num_periods, std::dec));
807 args.push_back ("-r");
808 args.push_back (to_string (options.samplerate, std::dec));
810 args.push_back ("-p");
811 args.push_back (to_string (options.period_size, std::dec));
813 if (get_jack_audio_driver_supports_latency_adjustment (options.driver)) {
814 if (options.input_latency) {
815 args.push_back ("-I");
816 args.push_back (to_string (options.input_latency, std::dec));
818 if (options.output_latency) {
819 args.push_back ("-O");
820 args.push_back (to_string (options.output_latency, std::dec));
824 if (options.input_device == options.output_device && options.input_device != default_device_name) {
825 args.push_back ("-d");
826 args.push_back (command_line_input_device_name);
829 if (options.driver == alsa_driver_name) {
830 if (options.hardware_metering) {
831 args.push_back ("-M");
833 if (options.hardware_monitoring) {
834 args.push_back ("-H");
837 string command_line_dither_mode;
838 if (get_jack_command_line_dither_mode (options.dither_mode, command_line_dither_mode)) {
839 args.push_back ("-z");
840 args.push_back (command_line_dither_mode);
842 if (options.force16_bit) {
843 args.push_back ("-S");
845 if (options.soft_mode) {
846 args.push_back ("-s");
849 if (!options.midi_driver.empty() && options.midi_driver != get_none_string ()) {
850 args.push_back ("-X");
851 args.push_back (options.midi_driver);
857 for (vector<string>::const_iterator i = args.begin(); i != args.end();) {
859 oss << quote_string (*i);
863 if (++i != args.end()) oss << ' ';
866 command_line = oss.str();
871 ARDOUR::get_jack_server_config_file_name ()
877 ARDOUR::get_jack_server_user_config_dir_path ()
879 return Glib::get_home_dir ();
883 ARDOUR::get_jack_server_user_config_file_path ()
885 return Glib::build_filename (get_jack_server_user_config_dir_path (), get_jack_server_config_file_name ());
889 ARDOUR::write_jack_config_file (const std::string& config_file_path, const string& command_line)
891 ofstream jackdrc (config_file_path.c_str());
894 error << string_compose (_("cannot open JACK rc file %1 to store parameters"), config_file_path) << endmsg;
898 jackdrc << command_line << endl;