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/source.h>
47 #include <ardour/utils.h>
48 #include <ardour/session.h>
49 #include <ardour/control_protocol_manager.h>
51 #include <ardour/mix.h>
53 #if defined (__APPLE__)
54 #include <Carbon/Carbon.h> // For Gestalt
59 ARDOUR::Configuration* ARDOUR::Config = 0;
60 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
62 using namespace ARDOUR;
65 MIDI::Port *default_mmc_port = 0;
66 MIDI::Port *default_mtc_port = 0;
67 MIDI::Port *default_midi_port = 0;
69 Change ARDOUR::StartChanged = ARDOUR::new_change ();
70 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
71 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
72 Change ARDOUR::NameChanged = ARDOUR::new_change ();
73 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
78 std::map<string,Configuration::MidiPortDescriptor*>::iterator i;
81 if ((nports = Config->midi_ports.size()) == 0) {
82 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
86 for (i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
87 Configuration::MidiPortDescriptor* port_descriptor;
89 port_descriptor = (*i).second;
91 MIDI::PortRequest request (port_descriptor->device,
93 port_descriptor->mode,
94 port_descriptor->type);
96 if (request.status != MIDI::PortRequest::OK) {
97 error << string_compose(_("MIDI port specifications for \"%1\" are not understandable."), port_descriptor->tag) << endmsg;
101 MIDI::Manager::instance()->add_port (request);
106 /* More than one port, so try using specific names for each port */
108 map<string,Configuration::MidiPortDescriptor *>::iterator i;
110 if (Config->get_mmc_port_name() != N_("default")) {
111 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
114 if (Config->get_mtc_port_name() != N_("default")) {
115 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
118 if (Config->get_midi_port_name() != N_("default")) {
119 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
122 /* If that didn't work, just use the first listed port */
124 if (default_mmc_port == 0) {
125 default_mmc_port = MIDI::Manager::instance()->port (0);
128 if (default_mtc_port == 0) {
129 default_mtc_port = MIDI::Manager::instance()->port (0);
132 if (default_midi_port == 0) {
133 default_midi_port = MIDI::Manager::instance()->port (0);
138 /* Only one port described, so use it for both MTC and MMC */
140 default_mmc_port = MIDI::Manager::instance()->port (0);
141 default_mtc_port = default_mmc_port;
142 default_midi_port = default_mmc_port;
145 if (default_mmc_port == 0) {
146 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name())
151 if (default_mtc_port == 0) {
152 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
156 if (default_midi_port == 0) {
157 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
165 ARDOUR::init (AudioEngine& engine, bool use_vst, bool try_optimization, void (*sighandler)(int,siginfo_t*,void*))
167 bool generic_mix_functions = true;
169 (void) bindtextdomain(PACKAGE, LOCALEDIR);
171 Config = new Configuration;
173 if (Config->load_state ()) {
177 Config->set_use_vst (use_vst);
184 if (Config->get_use_vst() && fst_init (sighandler)) {
189 if (try_optimization) {
191 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
193 unsigned int use_sse = 0;
195 #ifndef USE_X86_64_ASM
201 "andl $33554432, %%edx\n"
205 : "%eax", "%ecx", "%edx", "memory");
213 "andq $33554432, %%rdx\n"
217 : "%rax", "%rcx", "%rdx", "memory");
219 #endif /* USE_X86_64_ASM */
222 cerr << "Enabling SSE optimized routines" << endl;
225 Session::compute_peak = x86_sse_compute_peak;
226 Session::apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
227 Session::mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
228 Session::mix_buffers_no_gain = x86_sse_mix_buffers_no_gain;
230 generic_mix_functions = false;
234 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
237 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
240 if (sysVersion >= 0x00001040) { // Tiger at least
241 Session::compute_peak = veclib_compute_peak;
242 Session::apply_gain_to_buffer = veclib_apply_gain_to_buffer;
243 Session::mix_buffers_with_gain = veclib_mix_buffers_with_gain;
244 Session::mix_buffers_no_gain = veclib_mix_buffers_no_gain;
246 generic_mix_functions = false;
248 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
253 if (generic_mix_functions) {
255 Session::compute_peak = compute_peak;
256 Session::apply_gain_to_buffer = apply_gain_to_buffer;
257 Session::mix_buffers_with_gain = mix_buffers_with_gain;
258 Session::mix_buffers_no_gain = mix_buffers_no_gain;
260 info << "No H/W specific optimizations in use" << endmsg;
264 Library = new AudioLibrary;
266 /* singleton - first object is "it" */
267 new PluginManager (engine);
269 /* singleton - first object is "it" */
270 new ControlProtocolManager ();
271 ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
273 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
283 delete &ControlProtocolManager::instance();
294 ARDOUR::new_change ()
297 static uint32_t change_bit = 1;
299 /* XXX catch out-of-range */
301 c = Change (change_bit);
308 ARDOUR::get_user_ardour_path ()
313 if ((envvar = getenv ("HOME")) == 0 || strlen (envvar) == 0) {
318 path += "/.ardour2/";
320 /* create it if necessary */
322 mkdir (path.c_str (), 0755);
328 ARDOUR::get_system_ardour_path ()
339 find_file (string name, string dir, string subdir = "")
342 char* envvar = getenv("ARDOUR_PATH");
344 /* 1st attempt: any directory in ARDOUR_PATH */
348 vector<string> split_path;
350 split (envvar, split_path, ':');
352 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
355 if (access (path.c_str(), R_OK) == 0) {
356 cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
362 /* 2nd attempt: ~/.ardour/ */
364 path = get_user_ardour_path();
366 if (subdir.length()) {
367 path += subdir + "/";
371 if (access (path.c_str(), R_OK) == 0) {
375 /* 3rd attempt: dir/... */
380 if (subdir.length()) {
381 path += subdir + "/";
386 if (access (path.c_str(), R_OK) == 0) {
394 ARDOUR::find_config_file (string name)
398 if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
402 return find_file (name, envvar);
406 ARDOUR::find_data_file (string name, string subdir)
409 if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
413 return find_file (name, envvar, subdir);
416 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
418 old = strdup (setlocale (LC_NUMERIC, NULL));
419 if (strcmp (old, str)) {
420 setlocale (LC_NUMERIC, str);
424 ARDOUR::LocaleGuard::~LocaleGuard ()
426 setlocale (LC_NUMERIC, old);
431 ARDOUR::coverage (jack_nframes_t sa, jack_nframes_t ea,
432 jack_nframes_t sb, jack_nframes_t eb)
434 /* OverlapType returned reflects how the second (B)
435 range overlaps the first (A).
437 The diagrams show various relative placements
438 of A and B for each OverlapType.
441 Internal: the start points cannot coincide
442 External: the start and end points can coincide
443 Start: end points can coincide
444 End: start points can coincide
446 XXX Logically, Internal should disallow end
451 |--------------------| A
453 |-----------------| B
460 if ((sb >= sa) && (eb <= ea)) {
462 if ((sb > sa) && (eb <= ea)) {
464 return OverlapInternal;
468 |--------------------| A
470 -----------------------| B
473 "B overlaps the start of A"
477 if ((eb >= sa) && (eb <= ea)) {
481 |---------------------| A
483 |----------------------- B
486 "B overlaps the end of A"
489 if ((sb >= sa) && (sb <= ea)) {
493 |--------------------| A
494 -------------------------- B
495 |----------------------- B
496 ----------------------| B
497 |--------------------| B
500 "B overlaps all of A"
502 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
503 return OverlapExternal;
509 /* not sure where to put these */
511 std::istream& operator>>(std::istream& o, HeaderFormat hf) {
514 hf = (HeaderFormat) val;
518 std::istream& operator>>(std::istream& o, SampleFormat sf) {
521 sf = (SampleFormat) val;