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++/manager.h>
43 #include <midi++/mmc.h>
45 #include <ardour/ardour.h>
46 #include <ardour/audio_library.h>
47 #include <ardour/configuration.h>
48 #include <ardour/profile.h>
49 #include <ardour/plugin_manager.h>
50 #include <ardour/audiosource.h>
51 #include <ardour/utils.h>
52 #include <ardour/session.h>
53 #include <ardour/source_factory.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 ();
119 if (Config->midi_ports.size() == 0) {
120 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
124 for (std::map<string,XMLNode>::iterator i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
125 MIDI::Manager::instance()->add_port (i->second);
129 const MIDI::Manager::PortMap& ports = MIDI::Manager::instance()->get_midi_ports();
131 if (ports.size() > 1) {
133 first = ports.begin()->second;
135 /* More than one port, so try using specific names for each port */
137 if (Config->get_mmc_port_name() != N_("default")) {
138 default_mmc_port = MIDI::Manager::instance()->port (Config->get_mmc_port_name());
141 if (Config->get_mtc_port_name() != N_("default")) {
142 default_mtc_port = MIDI::Manager::instance()->port (Config->get_mtc_port_name());
145 if (Config->get_midi_port_name() != N_("default")) {
146 default_midi_port = MIDI::Manager::instance()->port (Config->get_midi_port_name());
149 /* If that didn't work, just use the first listed port */
151 if (default_mmc_port == 0) {
152 default_mmc_port = first;
155 if (default_mtc_port == 0) {
156 default_mtc_port = first;
159 if (default_midi_port == 0) {
160 default_midi_port = first;
163 } else if (ports.size() == 1) {
165 first = ports.begin()->second;
167 /* Only one port described, so use it for both MTC and MMC */
169 default_mmc_port = first;
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 setup_hardware_optimization (bool try_optimization)
196 bool generic_mix_functions = true;
198 if (try_optimization) {
202 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
206 info << "Using SSE optimized routines" << endmsg;
209 compute_peak = x86_sse_compute_peak;
210 find_peaks = x86_sse_find_peaks;
211 apply_gain_to_buffer = x86_sse_apply_gain_to_buffer;
212 mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
213 mix_buffers_no_gain = x86_sse_mix_buffers_no_gain;
215 generic_mix_functions = false;
219 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
222 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
225 if (sysVersion >= 0x00001040) { // Tiger at least
226 compute_peak = veclib_compute_peak;
227 find_peaks = veclib_find_peaks;
228 apply_gain_to_buffer = veclib_apply_gain_to_buffer;
229 mix_buffers_with_gain = veclib_mix_buffers_with_gain;
230 mix_buffers_no_gain = veclib_mix_buffers_no_gain;
232 generic_mix_functions = false;
234 info << "Apple VecLib H/W specific optimizations in use" << endmsg;
238 /* consider FPU denormal handling to be "h/w optimization" */
243 if (generic_mix_functions) {
245 compute_peak = default_compute_peak;
246 find_peaks = default_find_peaks;
247 apply_gain_to_buffer = default_apply_gain_to_buffer;
248 mix_buffers_with_gain = default_mix_buffers_with_gain;
249 mix_buffers_no_gain = default_mix_buffers_no_gain;
251 info << "No H/W specific optimizations in use" << endmsg;
256 ARDOUR::init (bool use_vst, bool try_optimization)
258 extern void setup_enum_writer ();
260 (void) bindtextdomain(PACKAGE, LOCALEDIR);
262 setup_enum_writer ();
265 Library = new AudioLibrary;
267 Config = new Configuration;
269 if (Config->load_state ()) {
273 Config->set_use_vst (use_vst);
275 Profile = new RuntimeProfile;
284 if (Config->get_use_vst() && fst_init ()) {
289 setup_hardware_optimization (try_optimization);
291 SourceFactory::init ();
293 /* singleton - first object is "it" */
294 new PluginManager ();
296 /* singleton - first object is "it" */
297 new ControlProtocolManager ();
298 ControlProtocolManager::instance().discover_control_protocols ();
301 if ((node = Config->control_protocol_state()) != 0) {
302 ControlProtocolManager::instance().set_state (*node);
305 BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
315 delete &ControlProtocolManager::instance();
321 ARDOUR::get_microseconds ()
323 /* XXX need JACK to export its functionality */
326 gettimeofday (&now, 0);
327 return now.tv_sec * 1000000ULL + now.tv_usec;
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_ardour_revision ()
357 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
359 old = strdup (setlocale (LC_NUMERIC, NULL));
360 if (strcmp (old, str)) {
361 setlocale (LC_NUMERIC, str);
365 ARDOUR::LocaleGuard::~LocaleGuard ()
367 setlocale (LC_NUMERIC, old);
375 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
376 // valgrind doesn't understand this assembler stuff
377 // September 10th, 2007
381 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
386 /* XXX use real code to determine if the processor supports
387 DenormalsAreZero and FlushToZero
390 if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
394 MXCSR = _mm_getcsr();
396 switch (Config->get_denormal_model()) {
398 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
402 if (fpu.has_flush_to_zero()) {
403 MXCSR |= _MM_FLUSH_ZERO_ON;
408 MXCSR &= ~_MM_FLUSH_ZERO_ON;
409 if (fpu.has_denormals_are_zero()) {
415 if (fpu.has_flush_to_zero()) {
416 if (fpu.has_denormals_are_zero()) {
417 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
419 MXCSR |= _MM_FLUSH_ZERO_ON;
431 ARDOUR::coverage (nframes_t sa, nframes_t ea,
432 nframes_t sb, 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 */
512 std::istream& int_to_type (std::istream& o, T& hf) {
519 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
520 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
521 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
522 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
523 std::istream& operator>>(std::istream& o, RemoteModel& var) { return int_to_type<RemoteModel> (o, var); }
524 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
525 std::istream& operator>>(std::istream& o, SoloModel& var) { return int_to_type<SoloModel> (o, var); }
526 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
527 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
528 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
529 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
530 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }
531 std::istream& operator>>(std::istream& o, SmpteFormat& var) { return int_to_type<SmpteFormat> (o, var); }
532 std::istream& operator>>(std::istream& o, DenormalModel& var) { return int_to_type<DenormalModel> (o, var); }