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>
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;
73 MIDI::Port *default_mmc_port = 0;
74 MIDI::Port *default_mtc_port = 0;
75 MIDI::Port *default_midi_port = 0;
77 Change ARDOUR::StartChanged = ARDOUR::new_change ();
78 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
79 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
80 Change ARDOUR::NameChanged = ARDOUR::new_change ();
81 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
87 /* no real cost to creating this object, and it avoids
88 conditionals anywhere that uses it
91 osc = new OSC (Config->get_osc_port());
93 if (Config->get_use_osc ()) {
104 std::map<string,Configuration::MidiPortDescriptor*>::iterator i;
107 if ((nports = Config->midi_ports.size()) == 0) {
108 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
112 for (i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
113 Configuration::MidiPortDescriptor* port_descriptor;
115 port_descriptor = (*i).second;
117 MIDI::PortRequest request (port_descriptor->device,
118 port_descriptor->tag,
119 port_descriptor->mode,
120 port_descriptor->type);
122 if (request.status != MIDI::PortRequest::OK) {
123 error << string_compose(_("MIDI port specifications for \"%1\" are not understandable."), port_descriptor->tag) << endmsg;
127 MIDI::Manager::instance()->add_port (request);
132 /* More than one port, so try using specific names for each port */
134 map<string,Configuration::MidiPortDescriptor *>::iterator i;
136 if (Config->get_mmc_port_name() != N_("default")) {
137 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
140 if (Config->get_mtc_port_name() != N_("default")) {
141 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
144 if (Config->get_midi_port_name() != N_("default")) {
145 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
148 /* If that didn't work, just use the first listed port */
150 if (default_mmc_port == 0) {
151 default_mmc_port = MIDI::Manager::instance()->port (0);
154 if (default_mtc_port == 0) {
155 default_mtc_port = MIDI::Manager::instance()->port (0);
158 if (default_midi_port == 0) {
159 default_midi_port = MIDI::Manager::instance()->port (0);
164 /* Only one port described, so use it for both MTC and MMC */
166 default_mmc_port = MIDI::Manager::instance()->port (0);
167 default_mtc_port = default_mmc_port;
168 default_midi_port = default_mmc_port;
171 if (default_mmc_port == 0) {
172 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name())
177 if (default_mtc_port == 0) {
178 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
182 if (default_midi_port == 0) {
183 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
191 ARDOUR::init (AudioEngine& engine, bool use_vst, bool try_optimization, void (*sighandler)(int,siginfo_t*,void*))
193 bool generic_mix_functions = true;
195 (void) bindtextdomain(PACKAGE, LOCALEDIR);
197 Config = new Configuration;
199 if (Config->load_state ()) {
203 Config->set_use_vst (use_vst);
216 if (Config->get_use_vst() && fst_init (sighandler)) {
221 if (try_optimization) {
223 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
225 unsigned int use_sse = 0;
227 #ifndef USE_X86_64_ASM
233 "andl $33554432, %%edx\n"
237 : "%eax", "%ecx", "%edx", "memory");
245 "andq $33554432, %%rdx\n"
249 : "%rax", "%rcx", "%rdx", "memory");
251 #endif /* USE_X86_64_ASM */
254 cerr << "Enabling SSE optimized routines" << endl;
257 Session::compute_peak = x86_sse_compute_peak;
258 Session::apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
259 Session::mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
260 Session::mix_buffers_no_gain = x86_sse_mix_buffers_no_gain;
262 generic_mix_functions = false;
266 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
269 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
272 if (sysVersion >= 0x00001040) { // Tiger at least
273 Session::compute_peak = veclib_compute_peak;
274 Session::apply_gain_to_buffer = veclib_apply_gain_to_buffer;
275 Session::mix_buffers_with_gain = veclib_mix_buffers_with_gain;
276 Session::mix_buffers_no_gain = veclib_mix_buffers_no_gain;
278 generic_mix_functions = false;
280 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
285 if (generic_mix_functions) {
287 Session::compute_peak = compute_peak;
288 Session::apply_gain_to_buffer = apply_gain_to_buffer;
289 Session::mix_buffers_with_gain = mix_buffers_with_gain;
290 Session::mix_buffers_no_gain = mix_buffers_no_gain;
292 info << "No H/W specific optimizations in use" << endmsg;
296 Library = new AudioLibrary;
298 /* singleton - first object is "it" */
299 new PluginManager (engine);
301 /* singleton - first object is "it" */
302 new ControlProtocolManager ();
303 ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
306 if ((node = Config->control_protocol_state()) != 0) {
307 ControlProtocolManager::instance().set_state (*node);
310 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
320 delete &ControlProtocolManager::instance();
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_user_ardour_path ()
357 if ((envvar = getenv ("HOME")) == 0 || strlen (envvar) == 0) {
362 path += "/.ardour2/";
364 /* create it if necessary */
366 mkdir (path.c_str (), 0755);
372 ARDOUR::get_system_data_path ()
383 ARDOUR::get_system_module_path ()
394 find_file (string name, string dir, string subdir = "")
397 char* envvar = getenv("ARDOUR_PATH");
399 /* 1st attempt: any directory in ARDOUR_PATH */
403 vector<string> split_path;
405 split (envvar, split_path, ':');
407 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
410 if (access (path.c_str(), R_OK) == 0) {
411 // cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
417 /* 2nd attempt: ~/.ardour/ */
419 path = get_user_ardour_path();
421 if (subdir.length()) {
422 path += subdir + "/";
426 if (access (path.c_str(), R_OK) == 0) {
430 /* 3rd attempt: dir/... */
435 if (subdir.length()) {
436 path += subdir + "/";
441 if (access (path.c_str(), R_OK) == 0) {
449 ARDOUR::find_config_file (string name)
453 if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
457 return find_file (name, envvar);
461 ARDOUR::find_data_file (string name, string subdir)
464 if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
468 return find_file (name, envvar, subdir);
471 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
473 old = strdup (setlocale (LC_NUMERIC, NULL));
474 if (strcmp (old, str)) {
475 setlocale (LC_NUMERIC, str);
479 ARDOUR::LocaleGuard::~LocaleGuard ()
481 setlocale (LC_NUMERIC, old);
486 ARDOUR::coverage (jack_nframes_t sa, jack_nframes_t ea,
487 jack_nframes_t sb, jack_nframes_t eb)
489 /* OverlapType returned reflects how the second (B)
490 range overlaps the first (A).
492 The diagrams show various relative placements
493 of A and B for each OverlapType.
496 Internal: the start points cannot coincide
497 External: the start and end points can coincide
498 Start: end points can coincide
499 End: start points can coincide
501 XXX Logically, Internal should disallow end
506 |--------------------| A
508 |-----------------| B
515 if ((sb >= sa) && (eb <= ea)) {
517 if ((sb > sa) && (eb <= ea)) {
519 return OverlapInternal;
523 |--------------------| A
525 -----------------------| B
528 "B overlaps the start of A"
532 if ((eb >= sa) && (eb <= ea)) {
536 |---------------------| A
538 |----------------------- B
541 "B overlaps the end of A"
544 if ((sb >= sa) && (sb <= ea)) {
548 |--------------------| A
549 -------------------------- B
550 |----------------------- B
551 ----------------------| B
552 |--------------------| B
555 "B overlaps all of A"
557 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
558 return OverlapExternal;
564 /* not sure where to put these */
566 std::istream& operator>>(std::istream& o, HeaderFormat& hf) {
569 hf = (HeaderFormat) val;
573 std::istream& operator>>(std::istream& o, SampleFormat& sf) {
576 sf = (SampleFormat) val;