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>
38 #ifdef HAVE_AUDIOUNITS
39 #include "ardour/audio_unit.h"
43 #include <xmmintrin.h>
46 #include <glibmm/fileutils.h>
47 #include <glibmm/miscutils.h>
51 #include "pbd/error.h"
53 #include "pbd/strsplit.h"
55 #include "pbd/file_utils.h"
57 #include "midi++/port.h"
58 #include "midi++/manager.h"
59 #include "midi++/mmc.h"
61 #include "ardour/ardour.h"
62 #include "ardour/debug.h"
63 #include "ardour/analyser.h"
64 #include "ardour/audio_library.h"
65 #include "ardour/rc_configuration.h"
66 #include "ardour/profile.h"
67 #include "ardour/plugin_manager.h"
68 #include "ardour/audiosource.h"
69 #include "ardour/utils.h"
70 #include "ardour/session.h"
71 #include "ardour/source_factory.h"
72 #include "ardour/control_protocol_manager.h"
73 #include "ardour/audioengine.h"
74 #include "ardour/filesystem_paths.h"
76 #include "ardour/mix.h"
77 #include "ardour/runtime_functions.h"
79 #if defined (__APPLE__)
80 #include <Carbon/Carbon.h> // For Gestalt
85 ARDOUR::RCConfiguration* ARDOUR::Config = 0;
86 ARDOUR::RuntimeProfile* ARDOUR::Profile = 0;
87 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
89 using namespace ARDOUR;
93 uint64_t ARDOUR::debug_bits = 0x0;
95 MIDI::Port *ARDOUR::default_mmc_port = 0;
96 MIDI::Port *ARDOUR::default_mtc_port = 0;
97 MIDI::Port *ARDOUR::default_midi_port = 0;
98 MIDI::Port *ARDOUR::default_midi_clock_port = 0;
100 Change ARDOUR::StartChanged = ARDOUR::new_change ();
101 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
102 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
103 Change ARDOUR::NameChanged = ARDOUR::new_change ();
104 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
106 compute_peak_t ARDOUR::compute_peak = 0;
107 find_peaks_t ARDOUR::find_peaks = 0;
108 apply_gain_to_buffer_t ARDOUR::apply_gain_to_buffer = 0;
109 mix_buffers_with_gain_t ARDOUR::mix_buffers_with_gain = 0;
110 mix_buffers_no_gain_t ARDOUR::mix_buffers_no_gain = 0;
112 sigc::signal<void,std::string> ARDOUR::BootMessage;
115 ARDOUR::debug_print (const char* prefix, std::string str)
117 cerr << prefix << ": " << str;
121 ARDOUR::set_debug_bits (uint64_t bits)
127 ARDOUR::setup_midi ()
129 if (Config->midi_ports.size() == 0) {
130 //warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
134 BootMessage (_("Configuring MIDI ports"));
136 for (std::map<string,XMLNode>::iterator i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
137 MIDI::Manager::instance()->add_port (i->second);
141 const MIDI::Manager::PortMap& ports = MIDI::Manager::instance()->get_midi_ports();
143 if (ports.size() > 1) {
145 first = ports.begin()->second;
147 /* More than one port, so try using specific names for each port */
149 if (Config->get_mmc_port_name() != N_("control")) {
150 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
153 if (Config->get_mtc_port_name() != N_("control")) {
154 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
157 if (Config->get_midi_port_name() != N_("control")) {
158 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
161 if (Config->get_midi_clock_port_name() != N_("control")) {
162 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_clock_port_name());
165 /* If that didn't work, just use the first listed port */
167 if (default_mmc_port == 0) {
168 default_mmc_port = first;
171 if (default_mtc_port == 0) {
172 default_mtc_port = first;
175 if (default_midi_port == 0) {
176 default_midi_port = first;
179 if (default_midi_clock_port == 0) {
180 default_midi_clock_port = first;
183 } else if (ports.size() == 1) {
185 first = ports.begin()->second;
187 /* Only one port described, so use it for both MTC and MMC */
189 default_mmc_port = first;
190 default_mtc_port = default_mmc_port;
191 default_midi_port = default_mmc_port;
192 default_midi_clock_port = default_mmc_port;
195 if (default_mmc_port == 0) {
196 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name())
202 if (default_mtc_port == 0) {
203 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
207 if (default_midi_port == 0) {
208 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
212 if (default_midi_clock_port == 0) {
213 warning << string_compose (_("No MIDI Clock support (MIDI port \"%1\" not available)"), Config->get_midi_clock_port_name())
221 setup_hardware_optimization (bool try_optimization)
223 bool generic_mix_functions = true;
225 if (try_optimization) {
229 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
233 info << "Using SSE optimized routines" << endmsg;
236 compute_peak = x86_sse_compute_peak;
237 find_peaks = x86_sse_find_peaks;
238 apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
239 mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
240 mix_buffers_no_gain = x86_sse_mix_buffers_no_gain;
242 generic_mix_functions = false;
246 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
249 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
252 if (sysVersion >= 0x00001040) { // Tiger at least
253 compute_peak = veclib_compute_peak;
254 find_peaks = veclib_find_peaks;
255 apply_gain_to_buffer = veclib_apply_gain_to_buffer;
256 mix_buffers_with_gain = veclib_mix_buffers_with_gain;
257 mix_buffers_no_gain = veclib_mix_buffers_no_gain;
259 generic_mix_functions = false;
261 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
265 /* consider FPU denormal handling to be "h/w optimization" */
270 if (generic_mix_functions) {
272 compute_peak = default_compute_peak;
273 find_peaks = default_find_peaks;
274 apply_gain_to_buffer = default_apply_gain_to_buffer;
275 mix_buffers_with_gain = default_mix_buffers_with_gain;
276 mix_buffers_no_gain = default_mix_buffers_no_gain;
278 info << "No H/W specific optimizations in use" << endmsg;
283 lotsa_files_please ()
287 if (getrlimit (RLIMIT_NOFILE, &rl) == 0) {
289 rl.rlim_cur = rl.rlim_max;
291 if (setrlimit (RLIMIT_NOFILE, &rl) != 0) {
292 if (rl.rlim_cur == RLIM_INFINITY) {
293 error << _("Could not set system open files limit to \"unlimited\"") << endmsg;
295 error << string_compose (_("Could not set system open files limit to %1"), rl.rlim_cur) << endmsg;
298 if (rl.rlim_cur == RLIM_INFINITY) {
299 info << _("Removed open file count limit. Excellent!") << endmsg;
301 info << string_compose (_("Ardour will be limited to %1 open files"), rl.rlim_cur) << endmsg;
305 error << string_compose (_("Could not get system open files limit (%1)"), strerror (errno)) << endmsg;
310 ARDOUR::init (bool use_vst, bool try_optimization)
312 extern void setup_enum_writer ();
314 (void) bindtextdomain(PACKAGE, LOCALEDIR);
316 /* provide a state version for the few cases that need it and are not
317 driven by reading state from disk (e.g. undo/redo)
320 Stateful::current_state_version = CURRENT_SESSION_FILE_VERSION;
322 setup_enum_writer ();
324 // allow ardour the absolute maximum number of open files
325 lotsa_files_please ();
328 Library = new AudioLibrary;
330 BootMessage (_("Loading configuration"));
332 Config = new RCConfiguration;
334 if (Config->load_state ()) {
338 Config->set_use_vst (use_vst);
340 Profile = new RuntimeProfile;
344 if (Config->get_use_vst() && fst_init (0)) {
349 #ifdef HAVE_AUDIOUNITS
350 AUPluginInfo::load_cached_info ();
353 /* Make VAMP look in our library ahead of anything else */
355 char *p = getenv ("VAMP_PATH");
356 string vamppath = VAMP_DIR;
361 setenv ("VAMP_PATH", vamppath.c_str(), 1);
364 setup_hardware_optimization (try_optimization);
366 SourceFactory::init ();
369 /* singleton - first object is "it" */
370 new PluginManager ();
372 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
378 ARDOUR::init_post_engine ()
380 ControlProtocolManager::instance().discover_control_protocols ();
383 if ((node = Config->control_protocol_state()) != 0) {
384 ControlProtocolManager::instance().set_state (*node, Stateful::loading_state_version);
393 delete &ControlProtocolManager::instance();
402 ARDOUR::get_microseconds ()
404 return (microseconds_t) jack_get_time ();
408 ARDOUR::new_change ()
411 static uint32_t change_bit = 1;
413 /* catch out-of-range */
416 fatal << _("programming error: ")
417 << "change_bit out of range in ARDOUR::new_change()"
422 c = Change (change_bit);
423 change_bit <<= 1; // if it shifts too far, change_bit == 0
429 ARDOUR::get_ardour_revision ()
435 ARDOUR::find_bindings_files (map<string,string>& files)
437 vector<sys::path> found;
438 SearchPath spath = ardour_search_path() + user_config_directory() + system_config_search_path();
440 if (getenv ("ARDOUR_SAE")) {
441 Glib::PatternSpec pattern("*SAE-*.bindings");
442 find_matching_files_in_search_path (spath, pattern, found);
444 Glib::PatternSpec pattern("*.bindings");
445 find_matching_files_in_search_path (spath, pattern, found);
452 for (vector<sys::path>::iterator x = found.begin(); x != found.end(); ++x) {
454 pair<string,string> namepath;
455 namepath.second = path.to_string();
456 namepath.first = path.leaf().substr (0, path.leaf().find_first_of ('.'));
457 files.insert (namepath);
462 ARDOUR::no_auto_connect()
464 return getenv ("ARDOUR_NO_AUTOCONNECT") != 0;
467 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
469 old = strdup (setlocale (LC_NUMERIC, NULL));
470 if (strcmp (old, str)) {
471 setlocale (LC_NUMERIC, str);
475 ARDOUR::LocaleGuard::~LocaleGuard ()
477 setlocale (LC_NUMERIC, old);
485 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
486 // valgrind doesn't understand this assembler stuff
487 // September 10th, 2007
491 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
496 /* XXX use real code to determine if the processor supports
497 DenormalsAreZero and FlushToZero
500 if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
504 MXCSR = _mm_getcsr();
506 switch (Config->get_denormal_model()) {
508 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
512 if (fpu.has_flush_to_zero()) {
513 MXCSR |= _MM_FLUSH_ZERO_ON;
518 MXCSR &= ~_MM_FLUSH_ZERO_ON;
519 if (fpu.has_denormals_are_zero()) {
525 if (fpu.has_flush_to_zero()) {
526 if (fpu.has_denormals_are_zero()) {
527 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
529 MXCSR |= _MM_FLUSH_ZERO_ON;
541 ARDOUR::coverage (nframes_t sa, nframes_t ea,
542 nframes_t sb, nframes_t eb)
544 /* OverlapType returned reflects how the second (B)
545 range overlaps the first (A).
547 The diagrams show various relative placements
548 of A and B for each OverlapType.
551 Internal: the start points cannot coincide
552 External: the start and end points can coincide
553 Start: end points can coincide
554 End: start points can coincide
556 XXX Logically, Internal should disallow end
561 |--------------------| A
563 |-----------------| B
570 if ((sb >= sa) && (eb <= ea)) {
572 if ((sb > sa) && (eb <= ea)) {
574 return OverlapInternal;
578 |--------------------| A
580 -----------------------| B
583 "B overlaps the start of A"
587 if ((eb >= sa) && (eb <= ea)) {
591 |---------------------| A
593 |----------------------- B
596 "B overlaps the end of A"
599 if ((sb > sa) && (sb <= ea)) {
603 |--------------------| A
604 -------------------------- B
605 |----------------------- B
606 ----------------------| B
607 |--------------------| B
610 "B overlaps all of A"
612 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
613 return OverlapExternal;
619 /* not sure where to put these */
622 std::istream& int_to_type (std::istream& o, T& hf) {
629 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
630 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
631 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
632 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
633 std::istream& operator>>(std::istream& o, RemoteModel& var) { return int_to_type<RemoteModel> (o, var); }
634 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
635 std::istream& operator>>(std::istream& o, ListenPosition& var) { return int_to_type<ListenPosition> (o, var); }
636 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
637 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
638 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
639 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
640 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }
641 std::istream& operator>>(std::istream& o, SmpteFormat& var) { return int_to_type<SmpteFormat> (o, var); }
642 std::istream& operator>>(std::istream& o, DenormalModel& var) { return int_to_type<DenormalModel> (o, var); }
643 std::istream& operator>>(std::istream& o, WaveformScale& var) { return int_to_type<WaveformScale> (o, var); }
644 std::istream& operator>>(std::istream& o, WaveformShape& var) { return int_to_type<WaveformShape> (o, var); }