2 Copyright (C) 2000 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "libardour-config.h"
27 #include <cstdio> // Needed so that libraptor (included in lrdf) won't complain
30 #include <sys/types.h>
32 #ifndef PLATFORM_WINDOWS
33 #include <sys/resource.h>
41 #include "pbd/gstdio_compat.h"
43 #ifdef PLATFORM_WINDOWS
44 #include <windows.h> // for LARGE_INTEGER
47 #ifdef WINDOWS_VST_SUPPORT
52 #include "ardour/linux_vst_support.h"
55 #ifdef AUDIOUNIT_SUPPORT
56 #include "ardour/audio_unit.h"
59 #if defined(__SSE__) || defined(USE_XMMINTRIN)
60 #include <xmmintrin.h>
64 #undef check /* stupid Apple and their un-namespaced, generic Carbon macros */
67 #include <glibmm/fileutils.h>
68 #include <glibmm/miscutils.h>
75 #include "pbd/error.h"
78 #include "pbd/strsplit.h"
80 #include "pbd/file_utils.h"
81 #include "pbd/enumwriter.h"
82 #include "pbd/basename.h"
84 #include "midi++/port.h"
85 #include "midi++/mmc.h"
87 #include "ardour/analyser.h"
88 #include "ardour/audio_library.h"
89 #include "ardour/audio_backend.h"
90 #include "ardour/audioengine.h"
91 #include "ardour/audioplaylist.h"
92 #include "ardour/audioregion.h"
93 #include "ardour/buffer_manager.h"
94 #include "ardour/control_protocol_manager.h"
95 #include "ardour/directory_names.h"
96 #include "ardour/event_type_map.h"
97 #include "ardour/filesystem_paths.h"
98 #include "ardour/midi_region.h"
99 #include "ardour/midiport_manager.h"
100 #include "ardour/mix.h"
101 #include "ardour/operations.h"
102 #include "ardour/panner_manager.h"
103 #include "ardour/plugin_manager.h"
104 #include "ardour/process_thread.h"
105 #include "ardour/profile.h"
106 #include "ardour/rc_configuration.h"
107 #include "ardour/region.h"
108 #include "ardour/route_group.h"
109 #include "ardour/runtime_functions.h"
110 #include "ardour/session_event.h"
111 #include "ardour/source_factory.h"
112 #include "ardour/uri_map.h"
114 #include "audiographer/routines.h"
116 #if defined (__APPLE__)
117 #include <Carbon/Carbon.h> // For Gestalt
122 ARDOUR::RCConfiguration* ARDOUR::Config = 0;
123 ARDOUR::RuntimeProfile* ARDOUR::Profile = 0;
124 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
126 using namespace ARDOUR;
130 bool libardour_initialized = false;
132 compute_peak_t ARDOUR::compute_peak = 0;
133 find_peaks_t ARDOUR::find_peaks = 0;
134 apply_gain_to_buffer_t ARDOUR::apply_gain_to_buffer = 0;
135 mix_buffers_with_gain_t ARDOUR::mix_buffers_with_gain = 0;
136 mix_buffers_no_gain_t ARDOUR::mix_buffers_no_gain = 0;
137 copy_vector_t ARDOUR::copy_vector = 0;
139 PBD::Signal1<void,std::string> ARDOUR::BootMessage;
140 PBD::Signal3<void,std::string,std::string,bool> ARDOUR::PluginScanMessage;
141 PBD::Signal1<void,int> ARDOUR::PluginScanTimeout;
142 PBD::Signal0<void> ARDOUR::GUIIdle;
143 PBD::Signal3<bool,std::string,std::string,int> ARDOUR::CopyConfigurationFiles;
145 std::vector<std::string> ARDOUR::reserved_io_names;
147 static bool have_old_configuration_files = false;
150 extern void setup_enum_writer ();
153 /* this is useful for quite a few things that want to check
154 if any bounds-related property has changed
156 PBD::PropertyChange ARDOUR::bounds_change;
159 setup_hardware_optimization (bool try_optimization)
161 bool generic_mix_functions = true;
163 if (try_optimization) {
165 FPU* fpu = FPU::instance();
167 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
169 #ifdef PLATFORM_WINDOWS
170 /* We have AVX-optimized code for Windows */
172 if (fpu->has_avx()) {
174 /* AVX code doesn't compile on Linux yet */
178 info << "Using AVX optimized routines" << endmsg;
181 compute_peak = x86_sse_avx_compute_peak;
182 find_peaks = x86_sse_avx_find_peaks;
183 apply_gain_to_buffer = x86_sse_avx_apply_gain_to_buffer;
184 mix_buffers_with_gain = x86_sse_avx_mix_buffers_with_gain;
185 mix_buffers_no_gain = x86_sse_avx_mix_buffers_no_gain;
186 copy_vector = x86_sse_avx_copy_vector;
188 generic_mix_functions = false;
190 } else if (fpu->has_sse()) {
192 info << "Using SSE optimized routines" << endmsg;
195 compute_peak = x86_sse_compute_peak;
196 find_peaks = x86_sse_find_peaks;
197 apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
198 mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
199 mix_buffers_no_gain = x86_sse_mix_buffers_no_gain;
200 copy_vector = default_copy_vector;
202 generic_mix_functions = false;
206 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
207 SInt32 sysVersion = 0;
209 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
212 if (sysVersion >= 0x00001040) { // Tiger at least
213 compute_peak = veclib_compute_peak;
214 find_peaks = veclib_find_peaks;
215 apply_gain_to_buffer = veclib_apply_gain_to_buffer;
216 mix_buffers_with_gain = veclib_mix_buffers_with_gain;
217 mix_buffers_no_gain = veclib_mix_buffers_no_gain;
218 copy_vector = default_copy_vector;
220 generic_mix_functions = false;
222 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
226 /* consider FPU denormal handling to be "h/w optimization" */
231 if (generic_mix_functions) {
233 compute_peak = default_compute_peak;
234 find_peaks = default_find_peaks;
235 apply_gain_to_buffer = default_apply_gain_to_buffer;
236 mix_buffers_with_gain = default_mix_buffers_with_gain;
237 mix_buffers_no_gain = default_mix_buffers_no_gain;
238 copy_vector = default_copy_vector;
240 info << "No H/W specific optimizations in use" << endmsg;
243 AudioGrapher::Routines::override_compute_peak (compute_peak);
244 AudioGrapher::Routines::override_apply_gain_to_buffer (apply_gain_to_buffer);
248 lotsa_files_please ()
250 #ifndef PLATFORM_WINDOWS
253 if (getrlimit (RLIMIT_NOFILE, &rl) == 0) {
256 /* See the COMPATIBILITY note on the Apple setrlimit() man page */
257 rl.rlim_cur = min ((rlim_t) OPEN_MAX, rl.rlim_max);
259 rl.rlim_cur = rl.rlim_max;
262 if (setrlimit (RLIMIT_NOFILE, &rl) != 0) {
263 if (rl.rlim_cur == RLIM_INFINITY) {
264 error << _("Could not set system open files limit to \"unlimited\"") << endmsg;
266 error << string_compose (_("Could not set system open files limit to %1"), rl.rlim_cur) << endmsg;
269 if (rl.rlim_cur != RLIM_INFINITY) {
270 info << string_compose (_("Your system is configured to limit %1 to only %2 open files"), PROGRAM_NAME, rl.rlim_cur) << endmsg;
274 error << string_compose (_("Could not get system open files limit (%1)"), strerror (errno)) << endmsg;
280 copy_configuration_files (string const & old_dir, string const & new_dir, int old_version)
285 /* ensure target directory exists */
287 if (g_mkdir_with_parents (new_dir.c_str(), 0755)) {
291 if (old_version == 3) {
293 old_name = Glib::build_filename (old_dir, X_("recent"));
294 new_name = Glib::build_filename (new_dir, X_("recent"));
296 copy_file (old_name, new_name);
298 old_name = Glib::build_filename (old_dir, X_("sfdb"));
299 new_name = Glib::build_filename (new_dir, X_("sfdb"));
301 copy_file (old_name, new_name);
303 /* can only copy ardour.rc/config - UI config is not compatible */
305 /* users who have been using git/nightlies since the last
306 * release of 3.5 will have $CONFIG/config rather than
307 * $CONFIG/ardour.rc. Pick up the newer "old" config file,
308 * to avoid confusion.
311 string old_name = Glib::build_filename (old_dir, X_("config"));
313 if (!Glib::file_test (old_name, Glib::FILE_TEST_EXISTS)) {
314 old_name = Glib::build_filename (old_dir, X_("ardour.rc"));
317 new_name = Glib::build_filename (new_dir, X_("config"));
319 copy_file (old_name, new_name);
321 /* copy templates and route templates */
323 old_name = Glib::build_filename (old_dir, X_("templates"));
324 new_name = Glib::build_filename (new_dir, X_("templates"));
326 copy_recurse (old_name, new_name);
328 old_name = Glib::build_filename (old_dir, X_("route_templates"));
329 new_name = Glib::build_filename (new_dir, X_("route_templates"));
331 copy_recurse (old_name, new_name);
335 old_name = Glib::build_filename (old_dir, X_("presets"));
336 new_name = Glib::build_filename (new_dir, X_("presets"));
338 copy_recurse (old_name, new_name);
342 old_name = Glib::build_filename (old_dir, X_("plugin_statuses"));
343 new_name = Glib::build_filename (new_dir, X_("plugin_statuses"));
345 copy_file (old_name, new_name);
349 old_name = Glib::build_filename (old_dir, export_formats_dir_name);
350 new_name = Glib::build_filename (new_dir, export_formats_dir_name);
352 vector<string> export_formats;
353 g_mkdir_with_parents (Glib::build_filename (new_dir, export_formats_dir_name).c_str(), 0755);
354 find_files_matching_pattern (export_formats, old_name, X_("*.format"));
355 for (vector<string>::iterator i = export_formats.begin(); i != export_formats.end(); ++i) {
356 std::string from = *i;
357 std::string to = Glib::build_filename (new_name, Glib::path_get_basename (*i));
358 copy_file (from, to);
366 ARDOUR::check_for_old_configuration_files ()
368 int current_version = atoi (X_(PROGRAM_VERSION));
370 if (current_version <= 1) {
374 int old_version = current_version - 1;
376 string old_config_dir = user_config_directory (old_version);
377 /* pass in the current version explicitly to avoid creation */
378 string current_config_dir = user_config_directory (current_version);
380 if (!Glib::file_test (current_config_dir, Glib::FILE_TEST_IS_DIR)) {
381 if (Glib::file_test (old_config_dir, Glib::FILE_TEST_IS_DIR)) {
382 have_old_configuration_files = true;
388 ARDOUR::handle_old_configuration_files (boost::function<bool (std::string const&, std::string const&, int)> ui_handler)
390 if (have_old_configuration_files) {
391 int current_version = atoi (X_(PROGRAM_VERSION));
392 assert (current_version > 1); // established in check_for_old_configuration_files ()
393 int old_version = current_version - 1;
394 string old_config_dir = user_config_directory (old_version);
395 string current_config_dir = user_config_directory (current_version);
397 if (ui_handler (old_config_dir, current_config_dir, old_version)) {
398 copy_configuration_files (old_config_dir, current_config_dir, old_version);
406 ARDOUR::init (bool use_windows_vst, bool try_optimization, const char* localedir)
408 if (libardour_initialized) {
412 if (!PBD::init()) return false;
415 (void) bindtextdomain(PACKAGE, localedir);
416 (void) bind_textdomain_codeset (PACKAGE, "UTF-8");
419 SessionEvent::init_event_pool ();
421 Operations::make_operations_quarks ();
422 SessionObject::make_property_quarks ();
423 Region::make_property_quarks ();
424 MidiRegion::make_property_quarks ();
425 AudioRegion::make_property_quarks ();
426 RouteGroup::make_property_quarks ();
427 Playlist::make_property_quarks ();
428 AudioPlaylist::make_property_quarks ();
430 /* this is a useful ready to use PropertyChange that many
431 things need to check. This avoids having to compose
432 it every time we want to check for any of the relevant
436 bounds_change.add (ARDOUR::Properties::start);
437 bounds_change.add (ARDOUR::Properties::position);
438 bounds_change.add (ARDOUR::Properties::length);
440 /* provide a state version for the few cases that need it and are not
441 driven by reading state from disk (e.g. undo/redo)
444 Stateful::current_state_version = CURRENT_SESSION_FILE_VERSION;
446 ARDOUR::setup_enum_writer ();
448 // allow ardour the absolute maximum number of open files
449 lotsa_files_please ();
454 Library = new AudioLibrary;
456 BootMessage (_("Loading configuration"));
458 Config = new RCConfiguration;
460 if (Config->load_state ()) {
464 Config->set_use_windows_vst (use_windows_vst);
466 Config->set_use_lxvst(true);
469 Profile = new RuntimeProfile;
472 #ifdef WINDOWS_VST_SUPPORT
473 if (Config->get_use_windows_vst() && fst_init (0)) {
479 if (Config->get_use_lxvst() && vstfx_init (0)) {
484 #ifdef AUDIOUNIT_SUPPORT
485 AUPluginInfo::load_cached_info ();
488 setup_hardware_optimization (try_optimization);
490 SourceFactory::init ();
493 /* singletons - first object is "it" */
494 (void) PluginManager::instance();
496 (void) URIMap::instance();
498 (void) EventTypeMap::instance();
500 ProcessThread::init ();
501 /* the + 4 is a bit of a handwave. i don't actually know
502 how many more per-thread buffer sets we need above
503 the h/w concurrency, but its definitely > 1 more.
505 BufferManager::init (hardware_concurrency() + 4);
507 PannerManager::instance().discover_panners();
509 ARDOUR::AudioEngine::create ();
511 /* it is unfortunate that we need to include reserved names here that
512 refer to control surfaces. But there's no way to ensure a complete
513 lack of collisions without doing this, since the control surface
514 support may not even be active. Without adding an API to control
515 surface support that would list their port names, we do have to
519 char const * const reserved[] = {
528 for (int n = 0; reserved[n]; ++n) {
529 reserved_io_names.push_back (reserved[n]);
532 libardour_initialized = true;
538 ARDOUR::init_post_engine ()
540 ControlProtocolManager::instance().discover_control_protocols ();
543 if ((node = Config->control_protocol_state()) != 0) {
544 ControlProtocolManager::instance().set_state (*node, Stateful::loading_state_version);
549 ARDOUR::PluginManager::instance().refresh (!Config->get_discover_vst_on_start());
555 if (!libardour_initialized) {
559 ARDOUR::AudioEngine::destroy ();
565 delete &ControlProtocolManager::instance();
566 #ifdef WINDOWS_VST_SUPPORT
573 delete &PluginManager::instance();
581 ARDOUR::find_bindings_files (map<string,string>& files)
583 vector<std::string> found;
584 Searchpath spath = ardour_config_search_path();
586 if (getenv ("ARDOUR_SAE")) {
587 find_files_matching_pattern (found, spath, "*SAE-*.bindings");
589 find_files_matching_pattern (found, spath, "*.bindings");
596 for (vector<std::string>::iterator x = found.begin(); x != found.end(); ++x) {
597 std::string path(*x);
598 pair<string,string> namepath;
599 namepath.second = path;
600 namepath.first = PBD::basename_nosuffix (path);
601 files.insert (namepath);
606 ARDOUR::no_auto_connect()
608 return getenv ("ARDOUR_NO_AUTOCONNECT") != 0;
614 FPU* fpu = FPU::instance ();
616 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
617 // valgrind doesn't understand this assembler stuff
618 // September 10th, 2007
622 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
626 if (!fpu->has_flush_to_zero() && !fpu->has_denormals_are_zero()) {
630 MXCSR = _mm_getcsr();
632 #ifdef DEBUG_DENORMAL_EXCEPTION
633 /* This will raise a FP exception if a denormal is detected */
634 MXCSR &= ~_MM_MASK_DENORM;
637 switch (Config->get_denormal_model()) {
639 MXCSR &= ~(_MM_FLUSH_ZERO_ON | 0x40);
643 if (fpu->has_flush_to_zero()) {
644 MXCSR |= _MM_FLUSH_ZERO_ON;
649 MXCSR &= ~_MM_FLUSH_ZERO_ON;
650 if (fpu->has_denormals_are_zero()) {
656 if (fpu->has_flush_to_zero()) {
657 if (fpu->has_denormals_are_zero()) {
658 MXCSR |= _MM_FLUSH_ZERO_ON | 0x40;
660 MXCSR |= _MM_FLUSH_ZERO_ON;
671 /* this can be changed to modify the translation behaviour for
672 cases where the user has never expressed a preference.
674 static const bool translate_by_default = true;
677 ARDOUR::translation_enable_path ()
679 return Glib::build_filename (user_config_directory(), ".translate");
683 ARDOUR::translations_are_enabled ()
685 int fd = g_open (ARDOUR::translation_enable_path().c_str(), O_RDONLY, 0444);
688 return translate_by_default;
694 if (::read (fd, &c, 1) == 1 && c == '1') {
704 ARDOUR::set_translations_enabled (bool yn)
706 string i18n_enabler = ARDOUR::translation_enable_path();
707 int fd = g_open (i18n_enabler.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644);
721 (void) ::write (fd, &c, 1);
729 ARDOUR::get_available_sync_options ()
731 vector<SyncSource> ret;
733 boost::shared_ptr<AudioBackend> backend = AudioEngine::instance()->current_backend();
734 if (backend && backend->name() == "JACK") {
735 ret.push_back (Engine);
739 ret.push_back (MIDIClock);
745 /** Return a monotonic value for the number of microseconds that have elapsed
746 * since an arbitrary zero origin.
750 /* Thanks Apple for not implementing this basic SUSv2, POSIX.1-2001 function
752 #include <mach/mach_time.h>
753 #define CLOCK_REALTIME 0
754 #define CLOCK_MONOTONIC 0
756 clock_gettime (int /*clk_id*/, struct timespec *t)
758 static bool initialized = false;
759 static mach_timebase_info_data_t timebase;
761 mach_timebase_info(&timebase);
765 time = mach_absolute_time();
766 double nseconds = ((double)time * (double)timebase.numer)/((double)timebase.denom);
767 double seconds = ((double)time * (double)timebase.numer)/((double)timebase.denom * 1e9);
769 t->tv_nsec = nseconds;
775 ARDOUR::get_microseconds ()
777 #ifdef PLATFORM_WINDOWS
778 microseconds_t ret = 0;
779 LARGE_INTEGER freq, time;
781 if (QueryPerformanceFrequency(&freq))
782 if (QueryPerformanceCounter(&time))
783 ret = (microseconds_t)((time.QuadPart * 1000000) / freq.QuadPart);
788 if (clock_gettime (CLOCK_MONOTONIC, &ts) != 0) {
792 return (microseconds_t) ts.tv_sec * 1000000 + (ts.tv_nsec/1000);
796 /** Return the number of bits per sample for a given sample format.
798 * This is closely related to sndfile_data_width() but does NOT
799 * return a "magic" value to differentiate between 32 bit integer
800 * and 32 bit floating point values.
804 ARDOUR::format_data_width (ARDOUR::SampleFormat format)
810 case ARDOUR::FormatInt16:
812 case ARDOUR::FormatInt24: