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.
19 #include <cstdio> // Needed so that libraptor (included in lrdf) won't complain
21 #include <sys/types.h>
23 #include <sys/resource.h>
33 #ifdef HAVE_AUDIOUNITS
34 #include "ardour/audio_unit.h"
38 #include <xmmintrin.h>
41 #include <glibmm/fileutils.h>
42 #include <glibmm/miscutils.h>
46 #include "pbd/error.h"
48 #include "pbd/strsplit.h"
50 #include "pbd/file_utils.h"
52 #include "midi++/port.h"
53 #include "midi++/manager.h"
54 #include "midi++/mmc.h"
56 #include "ardour/ardour.h"
57 #include "ardour/analyser.h"
58 #include "ardour/audio_library.h"
59 #include "ardour/rc_configuration.h"
60 #include "ardour/profile.h"
61 #include "ardour/plugin_manager.h"
62 #include "ardour/audiosource.h"
63 #include "ardour/utils.h"
64 #include "ardour/session.h"
65 #include "ardour/source_factory.h"
66 #include "ardour/control_protocol_manager.h"
67 #include "ardour/audioengine.h"
68 #include "ardour/filesystem_paths.h"
70 #include "ardour/mix.h"
71 #include "ardour/runtime_functions.h"
73 #if defined (__APPLE__)
74 #include <Carbon/Carbon.h> // For Gestalt
79 ARDOUR::RCConfiguration* ARDOUR::Config = 0;
80 ARDOUR::RuntimeProfile* ARDOUR::Profile = 0;
81 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
83 using namespace ARDOUR;
87 MIDI::Port *ARDOUR::default_mmc_port = 0;
88 MIDI::Port *ARDOUR::default_mtc_port = 0;
89 MIDI::Port *ARDOUR::default_midi_port = 0;
90 MIDI::Port *ARDOUR::default_midi_clock_port = 0;
92 Change ARDOUR::StartChanged = ARDOUR::new_change ();
93 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
94 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
95 Change ARDOUR::NameChanged = ARDOUR::new_change ();
96 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
98 compute_peak_t ARDOUR::compute_peak = 0;
99 find_peaks_t ARDOUR::find_peaks = 0;
100 apply_gain_to_buffer_t ARDOUR::apply_gain_to_buffer = 0;
101 mix_buffers_with_gain_t ARDOUR::mix_buffers_with_gain = 0;
102 mix_buffers_no_gain_t ARDOUR::mix_buffers_no_gain = 0;
104 sigc::signal<void,std::string> ARDOUR::BootMessage;
107 ARDOUR::setup_midi ()
109 if (Config->midi_ports.size() == 0) {
110 //warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
114 BootMessage (_("Configuring MIDI ports"));
116 for (std::map<string,XMLNode>::iterator i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
117 MIDI::Manager::instance()->add_port (i->second);
121 const MIDI::Manager::PortMap& ports = MIDI::Manager::instance()->get_midi_ports();
123 if (ports.size() > 1) {
125 first = ports.begin()->second;
127 /* More than one port, so try using specific names for each port */
129 if (Config->get_mmc_port_name() != N_("default")) {
130 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
133 if (Config->get_mtc_port_name() != N_("default")) {
134 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
137 if (Config->get_midi_port_name() != N_("default")) {
138 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
141 if (Config->get_midi_clock_port_name() != N_("default")) {
142 default_midi_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.begin()->second;
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())
182 if (default_mtc_port == 0) {
183 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
187 if (default_midi_port == 0) {
188 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
192 if (default_midi_clock_port == 0) {
193 warning << string_compose (_("No MIDI Clock support (MIDI port \"%1\" not available)"), Config->get_midi_clock_port_name())
201 setup_hardware_optimization (bool try_optimization)
203 bool generic_mix_functions = true;
205 if (try_optimization) {
209 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
213 info << "Using SSE optimized routines" << endmsg;
216 compute_peak = x86_sse_compute_peak;
217 find_peaks = x86_sse_find_peaks;
218 apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
219 mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
220 mix_buffers_no_gain = x86_sse_mix_buffers_no_gain;
222 generic_mix_functions = false;
226 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
229 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
232 if (sysVersion >= 0x00001040) { // Tiger at least
233 compute_peak = veclib_compute_peak;
234 find_peaks = veclib_find_peaks;
235 apply_gain_to_buffer = veclib_apply_gain_to_buffer;
236 mix_buffers_with_gain = veclib_mix_buffers_with_gain;
237 mix_buffers_no_gain = veclib_mix_buffers_no_gain;
239 generic_mix_functions = false;
241 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
245 /* consider FPU denormal handling to be "h/w optimization" */
250 if (generic_mix_functions) {
252 compute_peak = default_compute_peak;
253 find_peaks = default_find_peaks;
254 apply_gain_to_buffer = default_apply_gain_to_buffer;
255 mix_buffers_with_gain = default_mix_buffers_with_gain;
256 mix_buffers_no_gain = default_mix_buffers_no_gain;
258 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 RCConfiguration;
308 if (Config->load_state ()) {
312 Config->set_use_vst (use_vst);
314 Profile = new RuntimeProfile;
318 if (Config->get_use_vst() && fst_init (0)) {
323 #ifdef HAVE_AUDIOUNITS
324 AUPluginInfo::load_cached_info ();
327 /* Make VAMP look in our library ahead of anything else */
329 char *p = getenv ("VAMP_PATH");
330 string vamppath = VAMP_DIR;
335 setenv ("VAMP_PATH", vamppath.c_str(), 1);
338 setup_hardware_optimization (try_optimization);
340 SourceFactory::init ();
343 /* singleton - first object is "it" */
344 new PluginManager ();
346 /* singleton - first object is "it" */
347 new ControlProtocolManager ();
348 ControlProtocolManager::instance().discover_control_protocols ();
351 if ((node = Config->control_protocol_state()) != 0) {
352 ControlProtocolManager::instance().set_state (*node);
355 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
365 delete &ControlProtocolManager::instance();
374 ARDOUR::get_microseconds ()
376 return (microseconds_t) jack_get_time ();
380 ARDOUR::new_change ()
383 static uint32_t change_bit = 1;
385 /* catch out-of-range */
388 fatal << _("programming error: ")
389 << "change_bit out of range in ARDOUR::new_change()"
394 c = Change (change_bit);
395 change_bit <<= 1; // if it shifts too far, change_bit == 0
401 ARDOUR::get_ardour_revision ()
407 ARDOUR::find_bindings_files (map<string,string>& files)
409 vector<sys::path> found;
410 SearchPath spath = ardour_search_path() + user_config_directory() + system_config_search_path();
412 if (getenv ("ARDOUR_SAE")) {
413 Glib::PatternSpec pattern("*SAE-*.bindings");
414 find_matching_files_in_search_path (spath, pattern, found);
416 Glib::PatternSpec pattern("*.bindings");
417 find_matching_files_in_search_path (spath, pattern, found);
424 for (vector<sys::path>::iterator x = found.begin(); x != found.end(); ++x) {
426 pair<string,string> namepath;
427 namepath.second = path.to_string();
428 namepath.first = path.leaf().substr (0, path.leaf().find_first_of ('.'));
429 files.insert (namepath);
433 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
435 old = strdup (setlocale (LC_NUMERIC, NULL));
436 if (strcmp (old, str)) {
437 setlocale (LC_NUMERIC, str);
441 ARDOUR::LocaleGuard::~LocaleGuard ()
443 setlocale (LC_NUMERIC, old);
451 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
452 // valgrind doesn't understand this assembler stuff
453 // September 10th, 2007
457 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
462 /* XXX use real code to determine if the processor supports
463 DenormalsAreZero and FlushToZero
466 if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
470 MXCSR = _mm_getcsr();
472 switch (Config->get_denormal_model()) {
474 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
478 if (fpu.has_flush_to_zero()) {
479 MXCSR |= _MM_FLUSH_ZERO_ON;
484 MXCSR &= ~_MM_FLUSH_ZERO_ON;
485 if (fpu.has_denormals_are_zero()) {
491 if (fpu.has_flush_to_zero()) {
492 if (fpu.has_denormals_are_zero()) {
493 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
495 MXCSR |= _MM_FLUSH_ZERO_ON;
507 ARDOUR::coverage (nframes_t sa, nframes_t ea,
508 nframes_t sb, nframes_t eb)
510 /* OverlapType returned reflects how the second (B)
511 range overlaps the first (A).
513 The diagrams show various relative placements
514 of A and B for each OverlapType.
517 Internal: the start points cannot coincide
518 External: the start and end points can coincide
519 Start: end points can coincide
520 End: start points can coincide
522 XXX Logically, Internal should disallow end
527 |--------------------| A
529 |-----------------| B
536 if ((sb >= sa) && (eb <= ea)) {
538 if ((sb > sa) && (eb <= ea)) {
540 return OverlapInternal;
544 |--------------------| A
546 -----------------------| B
549 "B overlaps the start of A"
553 if ((eb >= sa) && (eb <= ea)) {
557 |---------------------| A
559 |----------------------- B
562 "B overlaps the end of A"
565 if ((sb > sa) && (sb <= ea)) {
569 |--------------------| A
570 -------------------------- B
571 |----------------------- B
572 ----------------------| B
573 |--------------------| B
576 "B overlaps all of A"
578 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
579 return OverlapExternal;
585 /* not sure where to put these */
588 std::istream& int_to_type (std::istream& o, T& hf) {
595 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
596 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
597 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
598 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
599 std::istream& operator>>(std::istream& o, RemoteModel& var) { return int_to_type<RemoteModel> (o, var); }
600 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
601 std::istream& operator>>(std::istream& o, SoloModel& var) { return int_to_type<SoloModel> (o, var); }
602 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
603 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
604 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
605 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
606 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }
607 std::istream& operator>>(std::istream& o, SmpteFormat& var) { return int_to_type<SmpteFormat> (o, var); }
608 std::istream& operator>>(std::istream& o, DenormalModel& var) { return int_to_type<DenormalModel> (o, var); }
609 std::istream& operator>>(std::istream& o, WaveformScale& var) { return int_to_type<WaveformScale> (o, var); }
610 std::istream& operator>>(std::istream& o, WaveformShape& var) { return int_to_type<WaveformShape> (o, var); }