Remove functions from ardour/ardour.h that are now unused
[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
19 #include <cstdio> // Needed so that libraptor (included in lrdf) won't complain
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <locale.h>
25
26 #ifdef VST_SUPPORT
27 #include <fst.h>
28 #endif
29
30 #ifdef __SSE__
31 #include <xmmintrin.h>
32 #endif
33
34 #include <lrdf.h>
35
36 #include <pbd/error.h>
37 #include <pbd/id.h>
38 #include <pbd/strsplit.h>
39 #include <pbd/fpu.h>
40
41 #include <midi++/port.h>
42 #include <midi++/port_request.h>
43 #include <midi++/manager.h>
44 #include <midi++/mmc.h>
45
46 #include <ardour/ardour.h>
47 #include <ardour/audio_library.h>
48 #include <ardour/configuration.h>
49 #include <ardour/profile.h>
50 #include <ardour/plugin_manager.h>
51 #include <ardour/audiosource.h>
52 #include <ardour/utils.h>
53 #include <ardour/session.h>
54 #include <ardour/control_protocol_manager.h>
55 #include <ardour/audioengine.h>
56
57 #ifdef HAVE_LIBLO
58 #include <ardour/osc.h>
59 #endif
60
61 #include <ardour/mix.h>
62 #include <ardour/runtime_functions.h>
63
64 #if defined (__APPLE__)
65        #include <Carbon/Carbon.h> // For Gestalt
66 #endif
67        
68 #include "i18n.h"
69
70 ARDOUR::Configuration* ARDOUR::Config = 0;
71 ARDOUR::RuntimeProfile* ARDOUR::Profile = 0;
72 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
73
74 #ifdef HAVE_LIBLO
75 ARDOUR::OSC* ARDOUR::osc = 0;
76 #endif
77
78 using namespace ARDOUR;
79 using namespace std;
80 using namespace PBD;
81
82 MIDI::Port *default_mmc_port = 0;
83 MIDI::Port *default_mtc_port = 0;
84 MIDI::Port *default_midi_port = 0;
85
86 Change ARDOUR::StartChanged = ARDOUR::new_change ();
87 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
88 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
89 Change ARDOUR::NameChanged = ARDOUR::new_change ();
90 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
91
92 compute_peak_t                  ARDOUR::compute_peak            = 0;
93 find_peaks_t                    ARDOUR::find_peaks              = 0;
94 apply_gain_to_buffer_t          ARDOUR::apply_gain_to_buffer    = 0;
95 mix_buffers_with_gain_t         ARDOUR::mix_buffers_with_gain   = 0;
96 mix_buffers_no_gain_t           ARDOUR::mix_buffers_no_gain     = 0;
97
98 #ifdef HAVE_LIBLO
99 static int
100 setup_osc ()
101 {
102         /* no real cost to creating this object, and it avoids
103            conditionals anywhere that uses it 
104         */
105         
106         osc = new OSC (Config->get_osc_port());
107         
108         if (Config->get_use_osc ()) {
109                 return osc->start ();
110         } else {
111                 return 0;
112         }
113 }
114 #endif
115
116 int 
117 ARDOUR::setup_midi (AudioEngine& engine)
118 {
119         std::map<string,Configuration::MidiPortDescriptor*>::iterator i;
120         int nports;
121
122         if ((nports = Config->midi_ports.size()) == 0) {
123                 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
124                 return 0;
125         }
126
127         MIDI::Manager::instance()->set_api_data(engine.jack());
128
129         for (i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
130                 Configuration::MidiPortDescriptor* port_descriptor;
131
132                 port_descriptor = (*i).second;
133
134                 MIDI::PortRequest request (port_descriptor->device, 
135                                            port_descriptor->tag, 
136                                            port_descriptor->mode, 
137                                            port_descriptor->type);
138
139                 if (request.status != MIDI::PortRequest::OK) {
140                         error << string_compose(_("MIDI port specifications for \"%1\" are not understandable."), port_descriptor->tag) << endmsg;
141                         continue;
142                 }
143                 
144                 MIDI::Manager::instance()->add_port (request);
145
146                 nports++;
147         }
148
149         if (nports > 1) {
150
151                 /* More than one port, so try using specific names for each port */
152
153                 map<string,Configuration::MidiPortDescriptor *>::iterator i;
154
155                 if (Config->get_mmc_port_name() != N_("default")) {
156                         default_mmc_port =  MIDI::Manager::instance()->port (Config->get_mmc_port_name());
157                 } 
158
159                 if (Config->get_mtc_port_name() != N_("default")) {
160                         default_mtc_port =  MIDI::Manager::instance()->port (Config->get_mtc_port_name());
161                 } 
162
163                 if (Config->get_midi_port_name() != N_("default")) {
164                         default_midi_port =  MIDI::Manager::instance()->port (Config->get_midi_port_name());
165                 } 
166                 
167                 /* If that didn't work, just use the first listed port */
168
169                 if (default_mmc_port == 0) {
170                         default_mmc_port = MIDI::Manager::instance()->port (0);
171                 }
172
173                 if (default_mtc_port == 0) {
174                         default_mtc_port = MIDI::Manager::instance()->port (0);
175                 }
176
177                 if (default_midi_port == 0) {
178                         default_midi_port = MIDI::Manager::instance()->port (0);
179                 }
180                 
181         } else {
182
183                 /* Only one port described, so use it for both MTC and MMC */
184
185                 default_mmc_port = MIDI::Manager::instance()->port (0);
186                 default_mtc_port = default_mmc_port;
187                 default_midi_port = default_mmc_port;
188         }
189
190         if (default_mmc_port == 0) {
191                 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name()) 
192                         << endmsg;
193                 return 0;
194         } 
195
196         if (default_mtc_port == 0) {
197                 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name()) 
198                         << endmsg;
199         }
200
201         if (default_midi_port == 0) {
202                 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name()) 
203                         << endmsg;
204         }
205
206         return 0;
207 }
208         
209 void
210 setup_hardware_optimization (bool try_optimization)
211 {
212         bool generic_mix_functions = true;
213         FPU fpu;
214
215         if (try_optimization) {
216
217 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
218                 
219                 if (fpu.has_sse()) {
220
221                         info << "Using SSE optimized routines" << endmsg;
222         
223                         // SSE SET
224                         compute_peak            = x86_sse_compute_peak;
225                         find_peaks              = x86_sse_find_peaks;
226                         apply_gain_to_buffer    = x86_sse_apply_gain_to_buffer;
227                         mix_buffers_with_gain   = x86_sse_mix_buffers_with_gain;
228                         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                         compute_peak           = veclib_compute_peak;
242                         find_peaks             = veclib_find_peaks;
243                         apply_gain_to_buffer   = veclib_apply_gain_to_buffer;
244                         mix_buffers_with_gain  = veclib_mix_buffers_with_gain;
245                         mix_buffers_no_gain    = veclib_mix_buffers_no_gain;
246
247                         generic_mix_functions = false;
248
249                         info << "Apple VecLib H/W specific optimizations in use" << endmsg;
250                 }
251 #endif
252         }
253
254         if (generic_mix_functions) {
255                 
256                 compute_peak            = default_compute_peak;
257                 find_peaks              = default_find_peaks;
258                 apply_gain_to_buffer    = default_apply_gain_to_buffer;
259                 mix_buffers_with_gain   = default_mix_buffers_with_gain;
260                 mix_buffers_no_gain     = default_mix_buffers_no_gain;
261                 
262                 info << "No H/W specific optimizations in use" << endmsg;
263         }
264
265         setup_fpu ();
266
267 }
268
269 int
270 ARDOUR::init (bool use_vst, bool try_optimization)
271 {
272         extern void setup_enum_writer ();
273
274         (void) bindtextdomain(PACKAGE, LOCALEDIR);
275
276         setup_enum_writer ();
277
278         lrdf_init();
279         Library = new AudioLibrary;
280
281         Config = new Configuration;
282
283         if (Config->load_state ()) {
284                 return -1;
285         }
286
287         Config->set_use_vst (use_vst);
288
289         Profile = new RuntimeProfile;
290
291 #ifdef HAVE_LIBLO
292         if (setup_osc ()) {
293                 return -1;
294         }
295 #endif
296
297 #ifdef VST_SUPPORT
298         if (Config->get_use_vst() && fst_init ()) {
299                 return -1;
300         }
301 #endif
302
303         setup_hardware_optimization (try_optimization);
304
305         /* singleton - first object is "it" */
306         new PluginManager ();
307         
308         /* singleton - first object is "it" */
309         new ControlProtocolManager ();
310         ControlProtocolManager::instance().discover_control_protocols ();
311
312         XMLNode* node;
313         if ((node = Config->control_protocol_state()) != 0) {
314                 ControlProtocolManager::instance().set_state (*node);
315         }
316         
317         BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
318
319         return 0;
320 }
321
322 int
323 ARDOUR::cleanup ()
324 {
325         delete Library;
326         lrdf_cleanup ();
327         delete &ControlProtocolManager::instance();
328         return 0;
329 }
330
331
332 microseconds_t
333 ARDOUR::get_microseconds ()
334 {
335         /* XXX need JACK to export its functionality */
336
337         struct timeval now;
338         gettimeofday (&now, 0);
339         return now.tv_sec * 1000000ULL + now.tv_usec;
340 }
341
342 ARDOUR::Change
343 ARDOUR::new_change ()
344 {
345         Change c;
346         static uint32_t change_bit = 1;
347
348         /* catch out-of-range */
349         if (!change_bit)
350         {
351                 fatal << _("programming error: ")
352                         << "change_bit out of range in ARDOUR::new_change()"
353                         << endmsg;
354                 /*NOTREACHED*/
355         }
356
357         c = Change (change_bit);
358         change_bit <<= 1;       // if it shifts too far, change_bit == 0
359
360         return c;
361 }
362
363 string
364 ARDOUR::get_ardour_revision ()
365 {
366         return "$Rev$";
367 }
368
369 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
370 {
371         old = strdup (setlocale (LC_NUMERIC, NULL));
372         if (strcmp (old, str)) {
373                 setlocale (LC_NUMERIC, str);
374         } 
375 }
376
377 ARDOUR::LocaleGuard::~LocaleGuard ()
378 {
379         setlocale (LC_NUMERIC, old);
380         free ((char*)old);
381 }
382
383 void
384 ARDOUR::setup_fpu ()
385 {
386 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
387
388         int MXCSR;
389         FPU fpu;
390
391         /* XXX use real code to determine if the processor supports
392            DenormalsAreZero and FlushToZero
393         */
394         
395         if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
396                 return;
397         }
398
399         MXCSR  = _mm_getcsr();
400
401         switch (Config->get_denormal_model()) {
402         case DenormalNone:
403                 MXCSR &= ~(_MM_FLUSH_ZERO_ON|0x8000);
404                 break;
405
406         case DenormalFTZ:
407                 if (fpu.has_flush_to_zero()) {
408                         MXCSR |= _MM_FLUSH_ZERO_ON;
409                 }
410                 break;
411
412         case DenormalDAZ:
413                 MXCSR &= ~_MM_FLUSH_ZERO_ON;
414                 if (fpu.has_denormals_are_zero()) {
415                         MXCSR |= 0x8000;
416                 }
417                 break;
418                 
419         case DenormalFTZDAZ:
420                 if (fpu.has_flush_to_zero()) {
421                         if (fpu.has_denormals_are_zero()) {
422                                 MXCSR |= _MM_FLUSH_ZERO_ON | 0x8000;
423                         } else {
424                                 MXCSR |= _MM_FLUSH_ZERO_ON;
425                         }
426                 }
427                 break;
428         }
429
430         _mm_setcsr (MXCSR);
431
432 #endif
433 }
434
435 ARDOUR::OverlapType
436 ARDOUR::coverage (nframes_t sa, nframes_t ea, 
437                   nframes_t sb, nframes_t eb)
438 {
439         /* OverlapType returned reflects how the second (B)
440            range overlaps the first (A).
441
442            The diagrams show various relative placements
443            of A and B for each OverlapType.
444
445            Notes:
446               Internal: the start points cannot coincide
447               External: the start and end points can coincide
448               Start: end points can coincide
449               End: start points can coincide
450
451            XXX Logically, Internal should disallow end
452            point equality.
453         */
454
455         /*
456              |--------------------|   A
457                   |------|            B
458                 |-----------------|   B
459
460
461              "B is internal to A"               
462
463         */
464 #ifdef OLD_COVERAGE
465         if ((sb >= sa) && (eb <= ea)) {
466 #else
467         if ((sb > sa) && (eb <= ea)) {
468 #endif
469                 return OverlapInternal;
470         }
471
472         /*
473              |--------------------|   A
474            ----|                      B
475            -----------------------|   B
476            --|                        B
477            
478              "B overlaps the start of A"
479
480         */
481
482         if ((eb >= sa) && (eb <= ea)) {
483                 return OverlapStart;
484         }
485         /* 
486              |---------------------|  A
487                    |----------------- B
488              |----------------------- B    
489                                    |- B
490
491             "B overlaps the end of A"                              
492
493         */
494         if ((sb >= sa) && (sb <= ea)) {
495                 return OverlapEnd;
496         }
497         /*
498              |--------------------|     A
499            --------------------------  B   
500              |-----------------------  B
501             ----------------------|    B
502              |--------------------|    B
503
504
505            "B overlaps all of A"
506         */
507         if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
508                 return OverlapExternal;
509         }
510
511         return OverlapNone;
512 }
513
514 /* not sure where to put these */
515
516 template<class T>
517 std::istream& int_to_type (std::istream& o, T& hf) {
518         int val;
519         o >> val;
520         hf = (T) val;
521         return o;
522 }
523
524 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
525 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
526 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
527 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
528 std::istream& operator>>(std::istream& o, RemoteModel& var) { return int_to_type<RemoteModel> (o, var); }
529 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
530 std::istream& operator>>(std::istream& o, SoloModel& var) { return int_to_type<SoloModel> (o, var); }
531 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
532 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
533 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
534 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
535 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }
536 std::istream& operator>>(std::istream& o, SmpteFormat& var) { return int_to_type<SmpteFormat> (o, var); }
537 std::istream& operator>>(std::istream& o, DenormalModel& var) { return int_to_type<DenormalModel> (o, var); }
538