the Properties & 64bit region commit
[ardour.git] / libs / ardour / globals.cc
1 /*
2     Copyright (C) 2000 Paul Davis
3
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.
8
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.
13
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.
17 */
18
19
20 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
23
24 #include <cstdio> // Needed so that libraptor (included in lrdf) won't complain
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/time.h>
28 #include <sys/resource.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <errno.h>
32
33 #ifdef VST_SUPPORT
34 #include <fst.h>
35 #endif
36
37 #ifdef HAVE_AUDIOUNITS
38 #include "ardour/audio_unit.h"
39 #endif
40
41 #ifdef __SSE__
42 #include <xmmintrin.h>
43 #endif
44
45 #include <glibmm/fileutils.h>
46 #include <glibmm/miscutils.h>
47
48 #include <lrdf.h>
49
50 #include "pbd/error.h"
51 #include "pbd/id.h"
52 #include "pbd/strsplit.h"
53 #include "pbd/fpu.h"
54 #include "pbd/file_utils.h"
55 #include "pbd/enumwriter.h"
56
57 #include "midi++/port.h"
58 #include "midi++/manager.h"
59 #include "midi++/mmc.h"
60
61 #include "ardour/analyser.h"
62 #include "ardour/ardour.h"
63 #include "ardour/audio_library.h"
64 #include "ardour/audioengine.h"
65 #include "ardour/audioregion.h"
66 #include "ardour/audiosource.h"
67 #include "ardour/control_protocol_manager.h"
68 #include "ardour/debug.h"
69 #include "ardour/filesystem_paths.h"
70 #include "ardour/mix.h"
71 #include "ardour/plugin_manager.h"
72 #include "ardour/profile.h"
73 #include "ardour/region.h"
74 #include "ardour/rc_configuration.h"
75 #include "ardour/route_group.h"
76 #include "ardour/runtime_functions.h"
77 #include "ardour/session.h"
78 #include "ardour/session_event.h"
79 #include "ardour/source_factory.h"
80 #include "ardour/utils.h"
81
82 #include "audiographer/routines.h"
83
84 #if defined (__APPLE__)
85        #include <Carbon/Carbon.h> // For Gestalt
86 #endif
87
88 #include "i18n.h"
89
90 ARDOUR::RCConfiguration* ARDOUR::Config = 0;
91 ARDOUR::RuntimeProfile* ARDOUR::Profile = 0;
92 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
93
94 using namespace ARDOUR;
95 using namespace std;
96 using namespace PBD;
97
98 uint64_t ARDOUR::debug_bits = 0x0;
99
100 MIDI::Port *ARDOUR::default_mmc_port = 0;
101 MIDI::Port *ARDOUR::default_mtc_port = 0;
102 MIDI::Port *ARDOUR::default_midi_port = 0;
103 MIDI::Port *ARDOUR::default_midi_clock_port = 0;
104
105 PropertyChange ARDOUR::StartChanged = PBD::new_change ();
106 PropertyChange ARDOUR::LengthChanged = PBD::new_change ();
107 PropertyChange ARDOUR::PositionChanged = PBD::new_change ();
108 PropertyChange ARDOUR::NameChanged = PBD::new_change ();
109 PropertyChange ARDOUR::BoundsChanged = PropertyChange (0); // see init(), below
110
111 compute_peak_t          ARDOUR::compute_peak = 0;
112 find_peaks_t            ARDOUR::find_peaks = 0;
113 apply_gain_to_buffer_t  ARDOUR::apply_gain_to_buffer = 0;
114 mix_buffers_with_gain_t ARDOUR::mix_buffers_with_gain = 0;
115 mix_buffers_no_gain_t   ARDOUR::mix_buffers_no_gain = 0;
116
117 PBD::Signal1<void,std::string> ARDOUR::BootMessage;
118
119 void ARDOUR::setup_enum_writer ();
120
121 int
122 ARDOUR::setup_midi ()
123 {
124         if (Config->midi_ports.size() == 0) {
125                 return 0;
126         }
127
128         BootMessage (_("Configuring MIDI ports"));
129
130         for (std::map<string,XMLNode>::iterator i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
131                 MIDI::Manager::instance()->add_port (i->second);
132         }
133
134         MIDI::Port* first;
135         const MIDI::Manager::PortList& ports = MIDI::Manager::instance()->get_midi_ports();
136
137         if (ports.size() > 1) {
138
139                 first = ports.front();
140
141                 /* More than one port, so try using specific names for each port */
142
143                 default_mmc_port =  MIDI::Manager::instance()->port (Config->get_mmc_port_name());
144                 default_mtc_port =  MIDI::Manager::instance()->port (Config->get_mtc_port_name());
145                 default_midi_port =  MIDI::Manager::instance()->port (Config->get_midi_port_name());
146                 default_midi_clock_port =  MIDI::Manager::instance()->port (Config->get_midi_clock_port_name());
147
148                 /* If that didn't work, just use the first listed port */
149
150                 if (default_mmc_port == 0) {
151                         default_mmc_port = first;
152                 }
153
154                 if (default_mtc_port == 0) {
155                         default_mtc_port = first;
156                 }
157
158                 if (default_midi_port == 0) {
159                         default_midi_port = first;
160                 }
161
162                 if (default_midi_clock_port == 0) {
163                         default_midi_clock_port = first;
164                 }
165
166         } else if (ports.size() == 1) {
167
168                 first = ports.front();
169
170                 /* Only one port described, so use it for both MTC and MMC */
171
172                 default_mmc_port = first;
173                 default_mtc_port = default_mmc_port;
174                 default_midi_port = default_mmc_port;
175                 default_midi_clock_port = default_mmc_port;
176         }
177
178         if (default_mmc_port == 0) {
179                 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name())
180                         << endmsg;
181         }
182
183
184         if (default_mtc_port == 0) {
185                 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name())
186                         << endmsg;
187         }
188
189         if (default_midi_port == 0) {
190                 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name())
191                         << endmsg;
192         }
193
194         if (default_midi_clock_port == 0) {
195                 warning << string_compose (_("No MIDI Clock support (MIDI port \"%1\" not available)"), Config->get_midi_clock_port_name())
196                         << endmsg;
197         }
198
199         return 0;
200 }
201
202 void
203 setup_hardware_optimization (bool try_optimization)
204 {
205         bool generic_mix_functions = true;
206
207         if (try_optimization) {
208
209                 FPU fpu;
210
211 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
212
213                 if (fpu.has_sse()) {
214
215                         info << "Using SSE optimized routines" << endmsg;
216
217                         // SSE SET
218                         compute_peak          = x86_sse_compute_peak;
219                         find_peaks            = x86_sse_find_peaks;
220                         apply_gain_to_buffer  = x86_sse_apply_gain_to_buffer;
221                         mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
222                         mix_buffers_no_gain   = x86_sse_mix_buffers_no_gain;
223
224                         generic_mix_functions = false;
225
226                 }
227
228 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
229                 long sysVersion = 0;
230
231                 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
232                         sysVersion = 0;
233
234                 if (sysVersion >= 0x00001040) { // Tiger at least
235                         compute_peak           = veclib_compute_peak;
236                         find_peaks             = veclib_find_peaks;
237                         apply_gain_to_buffer   = veclib_apply_gain_to_buffer;
238                         mix_buffers_with_gain  = veclib_mix_buffers_with_gain;
239                         mix_buffers_no_gain    = veclib_mix_buffers_no_gain;
240
241                         generic_mix_functions = false;
242
243                         info << "Apple VecLib H/W specific optimizations in use" << endmsg;
244                 }
245 #endif
246
247                 /* consider FPU denormal handling to be "h/w optimization" */
248
249                 setup_fpu ();
250         }
251
252         if (generic_mix_functions) {
253
254                 compute_peak          = default_compute_peak;
255                 find_peaks            = default_find_peaks;
256                 apply_gain_to_buffer  = default_apply_gain_to_buffer;
257                 mix_buffers_with_gain = default_mix_buffers_with_gain;
258                 mix_buffers_no_gain   = default_mix_buffers_no_gain;
259
260                 info << "No H/W specific optimizations in use" << endmsg;
261         }
262         
263         AudioGrapher::Routines::override_compute_peak (compute_peak);
264         AudioGrapher::Routines::override_apply_gain_to_buffer (apply_gain_to_buffer);
265 }
266
267 static void
268 lotsa_files_please ()
269 {
270         struct rlimit rl;
271
272         if (getrlimit (RLIMIT_NOFILE, &rl) == 0) {
273
274                 rl.rlim_cur = rl.rlim_max;
275
276                 if (setrlimit (RLIMIT_NOFILE, &rl) != 0) {
277                         if (rl.rlim_cur == RLIM_INFINITY) {
278                                 error << _("Could not set system open files limit to \"unlimited\"") << endmsg;
279                         } else {
280                                 error << string_compose (_("Could not set system open files limit to %1"), rl.rlim_cur) << endmsg;
281                         }
282                 } else {
283                         if (rl.rlim_cur == RLIM_INFINITY) {
284                                 info << _("Removed open file count limit. Excellent!") << endmsg;
285                         } else {
286                                 info << string_compose (_("Ardour will be limited to %1 open files"), rl.rlim_cur) << endmsg;
287                         }
288                 }
289         } else {
290                 error << string_compose (_("Could not get system open files limit (%1)"), strerror (errno)) << endmsg;
291         }
292 }
293
294 int
295 ARDOUR::init (bool use_vst, bool try_optimization)
296 {
297         if (!Glib::thread_supported()) {
298                 Glib::thread_init();
299         }
300
301         (void) bindtextdomain(PACKAGE, LOCALEDIR);
302
303         PBD::ID::init ();
304         SessionEvent::init_event_pool ();
305
306         SessionObject::make_property_quarks ();
307         Region::make_property_quarks ();
308         AudioRegion::make_property_quarks ();
309         RouteGroup::make_property_quarks ();
310
311         /* provide a state version for the few cases that need it and are not
312            driven by reading state from disk (e.g. undo/redo)
313         */
314
315         Stateful::current_state_version = CURRENT_SESSION_FILE_VERSION;
316
317         setup_enum_writer ();
318
319         // allow ardour the absolute maximum number of open files
320         lotsa_files_please ();
321
322         lrdf_init();
323         Library = new AudioLibrary;
324
325         BootMessage (_("Loading configuration"));
326
327         Config = new RCConfiguration;
328
329         if (Config->load_state ()) {
330                 return -1;
331         }
332
333         
334         Config->set_use_vst (use_vst);
335
336         cerr << "After config loaded, MTC port name = " << Config->get_mtc_port_name() << endl;
337
338         Profile = new RuntimeProfile;
339
340
341 #ifdef VST_SUPPORT
342         if (Config->get_use_vst() && fst_init (0)) {
343                 return -1;
344         }
345 #endif
346
347 #ifdef HAVE_AUDIOUNITS
348         AUPluginInfo::load_cached_info ();
349 #endif
350
351         /* Make VAMP look in our library ahead of anything else */
352
353         char *p = getenv ("VAMP_PATH");
354         string vamppath = VAMP_DIR;
355         if (p) {
356                 vamppath += ':';
357                 vamppath += p;
358         }
359         setenv ("VAMP_PATH", vamppath.c_str(), 1);
360
361
362         setup_hardware_optimization (try_optimization);
363
364         SourceFactory::init ();
365         Analyser::init ();
366
367         /* singleton - first object is "it" */
368         new PluginManager ();
369
370         BoundsChanged = PropertyChange (StartChanged|PositionChanged|LengthChanged);
371
372         return 0;
373 }
374
375 void
376 ARDOUR::init_post_engine ()
377 {
378         ControlProtocolManager::instance().discover_control_protocols ();
379
380         XMLNode* node;
381         if ((node = Config->control_protocol_state()) != 0) {
382                 ControlProtocolManager::instance().set_state (*node, Stateful::loading_state_version);
383         }
384 }
385
386 int
387 ARDOUR::cleanup ()
388 {
389         delete Library;
390         lrdf_cleanup ();
391         delete &ControlProtocolManager::instance();
392 #ifdef VST_SUPPORT
393         fst_exit ();
394 #endif
395         return 0;
396 }
397
398 string
399 ARDOUR::get_ardour_revision ()
400 {
401         return "$Rev$";
402 }
403
404 void
405 ARDOUR::find_bindings_files (map<string,string>& files)
406 {
407         vector<sys::path> found;
408         SearchPath spath = ardour_search_path() + user_config_directory() + system_config_search_path();
409
410         if (getenv ("ARDOUR_SAE")) {
411                 Glib::PatternSpec pattern("*SAE-*.bindings");
412                 find_matching_files_in_search_path (spath, pattern, found);
413         } else {
414                 Glib::PatternSpec pattern("*.bindings");
415                 find_matching_files_in_search_path (spath, pattern, found);
416         }
417
418         if (found.empty()) {
419                 return;
420         }
421
422         for (vector<sys::path>::iterator x = found.begin(); x != found.end(); ++x) {
423                 sys::path path = *x;
424                 pair<string,string> namepath;
425                 namepath.second = path.to_string();
426                 namepath.first = path.leaf().substr (0, path.leaf().find_first_of ('.'));
427                 files.insert (namepath);
428         }
429 }
430
431 bool
432 ARDOUR::no_auto_connect()
433 {
434         return getenv ("ARDOUR_NO_AUTOCONNECT") != 0;
435 }
436
437 void
438 ARDOUR::setup_fpu ()
439 {
440
441         if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
442                 // valgrind doesn't understand this assembler stuff
443                 // September 10th, 2007
444                 return;
445         }
446
447 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
448
449         int MXCSR;
450         FPU fpu;
451
452         /* XXX use real code to determine if the processor supports
453            DenormalsAreZero and FlushToZero
454         */
455
456         if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
457                 return;
458         }
459
460         MXCSR  = _mm_getcsr();
461
462         switch (Config->get_denormal_model()) {
463         case DenormalNone:
464                 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
465                 break;
466
467         case DenormalFTZ:
468                 if (fpu.has_flush_to_zero()) {
469                         MXCSR |= _MM_FLUSH_ZERO_ON;
470                 }
471                 break;
472
473         case DenormalDAZ:
474                 MXCSR &= ~_MM_FLUSH_ZERO_ON;
475                 if (fpu.has_denormals_are_zero()) {
476                         MXCSR |= 0x8000;
477                 }
478                 break;
479
480         case DenormalFTZDAZ:
481                 if (fpu.has_flush_to_zero()) {
482                         if (fpu.has_denormals_are_zero()) {
483                                 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
484                         } else {
485                                 MXCSR |= _MM_FLUSH_ZERO_ON;
486                         }
487                 }
488                 break;
489         }
490
491         _mm_setcsr (MXCSR);
492
493 #endif
494 }
495
496 ARDOUR::OverlapType
497 ARDOUR::coverage (nframes_t sa, nframes_t ea,
498                   nframes_t sb, nframes_t eb)
499 {
500         /* OverlapType returned reflects how the second (B)
501            range overlaps the first (A).
502
503            The diagrams show various relative placements
504            of A and B for each OverlapType.
505
506            Notes:
507               Internal: the start points cannot coincide
508               External: the start and end points can coincide
509               Start: end points can coincide
510               End: start points can coincide
511
512            XXX Logically, Internal should disallow end
513            point equality.
514         */
515
516         /*
517              |--------------------|   A
518                   |------|            B
519                 |-----------------|   B
520
521
522              "B is internal to A"
523
524         */
525 #ifdef OLD_COVERAGE
526         if ((sb >= sa) && (eb <= ea)) {
527 #else
528         if ((sb > sa) && (eb <= ea)) {
529 #endif
530                 return OverlapInternal;
531         }
532
533         /*
534              |--------------------|   A
535            ----|                      B
536            -----------------------|   B
537            --|                        B
538
539              "B overlaps the start of A"
540
541         */
542
543         if ((eb >= sa) && (eb <= ea)) {
544                 return OverlapStart;
545         }
546         /*
547              |---------------------|  A
548                    |----------------- B
549              |----------------------- B
550                                    |- B
551
552             "B overlaps the end of A"
553
554         */
555         if ((sb > sa) && (sb <= ea)) {
556                 return OverlapEnd;
557         }
558         /*
559              |--------------------|     A
560            --------------------------  B
561              |-----------------------  B
562             ----------------------|    B
563              |--------------------|    B
564
565
566            "B overlaps all of A"
567         */
568         if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
569                 return OverlapExternal;
570         }
571
572         return OverlapNone;
573 }
574