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>
37 #include <pbd/error.h>
39 #include <pbd/strsplit.h>
42 #include <midi++/port.h>
43 #include <midi++/port_request.h>
44 #include <midi++/manager.h>
45 #include <midi++/mmc.h>
47 #include <ardour/ardour.h>
48 #include <ardour/audio_library.h>
49 #include <ardour/configuration.h>
50 #include <ardour/profile.h>
51 #include <ardour/plugin_manager.h>
52 #include <ardour/audiosource.h>
53 #include <ardour/utils.h>
54 #include <ardour/session.h>
55 #include <ardour/control_protocol_manager.h>
58 #include <ardour/osc.h>
61 #include <ardour/mix.h>
63 #if defined (__APPLE__)
64 #include <Carbon/Carbon.h> // For Gestalt
69 ARDOUR::Configuration* ARDOUR::Config = 0;
70 ARDOUR::RuntimeProfile* ARDOUR::Profile = 0;
71 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
74 ARDOUR::OSC* ARDOUR::osc = 0;
77 using namespace ARDOUR;
81 MIDI::Port *default_mmc_port = 0;
82 MIDI::Port *default_mtc_port = 0;
83 MIDI::Port *default_midi_port = 0;
85 Change ARDOUR::StartChanged = ARDOUR::new_change ();
86 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
87 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
88 Change ARDOUR::NameChanged = ARDOUR::new_change ();
89 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
95 /* no real cost to creating this object, and it avoids
96 conditionals anywhere that uses it
99 osc = new OSC (Config->get_osc_port());
101 if (Config->get_use_osc ()) {
102 return osc->start ();
112 std::map<string,Configuration::MidiPortDescriptor*>::iterator i;
115 if ((nports = Config->midi_ports.size()) == 0) {
116 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
120 for (i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
121 Configuration::MidiPortDescriptor* port_descriptor;
123 port_descriptor = (*i).second;
125 MIDI::PortRequest request (port_descriptor->device,
126 port_descriptor->tag,
127 port_descriptor->mode,
128 port_descriptor->type);
130 if (request.status != MIDI::PortRequest::OK) {
131 error << string_compose(_("MIDI port specifications for \"%1\" are not understandable."), port_descriptor->tag) << endmsg;
135 MIDI::Manager::instance()->add_port (request);
142 /* More than one port, so try using specific names for each port */
144 map<string,Configuration::MidiPortDescriptor *>::iterator i;
146 if (Config->get_mmc_port_name() != N_("default")) {
147 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
150 if (Config->get_mtc_port_name() != N_("default")) {
151 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
154 if (Config->get_midi_port_name() != N_("default")) {
155 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
158 /* If that didn't work, just use the first listed port */
160 if (default_mmc_port == 0) {
161 default_mmc_port = MIDI::Manager::instance()->port (0);
164 if (default_mtc_port == 0) {
165 default_mtc_port = MIDI::Manager::instance()->port (0);
168 if (default_midi_port == 0) {
169 default_midi_port = MIDI::Manager::instance()->port (0);
174 /* Only one port described, so use it for both MTC and MMC */
176 default_mmc_port = MIDI::Manager::instance()->port (0);
177 default_mtc_port = default_mmc_port;
178 default_midi_port = default_mmc_port;
181 if (default_mmc_port == 0) {
182 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name())
187 if (default_mtc_port == 0) {
188 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
192 if (default_midi_port == 0) {
193 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
201 setup_hardware_optimization (bool try_optimization)
203 bool generic_mix_functions = true;
206 if (try_optimization) {
208 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
212 info << "Using SSE optimized routines" << endmsg;
215 Session::compute_peak = x86_sse_compute_peak;
216 Session::find_peaks = x86_sse_find_peaks;
217 Session::apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
218 Session::mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
219 Session::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 Session::compute_peak = veclib_compute_peak;
233 Session::find_peaks = veclib_find_peaks;
234 Session::apply_gain_to_buffer = veclib_apply_gain_to_buffer;
235 Session::mix_buffers_with_gain = veclib_mix_buffers_with_gain;
236 Session::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;
245 if (generic_mix_functions) {
247 Session::compute_peak = compute_peak;
248 Session::find_peaks = find_peaks;
249 Session::apply_gain_to_buffer = apply_gain_to_buffer;
250 Session::mix_buffers_with_gain = mix_buffers_with_gain;
251 Session::mix_buffers_no_gain = mix_buffers_no_gain;
253 info << "No H/W specific optimizations in use" << endmsg;
261 ARDOUR::init (bool use_vst, bool try_optimization)
263 extern void setup_enum_writer ();
265 (void) bindtextdomain(PACKAGE, LOCALEDIR);
267 setup_enum_writer ();
270 Library = new AudioLibrary;
272 Config = new Configuration;
274 if (Config->load_state ()) {
278 Config->set_use_vst (use_vst);
280 Profile = new RuntimeProfile;
293 if (Config->get_use_vst() && fst_init ()) {
298 setup_hardware_optimization (try_optimization);
300 /* singleton - first object is "it" */
301 new PluginManager ();
303 /* singleton - first object is "it" */
304 new ControlProtocolManager ();
305 ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
308 if ((node = Config->control_protocol_state()) != 0) {
309 ControlProtocolManager::instance().set_state (*node);
312 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
322 delete &ControlProtocolManager::instance();
328 ARDOUR::get_microseconds ()
330 /* XXX need JACK to export its functionality */
333 gettimeofday (&now, 0);
334 return now.tv_sec * 1000000ULL + now.tv_usec;
338 ARDOUR::new_change ()
341 static uint32_t change_bit = 1;
343 /* catch out-of-range */
346 fatal << _("programming error: ")
347 << "change_bit out of range in ARDOUR::new_change()"
352 c = Change (change_bit);
353 change_bit <<= 1; // if it shifts too far, change_bit == 0
359 ARDOUR::get_ardour_revision ()
365 ARDOUR::get_user_ardour_path ()
370 if ((envvar = getenv ("HOME")) == 0 || strlen (envvar) == 0) {
375 path += "/.ardour2/";
377 /* create it if necessary */
379 if (g_mkdir_with_parents (path.c_str (), 0755)) {
387 ARDOUR::get_system_data_path ()
393 if ((envvar = getenv ("ARDOUR_DATA_PATH")) != 0) {
404 ARDOUR::get_system_module_path ()
409 if ((envvar = getenv ("ARDOUR_MODULE_PATH")) != 0) {
420 find_file (string name, string dir, string subdir = "")
423 char* envvar = getenv("ARDOUR_PATH");
425 /* 1st attempt: any directory in ARDOUR_PATH */
429 vector<string> split_path;
431 split (envvar, split_path, ':');
433 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
436 if (access (path.c_str(), R_OK) == 0) {
437 // cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
443 /* 2nd attempt: ~/.ardour/ */
445 path = get_user_ardour_path();
447 if (subdir.length()) {
448 path += subdir + "/";
452 if (access (path.c_str(), R_OK) == 0) {
456 /* 3rd attempt: dir/... */
461 if (subdir.length()) {
462 path += subdir + "/";
467 if (access (path.c_str(), R_OK) == 0) {
475 ARDOUR::find_config_file (string name)
479 if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
483 return find_file (name, envvar);
487 ARDOUR::find_data_file (string name, string subdir)
490 if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
494 return find_file (name, envvar, subdir);
497 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
499 old = strdup (setlocale (LC_NUMERIC, NULL));
500 if (strcmp (old, str)) {
501 setlocale (LC_NUMERIC, str);
505 ARDOUR::LocaleGuard::~LocaleGuard ()
507 setlocale (LC_NUMERIC, old);
514 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
519 /* XXX use real code to determine if the processor supports
520 DenormalsAreZero and FlushToZero
523 if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
527 MXCSR = _mm_getcsr();
529 switch (Config->get_denormal_model()) {
531 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
535 if (fpu.has_flush_to_zero()) {
536 MXCSR |= _MM_FLUSH_ZERO_ON;
541 MXCSR &= ~_MM_FLUSH_ZERO_ON;
542 if (fpu.has_denormals_are_zero()) {
548 if (fpu.has_flush_to_zero()) {
549 if (fpu.has_denormals_are_zero()) {
550 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
552 MXCSR |= _MM_FLUSH_ZERO_ON;
564 ARDOUR::coverage (nframes_t sa, nframes_t ea,
565 nframes_t sb, nframes_t eb)
567 /* OverlapType returned reflects how the second (B)
568 range overlaps the first (A).
570 The diagrams show various relative placements
571 of A and B for each OverlapType.
574 Internal: the start points cannot coincide
575 External: the start and end points can coincide
576 Start: end points can coincide
577 End: start points can coincide
579 XXX Logically, Internal should disallow end
584 |--------------------| A
586 |-----------------| B
593 if ((sb >= sa) && (eb <= ea)) {
595 if ((sb > sa) && (eb <= ea)) {
597 return OverlapInternal;
601 |--------------------| A
603 -----------------------| B
606 "B overlaps the start of A"
610 if ((eb >= sa) && (eb <= ea)) {
614 |---------------------| A
616 |----------------------- B
619 "B overlaps the end of A"
622 if ((sb >= sa) && (sb <= ea)) {
626 |--------------------| A
627 -------------------------- B
628 |----------------------- B
629 ----------------------| B
630 |--------------------| B
633 "B overlaps all of A"
635 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
636 return OverlapExternal;
642 /* not sure where to put these */
645 std::istream& int_to_type (std::istream& o, T& hf) {
652 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
653 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
654 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
655 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
656 std::istream& operator>>(std::istream& o, RemoteModel& var) { return int_to_type<RemoteModel> (o, var); }
657 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
658 std::istream& operator>>(std::istream& o, SoloModel& var) { return int_to_type<SoloModel> (o, var); }
659 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
660 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
661 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
662 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
663 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }
664 std::istream& operator>>(std::istream& o, SmpteFormat& var) { return int_to_type<SmpteFormat> (o, var); }
665 std::istream& operator>>(std::istream& o, DenormalModel& var) { return int_to_type<DenormalModel> (o, var); }