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>
32 #include <xmmintrin.h>
35 #include <glibmm/fileutils.h>
39 #include <pbd/error.h>
41 #include <pbd/strsplit.h>
44 #include <midi++/port.h>
45 #include <midi++/manager.h>
46 #include <midi++/mmc.h>
48 #include <ardour/ardour.h>
49 #include <ardour/audio_library.h>
50 #include <ardour/configuration.h>
51 #include <ardour/profile.h>
52 #include <ardour/plugin_manager.h>
53 #include <ardour/audiosource.h>
54 #include <ardour/utils.h>
55 #include <ardour/session.h>
56 #include <ardour/source_factory.h>
57 #include <ardour/control_protocol_manager.h>
60 #include <ardour/osc.h>
63 #include <ardour/mix.h>
65 #if defined (__APPLE__)
66 #include <Carbon/Carbon.h> // For Gestalt
71 ARDOUR::Configuration* ARDOUR::Config = 0;
72 ARDOUR::RuntimeProfile* ARDOUR::Profile = 0;
73 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
76 ARDOUR::OSC* ARDOUR::osc = 0;
79 using namespace ARDOUR;
83 MIDI::Port *default_mmc_port = 0;
84 MIDI::Port *default_mtc_port = 0;
85 MIDI::Port *default_midi_port = 0;
87 Change ARDOUR::StartChanged = ARDOUR::new_change ();
88 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
89 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
90 Change ARDOUR::NameChanged = ARDOUR::new_change ();
91 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
97 /* no real cost to creating this object, and it avoids
98 conditionals anywhere that uses it
101 osc = new OSC (Config->get_osc_port());
103 if (Config->get_use_osc ()) {
104 return osc->start ();
114 if (Config->midi_ports.size() == 0) {
115 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
119 for (std::map<string,XMLNode>::iterator i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
120 MIDI::Manager::instance()->add_port (i->second);
124 const MIDI::Manager::PortMap& ports = MIDI::Manager::instance()->get_midi_ports();
126 if (ports.size() > 1) {
128 first = ports.begin()->second;
130 /* More than one port, so try using specific names for each port */
132 if (Config->get_mmc_port_name() != N_("default")) {
133 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
136 if (Config->get_mtc_port_name() != N_("default")) {
137 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
140 if (Config->get_midi_port_name() != N_("default")) {
141 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
144 /* If that didn't work, just use the first listed port */
146 if (default_mmc_port == 0) {
147 default_mmc_port = first;
150 if (default_mtc_port == 0) {
151 default_mtc_port = first;
154 if (default_midi_port == 0) {
155 default_midi_port = first;
158 } else if (ports.size() == 1) {
160 first = ports.begin()->second;
162 /* Only one port described, so use it for both MTC and MMC */
164 default_mmc_port = first;
165 default_mtc_port = default_mmc_port;
166 default_midi_port = default_mmc_port;
169 if (default_mmc_port == 0) {
170 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name())
175 if (default_mtc_port == 0) {
176 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
180 if (default_midi_port == 0) {
181 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
189 setup_hardware_optimization (bool try_optimization)
191 bool generic_mix_functions = true;
193 if (try_optimization) {
197 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
201 info << "Using SSE optimized routines" << endmsg;
204 Session::compute_peak = x86_sse_compute_peak;
205 Session::find_peaks = x86_sse_find_peaks;
206 Session::apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
207 Session::mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
208 Session::mix_buffers_no_gain = x86_sse_mix_buffers_no_gain;
210 generic_mix_functions = false;
214 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
217 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
220 if (sysVersion >= 0x00001040) { // Tiger at least
221 Session::compute_peak = veclib_compute_peak;
222 Session::find_peaks = veclib_find_peaks;
223 Session::apply_gain_to_buffer = veclib_apply_gain_to_buffer;
224 Session::mix_buffers_with_gain = veclib_mix_buffers_with_gain;
225 Session::mix_buffers_no_gain = veclib_mix_buffers_no_gain;
227 generic_mix_functions = false;
229 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
233 /* consider FPU denormal handling to be "h/w optimization" */
238 if (generic_mix_functions) {
240 Session::compute_peak = compute_peak;
241 Session::find_peaks = find_peaks;
242 Session::apply_gain_to_buffer = apply_gain_to_buffer;
243 Session::mix_buffers_with_gain = mix_buffers_with_gain;
244 Session::mix_buffers_no_gain = mix_buffers_no_gain;
246 info << "No H/W specific optimizations in use" << endmsg;
252 ARDOUR::init (bool use_vst, bool try_optimization)
254 extern void setup_enum_writer ();
256 (void) bindtextdomain(PACKAGE, LOCALEDIR);
258 setup_enum_writer ();
261 Library = new AudioLibrary;
263 Config = new Configuration;
265 if (Config->load_state ()) {
269 Config->set_use_vst (use_vst);
271 Profile = new RuntimeProfile;
284 if (Config->get_use_vst() && fst_init ()) {
289 setup_hardware_optimization (try_optimization);
291 SourceFactory::init ();
293 /* singleton - first object is "it" */
294 new PluginManager ();
296 /* singleton - first object is "it" */
297 new ControlProtocolManager ();
298 ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
301 if ((node = Config->control_protocol_state()) != 0) {
302 ControlProtocolManager::instance().set_state (*node);
305 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
315 delete &ControlProtocolManager::instance();
321 ARDOUR::get_microseconds ()
323 /* XXX need JACK to export its functionality */
326 gettimeofday (&now, 0);
327 return now.tv_sec * 1000000ULL + now.tv_usec;
331 ARDOUR::new_change ()
334 static uint32_t change_bit = 1;
336 /* catch out-of-range */
339 fatal << _("programming error: ")
340 << "change_bit out of range in ARDOUR::new_change()"
345 c = Change (change_bit);
346 change_bit <<= 1; // if it shifts too far, change_bit == 0
352 ARDOUR::get_ardour_revision ()
358 ARDOUR::get_user_ardour_path ()
363 if ((envvar = getenv ("HOME")) == 0 || strlen (envvar) == 0) {
368 path += "/.ardour2/";
370 /* create it if necessary */
372 if (g_mkdir_with_parents (path.c_str (), 0755)) {
380 ARDOUR::get_system_data_path ()
386 if ((envvar = getenv ("ARDOUR_DATA_PATH")) != 0) {
397 ARDOUR::get_system_module_path ()
402 if ((envvar = getenv ("ARDOUR_MODULE_PATH")) != 0) {
413 find_file (string name, string dir, string subdir = "")
416 char* envvar = getenv("ARDOUR_PATH");
418 /* 1st attempt: any directory in ARDOUR_PATH */
422 vector<string> split_path;
424 split (envvar, split_path, ':');
426 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
429 if (Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
430 // cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
436 /* 2nd attempt: ~/.ardour/ */
438 path = get_user_ardour_path();
440 if (subdir.length()) {
441 path += subdir + "/";
445 if (access (path.c_str(), R_OK) == 0) {
449 /* 3rd attempt: dir/... */
454 if (subdir.length()) {
455 path += subdir + "/";
460 if (access (path.c_str(), R_OK) == 0) {
468 ARDOUR::find_config_file (string name)
472 if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
476 return find_file (name, envvar);
480 ARDOUR::find_data_file (string name, string subdir)
483 if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
487 return find_file (name, envvar, subdir);
490 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
492 old = strdup (setlocale (LC_NUMERIC, NULL));
493 if (strcmp (old, str)) {
494 setlocale (LC_NUMERIC, str);
498 ARDOUR::LocaleGuard::~LocaleGuard ()
500 setlocale (LC_NUMERIC, old);
507 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
508 // valgrind doesn't understand this assembler stuff
509 // September 10th, 2007
513 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
518 /* XXX use real code to determine if the processor supports
519 DenormalsAreZero and FlushToZero
522 if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
526 MXCSR = _mm_getcsr();
528 switch (Config->get_denormal_model()) {
530 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
534 if (fpu.has_flush_to_zero()) {
535 MXCSR |= _MM_FLUSH_ZERO_ON;
540 MXCSR &= ~_MM_FLUSH_ZERO_ON;
541 if (fpu.has_denormals_are_zero()) {
547 if (fpu.has_flush_to_zero()) {
548 if (fpu.has_denormals_are_zero()) {
549 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
551 MXCSR |= _MM_FLUSH_ZERO_ON;
563 ARDOUR::coverage (nframes_t sa, nframes_t ea,
564 nframes_t sb, nframes_t eb)
566 /* OverlapType returned reflects how the second (B)
567 range overlaps the first (A).
569 The diagrams show various relative placements
570 of A and B for each OverlapType.
573 Internal: the start points cannot coincide
574 External: the start and end points can coincide
575 Start: end points can coincide
576 End: start points can coincide
578 XXX Logically, Internal should disallow end
583 |--------------------| A
585 |-----------------| B
592 if ((sb >= sa) && (eb <= ea)) {
594 if ((sb > sa) && (eb <= ea)) {
596 return OverlapInternal;
600 |--------------------| A
602 -----------------------| B
605 "B overlaps the start of A"
609 if ((eb >= sa) && (eb <= ea)) {
613 |---------------------| A
615 |----------------------- B
618 "B overlaps the end of A"
621 if ((sb >= sa) && (sb <= ea)) {
625 |--------------------| A
626 -------------------------- B
627 |----------------------- B
628 ----------------------| B
629 |--------------------| B
632 "B overlaps all of A"
634 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
635 return OverlapExternal;
641 /* not sure where to put these */
644 std::istream& int_to_type (std::istream& o, T& hf) {
651 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
652 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
653 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
654 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
655 std::istream& operator>>(std::istream& o, RemoteModel& var) { return int_to_type<RemoteModel> (o, var); }
656 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
657 std::istream& operator>>(std::istream& o, SoloModel& var) { return int_to_type<SoloModel> (o, var); }
658 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
659 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
660 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
661 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
662 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }
663 std::istream& operator>>(std::istream& o, SmpteFormat& var) { return int_to_type<SmpteFormat> (o, var); }
664 std::istream& operator>>(std::istream& o, DenormalModel& var) { return int_to_type<DenormalModel> (o, var); }