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