375068b9156e3ada8b3e8799953f02aacdef1a89
[ardour.git] / libs / ardour / globals.cc
1 /*
2     Copyright (C) 2000 Paul Davis 
3     Copyright (C) 2005 Sampo Savolainen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id$
20 */
21
22 #include <cstdio> // Needed so that libraptor (included in lrdf) won't complain
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <locale.h>
28
29 #ifdef VST_SUPPORT
30 #include <fst.h>
31 #endif
32
33 #include <lrdf.h>
34
35 #include <pbd/error.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
50 #include <ardour/mix.h>
51
52 #if defined (__APPLE__)
53        #include <Carbon/Carbon.h> // For Gestalt
54 #endif
55        
56 #include "i18n.h"
57
58 ARDOUR::Configuration* ARDOUR::Config = 0;
59 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
60
61 using namespace ARDOUR;
62 using namespace std;
63
64 MIDI::Port *default_mmc_port = 0;
65 MIDI::Port *default_mtc_port = 0;
66 MIDI::Port *default_midi_port = 0;
67
68 Change ARDOUR::StartChanged = ARDOUR::new_change ();
69 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
70 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
71 Change ARDOUR::NameChanged = ARDOUR::new_change ();
72 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
73
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                 asm volatile (
196                                  "mov $1, %%eax\n"
197                                  "pushl %%ebx\n"
198                                  "cpuid\n"
199                                  "popl %%ebx\n"
200                                  "andl $33554432, %%edx\n"
201                                  "movl %%edx, %0\n"
202                              : "=m" (use_sse)
203                              : 
204                          : "%eax", "%ecx", "%edx", "memory");
205
206                 if (use_sse) {
207                         cerr << "Enabling SSE optimized routines" << endl;
208         
209                         // SSE SET
210                         Session::compute_peak                   = x86_sse_compute_peak;
211                         Session::apply_gain_to_buffer   = x86_sse_apply_gain_to_buffer;
212                         Session::mix_buffers_with_gain  = x86_sse_mix_buffers_with_gain;
213                         Session::mix_buffers_no_gain    = x86_sse_mix_buffers_no_gain;
214
215                         generic_mix_functions = false;
216
217                 }
218
219  #elif defined (__APPLE__)
220                 long sysVersion = 0;
221
222                 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
223                         sysVersion = 0;
224
225                 if (sysVersion >= 0x00001040) { // Tiger at least
226                         Session::compute_peak           = veclib_compute_peak;
227                         Session::apply_gain_to_buffer   = veclib_apply_gain_to_buffer;
228                         Session::mix_buffers_with_gain  = veclib_mix_buffers_with_gain;
229                         Session::mix_buffers_no_gain    = veclib_mix_buffers_no_gain;
230
231                         generic_mix_functions = false;
232
233                         info << "Apple VecLib H/W specific optimizations in use" << endmsg;
234                 }
235 #endif
236         }
237
238         if (generic_mix_functions) {
239
240                 Session::compute_peak                   = compute_peak;
241                 Session::apply_gain_to_buffer   = apply_gain_to_buffer;
242                 Session::mix_buffers_with_gain  = mix_buffers_with_gain;
243                 Session::mix_buffers_no_gain    = mix_buffers_no_gain;
244                 
245                 info << "No H/W specific optimizations in use" << endmsg;
246         }
247         
248         lrdf_init();
249         Library = new AudioLibrary;
250
251         /* singleton - first object is "it" */
252         new PluginManager (engine);
253         
254         BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
255
256         return 0;
257 }
258
259 int
260 ARDOUR::cleanup ()
261 {
262         delete Library;
263         lrdf_cleanup ();
264         return 0;
265 }
266
267 ARDOUR::id_t
268 ARDOUR::new_id ()
269 {
270         return get_uid();
271 }
272
273 ARDOUR::Change
274 ARDOUR::new_change ()
275 {
276         Change c;
277         static uint32_t change_bit = 1;
278
279         /* XXX catch out-of-range */
280
281         c = Change (change_bit);
282         change_bit <<= 1;
283
284         return c;
285 }
286
287 static string
288 find_file (string name, string dir)
289 {
290         string path;
291
292         /* stop A: ~/.ardour/... */
293
294         path = getenv ("HOME");
295
296         if (path.length()) {
297                 
298                 path += "/.ardour/";
299
300                 /* try to ensure that the directory exists.
301                    failure doesn't mean much here.
302                 */
303
304                 mkdir (path.c_str(), 0755);
305
306                 path += name;
307
308                 if (access (path.c_str(), R_OK) == 0) {
309                         return path;
310                 }
311         }
312
313         /* stop B: dir/... */
314
315         path = dir;
316         path += "/ardour/";
317         path += name;
318         
319         if (access (path.c_str(), R_OK) == 0) {
320                 return path;
321         }
322
323         return "";
324 }
325
326 string
327 ARDOUR::find_config_file (string name)
328 {
329         char* envvar;
330         if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
331                 envvar = CONFIG_DIR;
332         }
333
334         return find_file (name, envvar);
335 }
336
337 string
338 ARDOUR::find_data_file (string name)
339 {
340         char* envvar;
341         if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
342                 envvar = DATA_DIR;
343         }
344
345         return find_file (name, envvar);
346 }
347
348 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
349 {
350         old = strdup (setlocale (LC_NUMERIC, NULL));
351         if (strcmp (old, str)) {
352                 setlocale (LC_NUMERIC, str);
353         } 
354 }
355
356 ARDOUR::LocaleGuard::~LocaleGuard ()
357 {
358         setlocale (LC_NUMERIC, old);
359         free ((char*)old);
360 }
361
362 ARDOUR::OverlapType
363 ARDOUR::coverage (jack_nframes_t sa, jack_nframes_t ea, 
364                   jack_nframes_t sb, jack_nframes_t eb)
365 {
366         /* OverlapType returned reflects how the second (B)
367            range overlaps the first (A).
368
369            The diagrams show various relative placements
370            of A and B for each OverlapType.
371
372            Notes:
373               Internal: the start points cannot coincide
374               External: the start and end points can coincide
375               Start: end points can coincide
376               End: start points can coincide
377
378            XXX Logically, Internal should disallow end
379            point equality.
380         */
381
382         /*
383              |--------------------|   A
384                   |------|            B
385                 |-----------------|   B
386
387
388              "B is internal to A"               
389
390         */
391 #ifdef OLD_COVERAGE
392         if ((sb >= sa) && (eb <= ea)) {
393 #else
394         if ((sb > sa) && (eb <= ea)) {
395 #endif
396                 return OverlapInternal;
397         }
398
399         /*
400              |--------------------|   A
401            ----|                      B
402            -----------------------|   B
403            --|                        B
404            
405              "B overlaps the start of A"
406
407         */
408
409         if ((eb >= sa) && (eb <= ea)) {
410                 return OverlapStart;
411         }
412         /* 
413              |---------------------|  A
414                    |----------------- B
415              |----------------------- B    
416                                    |- B
417
418             "B overlaps the end of A"                              
419
420         */
421         if ((sb >= sa) && (sb <= ea)) {
422                 return OverlapEnd;
423         }
424         /*
425              |--------------------|     A
426            --------------------------  B   
427              |-----------------------  B
428             ----------------------|    B
429              |--------------------|    B
430
431
432            "B overlaps all of A"
433         */
434         if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
435                 return OverlapExternal;
436         }
437
438         return OverlapNone;
439 }
440