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 <cstdio> // Needed so that libraptor (included in lrdf) won't complain
22 #include <sys/types.h>
24 #include <sys/resource.h>
34 #ifdef HAVE_AUDIOUNITS
35 #include <ardour/audio_unit.h>
39 #include <xmmintrin.h>
42 #include <glibmm/fileutils.h>
43 #include <glibmm/miscutils.h>
47 #include <pbd/error.h>
49 #include <pbd/strsplit.h>
51 #include <pbd/pathscanner.h>
53 #include <midi++/port.h>
54 #include <midi++/manager.h>
55 #include <midi++/mmc.h>
57 #include <ardour/ardour.h>
58 #include <ardour/analyser.h>
59 #include <ardour/audio_library.h>
60 #include <ardour/configuration.h>
61 #include <ardour/profile.h>
62 #include <ardour/plugin_manager.h>
63 #include <ardour/audiosource.h>
64 #include <ardour/utils.h>
65 #include <ardour/session.h>
66 #include <ardour/source_factory.h>
67 #include <ardour/control_protocol_manager.h>
70 #include <ardour/osc.h>
73 #include <ardour/mix.h>
75 #if defined (__APPLE__)
76 #include <Carbon/Carbon.h> // For Gestalt
81 ARDOUR::Configuration* ARDOUR::Config = 0;
82 ARDOUR::RuntimeProfile* ARDOUR::Profile = 0;
83 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
86 ARDOUR::OSC* ARDOUR::osc = 0;
89 using namespace ARDOUR;
93 MIDI::Port *ARDOUR::default_mmc_port = 0;
94 MIDI::Port *ARDOUR::default_mtc_port = 0;
95 MIDI::Port *ARDOUR::default_midi_port = 0;
97 Change ARDOUR::StartChanged = ARDOUR::new_change ();
98 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
99 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
100 Change ARDOUR::NameChanged = ARDOUR::new_change ();
101 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
103 sigc::signal<void,std::string> ARDOUR::BootMessage;
109 /* no real cost to creating this object, and it avoids
110 conditionals anywhere that uses it
113 osc = new OSC (Config->get_osc_port());
115 if (Config->get_use_osc ()) {
116 BootMessage (_("Starting OSC"));
117 return osc->start ();
126 ARDOUR::setup_midi ()
128 if (Config->midi_ports.size() == 0) {
129 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
133 BootMessage (_("Configuring MIDI ports"));
135 for (std::map<string,XMLNode>::iterator i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
136 MIDI::Manager::instance()->add_port (i->second);
140 const MIDI::Manager::PortMap& ports = MIDI::Manager::instance()->get_midi_ports();
142 if (ports.size() > 1) {
144 first = ports.begin()->second;
146 /* More than one port, so try using specific names for each port */
148 if (Config->get_mmc_port_name() != N_("default")) {
149 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
152 if (Config->get_mtc_port_name() != N_("default")) {
153 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
156 if (Config->get_midi_port_name() != N_("default")) {
157 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
160 /* If that didn't work, just use the first listed port */
162 if (default_mmc_port == 0) {
163 default_mmc_port = first;
166 if (default_mtc_port == 0) {
167 default_mtc_port = first;
170 if (default_midi_port == 0) {
171 default_midi_port = first;
174 } else if (ports.size() == 1) {
176 first = ports.begin()->second;
178 /* Only one port described, so use it for both MTC and MMC */
180 default_mmc_port = first;
181 default_mtc_port = default_mmc_port;
182 default_midi_port = default_mmc_port;
185 if (default_mmc_port == 0) {
186 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name())
192 if (default_mtc_port == 0) {
193 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
197 if (default_midi_port == 0) {
198 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
206 setup_hardware_optimization (bool try_optimization)
208 bool generic_mix_functions = true;
210 if (try_optimization) {
214 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
218 info << "Using SSE optimized routines" << endmsg;
221 Session::compute_peak = x86_sse_compute_peak;
222 Session::find_peaks = x86_sse_find_peaks;
223 Session::apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
224 Session::mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
225 Session::mix_buffers_no_gain = x86_sse_mix_buffers_no_gain;
227 generic_mix_functions = false;
231 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
234 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
237 if (sysVersion >= 0x00001040) { // Tiger at least
238 Session::compute_peak = veclib_compute_peak;
239 Session::find_peaks = veclib_find_peaks;
240 Session::apply_gain_to_buffer = veclib_apply_gain_to_buffer;
241 Session::mix_buffers_with_gain = veclib_mix_buffers_with_gain;
242 Session::mix_buffers_no_gain = veclib_mix_buffers_no_gain;
244 generic_mix_functions = false;
246 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
250 /* consider FPU denormal handling to be "h/w optimization" */
255 if (generic_mix_functions) {
257 Session::compute_peak = compute_peak;
258 Session::find_peaks = find_peaks;
259 Session::apply_gain_to_buffer = apply_gain_to_buffer;
260 Session::mix_buffers_with_gain = mix_buffers_with_gain;
261 Session::mix_buffers_no_gain = mix_buffers_no_gain;
263 info << "No H/W specific optimizations in use" << endmsg;
269 lotsa_files_please ()
273 if (getrlimit (RLIMIT_NOFILE, &rl) == 0) {
275 rl.rlim_cur = rl.rlim_max;
277 if (setrlimit (RLIMIT_NOFILE, &rl) != 0) {
278 if (rl.rlim_cur == RLIM_INFINITY) {
279 error << _("Could not set system open files limit to \"unlimited\"") << endmsg;
281 error << string_compose (_("Could not set system open files limit to %1"), rl.rlim_cur) << endmsg;
284 if (rl.rlim_cur == RLIM_INFINITY) {
285 info << _("Removed open file count limit. Excellent!") << endmsg;
287 info << string_compose (_("Ardour will be limited to %1 open files"), rl.rlim_cur) << endmsg;
291 error << string_compose (_("Could not get system open files limit (%1)"), strerror (errno)) << endmsg;
296 ARDOUR::init (bool use_vst, bool try_optimization)
298 extern void setup_enum_writer ();
300 (void) bindtextdomain(PACKAGE, LOCALEDIR);
302 setup_enum_writer ();
304 // allow ardour the absolute maximum number of open files
305 lotsa_files_please ();
308 Library = new AudioLibrary;
310 BootMessage (_("Loading configuration"));
312 Config = new Configuration;
314 if (Config->load_state ()) {
318 Config->set_use_vst (use_vst);
320 Profile = new RuntimeProfile;
333 if (Config->get_use_vst() && fst_init (0)) {
338 #ifdef HAVE_AUDIOUNITS
339 AUPluginInfo::load_cached_info ();
342 /* Make VAMP look in our library ahead of anything else */
344 char *p = getenv ("VAMP_PATH");
345 string vamppath = VAMP_DIR;
350 setenv ("VAMP_PATH", vamppath.c_str(), 1);
353 setup_hardware_optimization (try_optimization);
355 SourceFactory::init ();
358 /* singleton - first object is "it" */
359 new PluginManager ();
361 /* singleton - first object is "it" */
362 new ControlProtocolManager ();
363 ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
366 if ((node = Config->control_protocol_state()) != 0) {
367 ControlProtocolManager::instance().set_state (*node);
370 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
380 delete &ControlProtocolManager::instance();
389 ARDOUR::get_microseconds ()
391 return (microseconds_t) jack_get_time ();
395 ARDOUR::new_change ()
398 static uint32_t change_bit = 1;
400 /* catch out-of-range */
403 fatal << _("programming error: ")
404 << "change_bit out of range in ARDOUR::new_change()"
409 c = Change (change_bit);
410 change_bit <<= 1; // if it shifts too far, change_bit == 0
416 ARDOUR::get_ardour_revision ()
422 ARDOUR::get_user_ardour_path ()
426 path = Glib::get_home_dir();
432 path += "/.ardour2/";
434 /* create it if necessary */
436 if (g_mkdir_with_parents (path.c_str (), 0755)) {
444 ARDOUR::get_system_data_path ()
450 if ((envvar = getenv ("ARDOUR_DATA_PATH")) != 0) {
461 ARDOUR::get_system_module_path ()
466 if ((envvar = getenv ("ARDOUR_MODULE_PATH")) != 0) {
477 find_file (string name, string dir, string subdir = "")
480 char* envvar = getenv("ARDOUR_PATH");
482 /* 1st attempt: any directory in ARDOUR_PATH */
486 vector<string> split_path;
488 split (envvar, split_path, ':');
490 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
493 if (Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
494 // cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
500 /* 2nd attempt: ~/.ardour/ */
502 path = get_user_ardour_path();
504 if (subdir.length()) {
505 path = Glib::build_filename (path, subdir);
508 path = Glib::build_filename (path, name);
510 if (Glib::file_test (path.c_str(), Glib::FILE_TEST_EXISTS)) {
515 /* 3rd attempt: dir/... */
520 if (subdir.length()) {
521 path += subdir + "/";
526 if (access (path.c_str(), R_OK) == 0) {
534 static bool sae_binding_filter (const string& str, void* arg)
536 /* Not a dotfile, has a prefix before a period, suffix is ".bindings" and contains -sae- */
538 return str[0] != '.' && str.length() > 13 && str.find (".bindings") == (str.length() - 9)
539 && str.find ("SAE-") != string::npos;
542 static bool binding_filter (const string& str, void* arg)
544 /* Not a dotfile, has a prefix before a period, suffix is ".bindings" */
546 return str[0] != '.' && str.length() > 9 && str.find (".bindings") == (str.length() - 9);
550 ARDOUR::find_bindings_files (map<string,string>& files)
553 vector<string*> *found;
556 /* XXX building path, so separators are fixed */
558 full_path = get_user_ardour_path ();
560 full_path = get_system_data_path ();
562 if (getenv ("ARDOUR_SAE")) {
563 found = scanner (full_path, sae_binding_filter, 0, false, true);
565 found = scanner (full_path, binding_filter, 0, false, true);
572 for (vector<string*>::iterator x = found->begin(); x != found->end(); ++x) {
574 pair<string,string> namepath;
575 namepath.second = path;
576 path = Glib::path_get_basename (path);
577 namepath.first = path.substr (0, path.find_first_of ('.'));
579 files.insert (namepath);
587 ARDOUR::find_config_file (string name)
591 if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
595 return find_file (name, envvar);
599 ARDOUR::find_data_file (string name, string subdir)
602 if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
606 return find_file (name, envvar, subdir);
609 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
611 old = strdup (setlocale (LC_NUMERIC, NULL));
612 if (strcmp (old, str)) {
613 setlocale (LC_NUMERIC, str);
617 ARDOUR::LocaleGuard::~LocaleGuard ()
619 setlocale (LC_NUMERIC, old);
626 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
627 // valgrind doesn't understand this assembler stuff
628 // September 10th, 2007
632 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
637 /* XXX use real code to determine if the processor supports
638 DenormalsAreZero and FlushToZero
641 if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
645 MXCSR = _mm_getcsr();
647 switch (Config->get_denormal_model()) {
649 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
653 if (fpu.has_flush_to_zero()) {
654 MXCSR |= _MM_FLUSH_ZERO_ON;
659 MXCSR &= ~_MM_FLUSH_ZERO_ON;
660 if (fpu.has_denormals_are_zero()) {
666 if (fpu.has_flush_to_zero()) {
667 if (fpu.has_denormals_are_zero()) {
668 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
670 MXCSR |= _MM_FLUSH_ZERO_ON;
682 ARDOUR::coverage (nframes_t sa, nframes_t ea,
683 nframes_t sb, nframes_t eb)
685 /* OverlapType returned reflects how the second (B)
686 range overlaps the first (A).
688 The diagrams show various relative placements
689 of A and B for each OverlapType.
692 Internal: the start points cannot coincide
693 External: the start and end points can coincide
694 Start: end points can coincide
695 End: start points can coincide
697 XXX Logically, Internal should disallow end
702 |--------------------| A
704 |-----------------| B
711 if ((sb >= sa) && (eb <= ea)) {
713 if ((sb > sa) && (eb <= ea)) {
715 return OverlapInternal;
719 |--------------------| A
721 -----------------------| B
724 "B overlaps the start of A"
728 if ((eb >= sa) && (eb <= ea)) {
732 |---------------------| A
734 |----------------------- B
737 "B overlaps the end of A"
740 if ((sb >= sa) && (sb <= ea)) {
744 |--------------------| A
745 -------------------------- B
746 |----------------------- B
747 ----------------------| B
748 |--------------------| B
751 "B overlaps all of A"
753 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
754 return OverlapExternal;
760 /* not sure where to put these */
763 std::istream& int_to_type (std::istream& o, T& hf) {
770 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
771 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
772 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
773 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
774 std::istream& operator>>(std::istream& o, RemoteModel& var) { return int_to_type<RemoteModel> (o, var); }
775 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
776 std::istream& operator>>(std::istream& o, SoloModel& var) { return int_to_type<SoloModel> (o, var); }
777 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
778 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
779 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
780 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
781 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }
782 std::istream& operator>>(std::istream& o, SmpteFormat& var) { return int_to_type<SmpteFormat> (o, var); }
783 std::istream& operator>>(std::istream& o, DenormalModel& var) { return int_to_type<DenormalModel> (o, var); }