tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_Ditherer.h
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11namespace tracktion { inline namespace engine
12{
13
19{
20 void reset (int numBits) noexcept
21 {
22 auto wordLen = std::pow (2.0f, (float)(numBits - 1));
23 auto invWordLen = 1.0f / wordLen;
24 amp = invWordLen / double (RAND_MAX);
25 offset = invWordLen * 0.5f;
26 }
27
28 //==============================================================================
29 void process (float* samps, int num) noexcept
30 {
31 while (--num >= 0)
32 {
33 random2 = random1;
34 random1 = rand();
35
36 auto in = *samps;
37
38 // check for dodgy numbers coming in..
39 if (in < -0.000001f || in > 0.000001f)
40 {
41 in += 0.5f * (s1 + s1 - s2);
42 auto out = in + offset + amp * (float)(random1 - random2);
43 *samps++ = out;
44 s2 = s1;
45 s1 = in - out;
46 }
47 else
48 {
49 *samps++ = in;
50 in += 0.5f * (s1 + s1 - s2);
51 auto out = in + offset + amp * (float)(random1 - random2);
52 s2 = s1;
53 s1 = in - out;
54 }
55 }
56 }
57
58 //==============================================================================
59 int random1 = 0, random2 = 0;
60 float amp = 0, offset = 0;
61 float s1 = 0, s2 = 0;
62};
63
64}} // namespace tracktion { inline namespace engine
typedef double
T pow(T... args)
rand
An extremely naive ditherer.