1 /*
2  * Copyright (C) 2000-2018 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2005-2006 Taybin Rutkin <taybin@taybin.com>
4  * Copyright (C) 2007-2015 Tim Mayberry <mojofunk@gmail.com>
5  * Copyright (C) 2008-2013 David Robillard <d@drobilla.net>
6  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
7  * Copyright (C) 2013-2017 Robin Gareus <robin@gareus.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifndef __ardour_utils_h__
25 #define __ardour_utils_h__
26 
27 #ifdef WAF_BUILD
28 #include "libardour-config.h"
29 #endif
30 
31 #include <iostream>
32 #include <string>
33 #include <cmath>
34 
35 #include "boost/shared_ptr.hpp"
36 
37 #if __APPLE__
38 #include <CoreFoundation/CoreFoundation.h>
39 #endif /* __APPLE__ */
40 
41 #include "ardour/ardour.h"
42 #include "ardour/data_type.h"
43 #include "ardour/dB.h"
44 #include "ardour/types.h"
45 
46 #include "ardour/libardour_visibility.h"
47 
48 class XMLNode;
49 
50 namespace ARDOUR {
51 
52 class Route;
53 class Track;
54 
55 LIBARDOUR_API std::string legalize_for_path (const std::string& str);
56 LIBARDOUR_API std::string legalize_for_universal_path (const std::string& str);
57 LIBARDOUR_API std::string legalize_for_uri (const std::string& str);
58 LIBARDOUR_API std::string legalize_for_path_2X (const std::string& str);
59 LIBARDOUR_API XMLNode* find_named_node (const XMLNode& node, std::string name);
60 LIBARDOUR_API std::string bool_as_string (bool);
61 
f_max(float x,float a)62 static inline float f_max(float x, float a) {
63 	x -= a;
64 	x += fabsf (x);
65 	x *= 0.5f;
66 	x += a;
67 
68 	return (x);
69 }
70 
71 LIBARDOUR_API std::string bump_name_once(const std::string& s, char delimiter);
72 LIBARDOUR_API std::string bump_name_number(const std::string& s);
73 
74 LIBARDOUR_API int cmp_nocase (const std::string& s, const std::string& s2);
75 LIBARDOUR_API int cmp_nocase_utf8 (const std::string& s1, const std::string& s2);
76 
77 LIBARDOUR_API std::string region_name_from_path (std::string path, bool strip_channels, bool add_channel_suffix = false, uint32_t total = 0, uint32_t this_one = 0);
78 LIBARDOUR_API bool path_is_paired (std::string path, std::string& pair_base);
79 
80 LIBARDOUR_API void compute_equal_power_fades (ARDOUR::samplecnt_t nframes, float* in, float* out);
81 
82 LIBARDOUR_API const char* edit_mode_to_string (ARDOUR::EditMode);
83 LIBARDOUR_API ARDOUR::EditMode string_to_edit_mode (std::string);
84 
85 LIBARDOUR_API double gain_to_slider_position_with_max (double g, double max_gain = 2.0);
86 LIBARDOUR_API double slider_position_to_gain_with_max (double g, double max_gain = 2.0);
87 
88 /* I don't really like hard-coding these falloff rates here
89  * Probably should use a map of some kind that could be configured
90  * These rates are db/sec.
91 */
92 
93 #define METER_FALLOFF_OFF       0.0f
94 #define METER_FALLOFF_SLOWEST   6.6f  // BBC standard
95 #define METER_FALLOFF_SLOW      8.6f  // BBC standard, EBU  24dB / 2.8sec
96 #define METER_FALLOFF_SLOWISH   12.0f // DIN  20dB / 1.7 sec
97 #define METER_FALLOFF_MODERATE  13.3f // EBU-PPM, IRT PPM-   20dB / 1.5 sec
98 #define METER_FALLOFF_MEDIUM    20.0f
99 #define METER_FALLOFF_FAST      32.0f
100 
101 LIBARDOUR_API float meter_falloff_to_float (ARDOUR::MeterFalloff);
102 LIBARDOUR_API ARDOUR::MeterFalloff meter_falloff_from_float (float);
103 LIBARDOUR_API float meter_falloff_to_db_per_sec (float);
104 
105 LIBARDOUR_API const char* native_header_format_extension (ARDOUR::HeaderFormat, const ARDOUR::DataType& type);
106 LIBARDOUR_API bool matching_unsuffixed_filename_exists_in (const std::string& dir, const std::string& name);
107 
108 LIBARDOUR_API uint32_t how_many_dsp_threads ();
109 
110 LIBARDOUR_API std::string compute_sha1_of_file (std::string path);
111 
route_list_to_control_list(boost::shared_ptr<RouteList> rl,boost::shared_ptr<T> (Stripable::* get_control)()const)112 template<typename T> boost::shared_ptr<ControlList> route_list_to_control_list (boost::shared_ptr<RouteList> rl, boost::shared_ptr<T> (Stripable::*get_control)() const) {
113 	boost::shared_ptr<ControlList> cl (new ControlList);
114 	if (!rl) { return cl; }
115 	for (RouteList::const_iterator r = rl->begin(); r != rl->end(); ++r) {
116 		boost::shared_ptr<AutomationControl> ac = ((*r).get()->*get_control)();
117 		if (ac) {
118 			cl->push_back (ac);
119 		}
120 	}
121 	return cl;
122 }
123 
stripable_list_to_control_list(StripableList & sl,boost::shared_ptr<T> (Stripable::* get_control)()const)124 template<typename T> boost::shared_ptr<ControlList> stripable_list_to_control_list (StripableList& sl, boost::shared_ptr<T> (Stripable::*get_control)() const) {
125 	boost::shared_ptr<ControlList> cl (new ControlList);
126 	for (StripableList::const_iterator s = sl.begin(); s != sl.end(); ++s) {
127 		boost::shared_ptr<AutomationControl> ac = ((*s).get()->*get_control)();
128 		if (ac) {
129 			cl->push_back (ac);
130 		}
131 	}
132 	return cl;
133 }
134 
135 #if __APPLE__
136 LIBARDOUR_API std::string CFStringRefToStdString(CFStringRef stringRef);
137 #endif // __APPLE__
138 
139 } //namespave
140 
141 #endif /* __ardour_utils_h__ */
142