Change return type of Session::import_audiofiles to void as the int return value...
[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         setup_hardware_optimization (try_optimization);
290
291         SourceFactory::init ();
292
293         /* singleton - first object is "it" */
294         new PluginManager ();
295         
296         /* singleton - first object is "it" */
297         new ControlProtocolManager ();
298         ControlProtocolManager::instance().discover_control_protocols ();
299
300         XMLNode* node;
301         if ((node = Config->control_protocol_state()) != 0) {
302                 ControlProtocolManager::instance().set_state (*node);
303         }
304         
305         BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
306
307         return 0;
308 }
309
310 int
311 ARDOUR::cleanup ()
312 {
313         delete Library;
314         lrdf_cleanup ();
315         delete &ControlProtocolManager::instance();
316         return 0;
317 }
318
319
320 microseconds_t
321 ARDOUR::get_microseconds ()
322 {
323         /* XXX need JACK to export its functionality */
324
325         struct timeval now;
326         gettimeofday (&now, 0);
327         return now.tv_sec * 1000000ULL + now.tv_usec;
328 }
329
330 ARDOUR::Change
331 ARDOUR::new_change ()
332 {
333         Change c;
334         static uint32_t change_bit = 1;
335
336         /* catch out-of-range */
337         if (!change_bit)
338         {
339                 fatal << _("programming error: ")
340                         << "change_bit out of range in ARDOUR::new_change()"
341                         << endmsg;
342                 /*NOTREACHED*/
343         }
344
345         c = Change (change_bit);
346         change_bit <<= 1;       // if it shifts too far, change_bit == 0
347
348         return c;
349 }
350
351 string
352 ARDOUR::get_ardour_revision ()
353 {
354         return "$Rev$";
355 }
356
357 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
358 {
359         old = strdup (setlocale (LC_NUMERIC, NULL));
360         if (strcmp (old, str)) {
361                 setlocale (LC_NUMERIC, str);
362         } 
363 }
364
365 ARDOUR::LocaleGuard::~LocaleGuard ()
366 {
367         setlocale (LC_NUMERIC, old);
368         free ((char*)old);
369 }
370
371 void
372 ARDOUR::setup_fpu ()
373 {
374
375         if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
376                 // valgrind doesn't understand this assembler stuff
377                 // September 10th, 2007
378                 return;
379         }
380
381 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
382
383         int MXCSR;
384         FPU fpu;
385
386         /* XXX use real code to determine if the processor supports
387            DenormalsAreZero and FlushToZero
388         */
389         
390         if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
391                 return;
392         }
393
394         MXCSR  = _mm_getcsr();
395
396         switch (Config->get_denormal_model()) {
397         case DenormalNone:
398                 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
399                 break;
400
401         case DenormalFTZ:
402                 if (fpu.has_flush_to_zero()) {
403                         MXCSR |= _MM_FLUSH_ZERO_ON;
404                 }
405                 break;
406
407         case DenormalDAZ:
408                 MXCSR &= ~_MM_FLUSH_ZERO_ON;
409                 if (fpu.has_denormals_are_zero()) {
410                         MXCSR |= 0x8000;
411                 }
412                 break;
413                 
414         case DenormalFTZDAZ:
415                 if (fpu.has_flush_to_zero()) {
416                         if (fpu.has_denormals_are_zero()) {
417                                 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
418                         } else {
419                                 MXCSR |= _MM_FLUSH_ZERO_ON;
420                         }
421                 }
422                 break;
423         }
424
425         _mm_setcsr (MXCSR);
426
427 #endif
428 }
429
430 ARDOUR::OverlapType
431 ARDOUR::coverage (nframes_t sa, nframes_t ea, 
432                   nframes_t sb, nframes_t eb)
433 {
434         /* OverlapType returned reflects how the second (B)
435            range overlaps the first (A).
436
437            The diagrams show various relative placements
438            of A and B for each OverlapType.
439
440            Notes:
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
445
446            XXX Logically, Internal should disallow end
447            point equality.
448         */
449
450         /*
451              |--------------------|   A
452                   |------|            B
453                 |-----------------|   B
454
455
456              "B is internal to A"               
457
458         */
459 #ifdef OLD_COVERAGE
460         if ((sb >= sa) && (eb <= ea)) {
461 #else
462         if ((sb > sa) && (eb <= ea)) {
463 #endif
464                 return OverlapInternal;
465         }
466
467         /*
468              |--------------------|   A
469            ----|                      B
470            -----------------------|   B
471            --|                        B
472            
473              "B overlaps the start of A"
474
475         */
476
477         if ((eb >= sa) && (eb <= ea)) {
478                 return OverlapStart;
479         }
480         /* 
481              |---------------------|  A
482                    |----------------- B
483              |----------------------- B    
484                                    |- B
485
486             "B overlaps the end of A"                              
487
488         */
489         if ((sb > sa) && (sb <= ea)) {
490                 return OverlapEnd;
491         }
492         /*
493              |--------------------|     A
494            --------------------------  B   
495              |-----------------------  B
496             ----------------------|    B
497              |--------------------|    B
498
499
500            "B overlaps all of A"
501         */
502         if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
503                 return OverlapExternal;
504         }
505
506         return OverlapNone;
507 }
508
509 /* not sure where to put these */
510
511 template<class T>
512 std::istream& int_to_type (std::istream& o, T& hf) {
513         int val;
514         o >> val;
515         hf = (T) val;
516         return o;
517 }
518
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); }
533