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.
21 #include "libardour-config.h"
24 #include <cstdio> // Needed so that libraptor (included in lrdf) won't complain
26 #include <sys/types.h>
28 #include <sys/resource.h>
37 #ifdef HAVE_AUDIOUNITS
38 #include "ardour/audio_unit.h"
42 #include <xmmintrin.h>
45 #include <glibmm/fileutils.h>
46 #include <glibmm/miscutils.h>
50 #include "pbd/error.h"
52 #include "pbd/strsplit.h"
54 #include "pbd/file_utils.h"
55 #include "pbd/enumwriter.h"
57 #include "midi++/port.h"
58 #include "midi++/manager.h"
59 #include "midi++/mmc.h"
61 #include "ardour/analyser.h"
62 #include "ardour/ardour.h"
63 #include "ardour/audio_library.h"
64 #include "ardour/audioengine.h"
65 #include "ardour/audiosource.h"
66 #include "ardour/control_protocol_manager.h"
67 #include "ardour/debug.h"
68 #include "ardour/filesystem_paths.h"
69 #include "ardour/mix.h"
70 #include "ardour/plugin_manager.h"
71 #include "ardour/profile.h"
72 #include "ardour/rc_configuration.h"
73 #include "ardour/runtime_functions.h"
74 #include "ardour/session.h"
75 #include "ardour/session_event.h"
76 #include "ardour/source_factory.h"
77 #include "ardour/utils.h"
79 #include "audiographer/routines.h"
81 #if defined (__APPLE__)
82 #include <Carbon/Carbon.h> // For Gestalt
87 ARDOUR::RCConfiguration* ARDOUR::Config = 0;
88 ARDOUR::RuntimeProfile* ARDOUR::Profile = 0;
89 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
91 using namespace ARDOUR;
95 uint64_t ARDOUR::debug_bits = 0x0;
97 MIDI::Port *ARDOUR::default_mmc_port = 0;
98 MIDI::Port *ARDOUR::default_mtc_port = 0;
99 MIDI::Port *ARDOUR::default_midi_port = 0;
100 MIDI::Port *ARDOUR::default_midi_clock_port = 0;
102 Change ARDOUR::StartChanged = ARDOUR::new_change ();
103 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
104 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
105 Change ARDOUR::NameChanged = ARDOUR::new_change ();
106 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
108 compute_peak_t ARDOUR::compute_peak = 0;
109 find_peaks_t ARDOUR::find_peaks = 0;
110 apply_gain_to_buffer_t ARDOUR::apply_gain_to_buffer = 0;
111 mix_buffers_with_gain_t ARDOUR::mix_buffers_with_gain = 0;
112 mix_buffers_no_gain_t ARDOUR::mix_buffers_no_gain = 0;
114 PBD::Signal1<void,std::string> ARDOUR::BootMessage;
116 void ARDOUR::setup_enum_writer ();
119 ARDOUR::setup_midi ()
121 if (Config->midi_ports.size() == 0) {
125 BootMessage (_("Configuring MIDI ports"));
127 for (std::map<string,XMLNode>::iterator i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
128 MIDI::Manager::instance()->add_port (i->second);
132 const MIDI::Manager::PortList& ports = MIDI::Manager::instance()->get_midi_ports();
134 if (ports.size() > 1) {
136 first = ports.front();
138 /* More than one port, so try using specific names for each port */
140 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
141 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
142 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
143 default_midi_clock_port = MIDI::Manager::instance()->port (Config->get_midi_clock_port_name());
145 /* If that didn't work, just use the first listed port */
147 if (default_mmc_port == 0) {
148 default_mmc_port = first;
151 if (default_mtc_port == 0) {
152 default_mtc_port = first;
155 if (default_midi_port == 0) {
156 default_midi_port = first;
159 if (default_midi_clock_port == 0) {
160 default_midi_clock_port = first;
163 } else if (ports.size() == 1) {
165 first = ports.front();
167 /* Only one port described, so use it for both MTC and MMC */
169 default_mmc_port = first;
170 default_mtc_port = default_mmc_port;
171 default_midi_port = default_mmc_port;
172 default_midi_clock_port = default_mmc_port;
175 if (default_mmc_port == 0) {
176 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name())
181 if (default_mtc_port == 0) {
182 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
186 if (default_midi_port == 0) {
187 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
191 if (default_midi_clock_port == 0) {
192 warning << string_compose (_("No MIDI Clock support (MIDI port \"%1\" not available)"), Config->get_midi_clock_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 compute_peak = x86_sse_compute_peak;
216 find_peaks = x86_sse_find_peaks;
217 apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
218 mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
219 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 compute_peak = veclib_compute_peak;
233 find_peaks = veclib_find_peaks;
234 apply_gain_to_buffer = veclib_apply_gain_to_buffer;
235 mix_buffers_with_gain = veclib_mix_buffers_with_gain;
236 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 compute_peak = default_compute_peak;
252 find_peaks = default_find_peaks;
253 apply_gain_to_buffer = default_apply_gain_to_buffer;
254 mix_buffers_with_gain = default_mix_buffers_with_gain;
255 mix_buffers_no_gain = default_mix_buffers_no_gain;
257 info << "No H/W specific optimizations in use" << endmsg;
260 AudioGrapher::Routines::override_compute_peak (compute_peak);
261 AudioGrapher::Routines::override_apply_gain_to_buffer (apply_gain_to_buffer);
265 lotsa_files_please ()
269 if (getrlimit (RLIMIT_NOFILE, &rl) == 0) {
271 rl.rlim_cur = rl.rlim_max;
273 if (setrlimit (RLIMIT_NOFILE, &rl) != 0) {
274 if (rl.rlim_cur == RLIM_INFINITY) {
275 error << _("Could not set system open files limit to \"unlimited\"") << endmsg;
277 error << string_compose (_("Could not set system open files limit to %1"), rl.rlim_cur) << endmsg;
280 if (rl.rlim_cur == RLIM_INFINITY) {
281 info << _("Removed open file count limit. Excellent!") << endmsg;
283 info << string_compose (_("Ardour will be limited to %1 open files"), rl.rlim_cur) << endmsg;
287 error << string_compose (_("Could not get system open files limit (%1)"), strerror (errno)) << endmsg;
292 ARDOUR::init (bool use_vst, bool try_optimization)
294 if (!Glib::thread_supported()) {
298 (void) bindtextdomain(PACKAGE, LOCALEDIR);
301 SessionEvent::init_event_pool ();
304 /* provide a state version for the few cases that need it and are not
305 driven by reading state from disk (e.g. undo/redo)
308 Stateful::current_state_version = CURRENT_SESSION_FILE_VERSION;
310 setup_enum_writer ();
312 // allow ardour the absolute maximum number of open files
313 lotsa_files_please ();
316 Library = new AudioLibrary;
318 BootMessage (_("Loading configuration"));
320 Config = new RCConfiguration;
322 if (Config->load_state ()) {
327 Config->set_use_vst (use_vst);
329 cerr << "After config loaded, MTC port name = " << Config->get_mtc_port_name() << endl;
331 Profile = new RuntimeProfile;
335 if (Config->get_use_vst() && fst_init (0)) {
340 #ifdef HAVE_AUDIOUNITS
341 AUPluginInfo::load_cached_info ();
344 /* Make VAMP look in our library ahead of anything else */
346 char *p = getenv ("VAMP_PATH");
347 string vamppath = VAMP_DIR;
352 setenv ("VAMP_PATH", vamppath.c_str(), 1);
355 setup_hardware_optimization (try_optimization);
357 SourceFactory::init ();
360 /* singleton - first object is "it" */
361 new PluginManager ();
363 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
369 ARDOUR::init_post_engine ()
371 ControlProtocolManager::instance().discover_control_protocols ();
374 if ((node = Config->control_protocol_state()) != 0) {
375 ControlProtocolManager::instance().set_state (*node, Stateful::loading_state_version);
384 delete &ControlProtocolManager::instance();
392 ARDOUR::new_change ()
395 static uint32_t change_bit = 1;
397 /* catch out-of-range */
400 fatal << _("programming error: ")
401 << "change_bit out of range in ARDOUR::new_change()"
406 c = Change (change_bit);
407 change_bit <<= 1; // if it shifts too far, change_bit == 0
413 ARDOUR::get_ardour_revision ()
419 ARDOUR::find_bindings_files (map<string,string>& files)
421 vector<sys::path> found;
422 SearchPath spath = ardour_search_path() + user_config_directory() + system_config_search_path();
424 if (getenv ("ARDOUR_SAE")) {
425 Glib::PatternSpec pattern("*SAE-*.bindings");
426 find_matching_files_in_search_path (spath, pattern, found);
428 Glib::PatternSpec pattern("*.bindings");
429 find_matching_files_in_search_path (spath, pattern, found);
436 for (vector<sys::path>::iterator x = found.begin(); x != found.end(); ++x) {
438 pair<string,string> namepath;
439 namepath.second = path.to_string();
440 namepath.first = path.leaf().substr (0, path.leaf().find_first_of ('.'));
441 files.insert (namepath);
446 ARDOUR::no_auto_connect()
448 return getenv ("ARDOUR_NO_AUTOCONNECT") != 0;
455 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
456 // valgrind doesn't understand this assembler stuff
457 // September 10th, 2007
461 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
466 /* XXX use real code to determine if the processor supports
467 DenormalsAreZero and FlushToZero
470 if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
474 MXCSR = _mm_getcsr();
476 switch (Config->get_denormal_model()) {
478 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
482 if (fpu.has_flush_to_zero()) {
483 MXCSR |= _MM_FLUSH_ZERO_ON;
488 MXCSR &= ~_MM_FLUSH_ZERO_ON;
489 if (fpu.has_denormals_are_zero()) {
495 if (fpu.has_flush_to_zero()) {
496 if (fpu.has_denormals_are_zero()) {
497 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
499 MXCSR |= _MM_FLUSH_ZERO_ON;
511 ARDOUR::coverage (nframes_t sa, nframes_t ea,
512 nframes_t sb, nframes_t eb)
514 /* OverlapType returned reflects how the second (B)
515 range overlaps the first (A).
517 The diagrams show various relative placements
518 of A and B for each OverlapType.
521 Internal: the start points cannot coincide
522 External: the start and end points can coincide
523 Start: end points can coincide
524 End: start points can coincide
526 XXX Logically, Internal should disallow end
531 |--------------------| A
533 |-----------------| B
540 if ((sb >= sa) && (eb <= ea)) {
542 if ((sb > sa) && (eb <= ea)) {
544 return OverlapInternal;
548 |--------------------| A
550 -----------------------| B
553 "B overlaps the start of A"
557 if ((eb >= sa) && (eb <= ea)) {
561 |---------------------| A
563 |----------------------- B
566 "B overlaps the end of A"
569 if ((sb > sa) && (sb <= ea)) {
573 |--------------------| A
574 -------------------------- B
575 |----------------------- B
576 ----------------------| B
577 |--------------------| B
580 "B overlaps all of A"
582 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
583 return OverlapExternal;