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.
18 $Id: globals.cc 935 2006-09-29 21:39:39Z paul $
21 #include <cstdio> // Needed so that libraptor (included in lrdf) won't complain
23 #include <sys/types.h>
34 #include <pbd/error.h>
36 #include <pbd/strsplit.h>
38 #include <midi++/port.h>
39 #include <midi++/port_request.h>
40 #include <midi++/manager.h>
41 #include <midi++/mmc.h>
43 #include <ardour/ardour.h>
44 #include <ardour/audio_library.h>
45 #include <ardour/configuration.h>
46 #include <ardour/plugin_manager.h>
47 #include <ardour/audiosource.h>
48 #include <ardour/utils.h>
49 #include <ardour/session.h>
50 #include <ardour/control_protocol_manager.h>
53 #include <ardour/osc.h>
56 #include <ardour/mix.h>
58 #if defined (__APPLE__)
59 #include <Carbon/Carbon.h> // For Gestalt
64 ARDOUR::Configuration* ARDOUR::Config = 0;
65 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
68 ARDOUR::OSC* ARDOUR::osc = 0;
71 using namespace ARDOUR;
75 MIDI::Port *default_mmc_port = 0;
76 MIDI::Port *default_mtc_port = 0;
77 MIDI::Port *default_midi_port = 0;
79 Change ARDOUR::StartChanged = ARDOUR::new_change ();
80 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
81 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
82 Change ARDOUR::NameChanged = ARDOUR::new_change ();
83 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
89 /* no real cost to creating this object, and it avoids
90 conditionals anywhere that uses it
93 osc = new OSC (Config->get_osc_port());
95 if (Config->get_use_osc ()) {
106 std::map<string,Configuration::MidiPortDescriptor*>::iterator i;
109 if ((nports = Config->midi_ports.size()) == 0) {
110 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
114 for (i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
115 Configuration::MidiPortDescriptor* port_descriptor;
117 port_descriptor = (*i).second;
119 MIDI::PortRequest request (port_descriptor->device,
120 port_descriptor->tag,
121 port_descriptor->mode,
122 port_descriptor->type);
124 if (request.status != MIDI::PortRequest::OK) {
125 error << string_compose(_("MIDI port specifications for \"%1\" are not understandable."), port_descriptor->tag) << endmsg;
129 MIDI::Manager::instance()->add_port (request);
136 /* More than one port, so try using specific names for each port */
138 map<string,Configuration::MidiPortDescriptor *>::iterator i;
140 if (Config->get_mmc_port_name() != N_("default")) {
141 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
144 if (Config->get_mtc_port_name() != N_("default")) {
145 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
148 if (Config->get_midi_port_name() != N_("default")) {
149 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
152 /* If that didn't work, just use the first listed port */
154 if (default_mmc_port == 0) {
155 default_mmc_port = MIDI::Manager::instance()->port (0);
158 if (default_mtc_port == 0) {
159 default_mtc_port = MIDI::Manager::instance()->port (0);
162 if (default_midi_port == 0) {
163 default_midi_port = MIDI::Manager::instance()->port (0);
168 /* Only one port described, so use it for both MTC and MMC */
170 default_mmc_port = MIDI::Manager::instance()->port (0);
171 default_mtc_port = default_mmc_port;
172 default_midi_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())
181 if (default_mtc_port == 0) {
182 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
186 if (default_midi_port == 0) {
187 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
195 setup_hardware_optimization (bool try_optimization)
197 bool generic_mix_functions = true;
199 if (try_optimization) {
201 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
203 unsigned int use_sse = 0;
205 #ifndef USE_X86_64_ASM
211 "andl $33554432, %%edx\n"
215 : "%eax", "%ecx", "%edx", "memory");
223 "andq $33554432, %%rdx\n"
227 : "%rax", "%rcx", "%rdx", "memory");
229 #endif /* USE_X86_64_ASM */
232 info << "Using SSE optimized routines" << endmsg;
235 Session::compute_peak = x86_sse_compute_peak;
236 Session::apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
237 Session::mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
238 Session::mix_buffers_no_gain = x86_sse_mix_buffers_no_gain;
240 generic_mix_functions = false;
244 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
247 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
250 if (sysVersion >= 0x00001040) { // Tiger at least
251 Session::compute_peak = veclib_compute_peak;
252 Session::apply_gain_to_buffer = veclib_apply_gain_to_buffer;
253 Session::mix_buffers_with_gain = veclib_mix_buffers_with_gain;
254 Session::mix_buffers_no_gain = veclib_mix_buffers_no_gain;
256 generic_mix_functions = false;
258 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
263 if (generic_mix_functions) {
265 Session::compute_peak = compute_peak;
266 Session::apply_gain_to_buffer = apply_gain_to_buffer;
267 Session::mix_buffers_with_gain = mix_buffers_with_gain;
268 Session::mix_buffers_no_gain = mix_buffers_no_gain;
270 info << "No H/W specific optimizations in use" << endmsg;
275 ARDOUR::init (bool use_vst, bool try_optimization)
277 extern void setup_enum_writer ();
279 (void) bindtextdomain(PACKAGE, LOCALEDIR);
283 setup_enum_writer ();
286 Library = new AudioLibrary;
288 Config = new Configuration;
290 if (Config->load_state ()) {
294 Config->set_use_vst (use_vst);
307 if (Config->get_use_vst() && fst_init ()) {
312 setup_hardware_optimization (try_optimization);
314 /* singleton - first object is "it" */
315 new PluginManager ();
317 /* singleton - first object is "it" */
318 new ControlProtocolManager ();
319 ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
322 if ((node = Config->control_protocol_state()) != 0) {
323 ControlProtocolManager::instance().set_state (*node);
326 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
336 delete &ControlProtocolManager::instance();
342 ARDOUR::get_microseconds ()
344 /* XXX need JACK to export its functionality */
347 gettimeofday (&now, 0);
348 return now.tv_sec * 1000000ULL + now.tv_usec;
352 ARDOUR::new_change ()
355 static uint32_t change_bit = 1;
357 /* catch out-of-range */
360 fatal << _("programming error: ")
361 << "change_bit out of range in ARDOUR::new_change()"
366 c = Change (change_bit);
367 change_bit <<= 1; // if it shifts too far, change_bit == 0
373 ARDOUR::get_ardour_revision ()
379 ARDOUR::get_user_ardour_path ()
384 if ((envvar = getenv ("HOME")) == 0 || strlen (envvar) == 0) {
389 path += "/.ardour2/";
391 /* create it if necessary */
393 if (g_mkdir_with_parents (path.c_str (), 0755)) {
401 ARDOUR::get_system_data_path ()
407 if ((envvar = getenv ("ARDOUR_DATA_PATH")) != 0) {
418 ARDOUR::get_system_module_path ()
423 if ((envvar = getenv ("ARDOUR_MODULE_PATH")) != 0) {
434 find_file (string name, string dir, string subdir = "")
437 char* envvar = getenv("ARDOUR_PATH");
439 /* 1st attempt: any directory in ARDOUR_PATH */
443 vector<string> split_path;
445 split (envvar, split_path, ':');
447 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
450 if (access (path.c_str(), R_OK) == 0) {
451 // cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
457 /* 2nd attempt: ~/.ardour/ */
459 path = get_user_ardour_path();
461 if (subdir.length()) {
462 path += subdir + "/";
466 if (access (path.c_str(), R_OK) == 0) {
470 /* 3rd attempt: dir/... */
475 if (subdir.length()) {
476 path += subdir + "/";
481 if (access (path.c_str(), R_OK) == 0) {
489 ARDOUR::find_config_file (string name)
493 if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
497 return find_file (name, envvar);
501 ARDOUR::find_data_file (string name, string subdir)
504 if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
508 return find_file (name, envvar, subdir);
511 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
513 old = strdup (setlocale (LC_NUMERIC, NULL));
514 if (strcmp (old, str)) {
515 setlocale (LC_NUMERIC, str);
519 ARDOUR::LocaleGuard::~LocaleGuard ()
521 setlocale (LC_NUMERIC, old);
526 ARDOUR::coverage (nframes_t sa, nframes_t ea,
527 nframes_t sb, nframes_t eb)
529 /* OverlapType returned reflects how the second (B)
530 range overlaps the first (A).
532 The diagrams show various relative placements
533 of A and B for each OverlapType.
536 Internal: the start points cannot coincide
537 External: the start and end points can coincide
538 Start: end points can coincide
539 End: start points can coincide
541 XXX Logically, Internal should disallow end
546 |--------------------| A
548 |-----------------| B
555 if ((sb >= sa) && (eb <= ea)) {
557 if ((sb > sa) && (eb <= ea)) {
559 return OverlapInternal;
563 |--------------------| A
565 -----------------------| B
568 "B overlaps the start of A"
572 if ((eb >= sa) && (eb <= ea)) {
576 |---------------------| A
578 |----------------------- B
581 "B overlaps the end of A"
584 if ((sb >= sa) && (sb <= ea)) {
588 |--------------------| A
589 -------------------------- B
590 |----------------------- B
591 ----------------------| B
592 |--------------------| B
595 "B overlaps all of A"
597 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
598 return OverlapExternal;
604 /* not sure where to put these */
607 std::istream& int_to_type (std::istream& o, T& hf) {
614 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
615 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
616 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
617 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
618 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
619 std::istream& operator>>(std::istream& o, SoloModel& var) { return int_to_type<SoloModel> (o, var); }
620 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
621 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
622 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
623 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
624 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }
625 std::istream& operator>>(std::istream& o, SmpteFormat& var) { return int_to_type<SmpteFormat> (o, var); }