File: LCG.h

package info (click to toggle)
lsp-plugins 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 91,856 kB
  • sloc: cpp: 427,831; xml: 57,779; makefile: 9,961; php: 1,005; sh: 18
file content (156 lines) | stat: -rw-r--r-- 5,427 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (C) 2021 Linux Studio Plugins Project <https://lsp-plug.in/>
 *           (C) 2021 Stefano Tronci <stefano.tronci@protonmail.com>
 *
 * This file is part of lsp-dsp-units
 * Created on: 13 Jun 2021
 *
 * lsp-plugins is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * lsp-plugins is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with lsp-plugins. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef LSP_PLUG_IN_DSP_UNITS_NOISE_LCG_H_
#define LSP_PLUG_IN_DSP_UNITS_NOISE_LCG_H_

#include <lsp-plug.in/dsp-units/util/Randomizer.h>
#include <lsp-plug.in/dsp-units/iface/IStateDumper.h>

namespace lsp
{
    namespace dspu
    {
        enum lcg_dist_t
        {
            LCG_UNIFORM,        // Uniform over [-1, 1), X fAmplitude + fOffset
            LCG_EXPONENTIAL,    // Double sided exponential over [-1, 1], X fAmplitude + fOffset
            LCG_TRIANGULAR,     // Triangular over [-1, 1], X fAmplitude + fOffset
            LCG_GAUSSIAN,       // Gaussian of mean 0 and standard deviation 1, X fAmplitude + fOffset
            LCG_MAX
        };

        /** LCG stands for Linear Congruential Generator.
         * LCG generate pseudorandom sequences by using a recurrence relation.
         * This relation is defined in the Randomizer class.
         * This class builds upon Randomizer by providing support for common distributions.
         * Output noise is easily scalable, and offset is also supported.
         *
         */
        class LSP_DSP_UNITS_PUBLIC LCG
        {
            private:
                LCG & operator = (const LCG &);
                LCG(const LCG &);

            private:
                lcg_dist_t  enDistribution;

                float       fAmplitude;
                float       fOffset;

                Randomizer  sRand;

            public:
                explicit LCG();
                ~LCG();

                void construct();
                void destroy();

            public:

                /** Initialize random generator
                 *
                 * @param seed seed
                 */
                void init(uint32_t seed);

                /** Initialize random generator, take current time as seed
                 */
                void init();

                /** Set the distribution for the noise.
                 *
                 * @param dist distribution for the noise sequence.
                 */
                inline void set_distribution(lcg_dist_t dist)
                {
                    if ((dist < LCG_UNIFORM) || (dist >= LCG_MAX))
                        return;

                    enDistribution = dist;
                }

                /** Set the amplitude of the LCG sequence.
                 *
                 * @param amplitude amplitude value for the sequence.
                 */
                inline void set_amplitude(float amplitude)
                {
                    if (amplitude == fAmplitude)
                        return;

                    fAmplitude = amplitude;
                }

                /** Set the offset of the LCG sequence.
                 *
                 * @param offset offset value for the sequence.
                 */
                inline void set_offset(float offset)
                {
                    if (offset == fOffset)
                        return;

                    fOffset = offset;
                }

                /** Get a sample from the LCG generator.
                 *
                 * @return the next sample in the LCG sequence.
                 */
                float process_single();

                /** Output sequence to the destination buffer in additive mode
                 *
                 * @param dst output wave destination
                 * @param src input source, allowed to be NULL
                 * @param count number of samples to synthesise
                 */
                void process_add(float *dst, const float *src, size_t count);

                /** Output sequence to the destination buffer in multiplicative mode
                 *
                 * @param dst output wave destination
                 * @param src input source, allowed to be NULL
                 * @param count number of samples to process
                 */
                void process_mul(float *dst, const float *src, size_t count);

                /** Output sequence to a destination buffer overwriting its content
                 *
                 * @param dst output wave destination
                 * @param src input source, allowed to be NULLL
                 * @param count number of samples to process
                 */
                void process_overwrite(float *dst, size_t count);

                /**
                 * Dump the state
                 * @param dumper dumper
                 */
                void dump(IStateDumper *v) const;
        };
    }
}

#endif /* LSP_PLUG_IN_DSP_UNITS_NOISE_LCG_H_ */