Anklang 0.3.0-460-gc4ef46ba
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
datautils.hh
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#ifndef __ASE_DATAUTILS_HH__
3#define __ASE_DATAUTILS_HH__
4
5#include <ase/signalmath.hh>
6
7namespace Ase {
8
10constexpr const uint AUDIO_BLOCK_FLOAT_ZEROS_SIZE = 16384;
11
14
16float square_sum (uint n_values, const float *ivalues);
17
19float square_max (uint n_values, const float *ivalues);
20
21// Convert integer to float samples.
22template<class S, class D> inline void convert_samples (size_t n, S *src, D *dst, uint16 byte_order);
23
24// Convert float to integer samples with clipping.
25template<class S, class D> inline void convert_clip_samples (size_t n, S *src, D *dst, uint16 byte_order);
26
28extern inline void
29floatfill (float *dst, float f, size_t n)
30{
31 for (size_t i = 0; i < n; i++)
32 dst[i] = f;
33}
34
36extern inline void
37fast_copy (size_t n, float *d, const float *s)
38{
39 static_assert (sizeof (float) == 4, "");
40 static_assert (sizeof (wchar_t) == 4, "");
41 wmemcpy ((wchar_t*) d, (const wchar_t*) s, n);
42}
43
45extern inline void
46fast_copy (size_t n, uint32_t *d, const uint32_t *s)
47{
48 static_assert (sizeof (uint32_t) == 4, "");
49 static_assert (sizeof (wchar_t) == 4, "");
50 wmemcpy ((wchar_t*) d, (const wchar_t*) s, n);
51}
52
53// == Implementations ==
54template<> inline void
55convert_samples (size_t n, const int16_t *src, float *dst, uint16 byte_order)
56{
57 ASE_ASSERT_RETURN (__BYTE_ORDER__ == byte_order); // swapping __BYTE_ORDER__ not implemented
58 const auto bound = dst + n;
59 while (dst < bound)
60 *dst++ = *src++ * (1. / 32768.);
61}
62
63template<> inline void
64convert_clip_samples (size_t n, const float *src, int16_t *dst, uint16 byte_order)
65{
66 ASE_ASSERT_RETURN (__BYTE_ORDER__ == byte_order); // swapping __BYTE_ORDER__ not implemented
67 static_assert (int16_t (0.99999990F * 32768.F) == 32767);
68 const auto bound = src + n;
69 while (src < bound)
70 {
71 auto v = *src++;
72 v = std::min (0.99999990F, std::max (v, -1.0F));
73 *dst++ = v * 32768.;
74 }
75}
76
77} // Ase
78
79#endif // __ASE_DATAUTILS_HH__
#define ASE_ASSERT_RETURN(expr,...)
Return from the current function if expr evaluates to false and issue an assertion warning.
Definition cxxaux.hh:82
typedef float
T max(T... args)
T min(T... args)
The Anklang C++ API namespace.
Definition api.hh:9
constexpr const uint AUDIO_BLOCK_FLOAT_ZEROS_SIZE
Maximum number of values in the const_float_zeros block.
Definition datautils.hh:10
float square_sum(uint n_values, const float *ivalues)
Calculate suqare sum of a block of floats.
Definition datautils.cc:10
void floatfill(float *dst, float f, size_t n)
Fill n values of dst with f.
Definition datautils.hh:29
float const_float_zeros[AUDIO_BLOCK_FLOAT_ZEROS_SIZE]
Block of const floats allof value 0.
Definition datautils.cc:7
float square_max(uint n_values, const float *ivalues)
Find the maximum suqared value in a block of floats.
Definition datautils.cc:19
uint16_t uint16
A 16-bit unsigned integer.
Definition cxxaux.hh:23
uint32_t uint
Provide 'uint' as convenience type.
Definition cxxaux.hh:18
void fast_copy(size_t n, float *d, const float *s)
Copy a block of floats.
Definition datautils.hh:37
typedef uint32_t
wmemcpy