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>
42 #include <pbd/error.h>
44 #include <pbd/strsplit.h>
47 #include <midi++/port.h>
48 #include <midi++/manager.h>
49 #include <midi++/mmc.h>
51 #include <ardour/ardour.h>
52 #include <ardour/analyser.h>
53 #include <ardour/audio_library.h>
54 #include <ardour/configuration.h>
55 #include <ardour/profile.h>
56 #include <ardour/plugin_manager.h>
57 #include <ardour/audiosource.h>
58 #include <ardour/utils.h>
59 #include <ardour/session.h>
60 #include <ardour/source_factory.h>
61 #include <ardour/control_protocol_manager.h>
64 #include <ardour/osc.h>
67 #include <ardour/mix.h>
69 #if defined (__APPLE__)
70 #include <Carbon/Carbon.h> // For Gestalt
75 ARDOUR::Configuration* ARDOUR::Config = 0;
76 ARDOUR::RuntimeProfile* ARDOUR::Profile = 0;
77 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
80 ARDOUR::OSC* ARDOUR::osc = 0;
83 using namespace ARDOUR;
87 MIDI::Port *default_mmc_port = 0;
88 MIDI::Port *default_mtc_port = 0;
89 MIDI::Port *default_midi_port = 0;
91 Change ARDOUR::StartChanged = ARDOUR::new_change ();
92 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
93 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
94 Change ARDOUR::NameChanged = ARDOUR::new_change ();
95 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
101 /* no real cost to creating this object, and it avoids
102 conditionals anywhere that uses it
105 osc = new OSC (Config->get_osc_port());
107 if (Config->get_use_osc ()) {
108 return osc->start ();
118 if (Config->midi_ports.size() == 0) {
119 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
123 for (std::map<string,XMLNode>::iterator i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
124 MIDI::Manager::instance()->add_port (i->second);
128 const MIDI::Manager::PortMap& ports = MIDI::Manager::instance()->get_midi_ports();
130 if (ports.size() > 1) {
132 first = ports.begin()->second;
134 /* More than one port, so try using specific names for each port */
136 if (Config->get_mmc_port_name() != N_("default")) {
137 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
140 if (Config->get_mtc_port_name() != N_("default")) {
141 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
144 if (Config->get_midi_port_name() != N_("default")) {
145 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
148 /* If that didn't work, just use the first listed port */
150 if (default_mmc_port == 0) {
151 default_mmc_port = first;
154 if (default_mtc_port == 0) {
155 default_mtc_port = first;
158 if (default_midi_port == 0) {
159 default_midi_port = first;
162 } else if (ports.size() == 1) {
164 first = ports.begin()->second;
166 /* Only one port described, so use it for both MTC and MMC */
168 default_mmc_port = first;
169 default_mtc_port = default_mmc_port;
170 default_midi_port = default_mmc_port;
173 if (default_mmc_port == 0) {
174 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name())
179 if (default_mtc_port == 0) {
180 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
184 if (default_midi_port == 0) {
185 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
193 setup_hardware_optimization (bool try_optimization)
195 bool generic_mix_functions = true;
197 if (try_optimization) {
201 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
205 info << "Using SSE optimized routines" << endmsg;
208 Session::compute_peak = x86_sse_compute_peak;
209 Session::find_peaks = x86_sse_find_peaks;
210 Session::apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
211 Session::mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
212 Session::mix_buffers_no_gain = x86_sse_mix_buffers_no_gain;
214 generic_mix_functions = false;
218 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
221 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
224 if (sysVersion >= 0x00001040) { // Tiger at least
225 Session::compute_peak = veclib_compute_peak;
226 Session::find_peaks = veclib_find_peaks;
227 Session::apply_gain_to_buffer = veclib_apply_gain_to_buffer;
228 Session::mix_buffers_with_gain = veclib_mix_buffers_with_gain;
229 Session::mix_buffers_no_gain = veclib_mix_buffers_no_gain;
231 generic_mix_functions = false;
233 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
237 /* consider FPU denormal handling to be "h/w optimization" */
242 if (generic_mix_functions) {
244 Session::compute_peak = compute_peak;
245 Session::find_peaks = find_peaks;
246 Session::apply_gain_to_buffer = apply_gain_to_buffer;
247 Session::mix_buffers_with_gain = mix_buffers_with_gain;
248 Session::mix_buffers_no_gain = mix_buffers_no_gain;
250 info << "No H/W specific optimizations in use" << endmsg;
256 lotsa_files_please ()
260 if (getrlimit (RLIMIT_NOFILE, &rl) == 0) {
262 rl.rlim_cur = rl.rlim_max;
264 if (setrlimit (RLIMIT_NOFILE, &rl) != 0) {
265 if (rl.rlim_cur == RLIM_INFINITY) {
266 error << _("Could not set system open files limit to \"unlimited\"") << endmsg;
268 error << string_compose (_("Could not set system open files limit to %1"), rl.rlim_cur) << endmsg;
271 if (rl.rlim_cur == RLIM_INFINITY) {
272 info << _("Removed open file count limit. Excellent!") << endmsg;
274 info << string_compose (_("Ardour will be limited to %1 open files"), rl.rlim_cur) << endmsg;
278 error << string_compose (_("Could not get system open files limit (%1)"), strerror (errno)) << endmsg;
283 ARDOUR::init (bool use_vst, bool try_optimization)
285 extern void setup_enum_writer ();
287 (void) bindtextdomain(PACKAGE, LOCALEDIR);
289 setup_enum_writer ();
291 // allow ardour the absolute maximum number of open files
292 lotsa_files_please ();
295 Library = new AudioLibrary;
297 Config = new Configuration;
299 if (Config->load_state ()) {
303 Config->set_use_vst (use_vst);
305 Profile = new RuntimeProfile;
318 if (Config->get_use_vst() && fst_init ()) {
323 /* Make VAMP look in our library ahead of anything else */
325 char *p = getenv ("VAMP_PATH");
326 string vamppath = VAMP_DIR;
331 setenv ("VAMP_PATH", vamppath.c_str(), 1);
334 setup_hardware_optimization (try_optimization);
336 SourceFactory::init ();
339 /* singleton - first object is "it" */
340 new PluginManager ();
342 /* singleton - first object is "it" */
343 new ControlProtocolManager ();
344 ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
347 if ((node = Config->control_protocol_state()) != 0) {
348 ControlProtocolManager::instance().set_state (*node);
351 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
361 delete &ControlProtocolManager::instance();
367 ARDOUR::get_microseconds ()
369 /* XXX need JACK to export its functionality */
372 gettimeofday (&now, 0);
373 return now.tv_sec * 1000000ULL + now.tv_usec;
377 ARDOUR::new_change ()
380 static uint32_t change_bit = 1;
382 /* catch out-of-range */
385 fatal << _("programming error: ")
386 << "change_bit out of range in ARDOUR::new_change()"
391 c = Change (change_bit);
392 change_bit <<= 1; // if it shifts too far, change_bit == 0
398 ARDOUR::get_ardour_revision ()
404 ARDOUR::get_user_ardour_path ()
409 if ((envvar = getenv ("HOME")) == 0 || strlen (envvar) == 0) {
414 path += "/.ardour2/";
416 /* create it if necessary */
418 if (g_mkdir_with_parents (path.c_str (), 0755)) {
426 ARDOUR::get_system_data_path ()
432 if ((envvar = getenv ("ARDOUR_DATA_PATH")) != 0) {
443 ARDOUR::get_system_module_path ()
448 if ((envvar = getenv ("ARDOUR_MODULE_PATH")) != 0) {
459 find_file (string name, string dir, string subdir = "")
462 char* envvar = getenv("ARDOUR_PATH");
464 /* 1st attempt: any directory in ARDOUR_PATH */
468 vector<string> split_path;
470 split (envvar, split_path, ':');
472 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
475 if (Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
476 // cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
482 /* 2nd attempt: ~/.ardour/ */
484 path = get_user_ardour_path();
486 if (subdir.length()) {
487 path += subdir + "/";
491 if (access (path.c_str(), R_OK) == 0) {
495 /* 3rd attempt: dir/... */
500 if (subdir.length()) {
501 path += subdir + "/";
506 if (access (path.c_str(), R_OK) == 0) {
514 ARDOUR::find_config_file (string name)
518 if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
522 return find_file (name, envvar);
526 ARDOUR::find_data_file (string name, string subdir)
529 if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
533 return find_file (name, envvar, subdir);
536 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
538 old = strdup (setlocale (LC_NUMERIC, NULL));
539 if (strcmp (old, str)) {
540 setlocale (LC_NUMERIC, str);
544 ARDOUR::LocaleGuard::~LocaleGuard ()
546 setlocale (LC_NUMERIC, old);
553 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
554 // valgrind doesn't understand this assembler stuff
555 // September 10th, 2007
559 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
564 /* XXX use real code to determine if the processor supports
565 DenormalsAreZero and FlushToZero
568 if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
572 MXCSR = _mm_getcsr();
574 switch (Config->get_denormal_model()) {
576 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
580 if (fpu.has_flush_to_zero()) {
581 MXCSR |= _MM_FLUSH_ZERO_ON;
586 MXCSR &= ~_MM_FLUSH_ZERO_ON;
587 if (fpu.has_denormals_are_zero()) {
593 if (fpu.has_flush_to_zero()) {
594 if (fpu.has_denormals_are_zero()) {
595 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
597 MXCSR |= _MM_FLUSH_ZERO_ON;
609 ARDOUR::coverage (nframes_t sa, nframes_t ea,
610 nframes_t sb, nframes_t eb)
612 /* OverlapType returned reflects how the second (B)
613 range overlaps the first (A).
615 The diagrams show various relative placements
616 of A and B for each OverlapType.
619 Internal: the start points cannot coincide
620 External: the start and end points can coincide
621 Start: end points can coincide
622 End: start points can coincide
624 XXX Logically, Internal should disallow end
629 |--------------------| A
631 |-----------------| B
638 if ((sb >= sa) && (eb <= ea)) {
640 if ((sb > sa) && (eb <= ea)) {
642 return OverlapInternal;
646 |--------------------| A
648 -----------------------| B
651 "B overlaps the start of A"
655 if ((eb >= sa) && (eb <= ea)) {
659 |---------------------| A
661 |----------------------- B
664 "B overlaps the end of A"
667 if ((sb >= sa) && (sb <= ea)) {
671 |--------------------| A
672 -------------------------- B
673 |----------------------- B
674 ----------------------| B
675 |--------------------| B
678 "B overlaps all of A"
680 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
681 return OverlapExternal;
687 /* not sure where to put these */
690 std::istream& int_to_type (std::istream& o, T& hf) {
697 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
698 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
699 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
700 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
701 std::istream& operator>>(std::istream& o, RemoteModel& var) { return int_to_type<RemoteModel> (o, var); }
702 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
703 std::istream& operator>>(std::istream& o, SoloModel& var) { return int_to_type<SoloModel> (o, var); }
704 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
705 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
706 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
707 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
708 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }
709 std::istream& operator>>(std::istream& o, SmpteFormat& var) { return int_to_type<SmpteFormat> (o, var); }
710 std::istream& operator>>(std::istream& o, DenormalModel& var) { return int_to_type<DenormalModel> (o, var); }