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