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>
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>
51 #include <ardour/audioengine.h>
54 #include <ardour/osc.h>
57 #include <ardour/mix.h>
59 #if defined (__APPLE__)
60 #include <Carbon/Carbon.h> // For Gestalt
65 ARDOUR::Configuration* ARDOUR::Config = 0;
66 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
69 ARDOUR::OSC* ARDOUR::osc = 0;
72 using namespace ARDOUR;
76 MIDI::Port *default_mmc_port = 0;
77 MIDI::Port *default_mtc_port = 0;
78 MIDI::Port *default_midi_port = 0;
80 Change ARDOUR::StartChanged = ARDOUR::new_change ();
81 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
82 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
83 Change ARDOUR::NameChanged = ARDOUR::new_change ();
84 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
90 /* no real cost to creating this object, and it avoids
91 conditionals anywhere that uses it
94 osc = new OSC (Config->get_osc_port());
96 if (Config->get_use_osc ()) {
105 setup_midi (AudioEngine& engine )
107 std::map<string,Configuration::MidiPortDescriptor*>::iterator i;
110 if ((nports = Config->midi_ports.size()) == 0) {
111 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
115 MIDI::Manager::instance()->set_api_data(engine.jack());
117 for (i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
118 Configuration::MidiPortDescriptor* port_descriptor;
120 port_descriptor = (*i).second;
122 MIDI::PortRequest request (port_descriptor->device,
123 port_descriptor->tag,
124 port_descriptor->mode,
125 port_descriptor->type);
127 if (request.status != MIDI::PortRequest::OK) {
128 error << string_compose(_("MIDI port specifications for \"%1\" are not understandable."), port_descriptor->tag) << endmsg;
132 MIDI::Manager::instance()->add_port (request);
137 /* More than one port, so try using specific names for each port */
139 map<string,Configuration::MidiPortDescriptor *>::iterator i;
141 if (Config->get_mmc_port_name() != N_("default")) {
142 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
145 if (Config->get_mtc_port_name() != N_("default")) {
146 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
149 if (Config->get_midi_port_name() != N_("default")) {
150 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
153 /* If that didn't work, just use the first listed port */
155 if (default_mmc_port == 0) {
156 default_mmc_port = MIDI::Manager::instance()->port (0);
159 if (default_mtc_port == 0) {
160 default_mtc_port = MIDI::Manager::instance()->port (0);
163 if (default_midi_port == 0) {
164 default_midi_port = MIDI::Manager::instance()->port (0);
169 /* Only one port described, so use it for both MTC and MMC */
171 default_mmc_port = MIDI::Manager::instance()->port (0);
172 default_mtc_port = default_mmc_port;
173 default_midi_port = default_mmc_port;
176 if (default_mmc_port == 0) {
177 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name())
182 if (default_mtc_port == 0) {
183 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
187 if (default_midi_port == 0) {
188 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
196 ARDOUR::init (AudioEngine& engine, bool use_vst, bool try_optimization)
198 bool generic_mix_functions = true;
200 (void) bindtextdomain(PACKAGE, LOCALEDIR);
204 Config = new Configuration;
206 if (Config->load_state ()) {
210 Config->set_use_vst (use_vst);
212 if (setup_midi (engine)) {
223 if (Config->get_use_vst() && fst_init ()) {
228 if (try_optimization) {
230 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
232 unsigned int use_sse = 0;
234 #ifndef USE_X86_64_ASM
240 "andl $33554432, %%edx\n"
244 : "%eax", "%ecx", "%edx", "memory");
252 "andq $33554432, %%rdx\n"
256 : "%rax", "%rcx", "%rdx", "memory");
258 #endif /* USE_X86_64_ASM */
261 cerr << "Enabling SSE optimized routines" << endl;
264 Session::compute_peak = x86_sse_compute_peak;
265 Session::apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
266 Session::mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
267 Session::mix_buffers_no_gain = x86_sse_mix_buffers_no_gain;
269 generic_mix_functions = false;
273 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
276 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
279 if (sysVersion >= 0x00001040) { // Tiger at least
280 Session::compute_peak = veclib_compute_peak;
281 Session::apply_gain_to_buffer = veclib_apply_gain_to_buffer;
282 Session::mix_buffers_with_gain = veclib_mix_buffers_with_gain;
283 Session::mix_buffers_no_gain = veclib_mix_buffers_no_gain;
285 generic_mix_functions = false;
287 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
292 if (generic_mix_functions) {
294 Session::compute_peak = compute_peak;
295 Session::apply_gain_to_buffer = apply_gain_to_buffer;
296 Session::mix_buffers_with_gain = mix_buffers_with_gain;
297 Session::mix_buffers_no_gain = mix_buffers_no_gain;
299 info << "No H/W specific optimizations in use" << endmsg;
303 Library = new AudioLibrary;
305 /* singleton - first object is "it" */
306 new PluginManager (engine);
308 /* singleton - first object is "it" */
309 new ControlProtocolManager ();
310 ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
313 if ((node = Config->control_protocol_state()) != 0) {
314 ControlProtocolManager::instance().set_state (*node);
317 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
327 delete &ControlProtocolManager::instance();
333 ARDOUR::get_microseconds ()
335 /* XXX need JACK to export its functionality */
338 gettimeofday (&now, 0);
339 return now.tv_sec * 1000000ULL + now.tv_usec;
343 ARDOUR::new_change ()
346 static uint32_t change_bit = 1;
348 /* catch out-of-range */
351 fatal << _("programming error: ")
352 << "change_bit out of range in ARDOUR::new_change()"
357 c = Change (change_bit);
358 change_bit <<= 1; // if it shifts too far, change_bit == 0
364 ARDOUR::get_user_ardour_path ()
369 if ((envvar = getenv ("HOME")) == 0 || strlen (envvar) == 0) {
374 path += "/.ardour2/";
376 /* create it if necessary */
378 mkdir (path.c_str (), 0755);
384 ARDOUR::get_system_data_path ()
395 ARDOUR::get_system_module_path ()
406 find_file (string name, string dir, string subdir = "")
409 char* envvar = getenv("ARDOUR_PATH");
411 /* 1st attempt: any directory in ARDOUR_PATH */
415 vector<string> split_path;
417 split (envvar, split_path, ':');
419 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
422 if (access (path.c_str(), R_OK) == 0) {
423 // cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
429 /* 2nd attempt: ~/.ardour/ */
431 path = get_user_ardour_path();
433 if (subdir.length()) {
434 path += subdir + "/";
438 if (access (path.c_str(), R_OK) == 0) {
442 /* 3rd attempt: dir/... */
447 if (subdir.length()) {
448 path += subdir + "/";
453 if (access (path.c_str(), R_OK) == 0) {
461 ARDOUR::find_config_file (string name)
465 if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
469 return find_file (name, envvar);
473 ARDOUR::find_data_file (string name, string subdir)
476 if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
480 return find_file (name, envvar, subdir);
483 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
485 old = strdup (setlocale (LC_NUMERIC, NULL));
486 if (strcmp (old, str)) {
487 setlocale (LC_NUMERIC, str);
491 ARDOUR::LocaleGuard::~LocaleGuard ()
493 setlocale (LC_NUMERIC, old);
498 ARDOUR::coverage (jack_nframes_t sa, jack_nframes_t ea,
499 jack_nframes_t sb, jack_nframes_t eb)
501 /* OverlapType returned reflects how the second (B)
502 range overlaps the first (A).
504 The diagrams show various relative placements
505 of A and B for each OverlapType.
508 Internal: the start points cannot coincide
509 External: the start and end points can coincide
510 Start: end points can coincide
511 End: start points can coincide
513 XXX Logically, Internal should disallow end
518 |--------------------| A
520 |-----------------| B
527 if ((sb >= sa) && (eb <= ea)) {
529 if ((sb > sa) && (eb <= ea)) {
531 return OverlapInternal;
535 |--------------------| A
537 -----------------------| B
540 "B overlaps the start of A"
544 if ((eb >= sa) && (eb <= ea)) {
548 |---------------------| A
550 |----------------------- B
553 "B overlaps the end of A"
556 if ((sb >= sa) && (sb <= ea)) {
560 |--------------------| A
561 -------------------------- B
562 |----------------------- B
563 ----------------------| B
564 |--------------------| B
567 "B overlaps all of A"
569 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
570 return OverlapExternal;
576 /* not sure where to put these */
578 std::istream& operator>>(std::istream& o, HeaderFormat& hf) {
581 hf = (HeaderFormat) val;
585 std::istream& operator>>(std::istream& o, SampleFormat& sf) {
588 sf = (SampleFormat) val;