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.
19 #include <cstdio> // Needed so that libraptor (included in lrdf) won't complain
21 #include <sys/types.h>
32 #include <pbd/error.h>
34 #include <pbd/strsplit.h>
36 #include <midi++/port.h>
37 #include <midi++/port_request.h>
38 #include <midi++/manager.h>
39 #include <midi++/mmc.h>
41 #include <ardour/ardour.h>
42 #include <ardour/audio_library.h>
43 #include <ardour/configuration.h>
44 #include <ardour/plugin_manager.h>
45 #include <ardour/audiosource.h>
46 #include <ardour/utils.h>
47 #include <ardour/session.h>
48 #include <ardour/control_protocol_manager.h>
49 #include <ardour/audioengine.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 ()) {
103 setup_midi (AudioEngine& engine )
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 MIDI::Manager::instance()->set_api_data(engine.jack());
115 for (i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
116 Configuration::MidiPortDescriptor* port_descriptor;
118 port_descriptor = (*i).second;
120 MIDI::PortRequest request (port_descriptor->device,
121 port_descriptor->tag,
122 port_descriptor->mode,
123 port_descriptor->type);
125 if (request.status != MIDI::PortRequest::OK) {
126 error << string_compose(_("MIDI port specifications for \"%1\" are not understandable."), port_descriptor->tag) << endmsg;
130 MIDI::Manager::instance()->add_port (request);
135 /* More than one port, so try using specific names for each port */
137 map<string,Configuration::MidiPortDescriptor *>::iterator i;
139 if (Config->get_mmc_port_name() != N_("default")) {
140 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
143 if (Config->get_mtc_port_name() != N_("default")) {
144 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
147 if (Config->get_midi_port_name() != N_("default")) {
148 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
151 /* If that didn't work, just use the first listed port */
153 if (default_mmc_port == 0) {
154 default_mmc_port = MIDI::Manager::instance()->port (0);
157 if (default_mtc_port == 0) {
158 default_mtc_port = MIDI::Manager::instance()->port (0);
161 if (default_midi_port == 0) {
162 default_midi_port = MIDI::Manager::instance()->port (0);
167 /* Only one port described, so use it for both MTC and MMC */
169 default_mmc_port = MIDI::Manager::instance()->port (0);
170 default_mtc_port = default_mmc_port;
171 default_midi_port = default_mmc_port;
174 if (default_mmc_port == 0) {
175 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name())
180 if (default_mtc_port == 0) {
181 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
185 if (default_midi_port == 0) {
186 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
194 ARDOUR::init (ARDOUR::AudioEngine& engine, bool use_vst, bool try_optimization)
196 bool generic_mix_functions = true;
198 (void) bindtextdomain(PACKAGE, LOCALEDIR);
203 Library = new AudioLibrary;
205 Config = new Configuration;
207 if (Config->load_state ()) {
211 Config->set_use_vst (use_vst);
213 if (setup_midi (engine)) {
224 if (Config->get_use_vst() && fst_init ()) {
229 if (try_optimization) {
231 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
233 unsigned int use_sse = 0;
235 #ifndef USE_X86_64_ASM
241 "andl $33554432, %%edx\n"
245 : "%eax", "%ecx", "%edx", "memory");
253 "andq $33554432, %%rdx\n"
257 : "%rax", "%rcx", "%rdx", "memory");
259 #endif /* USE_X86_64_ASM */
262 cerr << "Enabling SSE optimized routines" << endl;
265 Session::compute_peak = x86_sse_compute_peak;
266 Session::apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
267 Session::mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
268 Session::mix_buffers_no_gain = x86_sse_mix_buffers_no_gain;
270 generic_mix_functions = false;
274 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
277 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
280 if (sysVersion >= 0x00001040) { // Tiger at least
281 Session::compute_peak = veclib_compute_peak;
282 Session::apply_gain_to_buffer = veclib_apply_gain_to_buffer;
283 Session::mix_buffers_with_gain = veclib_mix_buffers_with_gain;
284 Session::mix_buffers_no_gain = veclib_mix_buffers_no_gain;
286 generic_mix_functions = false;
288 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
293 if (generic_mix_functions) {
295 Session::compute_peak = compute_peak;
296 Session::apply_gain_to_buffer = apply_gain_to_buffer;
297 Session::mix_buffers_with_gain = mix_buffers_with_gain;
298 Session::mix_buffers_no_gain = mix_buffers_no_gain;
300 info << "No H/W specific optimizations in use" << endmsg;
303 /* singleton - first object is "it" */
304 new PluginManager ();
306 /* singleton - first object is "it" */
307 new ControlProtocolManager ();
308 ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
311 if ((node = Config->control_protocol_state()) != 0) {
312 ControlProtocolManager::instance().set_state (*node);
315 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
325 delete &ControlProtocolManager::instance();
331 ARDOUR::get_microseconds ()
333 /* XXX need JACK to export its functionality */
336 gettimeofday (&now, 0);
337 return now.tv_sec * 1000000ULL + now.tv_usec;
341 ARDOUR::new_change ()
344 static uint32_t change_bit = 1;
346 /* catch out-of-range */
349 fatal << _("programming error: ")
350 << "change_bit out of range in ARDOUR::new_change()"
355 c = Change (change_bit);
356 change_bit <<= 1; // if it shifts too far, change_bit == 0
362 ARDOUR::get_ardour_revision ()
368 ARDOUR::get_user_ardour_path ()
373 if ((envvar = getenv ("HOME")) == 0 || strlen (envvar) == 0) {
378 path += "/.ardour2/";
380 /* create it if necessary */
382 if (g_mkdir_with_parents (path.c_str (), 0755)) {
390 ARDOUR::get_system_data_path ()
401 ARDOUR::get_system_module_path ()
412 find_file (string name, string dir, string subdir = "")
415 char* envvar = getenv("ARDOUR_PATH");
417 /* 1st attempt: any directory in ARDOUR_PATH */
421 vector<string> split_path;
423 split (envvar, split_path, ':');
425 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
428 if (access (path.c_str(), R_OK) == 0) {
429 // cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
435 /* 2nd attempt: ~/.ardour/ */
437 path = get_user_ardour_path();
439 if (subdir.length()) {
440 path += subdir + "/";
444 if (access (path.c_str(), R_OK) == 0) {
448 /* 3rd attempt: dir/... */
453 if (subdir.length()) {
454 path += subdir + "/";
459 if (access (path.c_str(), R_OK) == 0) {
467 ARDOUR::find_config_file (string name)
471 if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
475 return find_file (name, envvar);
479 ARDOUR::find_data_file (string name, string subdir)
482 if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
486 return find_file (name, envvar, subdir);
489 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
491 old = strdup (setlocale (LC_NUMERIC, NULL));
492 if (strcmp (old, str)) {
493 setlocale (LC_NUMERIC, str);
497 ARDOUR::LocaleGuard::~LocaleGuard ()
499 setlocale (LC_NUMERIC, old);
504 ARDOUR::coverage (nframes_t sa, nframes_t ea,
505 nframes_t sb, nframes_t eb)
507 /* OverlapType returned reflects how the second (B)
508 range overlaps the first (A).
510 The diagrams show various relative placements
511 of A and B for each OverlapType.
514 Internal: the start points cannot coincide
515 External: the start and end points can coincide
516 Start: end points can coincide
517 End: start points can coincide
519 XXX Logically, Internal should disallow end
524 |--------------------| A
526 |-----------------| B
533 if ((sb >= sa) && (eb <= ea)) {
535 if ((sb > sa) && (eb <= ea)) {
537 return OverlapInternal;
541 |--------------------| A
543 -----------------------| B
546 "B overlaps the start of A"
550 if ((eb >= sa) && (eb <= ea)) {
554 |---------------------| A
556 |----------------------- B
559 "B overlaps the end of A"
562 if ((sb >= sa) && (sb <= ea)) {
566 |--------------------| A
567 -------------------------- B
568 |----------------------- B
569 ----------------------| B
570 |--------------------| B
573 "B overlaps all of A"
575 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
576 return OverlapExternal;
582 /* not sure where to put these */
585 std::istream& int_to_type (std::istream& o, T& hf) {
592 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
593 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
594 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
595 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
596 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
597 std::istream& operator>>(std::istream& o, SoloModel& var) { return int_to_type<SoloModel> (o, var); }
598 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
599 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
600 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
601 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
602 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }