6bb21a419cf94083a919d7ab2a0e5c3f4c7dec8f
[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 #include <cstdio> // Needed so that libraptor (included in lrdf) won't complain
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <locale.h>
25
26 #ifdef VST_SUPPORT
27 #include <fst.h>
28 #endif
29
30 #ifdef __SSE__
31 #include <xmmintrin.h>
32 #endif
33
34 #include <lrdf.h>
35
36 #include <pbd/error.h>
37 #include <pbd/id.h>
38 #include <pbd/strsplit.h>
39 #include <pbd/fpu.h>
40
41 #include <midi++/port.h>
42 #include <midi++/manager.h>
43 #include <midi++/mmc.h>
44
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>
56
57 #ifdef HAVE_LIBLO
58 #include <ardour/osc.h>
59 #endif
60
61 #include <ardour/mix.h>
62 #include <ardour/runtime_functions.h>
63
64 #if defined (__APPLE__)
65        #include <Carbon/Carbon.h> // For Gestalt
66 #endif
67        
68 #include "i18n.h"
69
70 ARDOUR::Configuration* ARDOUR::Config = 0;
71 ARDOUR::RuntimeProfile* ARDOUR::Profile = 0;
72 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
73
74 #ifdef HAVE_LIBLO
75 ARDOUR::OSC* ARDOUR::osc = 0;
76 #endif
77
78 using namespace ARDOUR;
79 using namespace std;
80 using namespace PBD;
81
82 MIDI::Port *default_mmc_port = 0;
83 MIDI::Port *default_mtc_port = 0;
84 MIDI::Port *default_midi_port = 0;
85
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
91
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;
97
98 #ifdef HAVE_LIBLO
99 static int
100 setup_osc ()
101 {
102         /* no real cost to creating this object, and it avoids
103            conditionals anywhere that uses it 
104         */
105         
106         osc = new OSC (Config->get_osc_port());
107         
108         if (Config->get_use_osc ()) {
109                 return osc->start ();
110         } else {
111                 return 0;
112         }
113 }
114 #endif
115
116 int 
117 setup_midi ()
118 {
119         if (Config->midi_ports.size() == 0) {
120                 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
121                 return 0;
122         }
123
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);
126         }
127
128         MIDI::Port* first;
129         const MIDI::Manager::PortMap& ports = MIDI::Manager::instance()->get_midi_ports();
130
131         if (ports.size() > 1) {
132
133                 first = ports.begin()->second;
134
135                 /* More than one port, so try using specific names for each port */
136
137                 if (Config->get_mmc_port_name() != N_("default")) {
138                         default_mmc_port =  MIDI::Manager::instance()->port (Config->get_mmc_port_name());
139                 } 
140
141                 if (Config->get_mtc_port_name() != N_("default")) {
142                         default_mtc_port =  MIDI::Manager::instance()->port (Config->get_mtc_port_name());
143                 } 
144
145                 if (Config->get_midi_port_name() != N_("default")) {
146                         default_midi_port =  MIDI::Manager::instance()->port (Config->get_midi_port_name());
147                 } 
148                 
149                 /* If that didn't work, just use the first listed port */
150
151                 if (default_mmc_port == 0) {
152                         default_mmc_port = first;
153                 }
154
155                 if (default_mtc_port == 0) {
156                         default_mtc_port = first;
157                 }
158
159                 if (default_midi_port == 0) {
160                         default_midi_port = first;
161                 }
162                 
163         } else if (ports.size() == 1) {
164
165                 first = ports.begin()->second;
166
167                 /* Only one port described, so use it for both MTC and MMC */
168
169                 default_mmc_port = first;
170                 default_mtc_port = default_mmc_port;
171                 default_midi_port = default_mmc_port;
172         }
173
174         if (default_mmc_port == 0) {
175                 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name()) 
176                         << endmsg;
177                 return 0;
178         } 
179
180         if (default_mtc_port == 0) {
181                 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name()) 
182                         << endmsg;
183         }
184
185         if (default_midi_port == 0) {
186                 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name()) 
187                         << endmsg;
188         }
189
190         return 0;
191 }
192
193 void
194 setup_hardware_optimization (bool try_optimization)
195 {
196         bool generic_mix_functions = true;
197
198         if (try_optimization) {
199
200                 FPU fpu;
201
202 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
203
204                 if (fpu.has_sse()) {
205
206                         info << "Using SSE optimized routines" << endmsg;
207
208                         // SSE SET
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;
214
215                         generic_mix_functions = false;
216
217                 }
218
219 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
220                 long sysVersion = 0;
221
222                 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
223                         sysVersion = 0;
224
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;
231
232                         generic_mix_functions = false;
233
234                         info << "Apple VecLib H/W specific optimizations in use" << endmsg;
235                 }
236 #endif
237
238                 /* consider FPU denormal handling to be "h/w optimization" */
239
240                 setup_fpu ();
241         }
242
243         if (generic_mix_functions) {
244
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;
250
251                 info << "No H/W specific optimizations in use" << endmsg;
252         }
253 }
254
255 int
256 ARDOUR::init (bool use_vst, bool try_optimization)
257 {
258         extern void setup_enum_writer ();
259
260         (void) bindtextdomain(PACKAGE, LOCALEDIR);
261
262         setup_enum_writer ();
263
264         lrdf_init();
265         Library = new AudioLibrary;
266
267         Config = new Configuration;
268
269         if (Config->load_state ()) {
270                 return -1;
271         }
272
273         Config->set_use_vst (use_vst);
274
275         Profile = new RuntimeProfile;
276
277 #ifdef HAVE_LIBLO
278         if (setup_osc ()) {
279                 return -1;
280         }
281 #endif
282
283 #ifdef VST_SUPPORT
284         if (Config->get_use_vst() && fst_init ()) {
285                 return -1;
286         }
287 #endif
288         
289         /* Make VAMP look in our library ahead of anything else */
290
291         char *p = getenv ("VAMP_PATH");
292         string vamppath = VAMP_DIR;
293         if (p) {
294                 vamppath += ':';
295                 vamppath += p;
296         } 
297         setenv ("VAMP_PATH", vamppath.c_str(), 1);
298
299
300         setup_hardware_optimization (try_optimization);
301
302         SourceFactory::init ();
303
304         /* singleton - first object is "it" */
305         new PluginManager ();
306         
307         /* singleton - first object is "it" */
308         new ControlProtocolManager ();
309         ControlProtocolManager::instance().discover_control_protocols ();
310
311         XMLNode* node;
312         if ((node = Config->control_protocol_state()) != 0) {
313                 ControlProtocolManager::instance().set_state (*node);
314         }
315         
316         BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
317
318         return 0;
319 }
320
321 int
322 ARDOUR::cleanup ()
323 {
324         delete Library;
325         lrdf_cleanup ();
326         delete &ControlProtocolManager::instance();
327         return 0;
328 }
329
330
331 microseconds_t
332 ARDOUR::get_microseconds ()
333 {
334         /* XXX need JACK to export its functionality */
335
336         struct timeval now;
337         gettimeofday (&now, 0);
338         return now.tv_sec * 1000000ULL + now.tv_usec;
339 }
340
341 ARDOUR::Change
342 ARDOUR::new_change ()
343 {
344         Change c;
345         static uint32_t change_bit = 1;
346
347         /* catch out-of-range */
348         if (!change_bit)
349         {
350                 fatal << _("programming error: ")
351                         << "change_bit out of range in ARDOUR::new_change()"
352                         << endmsg;
353                 /*NOTREACHED*/
354         }
355
356         c = Change (change_bit);
357         change_bit <<= 1;       // if it shifts too far, change_bit == 0
358
359         return c;
360 }
361
362 string
363 ARDOUR::get_ardour_revision ()
364 {
365         return "$Rev$";
366 }
367
368 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
369 {
370         old = strdup (setlocale (LC_NUMERIC, NULL));
371         if (strcmp (old, str)) {
372                 setlocale (LC_NUMERIC, str);
373         } 
374 }
375
376 ARDOUR::LocaleGuard::~LocaleGuard ()
377 {
378         setlocale (LC_NUMERIC, old);
379         free ((char*)old);
380 }
381
382 void
383 ARDOUR::setup_fpu ()
384 {
385
386         if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
387                 // valgrind doesn't understand this assembler stuff
388                 // September 10th, 2007
389                 return;
390         }
391
392 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
393
394         int MXCSR;
395         FPU fpu;
396
397         /* XXX use real code to determine if the processor supports
398            DenormalsAreZero and FlushToZero
399         */
400         
401         if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
402                 return;
403         }
404
405         MXCSR  = _mm_getcsr();
406
407         switch (Config->get_denormal_model()) {
408         case DenormalNone:
409                 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
410                 break;
411
412         case DenormalFTZ:
413                 if (fpu.has_flush_to_zero()) {
414                         MXCSR |= _MM_FLUSH_ZERO_ON;
415                 }
416                 break;
417
418         case DenormalDAZ:
419                 MXCSR &= ~_MM_FLUSH_ZERO_ON;
420                 if (fpu.has_denormals_are_zero()) {
421                         MXCSR |= 0x8000;
422                 }
423                 break;
424                 
425         case DenormalFTZDAZ:
426                 if (fpu.has_flush_to_zero()) {
427                         if (fpu.has_denormals_are_zero()) {
428                                 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
429                         } else {
430                                 MXCSR |= _MM_FLUSH_ZERO_ON;
431                         }
432                 }
433                 break;
434         }
435
436         _mm_setcsr (MXCSR);
437
438 #endif
439 }
440
441 ARDOUR::OverlapType
442 ARDOUR::coverage (nframes_t sa, nframes_t ea, 
443                   nframes_t sb, nframes_t eb)
444 {
445         /* OverlapType returned reflects how the second (B)
446            range overlaps the first (A).
447
448            The diagrams show various relative placements
449            of A and B for each OverlapType.
450
451            Notes:
452               Internal: the start points cannot coincide
453               External: the start and end points can coincide
454               Start: end points can coincide
455               End: start points can coincide
456
457            XXX Logically, Internal should disallow end
458            point equality.
459         */
460
461         /*
462              |--------------------|   A
463                   |------|            B
464                 |-----------------|   B
465
466
467              "B is internal to A"               
468
469         */
470 #ifdef OLD_COVERAGE
471         if ((sb >= sa) && (eb <= ea)) {
472 #else
473         if ((sb > sa) && (eb <= ea)) {
474 #endif
475                 return OverlapInternal;
476         }
477
478         /*
479              |--------------------|   A
480            ----|                      B
481            -----------------------|   B
482            --|                        B
483            
484              "B overlaps the start of A"
485
486         */
487
488         if ((eb >= sa) && (eb <= ea)) {
489                 return OverlapStart;
490         }
491         /* 
492              |---------------------|  A
493                    |----------------- B
494              |----------------------- B    
495                                    |- B
496
497             "B overlaps the end of A"                              
498
499         */
500         if ((sb > sa) && (sb <= ea)) {
501                 return OverlapEnd;
502         }
503         /*
504              |--------------------|     A
505            --------------------------  B   
506              |-----------------------  B
507             ----------------------|    B
508              |--------------------|    B
509
510
511            "B overlaps all of A"
512         */
513         if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
514                 return OverlapExternal;
515         }
516
517         return OverlapNone;
518 }
519
520 /* not sure where to put these */
521
522 template<class T>
523 std::istream& int_to_type (std::istream& o, T& hf) {
524         int val;
525         o >> val;
526         hf = (T) val;
527         return o;
528 }
529
530 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
531 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
532 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
533 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
534 std::istream& operator>>(std::istream& o, RemoteModel& var) { return int_to_type<RemoteModel> (o, var); }
535 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
536 std::istream& operator>>(std::istream& o, SoloModel& var) { return int_to_type<SoloModel> (o, var); }
537 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
538 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
539 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
540 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
541 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }
542 std::istream& operator>>(std::istream& o, SmpteFormat& var) { return int_to_type<SmpteFormat> (o, var); }
543 std::istream& operator>>(std::istream& o, DenormalModel& var) { return int_to_type<DenormalModel> (o, var); }
544