Merge pull request #18 from wruppelx/master
[asdcplib.git] / src / ST2095_PinkNoise.h
1 /*
2 Copyright (c) 2015, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    ST2095_PinkNoise.h
28     \version $Id$
29     \brief   Pink Noise filter and LCG generator
30 */
31
32
33 #ifndef _ST2095_PINKNOISE_H_
34 #define _ST2095_PINKNOISE_H_
35
36 #include <KM_fileio.h>
37 #include <cmath>
38
39 //
40 // No attempt will be made here to explain the theory of operation of
41 // this noise source since all of the gory detail can be found in SMPTE
42 // ST 2095-1:2015.  Get your copy from SMPTE today!
43 //
44
45
46 namespace ASDCP
47 {
48   // A source of pseudo-random numbers suitable for use in generating
49   // noise signals.  NOT TO BE USED FOR CRYPTOGRAPHIC FUNCTIONS as its
50   // output is 100% deterministic.
51   class LinearCongruentialGenerator
52   {
53     ui32_t m_Seed, m_RandMax;
54     float m_ScaleFactor;
55
56     KM_NO_COPY_CONSTRUCT(LinearCongruentialGenerator);
57
58   public:
59
60     LinearCongruentialGenerator(const ui32_t sample_rate);
61     float GetNextSample();
62   };
63
64   //
65   float const PinkFilterHighPassConstant = 10.0; // Hz
66   float const PinkFilterLowPassConstant = 22400.0; // Hz
67
68   //
69   class PinkFilter
70   {
71     // storage for biquad coefficients for bandpass filter components
72     float hp1_a1, hp1_a2;
73     float hp1_b0, hp1_b1, hp1_b2;
74     float hp2_a1, hp2_a2;
75     float hp2_b0, hp2_b1, hp2_b2;
76     float lp1_a1, lp1_a2;
77     float lp1_b0, lp1_b1, lp1_b2;
78     float lp2_a1, lp2_a2;
79     float lp2_b0, lp2_b1, lp2_b2;
80
81     // storage for delay line variables for bandpass filter and initialize to zero
82     float hp1w1, hp1w2, hp2w1, hp2w2;
83     float lp1w1, lp1w2, lp2w1, lp2w2;
84
85     // storage for delay lines for pink filter network and initialize to zero
86     float lp1, lp2, lp3, lp4, lp5, lp6;
87
88     KM_NO_COPY_CONSTRUCT(PinkFilter);
89
90   public:
91
92     PinkFilter(const i32_t SampleRate, const float HpFc, const float LpFc);
93     
94     // Using a white noise sample as input, produce a pink noise sample
95     // having properties as defined by SMPTE ST 2095-1:2015.
96     float GetNextSample(const float white);
97   };
98
99   // Create a little-endian integer audio sample of the sepcified word size
100   // (1-4 bytes) from the normalized input value, write it to the buffer at p.
101   void ScalePackSample(float sample, byte_t* p, ui32_t word_size);
102
103
104 } // namespace ASDCP
105
106
107
108 #endif // _ST2095_PINKNOISE_H_
109
110
111 //
112 // end ST2095_PinkNoise.h
113 //