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 <cstdio> // Needed so that libraptor (included in lrdf) won't complain
23 #include <sys/types.h>
34 #include <pbd/error.h>
35 #include <pbd/strsplit.h>
37 #include <midi++/port.h>
38 #include <midi++/port_request.h>
39 #include <midi++/manager.h>
40 #include <midi++/mmc.h>
42 #include <ardour/ardour.h>
43 #include <ardour/audio_library.h>
44 #include <ardour/configuration.h>
45 #include <ardour/plugin_manager.h>
46 #include <ardour/audiosource.h>
47 #include <ardour/utils.h>
48 #include <ardour/session.h>
49 #include <ardour/control_protocol_manager.h>
52 #include <ardour/osc.h>
55 #include <ardour/mix.h>
57 #if defined (__APPLE__)
58 #include <Carbon/Carbon.h> // For Gestalt
63 ARDOUR::Configuration* ARDOUR::Config = 0;
64 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
67 ARDOUR::OSC* ARDOUR::osc = 0;
70 using namespace ARDOUR;
74 MIDI::Port *default_mmc_port = 0;
75 MIDI::Port *default_mtc_port = 0;
76 MIDI::Port *default_midi_port = 0;
78 Change ARDOUR::StartChanged = ARDOUR::new_change ();
79 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
80 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
81 Change ARDOUR::NameChanged = ARDOUR::new_change ();
82 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
88 /* no real cost to creating this object, and it avoids
89 conditionals anywhere that uses it
92 osc = new OSC (Config->get_osc_port());
94 if (Config->get_use_osc ()) {
105 std::map<string,Configuration::MidiPortDescriptor*>::iterator i;
108 if ((nports = Config->midi_ports.size()) == 0) {
109 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
113 for (i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
114 Configuration::MidiPortDescriptor* port_descriptor;
116 port_descriptor = (*i).second;
118 MIDI::PortRequest request (port_descriptor->device,
119 port_descriptor->tag,
120 port_descriptor->mode,
121 port_descriptor->type);
123 if (request.status != MIDI::PortRequest::OK) {
124 error << string_compose(_("MIDI port specifications for \"%1\" are not understandable."), port_descriptor->tag) << endmsg;
128 MIDI::Manager::instance()->add_port (request);
133 /* More than one port, so try using specific names for each port */
135 map<string,Configuration::MidiPortDescriptor *>::iterator i;
137 if (Config->get_mmc_port_name() != N_("default")) {
138 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
141 if (Config->get_mtc_port_name() != N_("default")) {
142 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
145 if (Config->get_midi_port_name() != N_("default")) {
146 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
149 /* If that didn't work, just use the first listed port */
151 if (default_mmc_port == 0) {
152 default_mmc_port = MIDI::Manager::instance()->port (0);
155 if (default_mtc_port == 0) {
156 default_mtc_port = MIDI::Manager::instance()->port (0);
159 if (default_midi_port == 0) {
160 default_midi_port = MIDI::Manager::instance()->port (0);
165 /* Only one port described, so use it for both MTC and MMC */
167 default_mmc_port = MIDI::Manager::instance()->port (0);
168 default_mtc_port = default_mmc_port;
169 default_midi_port = default_mmc_port;
172 if (default_mmc_port == 0) {
173 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name())
178 if (default_mtc_port == 0) {
179 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
183 if (default_midi_port == 0) {
184 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
192 ARDOUR::init (AudioEngine& engine, bool use_vst, bool try_optimization)
194 bool generic_mix_functions = true;
196 (void) bindtextdomain(PACKAGE, LOCALEDIR);
198 Config = new Configuration;
200 if (Config->load_state ()) {
204 Config->set_use_vst (use_vst);
217 if (Config->get_use_vst() && fst_init ()) {
222 if (try_optimization) {
224 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
226 unsigned int use_sse = 0;
228 #ifndef USE_X86_64_ASM
234 "andl $33554432, %%edx\n"
238 : "%eax", "%ecx", "%edx", "memory");
246 "andq $33554432, %%rdx\n"
250 : "%rax", "%rcx", "%rdx", "memory");
252 #endif /* USE_X86_64_ASM */
255 cerr << "Enabling SSE optimized routines" << endl;
258 Session::compute_peak = x86_sse_compute_peak;
259 Session::apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
260 Session::mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
261 Session::mix_buffers_no_gain = x86_sse_mix_buffers_no_gain;
263 generic_mix_functions = false;
267 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
270 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
273 if (sysVersion >= 0x00001040) { // Tiger at least
274 Session::compute_peak = veclib_compute_peak;
275 Session::apply_gain_to_buffer = veclib_apply_gain_to_buffer;
276 Session::mix_buffers_with_gain = veclib_mix_buffers_with_gain;
277 Session::mix_buffers_no_gain = veclib_mix_buffers_no_gain;
279 generic_mix_functions = false;
281 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
286 if (generic_mix_functions) {
288 Session::compute_peak = compute_peak;
289 Session::apply_gain_to_buffer = apply_gain_to_buffer;
290 Session::mix_buffers_with_gain = mix_buffers_with_gain;
291 Session::mix_buffers_no_gain = mix_buffers_no_gain;
293 info << "No H/W specific optimizations in use" << endmsg;
297 Library = new AudioLibrary;
299 /* singleton - first object is "it" */
300 new PluginManager (engine);
302 /* singleton - first object is "it" */
303 new ControlProtocolManager ();
304 ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
307 if ((node = Config->control_protocol_state()) != 0) {
308 ControlProtocolManager::instance().set_state (*node);
311 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
321 delete &ControlProtocolManager::instance();
332 ARDOUR::new_change ()
335 static uint32_t change_bit = 1;
337 /* catch out-of-range */
340 fatal << _("programming error: ")
341 << "change_bit out of range in ARDOUR::new_change()"
346 c = Change (change_bit);
347 change_bit <<= 1; // if it shifts too far, change_bit == 0
353 ARDOUR::get_user_ardour_path ()
358 if ((envvar = getenv ("HOME")) == 0 || strlen (envvar) == 0) {
363 path += "/.ardour2/";
365 /* create it if necessary */
367 mkdir (path.c_str (), 0755);
373 ARDOUR::get_system_data_path ()
384 ARDOUR::get_system_module_path ()
395 find_file (string name, string dir, string subdir = "")
398 char* envvar = getenv("ARDOUR_PATH");
400 /* 1st attempt: any directory in ARDOUR_PATH */
404 vector<string> split_path;
406 split (envvar, split_path, ':');
408 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
411 if (access (path.c_str(), R_OK) == 0) {
412 // cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
418 /* 2nd attempt: ~/.ardour/ */
420 path = get_user_ardour_path();
422 if (subdir.length()) {
423 path += subdir + "/";
427 if (access (path.c_str(), R_OK) == 0) {
431 /* 3rd attempt: dir/... */
436 if (subdir.length()) {
437 path += subdir + "/";
442 if (access (path.c_str(), R_OK) == 0) {
450 ARDOUR::find_config_file (string name)
454 if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
458 return find_file (name, envvar);
462 ARDOUR::find_data_file (string name, string subdir)
465 if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
469 return find_file (name, envvar, subdir);
472 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
474 old = strdup (setlocale (LC_NUMERIC, NULL));
475 if (strcmp (old, str)) {
476 setlocale (LC_NUMERIC, str);
480 ARDOUR::LocaleGuard::~LocaleGuard ()
482 setlocale (LC_NUMERIC, old);
487 ARDOUR::coverage (jack_nframes_t sa, jack_nframes_t ea,
488 jack_nframes_t sb, jack_nframes_t eb)
490 /* OverlapType returned reflects how the second (B)
491 range overlaps the first (A).
493 The diagrams show various relative placements
494 of A and B for each OverlapType.
497 Internal: the start points cannot coincide
498 External: the start and end points can coincide
499 Start: end points can coincide
500 End: start points can coincide
502 XXX Logically, Internal should disallow end
507 |--------------------| A
509 |-----------------| B
516 if ((sb >= sa) && (eb <= ea)) {
518 if ((sb > sa) && (eb <= ea)) {
520 return OverlapInternal;
524 |--------------------| A
526 -----------------------| B
529 "B overlaps the start of A"
533 if ((eb >= sa) && (eb <= ea)) {
537 |---------------------| A
539 |----------------------- B
542 "B overlaps the end of A"
545 if ((sb >= sa) && (sb <= ea)) {
549 |--------------------| A
550 -------------------------- B
551 |----------------------- B
552 ----------------------| B
553 |--------------------| B
556 "B overlaps all of A"
558 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
559 return OverlapExternal;
565 /* not sure where to put these */
567 std::istream& operator>>(std::istream& o, HeaderFormat& hf) {
570 hf = (HeaderFormat) val;
574 std::istream& operator>>(std::istream& o, SampleFormat& sf) {
577 sf = (SampleFormat) val;