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>
31 #include <xmmintrin.h>
36 #include <pbd/error.h>
38 #include <pbd/strsplit.h>
41 #include <midi++/port.h>
42 #include <midi++/port_request.h>
43 #include <midi++/manager.h>
44 #include <midi++/mmc.h>
46 #include <ardour/ardour.h>
47 #include <ardour/audio_library.h>
48 #include <ardour/configuration.h>
49 #include <ardour/profile.h>
50 #include <ardour/plugin_manager.h>
51 #include <ardour/audiosource.h>
52 #include <ardour/utils.h>
53 #include <ardour/session.h>
54 #include <ardour/control_protocol_manager.h>
55 #include <ardour/audioengine.h>
58 #include <ardour/osc.h>
61 #include <ardour/mix.h>
62 #include <ardour/runtime_functions.h>
64 #if defined (__APPLE__)
65 #include <Carbon/Carbon.h> // For Gestalt
70 ARDOUR::Configuration* ARDOUR::Config = 0;
71 ARDOUR::RuntimeProfile* ARDOUR::Profile = 0;
72 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
75 ARDOUR::OSC* ARDOUR::osc = 0;
78 using namespace ARDOUR;
82 MIDI::Port *default_mmc_port = 0;
83 MIDI::Port *default_mtc_port = 0;
84 MIDI::Port *default_midi_port = 0;
86 Change ARDOUR::StartChanged = ARDOUR::new_change ();
87 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
88 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
89 Change ARDOUR::NameChanged = ARDOUR::new_change ();
90 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
92 compute_peak_t ARDOUR::compute_peak = 0;
93 find_peaks_t ARDOUR::find_peaks = 0;
94 apply_gain_to_buffer_t ARDOUR::apply_gain_to_buffer = 0;
95 mix_buffers_with_gain_t ARDOUR::mix_buffers_with_gain = 0;
96 mix_buffers_no_gain_t ARDOUR::mix_buffers_no_gain = 0;
102 /* no real cost to creating this object, and it avoids
103 conditionals anywhere that uses it
106 osc = new OSC (Config->get_osc_port());
108 if (Config->get_use_osc ()) {
109 return osc->start ();
117 ARDOUR::setup_midi (AudioEngine& engine)
119 std::map<string,Configuration::MidiPortDescriptor*>::iterator i;
122 if ((nports = Config->midi_ports.size()) == 0) {
123 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
127 MIDI::Manager::instance()->set_api_data(engine.jack());
129 for (i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
130 Configuration::MidiPortDescriptor* port_descriptor;
132 port_descriptor = (*i).second;
134 MIDI::PortRequest request (port_descriptor->device,
135 port_descriptor->tag,
136 port_descriptor->mode,
137 port_descriptor->type);
139 if (request.status != MIDI::PortRequest::OK) {
140 error << string_compose(_("MIDI port specifications for \"%1\" are not understandable."), port_descriptor->tag) << endmsg;
144 MIDI::Manager::instance()->add_port (request);
151 /* More than one port, so try using specific names for each port */
153 map<string,Configuration::MidiPortDescriptor *>::iterator i;
155 if (Config->get_mmc_port_name() != N_("default")) {
156 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
159 if (Config->get_mtc_port_name() != N_("default")) {
160 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
163 if (Config->get_midi_port_name() != N_("default")) {
164 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
167 /* If that didn't work, just use the first listed port */
169 if (default_mmc_port == 0) {
170 default_mmc_port = MIDI::Manager::instance()->port (0);
173 if (default_mtc_port == 0) {
174 default_mtc_port = MIDI::Manager::instance()->port (0);
177 if (default_midi_port == 0) {
178 default_midi_port = MIDI::Manager::instance()->port (0);
183 /* Only one port described, so use it for both MTC and MMC */
185 default_mmc_port = MIDI::Manager::instance()->port (0);
186 default_mtc_port = default_mmc_port;
187 default_midi_port = default_mmc_port;
190 if (default_mmc_port == 0) {
191 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name())
196 if (default_mtc_port == 0) {
197 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
201 if (default_midi_port == 0) {
202 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
210 setup_hardware_optimization (bool try_optimization)
212 bool generic_mix_functions = true;
215 if (try_optimization) {
217 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
221 info << "Using SSE optimized routines" << endmsg;
224 compute_peak = x86_sse_compute_peak;
225 find_peaks = x86_sse_find_peaks;
226 apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
227 mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
228 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 compute_peak = veclib_compute_peak;
242 find_peaks = veclib_find_peaks;
243 apply_gain_to_buffer = veclib_apply_gain_to_buffer;
244 mix_buffers_with_gain = veclib_mix_buffers_with_gain;
245 mix_buffers_no_gain = veclib_mix_buffers_no_gain;
247 generic_mix_functions = false;
249 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
254 if (generic_mix_functions) {
256 compute_peak = default_compute_peak;
257 find_peaks = default_find_peaks;
258 apply_gain_to_buffer = default_apply_gain_to_buffer;
259 mix_buffers_with_gain = default_mix_buffers_with_gain;
260 mix_buffers_no_gain = default_mix_buffers_no_gain;
262 info << "No H/W specific optimizations in use" << endmsg;
270 ARDOUR::init (bool use_vst, bool try_optimization)
272 extern void setup_enum_writer ();
274 (void) bindtextdomain(PACKAGE, LOCALEDIR);
276 setup_enum_writer ();
279 Library = new AudioLibrary;
281 Config = new Configuration;
283 if (Config->load_state ()) {
287 Config->set_use_vst (use_vst);
289 Profile = new RuntimeProfile;
298 if (Config->get_use_vst() && fst_init ()) {
303 setup_hardware_optimization (try_optimization);
305 /* singleton - first object is "it" */
306 new PluginManager ();
308 /* singleton - first object is "it" */
309 new ControlProtocolManager ();
310 ControlProtocolManager::instance().discover_control_protocols ();
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_ardour_revision ()
369 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
371 old = strdup (setlocale (LC_NUMERIC, NULL));
372 if (strcmp (old, str)) {
373 setlocale (LC_NUMERIC, str);
377 ARDOUR::LocaleGuard::~LocaleGuard ()
379 setlocale (LC_NUMERIC, old);
386 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
391 /* XXX use real code to determine if the processor supports
392 DenormalsAreZero and FlushToZero
395 if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
399 MXCSR = _mm_getcsr();
401 switch (Config->get_denormal_model()) {
403 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
407 if (fpu.has_flush_to_zero()) {
408 MXCSR |= _MM_FLUSH_ZERO_ON;
413 MXCSR &= ~_MM_FLUSH_ZERO_ON;
414 if (fpu.has_denormals_are_zero()) {
420 if (fpu.has_flush_to_zero()) {
421 if (fpu.has_denormals_are_zero()) {
422 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
424 MXCSR |= _MM_FLUSH_ZERO_ON;
436 ARDOUR::coverage (nframes_t sa, nframes_t ea,
437 nframes_t sb, nframes_t eb)
439 /* OverlapType returned reflects how the second (B)
440 range overlaps the first (A).
442 The diagrams show various relative placements
443 of A and B for each OverlapType.
446 Internal: the start points cannot coincide
447 External: the start and end points can coincide
448 Start: end points can coincide
449 End: start points can coincide
451 XXX Logically, Internal should disallow end
456 |--------------------| A
458 |-----------------| B
465 if ((sb >= sa) && (eb <= ea)) {
467 if ((sb > sa) && (eb <= ea)) {
469 return OverlapInternal;
473 |--------------------| A
475 -----------------------| B
478 "B overlaps the start of A"
482 if ((eb >= sa) && (eb <= ea)) {
486 |---------------------| A
488 |----------------------- B
491 "B overlaps the end of A"
494 if ((sb >= sa) && (sb <= ea)) {
498 |--------------------| A
499 -------------------------- B
500 |----------------------- B
501 ----------------------| B
502 |--------------------| B
505 "B overlaps all of A"
507 if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
508 return OverlapExternal;
514 /* not sure where to put these */
517 std::istream& int_to_type (std::istream& o, T& hf) {
524 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
525 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
526 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
527 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
528 std::istream& operator>>(std::istream& o, RemoteModel& var) { return int_to_type<RemoteModel> (o, var); }
529 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
530 std::istream& operator>>(std::istream& o, SoloModel& var) { return int_to_type<SoloModel> (o, var); }
531 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
532 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
533 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
534 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
535 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }
536 std::istream& operator>>(std::istream& o, SmpteFormat& var) { return int_to_type<SmpteFormat> (o, var); }
537 std::istream& operator>>(std::istream& o, DenormalModel& var) { return int_to_type<DenormalModel> (o, var); }