1 /*
2  * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3  *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
4  *
5  * This file is part of lsp-plugins
6  * Created on: 20 февр. 2016 г.
7  *
8  * lsp-plugins is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * any later version.
12  *
13  * lsp-plugins is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with lsp-plugins. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <core/envelope.h>
23 #include <core/stdlib/math.h>
24 
25 namespace lsp
26 {
27     namespace envelope
28     {
29         const char *envelopes[] =
30         {
31             "Violet noise",
32             "Blue noise",
33             "White noise",
34             "Pink noise",
35             "Brown noise",
36             "Fall-off 4.5dB/oct",
37             "Raise 4.5dB/oct",
38             NULL
39         };
40 
basic_noise(float * dst,size_t n,float k)41         static void basic_noise(float *dst, size_t n, float k)
42         {
43             if (n == 0)
44                 return;
45 
46             dst[0]      = 1.0f;
47             float kd    = (SPEC_FREQ_MAX / SPEC_FREQ_MIN) / n;
48             for (size_t i=1; i < n; ++i)
49                 dst[i]      = expf(k * logf(i * kd));
50         }
51 
noise(float * dst,size_t n,envelope_t type)52         void noise(float *dst, size_t n, envelope_t type)
53         {
54             switch (type)
55             {
56                 case WHITE_NOISE:   white_noise(dst, n);    return;
57                 case PINK_NOISE:    pink_noise(dst, n);     return;
58                 case BROWN_NOISE:   brown_noise(dst, n);    return;
59                 case BLUE_NOISE:    blue_noise(dst, n);     return;
60                 case VIOLET_NOISE:  violet_noise(dst, n);   return;
61                 case PLUS_4_5_DB:   basic_noise(dst, n, 4.5 / (20.0 * M_LOG10_2));  return;
62                 case MINUS_4_5_DB:  basic_noise(dst, n, -4.5 / (20.0 * M_LOG10_2));  return;
63                 default:
64                     return;
65             }
66         }
67 
reverse_noise(float * dst,size_t n,envelope_t type)68         void reverse_noise(float *dst, size_t n, envelope_t type)
69         {
70             switch (type)
71             {
72                 case WHITE_NOISE:   white_noise(dst, n);    return;
73                 case PINK_NOISE:    blue_noise(dst, n);     return;
74                 case BROWN_NOISE:   violet_noise(dst, n);   return;
75                 case BLUE_NOISE:    pink_noise(dst, n);     return;
76                 case VIOLET_NOISE:  brown_noise(dst, n);    return;
77                 case PLUS_4_5_DB:   basic_noise(dst, n, -4.5 / (20.0 * M_LOG10_2));  return;
78                 case MINUS_4_5_DB:  basic_noise(dst, n, 4.5 / (20.0 * M_LOG10_2));  return;
79                 default:
80                     return;
81             }
82         }
83 
white_noise(float * dst,size_t n)84         void white_noise(float *dst, size_t n)
85         {
86             while (n--)
87                 *(dst++)        = 1.0f;
88         }
89 
pink_noise(float * dst,size_t n)90         void pink_noise(float *dst, size_t n)
91         {
92             basic_noise(dst, n, logf(0.5) / logf(4));
93         }
94 
brown_noise(float * dst,size_t n)95         void brown_noise(float *dst, size_t n)
96         {
97             basic_noise(dst, n, -1);
98         }
99 
blue_noise(float * dst,size_t n)100         void blue_noise(float *dst, size_t n)
101         {
102             basic_noise(dst, n, logf(2) / logf(4));
103         }
104 
violet_noise(float * dst,size_t n)105         void violet_noise(float *dst, size_t n)
106         {
107             basic_noise(dst, n, 1);
108         }
109 
110     }
111 }
112 
113