Use sys::path and SessionDirectory in Session::create_session_file_from_template...
[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++/port_request.h>
43 #include <midi++/manager.h>
44 #include <midi++/mmc.h>
45
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>
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 ARDOUR::setup_midi (AudioEngine& engine)
118 {
119         std::map<string,Configuration::MidiPortDescriptor*>::iterator i;
120         int nports;
121
122         if ((nports = Config->midi_ports.size()) == 0) {
123                 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
124                 return 0;
125         }
126
127         MIDI::Manager::instance()->set_api_data(engine.jack());
128
129         for (i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
130                 Configuration::MidiPortDescriptor* port_descriptor;
131
132                 port_descriptor = (*i).second;
133
134                 MIDI::PortRequest request (port_descriptor->device, 
135                                            port_descriptor->tag, 
136                                            port_descriptor->mode, 
137                                            port_descriptor->type);
138
139                 if (request.status != MIDI::PortRequest::OK) {
140                         error << string_compose(_("MIDI port specifications for \"%1\" are not understandable."), port_descriptor->tag) << endmsg;
141                         continue;
142                 }
143                 
144                 MIDI::Manager::instance()->add_port (request);
145
146                 nports++;
147         }
148
149         MIDI::Port* first;
150         const MIDI::Manager::PortMap& ports = MIDI::Manager::instance()->get_midi_ports();
151         first = ports.begin()->second;
152
153         if (nports > 1) {
154
155                 /* More than one port, so try using specific names for each port */
156
157                 map<string,Configuration::MidiPortDescriptor *>::iterator i;
158
159                 if (Config->get_mmc_port_name() != N_("default")) {
160                         default_mmc_port =  MIDI::Manager::instance()->port (Config->get_mmc_port_name());
161                 } 
162
163                 if (Config->get_mtc_port_name() != N_("default")) {
164                         default_mtc_port =  MIDI::Manager::instance()->port (Config->get_mtc_port_name());
165                 } 
166
167                 if (Config->get_midi_port_name() != N_("default")) {
168                         default_midi_port =  MIDI::Manager::instance()->port (Config->get_midi_port_name());
169                 } 
170                 
171                 /* If that didn't work, just use the first listed port */
172
173                 if (default_mmc_port == 0) {
174                         default_mmc_port = first;
175                 }
176
177                 if (default_mtc_port == 0) {
178                         default_mtc_port = first;
179                 }
180
181                 if (default_midi_port == 0) {
182                         default_midi_port = first;
183                 }
184                 
185         } else {
186
187                 /* Only one port described, so use it for both MTC and MMC */
188
189                 default_mmc_port = MIDI::Manager::instance()->port ("");
190                 default_mtc_port = default_mmc_port;
191                 default_midi_port = default_mmc_port;
192         }
193
194         if (default_mmc_port == 0) {
195                 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name()) 
196                         << endmsg;
197                 return 0;
198         } 
199
200         if (default_mtc_port == 0) {
201                 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name()) 
202                         << endmsg;
203         }
204
205         if (default_midi_port == 0) {
206                 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name()) 
207                         << endmsg;
208         }
209
210         return 0;
211 }
212         
213 void
214 setup_hardware_optimization (bool try_optimization)
215 {
216         bool generic_mix_functions = true;
217         FPU fpu;
218
219         if (try_optimization) {
220
221 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
222                 
223                 if (fpu.has_sse()) {
224
225                         info << "Using SSE optimized routines" << endmsg;
226         
227                         // SSE SET
228                         compute_peak            = x86_sse_compute_peak;
229                         find_peaks              = x86_sse_find_peaks;
230                         apply_gain_to_buffer    = x86_sse_apply_gain_to_buffer;
231                         mix_buffers_with_gain   = x86_sse_mix_buffers_with_gain;
232                         mix_buffers_no_gain     = x86_sse_mix_buffers_no_gain;
233
234                         generic_mix_functions = false;
235
236                 }
237
238 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
239                 long sysVersion = 0;
240
241                 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
242                         sysVersion = 0;
243
244                 if (sysVersion >= 0x00001040) { // Tiger at least
245                         compute_peak           = veclib_compute_peak;
246                         find_peaks             = veclib_find_peaks;
247                         apply_gain_to_buffer   = veclib_apply_gain_to_buffer;
248                         mix_buffers_with_gain  = veclib_mix_buffers_with_gain;
249                         mix_buffers_no_gain    = veclib_mix_buffers_no_gain;
250
251                         generic_mix_functions = false;
252
253                         info << "Apple VecLib H/W specific optimizations in use" << endmsg;
254                 }
255 #endif
256         }
257
258         if (generic_mix_functions) {
259                 
260                 compute_peak            = default_compute_peak;
261                 find_peaks              = default_find_peaks;
262                 apply_gain_to_buffer    = default_apply_gain_to_buffer;
263                 mix_buffers_with_gain   = default_mix_buffers_with_gain;
264                 mix_buffers_no_gain     = default_mix_buffers_no_gain;
265                 
266                 info << "No H/W specific optimizations in use" << endmsg;
267         }
268
269         setup_fpu ();
270
271 }
272
273 int
274 ARDOUR::init (bool use_vst, bool try_optimization)
275 {
276         extern void setup_enum_writer ();
277
278         (void) bindtextdomain(PACKAGE, LOCALEDIR);
279
280         setup_enum_writer ();
281
282         lrdf_init();
283         Library = new AudioLibrary;
284
285         Config = new Configuration;
286
287         if (Config->load_state ()) {
288                 return -1;
289         }
290
291         Config->set_use_vst (use_vst);
292
293         Profile = new RuntimeProfile;
294
295 #ifdef HAVE_LIBLO
296         if (setup_osc ()) {
297                 return -1;
298         }
299 #endif
300
301 #ifdef VST_SUPPORT
302         if (Config->get_use_vst() && fst_init ()) {
303                 return -1;
304         }
305 #endif
306
307         setup_hardware_optimization (try_optimization);
308
309         /* singleton - first object is "it" */
310         new PluginManager ();
311         
312         /* singleton - first object is "it" */
313         new ControlProtocolManager ();
314         ControlProtocolManager::instance().discover_control_protocols ();
315
316         XMLNode* node;
317         if ((node = Config->control_protocol_state()) != 0) {
318                 ControlProtocolManager::instance().set_state (*node);
319         }
320         
321         BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
322
323         return 0;
324 }
325
326 int
327 ARDOUR::cleanup ()
328 {
329         delete Library;
330         lrdf_cleanup ();
331         delete &ControlProtocolManager::instance();
332         return 0;
333 }
334
335
336 microseconds_t
337 ARDOUR::get_microseconds ()
338 {
339         /* XXX need JACK to export its functionality */
340
341         struct timeval now;
342         gettimeofday (&now, 0);
343         return now.tv_sec * 1000000ULL + now.tv_usec;
344 }
345
346 ARDOUR::Change
347 ARDOUR::new_change ()
348 {
349         Change c;
350         static uint32_t change_bit = 1;
351
352         /* catch out-of-range */
353         if (!change_bit)
354         {
355                 fatal << _("programming error: ")
356                         << "change_bit out of range in ARDOUR::new_change()"
357                         << endmsg;
358                 /*NOTREACHED*/
359         }
360
361         c = Change (change_bit);
362         change_bit <<= 1;       // if it shifts too far, change_bit == 0
363
364         return c;
365 }
366
367 string
368 ARDOUR::get_ardour_revision ()
369 {
370         return "$Rev$";
371 }
372
373 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
374 {
375         old = strdup (setlocale (LC_NUMERIC, NULL));
376         if (strcmp (old, str)) {
377                 setlocale (LC_NUMERIC, str);
378         } 
379 }
380
381 ARDOUR::LocaleGuard::~LocaleGuard ()
382 {
383         setlocale (LC_NUMERIC, old);
384         free ((char*)old);
385 }
386
387 void
388 ARDOUR::setup_fpu ()
389 {
390 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
391
392         int MXCSR;
393         FPU fpu;
394
395         /* XXX use real code to determine if the processor supports
396            DenormalsAreZero and FlushToZero
397         */
398         
399         if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
400                 return;
401         }
402
403         MXCSR  = _mm_getcsr();
404
405         switch (Config->get_denormal_model()) {
406         case DenormalNone:
407                 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
408                 break;
409
410         case DenormalFTZ:
411                 if (fpu.has_flush_to_zero()) {
412                         MXCSR |= _MM_FLUSH_ZERO_ON;
413                 }
414                 break;
415
416         case DenormalDAZ:
417                 MXCSR &= ~_MM_FLUSH_ZERO_ON;
418                 if (fpu.has_denormals_are_zero()) {
419                         MXCSR |= 0x8000;
420                 }
421                 break;
422                 
423         case DenormalFTZDAZ:
424                 if (fpu.has_flush_to_zero()) {
425                         if (fpu.has_denormals_are_zero()) {
426                                 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
427                         } else {
428                                 MXCSR |= _MM_FLUSH_ZERO_ON;
429                         }
430                 }
431                 break;
432         }
433
434         _mm_setcsr (MXCSR);
435
436 #endif
437 }
438
439 ARDOUR::OverlapType
440 ARDOUR::coverage (nframes_t sa, nframes_t ea, 
441                   nframes_t sb, nframes_t eb)
442 {
443         /* OverlapType returned reflects how the second (B)
444            range overlaps the first (A).
445
446            The diagrams show various relative placements
447            of A and B for each OverlapType.
448
449            Notes:
450               Internal: the start points cannot coincide
451               External: the start and end points can coincide
452               Start: end points can coincide
453               End: start points can coincide
454
455            XXX Logically, Internal should disallow end
456            point equality.
457         */
458
459         /*
460              |--------------------|   A
461                   |------|            B
462                 |-----------------|   B
463
464
465              "B is internal to A"               
466
467         */
468 #ifdef OLD_COVERAGE
469         if ((sb >= sa) && (eb <= ea)) {
470 #else
471         if ((sb > sa) && (eb <= ea)) {
472 #endif
473                 return OverlapInternal;
474         }
475
476         /*
477              |--------------------|   A
478            ----|                      B
479            -----------------------|   B
480            --|                        B
481            
482              "B overlaps the start of A"
483
484         */
485
486         if ((eb >= sa) && (eb <= ea)) {
487                 return OverlapStart;
488         }
489         /* 
490              |---------------------|  A
491                    |----------------- B
492              |----------------------- B    
493                                    |- B
494
495             "B overlaps the end of A"                              
496
497         */
498         if ((sb >= sa) && (sb <= ea)) {
499                 return OverlapEnd;
500         }
501         /*
502              |--------------------|     A
503            --------------------------  B   
504              |-----------------------  B
505             ----------------------|    B
506              |--------------------|    B
507
508
509            "B overlaps all of A"
510         */
511         if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
512                 return OverlapExternal;
513         }
514
515         return OverlapNone;
516 }
517
518 /* not sure where to put these */
519
520 template<class T>
521 std::istream& int_to_type (std::istream& o, T& hf) {
522         int val;
523         o >> val;
524         hf = (T) val;
525         return o;
526 }
527
528 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
529 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
530 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
531 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
532 std::istream& operator>>(std::istream& o, RemoteModel& var) { return int_to_type<RemoteModel> (o, var); }
533 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
534 std::istream& operator>>(std::istream& o, SoloModel& var) { return int_to_type<SoloModel> (o, var); }
535 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
536 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
537 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
538 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
539 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }
540 std::istream& operator>>(std::istream& o, SmpteFormat& var) { return int_to_type<SmpteFormat> (o, var); }
541 std::istream& operator>>(std::istream& o, DenormalModel& var) { return int_to_type<DenormalModel> (o, var); }
542