Anklang-0.3.0.dev956+gd75ac925 anklang-0.3.0.dev956+gd75ac925
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#pragma once
3
4#include <ase/signalmath.hh>
5
6namespace Ase {
7
9constexpr const uint AUDIO_BLOCK_FLOAT_ZEROS_SIZE = 16384;
10
13
15float square_sum (uint n_values, const float *ivalues);
16
18float square_max (uint n_values, const float *ivalues);
19
20// Convert integer to float samples.
21template<class S, class D> inline void convert_samples (size_t n, S *src, D *dst, uint16 byte_order);
22
23// Convert float to integer samples with clipping.
24template<class S, class D> inline void convert_clip_samples (size_t n, S *src, D *dst, uint16 byte_order);
25
27extern inline void
28floatfill (float *dst, float f, size_t n)
29{
30 for (size_t i = 0; i < n; i++)
31 dst[i] = f;
32}
33
35extern inline void
36fast_copy (size_t n, float *d, const float *s)
37{
38 static_assert (sizeof (float) == 4, "");
39 static_assert (sizeof (wchar_t) == 4, "");
40 wmemcpy ((wchar_t*) d, (const wchar_t*) s, n);
41}
42
44extern inline void
45fast_copy (size_t n, uint32_t *d, const uint32_t *s)
46{
47 static_assert (sizeof (uint32_t) == 4, "");
48 static_assert (sizeof (wchar_t) == 4, "");
49 wmemcpy ((wchar_t*) d, (const wchar_t*) s, n);
50}
51
52// == Implementations ==
53template<> inline void
54convert_samples (size_t n, const int16_t *src, float *dst, uint16 byte_order)
55{
56 ASE_ASSERT_RETURN (__BYTE_ORDER__ == byte_order); // swapping __BYTE_ORDER__ not implemented
57 const auto bound = dst + n;
58 while (dst < bound)
59 *dst++ = *src++ * (1. / 32768.);
60}
61
62template<> inline void
63convert_clip_samples (size_t n, const float *src, int16_t *dst, uint16 byte_order)
64{
65 ASE_ASSERT_RETURN (__BYTE_ORDER__ == byte_order); // swapping __BYTE_ORDER__ not implemented
66 static_assert (int16_t (0.99999990F * 32768.F) == 32767);
67 const auto bound = src + n;
68 while (src < bound)
69 {
70 auto v = *src++;
71 v = std::min (0.99999990F, std::max (v, -1.0F));
72 *dst++ = v * 32768.;
73 }
74}
75
76} // Ase
77
#define ASE_ASSERT_RETURN(expr,...)
Return from the current function if expr evaluates to false and issue an assertion warning.
Definition cxxaux.hh:81
typedef float
T max(T... args)
T min(T... args)
The Anklang C++ API namespace.
Definition api.hh:8
constexpr const uint AUDIO_BLOCK_FLOAT_ZEROS_SIZE
Maximum number of values in the const_float_zeros block.
Definition datautils.hh:9
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:28
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:22
uint32_t uint
Provide 'uint' as convenience type.
Definition cxxaux.hh:17
void fast_copy(size_t n, float *d, const float *s)
Copy a block of floats.
Definition datautils.hh:36
typedef uint32_t
wmemcpy