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/source_factory.h"
76 #include "ardour/utils.h"
78 #if defined (__APPLE__)
79 #include <Carbon/Carbon.h> // For Gestalt
84 ARDOUR::RCConfiguration* ARDOUR::Config = 0;
85 ARDOUR::RuntimeProfile* ARDOUR::Profile = 0;
86 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
88 using namespace ARDOUR;
92 uint64_t ARDOUR::debug_bits = 0x0;
94 MIDI::Port *ARDOUR::default_mmc_port = 0;
95 MIDI::Port *ARDOUR::default_mtc_port = 0;
96 MIDI::Port *ARDOUR::default_midi_port = 0;
97 MIDI::Port *ARDOUR::default_midi_clock_port = 0;
99 Change ARDOUR::StartChanged = ARDOUR::new_change ();
100 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
101 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
102 Change ARDOUR::NameChanged = ARDOUR::new_change ();
103 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
105 compute_peak_t ARDOUR::compute_peak = 0;
106 find_peaks_t ARDOUR::find_peaks = 0;
107 apply_gain_to_buffer_t ARDOUR::apply_gain_to_buffer = 0;
108 mix_buffers_with_gain_t ARDOUR::mix_buffers_with_gain = 0;
109 mix_buffers_no_gain_t ARDOUR::mix_buffers_no_gain = 0;
111 sigc::signal<void,std::string> ARDOUR::BootMessage;
114 ARDOUR::debug_print (const char* prefix, std::string str)
116 cerr << prefix << ": " << str;
120 ARDOUR::set_debug_bits (uint64_t bits)
126 ARDOUR::setup_midi ()
128 if (Config->midi_ports.size() == 0) {
132 BootMessage (_("Configuring MIDI ports"));
134 for (std::map<string,XMLNode>::iterator i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
135 MIDI::Manager::instance()->add_port (i->second);
139 const MIDI::Manager::PortList& ports = MIDI::Manager::instance()->get_midi_ports();
141 if (ports.size() > 1) {
143 first = ports.front();
145 /* More than one port, so try using specific names for each port */
147 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
148 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
149 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
150 default_midi_clock_port = MIDI::Manager::instance()->port (Config->get_midi_clock_port_name());
152 /* If that didn't work, just use the first listed port */
154 if (default_mmc_port == 0) {
155 default_mmc_port = first;
158 if (default_mtc_port == 0) {
159 default_mtc_port = first;
162 if (default_midi_port == 0) {
163 default_midi_port = first;
166 if (default_midi_clock_port == 0) {
167 default_midi_clock_port = first;
170 } else if (ports.size() == 1) {
172 first = ports.front();
174 /* Only one port described, so use it for both MTC and MMC */
176 default_mmc_port = first;
177 default_mtc_port = default_mmc_port;
178 default_midi_port = default_mmc_port;
179 default_midi_clock_port = default_mmc_port;
182 if (default_mmc_port == 0) {
183 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name())
188 if (default_mtc_port == 0) {
189 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
193 if (default_midi_port == 0) {
194 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
198 if (default_midi_clock_port == 0) {
199 warning << string_compose (_("No MIDI Clock support (MIDI port \"%1\" not available)"), Config->get_midi_clock_port_name())
207 setup_hardware_optimization (bool try_optimization)
209 bool generic_mix_functions = true;
211 if (try_optimization) {
215 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
219 info << "Using SSE optimized routines" << endmsg;
222 compute_peak = x86_sse_compute_peak;
223 find_peaks = x86_sse_find_peaks;
224 apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
225 mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
226 mix_buffers_no_gain = x86_sse_mix_buffers_no_gain;
228 generic_mix_functions = false;
232 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
235 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
238 if (sysVersion >= 0x00001040) { // Tiger at least
239 compute_peak = veclib_compute_peak;
240 find_peaks = veclib_find_peaks;
241 apply_gain_to_buffer = veclib_apply_gain_to_buffer;
242 mix_buffers_with_gain = veclib_mix_buffers_with_gain;
243 mix_buffers_no_gain = veclib_mix_buffers_no_gain;
245 generic_mix_functions = false;
247 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
251 /* consider FPU denormal handling to be "h/w optimization" */
256 if (generic_mix_functions) {
258 compute_peak = default_compute_peak;
259 find_peaks = default_find_peaks;
260 apply_gain_to_buffer = default_apply_gain_to_buffer;
261 mix_buffers_with_gain = default_mix_buffers_with_gain;
262 mix_buffers_no_gain = default_mix_buffers_no_gain;
264 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 if (!Glib::thread_supported())
303 extern void setup_enum_writer ();
305 (void) bindtextdomain(PACKAGE, LOCALEDIR);
307 /* provide a state version for the few cases that need it and are not
308 driven by reading state from disk (e.g. undo/redo)
311 Stateful::current_state_version = CURRENT_SESSION_FILE_VERSION;
313 setup_enum_writer ();
315 // allow ardour the absolute maximum number of open files
316 lotsa_files_please ();
319 Library = new AudioLibrary;
321 BootMessage (_("Loading configuration"));
323 Config = new RCConfiguration;
325 if (Config->load_state ()) {
330 Config->set_use_vst (use_vst);
332 cerr << "After config loaded, MTC port name = " << Config->get_mtc_port_name() << endl;
334 Profile = new RuntimeProfile;
338 if (Config->get_use_vst() && fst_init (0)) {
343 #ifdef HAVE_AUDIOUNITS
344 AUPluginInfo::load_cached_info ();
347 /* Make VAMP look in our library ahead of anything else */
349 char *p = getenv ("VAMP_PATH");
350 string vamppath = VAMP_DIR;
355 setenv ("VAMP_PATH", vamppath.c_str(), 1);
358 setup_hardware_optimization (try_optimization);
360 SourceFactory::init ();
363 /* singleton - first object is "it" */
364 new PluginManager ();
366 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
372 ARDOUR::init_post_engine ()
374 ControlProtocolManager::instance().discover_control_protocols ();
377 if ((node = Config->control_protocol_state()) != 0) {
378 ControlProtocolManager::instance().set_state (*node, Stateful::loading_state_version);
387 delete &ControlProtocolManager::instance();
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::find_bindings_files (map<string,string>& files)
424 vector<sys::path> found;
425 SearchPath spath = ardour_search_path() + user_config_directory() + system_config_search_path();
427 if (getenv ("ARDOUR_SAE")) {
428 Glib::PatternSpec pattern("*SAE-*.bindings");
429 find_matching_files_in_search_path (spath, pattern, found);
431 Glib::PatternSpec pattern("*.bindings");
432 find_matching_files_in_search_path (spath, pattern, found);
439 for (vector<sys::path>::iterator x = found.begin(); x != found.end(); ++x) {
441 pair<string,string> namepath;
442 namepath.second = path.to_string();
443 namepath.first = path.leaf().substr (0, path.leaf().find_first_of ('.'));
444 files.insert (namepath);
449 ARDOUR::no_auto_connect()
451 return getenv ("ARDOUR_NO_AUTOCONNECT") != 0;
458 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
459 // valgrind doesn't understand this assembler stuff
460 // September 10th, 2007
464 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
469 /* XXX use real code to determine if the processor supports
470 DenormalsAreZero and FlushToZero
473 if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
477 MXCSR = _mm_getcsr();
479 switch (Config->get_denormal_model()) {
481 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
485 if (fpu.has_flush_to_zero()) {
486 MXCSR |= _MM_FLUSH_ZERO_ON;
491 MXCSR &= ~_MM_FLUSH_ZERO_ON;
492 if (fpu.has_denormals_are_zero()) {
498 if (fpu.has_flush_to_zero()) {
499 if (fpu.has_denormals_are_zero()) {
500 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
502 MXCSR |= _MM_FLUSH_ZERO_ON;
514 ARDOUR::coverage (nframes_t sa, nframes_t ea,
515 nframes_t sb, nframes_t eb)
517 /* OverlapType returned reflects how the second (B)
518 range overlaps the first (A).
520 The diagrams show various relative placements
521 of A and B for each OverlapType.
524 Internal: the start points cannot coincide
525 External: the start and end points can coincide
526 Start: end points can coincide
527 End: start points can coincide
529 XXX Logically, Internal should disallow end
534 |--------------------| A
536 |-----------------| B
543 if ((sb >= sa) && (eb <= ea)) {
545 if ((sb > sa) && (eb <= ea)) {
547 return OverlapInternal;
551 |--------------------| A
553 -----------------------| B
556 "B overlaps the start of A"
560 if ((eb >= sa) && (eb <= ea)) {
564 |---------------------| A
566 |----------------------- B
569 "B overlaps the end of A"
572 if ((sb > sa) && (sb <= ea)) {
576 |--------------------| A
577 -------------------------- B
578 |----------------------- B
579 ----------------------| B
580 |--------------------| B
583 "B overlaps all of A"
585 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
586 return OverlapExternal;