remove all lines to avoid recompiles after commits
[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 #include <cstdio> // Needed so that libraptor (included in lrdf) won't complain
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <locale.h>
26
27 #ifdef VST_SUPPORT
28 #include <fst.h>
29 #endif
30
31 #include <lrdf.h>
32
33 #include <pbd/error.h>
34 #include <pbd/id.h>
35 #include <pbd/strsplit.h>
36
37 #include <midi++/port.h>
38 #include <midi++/port_request.h>
39 #include <midi++/manager.h>
40 #include <midi++/mmc.h>
41
42 #include <ardour/ardour.h>
43 #include <ardour/audio_library.h>
44 #include <ardour/configuration.h>
45 #include <ardour/plugin_manager.h>
46 #include <ardour/audiosource.h>
47 #include <ardour/utils.h>
48 #include <ardour/session.h>
49 #include <ardour/control_protocol_manager.h>
50
51 #ifdef HAVE_LIBLO
52 #include <ardour/osc.h>
53 #endif
54
55 #include <ardour/mix.h>
56
57 #if defined (__APPLE__)
58        #include <Carbon/Carbon.h> // For Gestalt
59 #endif
60        
61 #include "i18n.h"
62
63 ARDOUR::Configuration* ARDOUR::Config = 0;
64 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
65
66 #ifdef HAVE_LIBLO
67 ARDOUR::OSC* ARDOUR::osc = 0;
68 #endif
69
70 using namespace ARDOUR;
71 using namespace std;
72 using namespace PBD;
73
74 MIDI::Port *default_mmc_port = 0;
75 MIDI::Port *default_mtc_port = 0;
76 MIDI::Port *default_midi_port = 0;
77
78 Change ARDOUR::StartChanged = ARDOUR::new_change ();
79 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
80 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
81 Change ARDOUR::NameChanged = ARDOUR::new_change ();
82 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
83
84 #ifdef HAVE_LIBLO
85 static int
86 setup_osc ()
87 {
88         /* no real cost to creating this object, and it avoids
89            conditionals anywhere that uses it 
90         */
91         
92         osc = new OSC (Config->get_osc_port());
93         
94         if (Config->get_use_osc ()) {
95                 return osc->start ();
96         } else {
97                 return 0;
98         }
99 }
100 #endif
101
102 static int 
103 setup_midi ()
104 {
105         std::map<string,Configuration::MidiPortDescriptor*>::iterator i;
106         int nports;
107
108         if ((nports = Config->midi_ports.size()) == 0) {
109                 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
110                 return 0;
111         }
112
113         for (i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
114                 Configuration::MidiPortDescriptor* port_descriptor;
115
116                 port_descriptor = (*i).second;
117
118                 MIDI::PortRequest request (port_descriptor->device, 
119                                            port_descriptor->tag, 
120                                            port_descriptor->mode, 
121                                            port_descriptor->type);
122
123                 if (request.status != MIDI::PortRequest::OK) {
124                         error << string_compose(_("MIDI port specifications for \"%1\" are not understandable."), port_descriptor->tag) << endmsg;
125                         continue;
126                 }
127                 
128                 MIDI::Manager::instance()->add_port (request);
129
130                 nports++;
131         }
132
133         if (nports > 1) {
134
135                 /* More than one port, so try using specific names for each port */
136
137                 map<string,Configuration::MidiPortDescriptor *>::iterator i;
138
139                 if (Config->get_mmc_port_name() != N_("default")) {
140                         default_mmc_port =  MIDI::Manager::instance()->port (Config->get_mmc_port_name());
141                 } 
142
143                 if (Config->get_mtc_port_name() != N_("default")) {
144                         default_mtc_port =  MIDI::Manager::instance()->port (Config->get_mtc_port_name());
145                 } 
146
147                 if (Config->get_midi_port_name() != N_("default")) {
148                         default_midi_port =  MIDI::Manager::instance()->port (Config->get_midi_port_name());
149                 } 
150                 
151                 /* If that didn't work, just use the first listed port */
152
153                 if (default_mmc_port == 0) {
154                         default_mmc_port = MIDI::Manager::instance()->port (0);
155                 }
156
157                 if (default_mtc_port == 0) {
158                         default_mtc_port = MIDI::Manager::instance()->port (0);
159                 }
160
161                 if (default_midi_port == 0) {
162                         default_midi_port = MIDI::Manager::instance()->port (0);
163                 }
164                 
165         } else {
166
167                 /* Only one port described, so use it for both MTC and MMC */
168
169                 default_mmc_port = MIDI::Manager::instance()->port (0);
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
199         if (try_optimization) {
200
201 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
202         
203                 unsigned long use_sse = 0;
204
205 #ifndef USE_X86_64_ASM
206                 asm (
207                                  "mov $1, %%eax\n"
208                                  "pushl %%ebx\n"
209                                  "cpuid\n"
210                                  "movl %%edx, %0\n"
211                                  "popl %%ebx\n"
212                              : "=r" (use_sse)
213                              : 
214                          : "%eax", "%ecx", "%edx", "memory");
215
216 #else
217
218                 asm (
219                                  "pushq %%rbx\n"
220                                  "movq $1, %%rax\n"
221                                  "cpuid\n"
222                                  "movq %%rdx, %0\n"
223                                  "popq %%rbx\n"
224                              : "=r" (use_sse)
225                              : 
226                          : "%rax", "%rcx", "%rdx", "memory");
227
228 #endif /* USE_X86_64_ASM */
229                 use_sse &= (1 << 25); // bit 25 = SSE support
230                 
231                 if (use_sse) {
232                         info << "Using SSE optimized routines" << endmsg;
233         
234                         // SSE SET
235                         Session::compute_peak           = x86_sse_compute_peak;
236                         Session::apply_gain_to_buffer   = x86_sse_apply_gain_to_buffer;
237                         Session::mix_buffers_with_gain  = x86_sse_mix_buffers_with_gain;
238                         Session::mix_buffers_no_gain    = x86_sse_mix_buffers_no_gain;
239
240                         generic_mix_functions = false;
241
242                 }
243
244 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
245                 long sysVersion = 0;
246
247                 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
248                         sysVersion = 0;
249
250                 if (sysVersion >= 0x00001040) { // Tiger at least
251                         Session::compute_peak           = veclib_compute_peak;
252                         Session::apply_gain_to_buffer   = veclib_apply_gain_to_buffer;
253                         Session::mix_buffers_with_gain  = veclib_mix_buffers_with_gain;
254                         Session::mix_buffers_no_gain    = veclib_mix_buffers_no_gain;
255
256                         generic_mix_functions = false;
257
258                         info << "Apple VecLib H/W specific optimizations in use" << endmsg;
259                 }
260 #endif
261         }
262
263         if (generic_mix_functions) {
264
265                 Session::compute_peak                   = compute_peak;
266                 Session::apply_gain_to_buffer   = apply_gain_to_buffer;
267                 Session::mix_buffers_with_gain  = mix_buffers_with_gain;
268                 Session::mix_buffers_no_gain    = mix_buffers_no_gain;
269                 
270                 info << "No H/W specific optimizations in use" << endmsg;
271         }
272 }
273
274 int
275 ARDOUR::init (bool use_vst, bool try_optimization)
276 {
277         extern void setup_enum_writer ();
278
279         (void) bindtextdomain(PACKAGE, LOCALEDIR);
280
281         setup_enum_writer ();
282
283         lrdf_init();
284         Library = new AudioLibrary;
285
286         Config = new Configuration;
287
288         if (Config->load_state ()) {
289                 return -1;
290         }
291
292         Config->set_use_vst (use_vst);
293
294         if (setup_midi ()) {
295                 return -1;
296         }
297     
298 #ifdef HAVE_LIBLO
299         if (setup_osc ()) {
300                 return -1;
301         }
302 #endif
303
304 #ifdef VST_SUPPORT
305         if (Config->get_use_vst() && fst_init ()) {
306                 return -1;
307         }
308 #endif
309
310         setup_hardware_optimization (try_optimization);
311
312         /* singleton - first object is "it" */
313         new PluginManager ();
314         
315         /* singleton - first object is "it" */
316         new ControlProtocolManager ();
317         ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
318
319         XMLNode* node;
320         if ((node = Config->control_protocol_state()) != 0) {
321                 ControlProtocolManager::instance().set_state (*node);
322         }
323         
324         BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
325
326         return 0;
327 }
328
329 int
330 ARDOUR::cleanup ()
331 {
332         delete Library;
333         lrdf_cleanup ();
334         delete &ControlProtocolManager::instance();
335         return 0;
336 }
337
338
339 microseconds_t
340 ARDOUR::get_microseconds ()
341 {
342         /* XXX need JACK to export its functionality */
343
344         struct timeval now;
345         gettimeofday (&now, 0);
346         return now.tv_sec * 1000000ULL + now.tv_usec;
347 }
348
349 ARDOUR::Change
350 ARDOUR::new_change ()
351 {
352         Change c;
353         static uint32_t change_bit = 1;
354
355         /* catch out-of-range */
356         if (!change_bit)
357         {
358                 fatal << _("programming error: ")
359                         << "change_bit out of range in ARDOUR::new_change()"
360                         << endmsg;
361                 /*NOTREACHED*/
362         }
363
364         c = Change (change_bit);
365         change_bit <<= 1;       // if it shifts too far, change_bit == 0
366
367         return c;
368 }
369
370 string
371 ARDOUR::get_ardour_revision ()
372 {
373         return "$Rev$";
374 }
375
376 string
377 ARDOUR::get_user_ardour_path ()
378 {
379         string path;
380         char* envvar;
381         
382         if ((envvar = getenv ("HOME")) == 0 || strlen (envvar) == 0) {
383                 return "/";
384         }
385                 
386         path = envvar;
387         path += "/.ardour2/";
388
389         /* create it if necessary */
390
391         if (g_mkdir_with_parents (path.c_str (), 0755)) {
392                 throw exception ();
393         }
394
395         return path;
396 }
397
398 string
399 ARDOUR::get_system_data_path ()
400 {
401         string path;
402
403         char *envvar;
404
405         if ((envvar = getenv ("ARDOUR_DATA_PATH")) != 0) {
406                 path = envvar;
407         } else {
408                 path += DATA_DIR;
409                 path += "/ardour2/";
410         }
411         
412         return path;
413 }
414
415 string
416 ARDOUR::get_system_module_path ()
417 {
418         string path;
419         char *envvar;
420
421         if ((envvar = getenv ("ARDOUR_MODULE_PATH")) != 0) {
422                 path = envvar;
423         } else {
424                 path += MODULE_DIR;
425                 path += "/ardour2/";
426         }
427         
428         return path;
429 }
430
431 static string
432 find_file (string name, string dir, string subdir = "")
433 {
434         string path;
435         char* envvar = getenv("ARDOUR_PATH");
436
437         /* 1st attempt: any directory in ARDOUR_PATH */
438         
439         if (envvar != 0) {
440
441                 vector<string> split_path;
442         
443                 split (envvar, split_path, ':');
444                 
445                 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
446                         path = *i;
447                         path += "/" + name;
448                         if (access (path.c_str(), R_OK) == 0) {
449                                 // cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
450                                 return path;
451                         }
452                 }
453         }
454
455         /* 2nd attempt: ~/.ardour/ */
456
457         path = get_user_ardour_path();
458                 
459         if (subdir.length()) {
460                 path += subdir + "/";
461         }
462                 
463         path += name;
464         if (access (path.c_str(), R_OK) == 0) {
465                 return path;
466         }
467
468         /* 3rd attempt: dir/... */
469         
470         path = dir;
471         path += "/ardour2/";
472         
473         if (subdir.length()) {
474                 path += subdir + "/";
475         }
476         
477         path += name;
478         
479         if (access (path.c_str(), R_OK) == 0) {
480                 return path;
481         }
482
483         return "";
484 }
485
486 string
487 ARDOUR::find_config_file (string name)
488 {
489         char* envvar;
490
491         if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
492                 envvar = CONFIG_DIR;
493         }
494
495         return find_file (name, envvar);
496 }
497
498 string
499 ARDOUR::find_data_file (string name, string subdir)
500 {
501         char* envvar;
502         if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
503                 envvar = DATA_DIR;
504         }
505
506         return find_file (name, envvar, subdir);
507 }
508
509 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
510 {
511         old = strdup (setlocale (LC_NUMERIC, NULL));
512         if (strcmp (old, str)) {
513                 setlocale (LC_NUMERIC, str);
514         } 
515 }
516
517 ARDOUR::LocaleGuard::~LocaleGuard ()
518 {
519         setlocale (LC_NUMERIC, old);
520         free ((char*)old);
521 }
522
523 ARDOUR::OverlapType
524 ARDOUR::coverage (nframes_t sa, nframes_t ea, 
525                   nframes_t sb, nframes_t eb)
526 {
527         /* OverlapType returned reflects how the second (B)
528            range overlaps the first (A).
529
530            The diagrams show various relative placements
531            of A and B for each OverlapType.
532
533            Notes:
534               Internal: the start points cannot coincide
535               External: the start and end points can coincide
536               Start: end points can coincide
537               End: start points can coincide
538
539            XXX Logically, Internal should disallow end
540            point equality.
541         */
542
543         /*
544              |--------------------|   A
545                   |------|            B
546                 |-----------------|   B
547
548
549              "B is internal to A"               
550
551         */
552 #ifdef OLD_COVERAGE
553         if ((sb >= sa) && (eb <= ea)) {
554 #else
555         if ((sb > sa) && (eb <= ea)) {
556 #endif
557                 return OverlapInternal;
558         }
559
560         /*
561              |--------------------|   A
562            ----|                      B
563            -----------------------|   B
564            --|                        B
565            
566              "B overlaps the start of A"
567
568         */
569
570         if ((eb >= sa) && (eb <= ea)) {
571                 return OverlapStart;
572         }
573         /* 
574              |---------------------|  A
575                    |----------------- B
576              |----------------------- B    
577                                    |- B
578
579             "B overlaps the end of A"                              
580
581         */
582         if ((sb >= sa) && (sb <= ea)) {
583                 return OverlapEnd;
584         }
585         /*
586              |--------------------|     A
587            --------------------------  B   
588              |-----------------------  B
589             ----------------------|    B
590              |--------------------|    B
591
592
593            "B overlaps all of A"
594         */
595         if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
596                 return OverlapExternal;
597         }
598
599         return OverlapNone;
600 }
601
602 /* not sure where to put these */
603
604 template<class T>
605 std::istream& int_to_type (std::istream& o, T& hf) {
606         int val;
607         o >> val;
608         hf = (T) val;
609         return o;
610 }
611
612 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
613 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
614 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
615 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
616 std::istream& operator>>(std::istream& o, RemoteModel& var) { return int_to_type<RemoteModel> (o, var); }
617 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
618 std::istream& operator>>(std::istream& o, SoloModel& var) { return int_to_type<SoloModel> (o, var); }
619 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
620 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
621 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
622 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
623 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }
624 std::istream& operator>>(std::istream& o, SmpteFormat& var) { return int_to_type<SmpteFormat> (o, var); }
625