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>
35 #include <xmmintrin.h>
38 #include <glibmm/fileutils.h>
39 #include <glibmm/miscutils.h>
43 #include <pbd/error.h>
45 #include <pbd/strsplit.h>
47 #include <pbd/pathscanner.h>
49 #include <midi++/port.h>
50 #include <midi++/manager.h>
51 #include <midi++/mmc.h>
53 #include <ardour/ardour.h>
54 #include <ardour/analyser.h>
55 #include <ardour/audio_library.h>
56 #include <ardour/configuration.h>
57 #include <ardour/profile.h>
58 #include <ardour/plugin_manager.h>
59 #include <ardour/audiosource.h>
60 #include <ardour/utils.h>
61 #include <ardour/session.h>
62 #include <ardour/source_factory.h>
63 #include <ardour/control_protocol_manager.h>
66 #include <ardour/osc.h>
69 #include <ardour/mix.h>
71 #if defined (__APPLE__)
72 #include <Carbon/Carbon.h> // For Gestalt
77 ARDOUR::Configuration* ARDOUR::Config = 0;
78 ARDOUR::RuntimeProfile* ARDOUR::Profile = 0;
79 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
82 ARDOUR::OSC* ARDOUR::osc = 0;
85 using namespace ARDOUR;
89 MIDI::Port *default_mmc_port = 0;
90 MIDI::Port *default_mtc_port = 0;
91 MIDI::Port *default_midi_port = 0;
93 Change ARDOUR::StartChanged = ARDOUR::new_change ();
94 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
95 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
96 Change ARDOUR::NameChanged = ARDOUR::new_change ();
97 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
99 sigc::signal<void,std::string> ARDOUR::BootMessage;
105 /* no real cost to creating this object, and it avoids
106 conditionals anywhere that uses it
109 osc = new OSC (Config->get_osc_port());
111 if (Config->get_use_osc ()) {
112 BootMessage (_("Starting OSC"));
113 return osc->start ();
123 if (Config->midi_ports.size() == 0) {
124 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
128 BootMessage (_("Configuring MIDI ports"));
130 for (std::map<string,XMLNode>::iterator i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
131 MIDI::Manager::instance()->add_port (i->second);
135 const MIDI::Manager::PortMap& ports = MIDI::Manager::instance()->get_midi_ports();
137 if (ports.size() > 1) {
139 first = ports.begin()->second;
141 /* More than one port, so try using specific names for each port */
143 if (Config->get_mmc_port_name() != N_("default")) {
144 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
147 if (Config->get_mtc_port_name() != N_("default")) {
148 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
151 if (Config->get_midi_port_name() != N_("default")) {
152 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
155 /* If that didn't work, just use the first listed port */
157 if (default_mmc_port == 0) {
158 default_mmc_port = first;
161 if (default_mtc_port == 0) {
162 default_mtc_port = first;
165 if (default_midi_port == 0) {
166 default_midi_port = first;
169 } else if (ports.size() == 1) {
171 first = ports.begin()->second;
173 /* Only one port described, so use it for both MTC and MMC */
175 default_mmc_port = first;
176 default_mtc_port = default_mmc_port;
177 default_midi_port = default_mmc_port;
180 if (default_mmc_port == 0) {
181 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name())
186 if (default_mtc_port == 0) {
187 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
191 if (default_midi_port == 0) {
192 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
200 setup_hardware_optimization (bool try_optimization)
202 bool generic_mix_functions = true;
204 if (try_optimization) {
208 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
212 info << "Using SSE optimized routines" << endmsg;
215 Session::compute_peak = x86_sse_compute_peak;
216 Session::find_peaks = x86_sse_find_peaks;
217 Session::apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
218 Session::mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
219 Session::mix_buffers_no_gain = x86_sse_mix_buffers_no_gain;
221 generic_mix_functions = false;
225 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
228 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
231 if (sysVersion >= 0x00001040) { // Tiger at least
232 Session::compute_peak = veclib_compute_peak;
233 Session::find_peaks = veclib_find_peaks;
234 Session::apply_gain_to_buffer = veclib_apply_gain_to_buffer;
235 Session::mix_buffers_with_gain = veclib_mix_buffers_with_gain;
236 Session::mix_buffers_no_gain = veclib_mix_buffers_no_gain;
238 generic_mix_functions = false;
240 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
244 /* consider FPU denormal handling to be "h/w optimization" */
249 if (generic_mix_functions) {
251 Session::compute_peak = compute_peak;
252 Session::find_peaks = find_peaks;
253 Session::apply_gain_to_buffer = apply_gain_to_buffer;
254 Session::mix_buffers_with_gain = mix_buffers_with_gain;
255 Session::mix_buffers_no_gain = mix_buffers_no_gain;
257 info << "No H/W specific optimizations in use" << endmsg;
263 lotsa_files_please ()
267 if (getrlimit (RLIMIT_NOFILE, &rl) == 0) {
269 rl.rlim_cur = rl.rlim_max;
271 if (setrlimit (RLIMIT_NOFILE, &rl) != 0) {
272 if (rl.rlim_cur == RLIM_INFINITY) {
273 error << _("Could not set system open files limit to \"unlimited\"") << endmsg;
275 error << string_compose (_("Could not set system open files limit to %1"), rl.rlim_cur) << endmsg;
278 if (rl.rlim_cur == RLIM_INFINITY) {
279 info << _("Removed open file count limit. Excellent!") << endmsg;
281 info << string_compose (_("Ardour will be limited to %1 open files"), rl.rlim_cur) << endmsg;
285 error << string_compose (_("Could not get system open files limit (%1)"), strerror (errno)) << endmsg;
290 ARDOUR::init (bool use_vst, bool try_optimization)
292 extern void setup_enum_writer ();
294 (void) bindtextdomain(PACKAGE, LOCALEDIR);
296 setup_enum_writer ();
298 // allow ardour the absolute maximum number of open files
299 lotsa_files_please ();
302 Library = new AudioLibrary;
304 BootMessage (_("Loading configuration"));
306 Config = new Configuration;
308 if (Config->load_state ()) {
312 Config->set_use_vst (use_vst);
314 Profile = new RuntimeProfile;
327 if (Config->get_use_vst() && fst_init ()) {
332 /* Make VAMP look in our library ahead of anything else */
334 char *p = getenv ("VAMP_PATH");
335 string vamppath = VAMP_DIR;
340 setenv ("VAMP_PATH", vamppath.c_str(), 1);
343 setup_hardware_optimization (try_optimization);
345 SourceFactory::init ();
348 /* singleton - first object is "it" */
349 new PluginManager ();
351 /* singleton - first object is "it" */
352 new ControlProtocolManager ();
353 ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
356 if ((node = Config->control_protocol_state()) != 0) {
357 ControlProtocolManager::instance().set_state (*node);
360 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
370 delete &ControlProtocolManager::instance();
376 ARDOUR::get_microseconds ()
378 /* XXX need JACK to export its functionality */
381 gettimeofday (&now, 0);
382 return now.tv_sec * 1000000ULL + now.tv_usec;
386 ARDOUR::new_change ()
389 static uint32_t change_bit = 1;
391 /* catch out-of-range */
394 fatal << _("programming error: ")
395 << "change_bit out of range in ARDOUR::new_change()"
400 c = Change (change_bit);
401 change_bit <<= 1; // if it shifts too far, change_bit == 0
407 ARDOUR::get_ardour_revision ()
413 ARDOUR::get_user_ardour_path ()
417 path = Glib::get_home_dir();
423 path += "/.ardour2/";
425 /* create it if necessary */
427 if (g_mkdir_with_parents (path.c_str (), 0755)) {
435 ARDOUR::get_system_data_path ()
441 if ((envvar = getenv ("ARDOUR_DATA_PATH")) != 0) {
452 ARDOUR::get_system_module_path ()
457 if ((envvar = getenv ("ARDOUR_MODULE_PATH")) != 0) {
468 find_file (string name, string dir, string subdir = "")
471 char* envvar = getenv("ARDOUR_PATH");
473 /* 1st attempt: any directory in ARDOUR_PATH */
477 vector<string> split_path;
479 split (envvar, split_path, ':');
481 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
484 if (Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
485 // cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
491 /* 2nd attempt: ~/.ardour/ */
493 path = get_user_ardour_path();
495 if (subdir.length()) {
496 path += subdir + "/";
500 if (access (path.c_str(), R_OK) == 0) {
505 /* 3rd attempt: dir/... */
510 if (subdir.length()) {
511 path += subdir + "/";
516 if (access (path.c_str(), R_OK) == 0) {
524 static bool sae_binding_filter (const string& str, void* arg)
526 /* Not a dotfile, has a prefix before a period, suffix is ".bindings" and contains -sae- */
528 return str[0] != '.' && str.length() > 13 && str.find (".bindings") == (str.length() - 9)
529 && str.find ("SAE-") != string::npos;
532 static bool binding_filter (const string& str, void* arg)
534 /* Not a dotfile, has a prefix before a period, suffix is ".bindings" */
536 return str[0] != '.' && str.length() > 9 && str.find (".bindings") == (str.length() - 9);
540 ARDOUR::find_bindings_files (map<string,string>& files)
543 vector<string*> *found;
546 full_path = get_user_ardour_path ();
548 full_path = get_system_data_path ();
550 if (getenv ("ARDOUR_SAE")) {
551 found = scanner (full_path, sae_binding_filter, 0, false, true);
553 found = scanner (full_path, binding_filter, 0, false, true);
560 for (vector<string*>::iterator x = found->begin(); x != found->end(); ++x) {
562 pair<string,string> namepath;
563 namepath.second = path;
564 path = Glib::path_get_basename (path);
565 namepath.first = path.substr (0, path.find_first_of ('.'));
567 files.insert (namepath);
575 ARDOUR::find_config_file (string name)
579 if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
583 return find_file (name, envvar);
587 ARDOUR::find_data_file (string name, string subdir)
590 if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
594 return find_file (name, envvar, subdir);
597 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
599 old = strdup (setlocale (LC_NUMERIC, NULL));
600 if (strcmp (old, str)) {
601 setlocale (LC_NUMERIC, str);
605 ARDOUR::LocaleGuard::~LocaleGuard ()
607 setlocale (LC_NUMERIC, old);
614 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
615 // valgrind doesn't understand this assembler stuff
616 // September 10th, 2007
620 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
625 /* XXX use real code to determine if the processor supports
626 DenormalsAreZero and FlushToZero
629 if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
633 MXCSR = _mm_getcsr();
635 switch (Config->get_denormal_model()) {
637 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
641 if (fpu.has_flush_to_zero()) {
642 MXCSR |= _MM_FLUSH_ZERO_ON;
647 MXCSR &= ~_MM_FLUSH_ZERO_ON;
648 if (fpu.has_denormals_are_zero()) {
654 if (fpu.has_flush_to_zero()) {
655 if (fpu.has_denormals_are_zero()) {
656 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
658 MXCSR |= _MM_FLUSH_ZERO_ON;
670 ARDOUR::coverage (nframes_t sa, nframes_t ea,
671 nframes_t sb, nframes_t eb)
673 /* OverlapType returned reflects how the second (B)
674 range overlaps the first (A).
676 The diagrams show various relative placements
677 of A and B for each OverlapType.
680 Internal: the start points cannot coincide
681 External: the start and end points can coincide
682 Start: end points can coincide
683 End: start points can coincide
685 XXX Logically, Internal should disallow end
690 |--------------------| A
692 |-----------------| B
699 if ((sb >= sa) && (eb <= ea)) {
701 if ((sb > sa) && (eb <= ea)) {
703 return OverlapInternal;
707 |--------------------| A
709 -----------------------| B
712 "B overlaps the start of A"
716 if ((eb >= sa) && (eb <= ea)) {
720 |---------------------| A
722 |----------------------- B
725 "B overlaps the end of A"
728 if ((sb >= sa) && (sb <= ea)) {
732 |--------------------| A
733 -------------------------- B
734 |----------------------- B
735 ----------------------| B
736 |--------------------| B
739 "B overlaps all of A"
741 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
742 return OverlapExternal;
748 /* not sure where to put these */
751 std::istream& int_to_type (std::istream& o, T& hf) {
758 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
759 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
760 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
761 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
762 std::istream& operator>>(std::istream& o, RemoteModel& var) { return int_to_type<RemoteModel> (o, var); }
763 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
764 std::istream& operator>>(std::istream& o, SoloModel& var) { return int_to_type<SoloModel> (o, var); }
765 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
766 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
767 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
768 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
769 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }
770 std::istream& operator>>(std::istream& o, SmpteFormat& var) { return int_to_type<SmpteFormat> (o, var); }
771 std::istream& operator>>(std::istream& o, DenormalModel& var) { return int_to_type<DenormalModel> (o, var); }