BEAST - Free Software Audio Synthesizer and Tracker  0.10.0
bseblockutils.hh
Go to the documentation of this file.
1  // Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
2 #ifndef __BSE_BLOCK_UTILS_H__
3 #define __BSE_BLOCK_UTILS_H__
4 #include <wchar.h> /* wmemset */
5 #include <bse/bseieee754.hh>
6 
7 
8 template<class TYPE> inline
9 void bse_block_fill_0 (size_t n_values, /* 4-byte variant of memset */
10  TYPE *values);
11 /* --- C API --- */
12 G_BEGIN_DECLS
13 const
14 char* bse_block_impl_name (void);
15 static inline
16 void bse_block_fill_uint32 (guint n_values, /* 4-byte variant of memset for ints */
17  guint32 *values,
18  guint32 vuint32);
19 static inline
20 void bse_block_fill_float (uint n_values, /* 4-byte variant of memset for floats */
21  float *values,
22  const float value);
23 static inline
24 void bse_block_copy_uint32 (guint n_values, /* 4-byte variant of memcpy for ints */
25  guint32 *values,
26  const guint32 *ivalues);
27 static inline
28 void bse_block_copy_float (guint n_values, /* 4-byte variant of memcpy for floats */
29  gfloat *values,
30  const gfloat *ivalues);
31 void bse_block_add_floats (guint n_values,
32  float *ovalues,
33  const float *ivalues);
34 void bse_block_sub_floats (guint n_values,
35  float *ovalues,
36  const float *ivalues);
37 void bse_block_mul_floats (guint n_values,
38  float *ovalues,
39  const float *ivalues);
40 void bse_block_scale_floats (guint n_values,
41  float *ovalues,
42  const float *ivalues,
43  const float level);
44 void bse_block_interleave2_floats (guint n_ivalues,
45  float *ovalues, /* length_ovalues = n_ivalues * 2 */
46  const float *ivalues,
47  guint offset); /* 0=left, 1=right */
48 void bse_block_interleave2_add_floats (guint n_ivalues,
49  float *ovalues, /* length_ovalues = n_ivalues * 2 */
50  const float *ivalues,
51  guint offset); /* 0=left, 1=right */
52 void bse_block_calc_float_range (guint n_values,
53  const float *ivalues,
54  float *min_value,
55  float *max_value);
56 float bse_block_calc_float_square_sum (guint n_values,
57  const float *ivalues);
58 float bse_block_calc_float_range_and_square_sum (guint n_values,
59  const float *ivalues,
60  float *min_value,
61  float *max_value);
62 
63 G_END_DECLS
64 
65 #ifdef __cplusplus
66 #include <bse/bseresampler.hh>
67 namespace Bse {
68 
69 /* --- C++ API --- */
70 class Block {
71 public:
72  static const char* impl_name () { return singleton->impl_name (); }
73  static inline void fill (guint n_values,
74  float *values,
75  float value);
76  static inline void fill (guint n_values,
77  guint32 *values,
78  guint32 value);
79  static inline void copy (guint n_values,
80  float *values,
81  const float *ivalues);
82  static inline void copy (guint n_values,
83  guint32 *values,
84  const guint32 *ivalues);
85  static inline void add (guint n_values,
86  float *ovalues,
87  const float *ivalues) { singleton->add (n_values, ovalues, ivalues); }
88  static inline void sub (guint n_values,
89  float *ovalues,
90  const float *ivalues) { singleton->sub (n_values, ovalues, ivalues); }
91  static inline void mul (guint n_values,
92  float *ovalues,
93  const float *ivalues) { singleton->mul (n_values, ovalues, ivalues); }
94  static inline void scale (guint n_values,
95  float *ovalues,
96  const float *ivalues,
97  const float level) { singleton->scale (n_values, ovalues, ivalues, level); }
98  static inline void interleave2 (guint n_ivalues,
99  float *ovalues,
100  const float *ivalues,
101  guint offset) { singleton->interleave2 (n_ivalues, ovalues, ivalues, offset); }
102  static inline void interleave2_add (guint n_ivalues,
103  float *ovalues,
104  const float *ivalues,
105  guint offset) { singleton->interleave2_add (n_ivalues, ovalues, ivalues, offset); }
106  static inline void range (guint n_values,
107  const float *ivalues,
108  float& min_value,
109  float& max_value) { singleton->range (n_values, ivalues, min_value, max_value); }
110  static inline float square_sum (guint n_values,
111  const float *ivalues) { return singleton->square_sum (n_values, ivalues); }
112  static inline float range_and_square_sum (guint n_values,
113  const float *ivalues,
114  float& min_value,
115  float& max_value) { return singleton->range_and_square_sum (n_values, ivalues, min_value, max_value); }
116 
117  typedef Resampler::Resampler2 Resampler2;
118  static inline
119  Resampler2* create_resampler2 (BseResampler2Mode mode,
120  BseResampler2Precision precision) { return singleton->create_resampler2 (mode, precision); }
121  static inline
122  bool test_resampler2 (bool verbose) { return singleton->test_resampler2 (verbose); }
123 
124 
125 
126  class Impl {
127  protected:
128  virtual ~Impl ();
129  virtual const char* impl_name () = 0;
130  virtual void add (guint n_values,
131  float *ovalues,
132  const float *ivalues) = 0;
133  virtual void sub (guint n_values,
134  float *ovalues,
135  const float *ivalues) = 0;
136  virtual void mul (guint n_values,
137  float *ovalues,
138  const float *ivalues) = 0;
139  virtual void scale (guint n_values,
140  float *ovalues,
141  const float *ivalues,
142  const float level) = 0;
143  virtual void interleave2 (guint n_ivalues,
144  float *ovalues, /* length_ovalues = n_ivalues * 2 */
145  const float *ivalues,
146  guint offset) = 0; /* 0=left, 1=right */
147  virtual void interleave2_add (guint n_ivalues,
148  float *ovalues, /* length_ovalues = n_ivalues * 2 */
149  const float *ivalues,
150  guint offset) = 0; /* 0=left, 1=right */
151  virtual void range (guint n_values,
152  const float *ivalues,
153  float& min_value,
154  float& max_value) = 0;
155  virtual float square_sum (guint n_values,
156  const float *ivalues) = 0;
157  virtual float range_and_square_sum (guint n_values,
158  const float *ivalues,
159  float& min_value,
160  float& max_value) = 0;
161  virtual
162  Resampler2* create_resampler2 (BseResampler2Mode mode,
163  BseResampler2Precision precision) = 0;
164  virtual bool test_resampler2 (bool verbose) = 0;
165  friend class Block;
166  static void substitute (Impl *substitute_impl);
167  };
168  static Impl* default_singleton ();
169  static Impl* current_singleton ();
170 private:
171  static Impl *singleton;
172 };
173 
174 /* --- C++ implementation bits --- */
175 inline void
176 Block::fill (guint n_values,
177  float *values,
178  float value)
179 {
180  RAPICORN_STATIC_ASSERT (sizeof (float) == 4);
181  RAPICORN_STATIC_ASSERT (sizeof (wchar_t) == 4);
182  const union { float f; guint32 vuint32; } u = { value };
183  wmemset ((wchar_t*) values, u.vuint32, n_values);
184 }
185 
186 inline void
187 Block::fill (guint n_values,
188  guint32 *values,
189  guint32 value)
190 {
191  RAPICORN_STATIC_ASSERT (sizeof (wchar_t) == 4);
192  wmemset ((wchar_t*) values, value, n_values);
193 }
194 
195 inline void
196 Block::copy (guint n_values,
197  guint32 *values,
198  const guint32 *ivalues)
199 {
200  RAPICORN_STATIC_ASSERT (sizeof (wchar_t) == 4);
201  wmemcpy ((wchar_t*) values, (const wchar_t*) ivalues, n_values);
202 }
203 
204 inline void
205 Block::copy (guint n_values,
206  gfloat *values,
207  const gfloat *ivalues)
208 {
209  RAPICORN_STATIC_ASSERT (sizeof (float) == 4);
210  RAPICORN_STATIC_ASSERT (sizeof (wchar_t) == 4);
211  wmemcpy ((wchar_t*) values, (const wchar_t*) ivalues, n_values);
212 }
213 
214 } // Bse
215 #endif /* __cplusplus */
216 
217 /* --- C implementation bits --- */
218 G_BEGIN_DECLS
219 
220 static inline void
221 bse_block_fill_uint32 (guint n_values,
222  guint32 *values,
223  guint32 vuint32)
224 {
225  RAPICORN_STATIC_ASSERT (sizeof (wchar_t) == 4);
226  wmemset ((wchar_t*) values, vuint32, n_values);
227 }
228 
229 static inline void
230 bse_block_fill_float (guint n_values,
231  float *values,
232  const float value)
233 {
234  RAPICORN_STATIC_ASSERT (sizeof (float) == 4);
235  const union { float f; guint32 vuint32; } u = { value };
236  RAPICORN_STATIC_ASSERT (sizeof (wchar_t) == 4);
237  wmemset ((wchar_t*) values, u.vuint32, n_values);
238 }
239 
240 static inline void
241 bse_block_copy_uint32 (guint n_values,
242  guint32 *values,
243  const guint32 *ivalues)
244 {
245  RAPICORN_STATIC_ASSERT (sizeof (wchar_t) == 4);
246  wmemcpy ((wchar_t*) values, (const wchar_t*) ivalues, n_values);
247 }
248 
249 static inline void
250 bse_block_copy_float (guint n_values,
251  gfloat *values,
252  const gfloat *ivalues)
253 {
254  RAPICORN_STATIC_ASSERT (sizeof (float) == 4);
255  RAPICORN_STATIC_ASSERT (sizeof (wchar_t) == 4);
256  wmemcpy ((wchar_t*) values, (const wchar_t*) ivalues, n_values);
257 }
258 G_END_DECLS
259 
260 // == C++ Implementations ==
261 template<class TYPE> inline void
262 bse_block_fill_0 (size_t n_values, TYPE *values)
263 {
264  size_t n_bytes = n_values * sizeof (TYPE);
265  char *p = (char*) values;
266  uint r = size_t (p) & 3;
267  if (RAPICORN_UNLIKELY (r))
268  {
269  r = MIN (r, n_values); // rest for pointer alignment
270  memset (p, 0, r);
271  p += r;
272  n_bytes -= r;
273  }
274  const size_t n_aligned = n_bytes / 4;
275  wmemset ((wchar_t*) p, 0, n_aligned);
276  n_bytes -= n_aligned * 4;
277  if (RAPICORN_UNLIKELY (n_bytes))
278  {
279  p += n_aligned * 4;
280  memset (p, 0, n_bytes);
281  }
282 }
283 
284 #endif /* __BSE_BLOCK_UTILS_H__ */
T copy(T...args)
The Bse namespace contains all functions of the synthesis engine.
Definition: bstbseutils.cc:88
wmemset
memset
wmemcpy
typedef size_t
T fill(T...args)