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