id_t becomes a fully-fledged object, UUID's used for IDs, generic MIDI now owns bindi...
[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
326 microseconds_t
327 ARDOUR::get_microseconds ()
328 {
329         /* XXX need JACK to export its functionality */
330
331         struct timeval now;
332         gettimeofday (&now, 0);
333         return now.tv_sec * 1000000ULL + now.tv_usec;
334 }
335
336 ARDOUR::Change
337 ARDOUR::new_change ()
338 {
339         Change c;
340         static uint32_t change_bit = 1;
341
342         /* catch out-of-range */
343         if (!change_bit)
344         {
345                 fatal << _("programming error: ")
346                         << "change_bit out of range in ARDOUR::new_change()"
347                         << endmsg;
348                 /*NOTREACHED*/
349         }
350
351         c = Change (change_bit);
352         change_bit <<= 1;       // if it shifts too far, change_bit == 0
353
354         return c;
355 }
356
357 string
358 ARDOUR::get_user_ardour_path ()
359 {
360         string path;
361         char* envvar;
362         
363         if ((envvar = getenv ("HOME")) == 0 || strlen (envvar) == 0) {
364                 return "/";
365         }
366                 
367         path = envvar;
368         path += "/.ardour2/";
369
370         /* create it if necessary */
371
372         mkdir (path.c_str (), 0755);
373
374         return path;
375 }
376
377 string
378 ARDOUR::get_system_data_path ()
379 {
380         string path;
381
382         path += DATA_DIR;
383         path += "/ardour2/";
384         
385         return path;
386 }
387
388 string
389 ARDOUR::get_system_module_path ()
390 {
391         string path;
392
393         path += MODULE_DIR;
394         path += "/ardour2/";
395         
396         return path;
397 }
398
399 static string
400 find_file (string name, string dir, string subdir = "")
401 {
402         string path;
403         char* envvar = getenv("ARDOUR_PATH");
404
405         /* 1st attempt: any directory in ARDOUR_PATH */
406         
407         if (envvar != 0) {
408
409                 vector<string> split_path;
410         
411                 split (envvar, split_path, ':');
412                 
413                 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
414                         path = *i;
415                         path += "/" + name;
416                         if (access (path.c_str(), R_OK) == 0) {
417                                 // cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
418                                 return path;
419                         }
420                 }
421         }
422
423         /* 2nd attempt: ~/.ardour/ */
424
425         path = get_user_ardour_path();
426                 
427         if (subdir.length()) {
428                 path += subdir + "/";
429         }
430                 
431         path += name;
432         if (access (path.c_str(), R_OK) == 0) {
433                 return path;
434         }
435
436         /* 3rd attempt: dir/... */
437         
438         path = dir;
439         path += "/ardour2/";
440         
441         if (subdir.length()) {
442                 path += subdir + "/";
443         }
444         
445         path += name;
446         
447         if (access (path.c_str(), R_OK) == 0) {
448                 return path;
449         }
450
451         return "";
452 }
453
454 string
455 ARDOUR::find_config_file (string name)
456 {
457         char* envvar;
458
459         if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
460                 envvar = CONFIG_DIR;
461         }
462
463         return find_file (name, envvar);
464 }
465
466 string
467 ARDOUR::find_data_file (string name, string subdir)
468 {
469         char* envvar;
470         if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
471                 envvar = DATA_DIR;
472         }
473
474         return find_file (name, envvar, subdir);
475 }
476
477 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
478 {
479         old = strdup (setlocale (LC_NUMERIC, NULL));
480         if (strcmp (old, str)) {
481                 setlocale (LC_NUMERIC, str);
482         } 
483 }
484
485 ARDOUR::LocaleGuard::~LocaleGuard ()
486 {
487         setlocale (LC_NUMERIC, old);
488         free ((char*)old);
489 }
490
491 ARDOUR::OverlapType
492 ARDOUR::coverage (jack_nframes_t sa, jack_nframes_t ea, 
493                   jack_nframes_t sb, jack_nframes_t eb)
494 {
495         /* OverlapType returned reflects how the second (B)
496            range overlaps the first (A).
497
498            The diagrams show various relative placements
499            of A and B for each OverlapType.
500
501            Notes:
502               Internal: the start points cannot coincide
503               External: the start and end points can coincide
504               Start: end points can coincide
505               End: start points can coincide
506
507            XXX Logically, Internal should disallow end
508            point equality.
509         */
510
511         /*
512              |--------------------|   A
513                   |------|            B
514                 |-----------------|   B
515
516
517              "B is internal to A"               
518
519         */
520 #ifdef OLD_COVERAGE
521         if ((sb >= sa) && (eb <= ea)) {
522 #else
523         if ((sb > sa) && (eb <= ea)) {
524 #endif
525                 return OverlapInternal;
526         }
527
528         /*
529              |--------------------|   A
530            ----|                      B
531            -----------------------|   B
532            --|                        B
533            
534              "B overlaps the start of A"
535
536         */
537
538         if ((eb >= sa) && (eb <= ea)) {
539                 return OverlapStart;
540         }
541         /* 
542              |---------------------|  A
543                    |----------------- B
544              |----------------------- B    
545                                    |- B
546
547             "B overlaps the end of A"                              
548
549         */
550         if ((sb >= sa) && (sb <= ea)) {
551                 return OverlapEnd;
552         }
553         /*
554              |--------------------|     A
555            --------------------------  B   
556              |-----------------------  B
557             ----------------------|    B
558              |--------------------|    B
559
560
561            "B overlaps all of A"
562         */
563         if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
564                 return OverlapExternal;
565         }
566
567         return OverlapNone;
568 }
569
570 /* not sure where to put these */
571
572 std::istream& operator>>(std::istream& o, HeaderFormat& hf) {
573         int val;
574         o >> val;
575         hf = (HeaderFormat) val;
576         return o;
577 }
578
579 std::istream& operator>>(std::istream& o, SampleFormat& sf) {
580         int val;
581         o >> val;
582         sf = (SampleFormat) val;
583         return o;
584 }