f80c9dc287faa5c93bbc251cdbf4fddf9b3bff95
[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/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
131         if (nports > 1) {
132
133                 /* More than one port, so try using specific names for each port */
134
135                 map<string,Configuration::MidiPortDescriptor *>::iterator i;
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 = MIDI::Manager::instance()->port (0);
153                 }
154
155                 if (default_mtc_port == 0) {
156                         default_mtc_port = MIDI::Manager::instance()->port (0);
157                 }
158
159                 if (default_midi_port == 0) {
160                         default_midi_port = MIDI::Manager::instance()->port (0);
161                 }
162                 
163         } else {
164
165                 /* Only one port described, so use it for both MTC and MMC */
166
167                 default_mmc_port = MIDI::Manager::instance()->port (0);
168                 default_mtc_port = default_mmc_port;
169                 default_midi_port = default_mmc_port;
170         }
171
172         if (default_mmc_port == 0) {
173                 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name()) 
174                         << endmsg;
175                 return 0;
176         } 
177
178         if (default_mtc_port == 0) {
179                 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name()) 
180                         << endmsg;
181         }
182
183         if (default_midi_port == 0) {
184                 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name()) 
185                         << endmsg;
186         }
187
188         return 0;
189 }
190
191 int
192 ARDOUR::init (AudioEngine& engine, bool use_vst, bool try_optimization)
193 {
194         bool generic_mix_functions = true;
195
196         (void) bindtextdomain(PACKAGE, LOCALEDIR);
197
198         Config = new Configuration;
199
200         if (Config->load_state ()) {
201                 return -1;
202         }
203
204         Config->set_use_vst (use_vst);
205
206         if (setup_midi ()) {
207                 return -1;
208         }
209     
210 #ifdef HAVE_LIBLO
211         if (setup_osc ()) {
212                 return -1;
213         }
214 #endif
215
216 #ifdef VST_SUPPORT
217         if (Config->get_use_vst() && fst_init ()) {
218                 return -1;
219         }
220 #endif
221
222         if (try_optimization) {
223
224 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
225         
226                 unsigned int use_sse = 0;
227
228 #ifndef USE_X86_64_ASM
229                 asm volatile (
230                                  "mov $1, %%eax\n"
231                                  "pushl %%ebx\n"
232                                  "cpuid\n"
233                                  "popl %%ebx\n"
234                                  "andl $33554432, %%edx\n"
235                                  "movl %%edx, %0\n"
236                              : "=m" (use_sse)
237                              : 
238                          : "%eax", "%ecx", "%edx", "memory");
239 #else
240
241                 asm volatile (
242                                  "movq $1, %%rax\n"
243                                  "pushq %%rbx\n"
244                                  "cpuid\n"
245                                  "popq %%rbx\n"
246                                  "andq $33554432, %%rdx\n"
247                                  "movq %%rdx, %0\n"
248                              : "=m" (use_sse)
249                              : 
250                          : "%rax", "%rcx", "%rdx", "memory");
251
252 #endif /* USE_X86_64_ASM */
253                 
254                 if (use_sse) {
255                         cerr << "Enabling SSE optimized routines" << endl;
256         
257                         // SSE SET
258                         Session::compute_peak                   = x86_sse_compute_peak;
259                         Session::apply_gain_to_buffer   = x86_sse_apply_gain_to_buffer;
260                         Session::mix_buffers_with_gain  = x86_sse_mix_buffers_with_gain;
261                         Session::mix_buffers_no_gain    = x86_sse_mix_buffers_no_gain;
262
263                         generic_mix_functions = false;
264
265                 }
266
267 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
268                 long sysVersion = 0;
269
270                 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
271                         sysVersion = 0;
272
273                 if (sysVersion >= 0x00001040) { // Tiger at least
274                         Session::compute_peak           = veclib_compute_peak;
275                         Session::apply_gain_to_buffer   = veclib_apply_gain_to_buffer;
276                         Session::mix_buffers_with_gain  = veclib_mix_buffers_with_gain;
277                         Session::mix_buffers_no_gain    = veclib_mix_buffers_no_gain;
278
279                         generic_mix_functions = false;
280
281                         info << "Apple VecLib H/W specific optimizations in use" << endmsg;
282                 }
283 #endif
284         }
285
286         if (generic_mix_functions) {
287
288                 Session::compute_peak                   = compute_peak;
289                 Session::apply_gain_to_buffer   = apply_gain_to_buffer;
290                 Session::mix_buffers_with_gain  = mix_buffers_with_gain;
291                 Session::mix_buffers_no_gain    = mix_buffers_no_gain;
292                 
293                 info << "No H/W specific optimizations in use" << endmsg;
294         }
295         
296         lrdf_init();
297         Library = new AudioLibrary;
298
299         /* singleton - first object is "it" */
300         new PluginManager (engine);
301         
302         /* singleton - first object is "it" */
303         new ControlProtocolManager ();
304         ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
305
306         XMLNode* node;
307         if ((node = Config->control_protocol_state()) != 0) {
308                 ControlProtocolManager::instance().set_state (*node);
309         }
310         
311         BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
312
313         return 0;
314 }
315
316 int
317 ARDOUR::cleanup ()
318 {
319         delete Library;
320         lrdf_cleanup ();
321         delete &ControlProtocolManager::instance();
322         return 0;
323 }
324
325 ARDOUR::id_t
326 ARDOUR::new_id ()
327 {
328         return get_uid();
329 }
330
331 ARDOUR::Change
332 ARDOUR::new_change ()
333 {
334         Change c;
335         static uint32_t change_bit = 1;
336
337         /* catch out-of-range */
338         if (!change_bit)
339         {
340                 fatal << _("programming error: ")
341                         << "change_bit out of range in ARDOUR::new_change()"
342                         << endmsg;
343                 /*NOTREACHED*/
344         }
345
346         c = Change (change_bit);
347         change_bit <<= 1;       // if it shifts too far, change_bit == 0
348
349         return c;
350 }
351
352 string
353 ARDOUR::get_user_ardour_path ()
354 {
355         string path;
356         char* envvar;
357         
358         if ((envvar = getenv ("HOME")) == 0 || strlen (envvar) == 0) {
359                 return "/";
360         }
361                 
362         path = envvar;
363         path += "/.ardour2/";
364
365         /* create it if necessary */
366
367         mkdir (path.c_str (), 0755);
368
369         return path;
370 }
371
372 string
373 ARDOUR::get_system_data_path ()
374 {
375         string path;
376
377         path += DATA_DIR;
378         path += "/ardour2/";
379         
380         return path;
381 }
382
383 string
384 ARDOUR::get_system_module_path ()
385 {
386         string path;
387
388         path += MODULE_DIR;
389         path += "/ardour2/";
390         
391         return path;
392 }
393
394 static string
395 find_file (string name, string dir, string subdir = "")
396 {
397         string path;
398         char* envvar = getenv("ARDOUR_PATH");
399
400         /* 1st attempt: any directory in ARDOUR_PATH */
401         
402         if (envvar != 0) {
403
404                 vector<string> split_path;
405         
406                 split (envvar, split_path, ':');
407                 
408                 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
409                         path = *i;
410                         path += "/" + name;
411                         if (access (path.c_str(), R_OK) == 0) {
412                                 // cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
413                                 return path;
414                         }
415                 }
416         }
417
418         /* 2nd attempt: ~/.ardour/ */
419
420         path = get_user_ardour_path();
421                 
422         if (subdir.length()) {
423                 path += subdir + "/";
424         }
425                 
426         path += name;
427         if (access (path.c_str(), R_OK) == 0) {
428                 return path;
429         }
430
431         /* 3rd attempt: dir/... */
432         
433         path = dir;
434         path += "/ardour2/";
435         
436         if (subdir.length()) {
437                 path += subdir + "/";
438         }
439         
440         path += name;
441         
442         if (access (path.c_str(), R_OK) == 0) {
443                 return path;
444         }
445
446         return "";
447 }
448
449 string
450 ARDOUR::find_config_file (string name)
451 {
452         char* envvar;
453
454         if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
455                 envvar = CONFIG_DIR;
456         }
457
458         return find_file (name, envvar);
459 }
460
461 string
462 ARDOUR::find_data_file (string name, string subdir)
463 {
464         char* envvar;
465         if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
466                 envvar = DATA_DIR;
467         }
468
469         return find_file (name, envvar, subdir);
470 }
471
472 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
473 {
474         old = strdup (setlocale (LC_NUMERIC, NULL));
475         if (strcmp (old, str)) {
476                 setlocale (LC_NUMERIC, str);
477         } 
478 }
479
480 ARDOUR::LocaleGuard::~LocaleGuard ()
481 {
482         setlocale (LC_NUMERIC, old);
483         free ((char*)old);
484 }
485
486 ARDOUR::OverlapType
487 ARDOUR::coverage (jack_nframes_t sa, jack_nframes_t ea, 
488                   jack_nframes_t sb, jack_nframes_t eb)
489 {
490         /* OverlapType returned reflects how the second (B)
491            range overlaps the first (A).
492
493            The diagrams show various relative placements
494            of A and B for each OverlapType.
495
496            Notes:
497               Internal: the start points cannot coincide
498               External: the start and end points can coincide
499               Start: end points can coincide
500               End: start points can coincide
501
502            XXX Logically, Internal should disallow end
503            point equality.
504         */
505
506         /*
507              |--------------------|   A
508                   |------|            B
509                 |-----------------|   B
510
511
512              "B is internal to A"               
513
514         */
515 #ifdef OLD_COVERAGE
516         if ((sb >= sa) && (eb <= ea)) {
517 #else
518         if ((sb > sa) && (eb <= ea)) {
519 #endif
520                 return OverlapInternal;
521         }
522
523         /*
524              |--------------------|   A
525            ----|                      B
526            -----------------------|   B
527            --|                        B
528            
529              "B overlaps the start of A"
530
531         */
532
533         if ((eb >= sa) && (eb <= ea)) {
534                 return OverlapStart;
535         }
536         /* 
537              |---------------------|  A
538                    |----------------- B
539              |----------------------- B    
540                                    |- B
541
542             "B overlaps the end of A"                              
543
544         */
545         if ((sb >= sa) && (sb <= ea)) {
546                 return OverlapEnd;
547         }
548         /*
549              |--------------------|     A
550            --------------------------  B   
551              |-----------------------  B
552             ----------------------|    B
553              |--------------------|    B
554
555
556            "B overlaps all of A"
557         */
558         if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
559                 return OverlapExternal;
560         }
561
562         return OverlapNone;
563 }
564
565 /* not sure where to put these */
566
567 std::istream& operator>>(std::istream& o, HeaderFormat& hf) {
568         int val;
569         o >> val;
570         hf = (HeaderFormat) val;
571         return o;
572 }
573
574 std::istream& operator>>(std::istream& o, SampleFormat& sf) {
575         int val;
576         o >> val;
577         sf = (SampleFormat) val;
578         return o;
579 }