ofxPDSP
WhiteNoise.h
1 
2 // WhiteNoise.h
3 // ofxPDSP
4 // Nicola Pisanti, MIT License, 2016
5 
6 
7 #ifndef PDSP_RANDOM_PRNOISEGEN_H_INCLUDED
8 #define PDSP_RANDOM_PRNOISEGEN_H_INCLUDED
9 
10 #include <time.h>
11 #include "../pdspCore.h"
12 
13 
14 #ifdef _MSC_VER
15 #pragma warning( push )
16 #pragma warning( disable : C4838)
17 #pragma warning( disable : C4554)
18 #endif
19 
20 #ifdef __GNUC__
21  #pragma GCC diagnostic push
22  #pragma GCC diagnostic ignored "-Wparentheses"
23 #endif
24 
25 
26 #define EXTRACT_BITS(the_val , bits_start , bits_len) ((the_val >> (bits_start-1)) & ((1<<bits_len-1)))
27 
28 namespace pdsp{
29 
30 static const float PNDivisor = 1.0 / 268435456.0; // 1/(2^32 / 16 )
31 
32 
33 inline_f float PNSequenceNext(uint32_t& PNRegister){
34  //get the bits
35  uint32_t b0 = EXTRACT_BITS(PNRegister, 1, 1); //1 = b0
36  uint32_t b1 = EXTRACT_BITS(PNRegister, 2, 1); //2 = b1
37  uint32_t b27 = EXTRACT_BITS(PNRegister, 28, 1); //28 = b27
38  uint32_t b28 = EXTRACT_BITS(PNRegister, 29, 1); //29 = b28
39 
40  //form the XOR
41  uint32_t b31 = b0^b1^b27^b28;
42 
43  //form the mask to OR with the register to load b31
44  if (b31 == 1){
45  b31 = 0x10000000;
46  }
47 
48  PNRegister >>= 1;
49  PNRegister |= b31;
50 
51  return ((PNRegister * PNDivisor) - 1.0);
52 
53 }
54 
61 class WhiteNoise : public Unit{
62 public:
63  WhiteNoise();
64 
69 
74 
79 
80 private:
81  void prepareUnit( int expectedBufferSize, double sampleRate ) override;
82  void releaseResources() override;
83 
84  void process(int bufferSize) noexcept override;
85  void process_run(const int &bufferSize) noexcept;
86  void process_PA(const float* &trigSyncBuffer, const int &bufferSize) noexcept;
87  void process_SA_TA(const float* &trigSyncBuffer, const float* &trigSeedBuffer, const int &bufferSize) noexcept;
88 
89  inline_f void reSeed();
90 
91  OutputNode output;
92  InputNode input_trig_clock;
93  InputNode input_trig_seed;
94 
95  uint32_t seed;
96  uint32_t seedMult;
97  uint32_t pnRegister;
98 
99 };
100 
101 }
102 
103 
104 #ifdef _MSC_VER
105 #pragma warning( pop )
106 #endif
107 
108 #ifdef __GNUC__
109  #pragma GCC diagnostic pop
110 #endif
111 
112 
113 #endif // PDSP_RANDOM_PRNOISEGEN_H_INCLUDED
Patchable & in_clock()
Sets "clock" as selected input and returns this Unit ready to be patched. This is the default input...
Definition: WhiteNoise.cpp:24
Abstract class for implementing Units.
Definition: BasicNodes.h:223
PseudoRandom Noise Generator (used for white noise, but it can also generate more complex noise timbr...
Definition: WhiteNoise.h:61
Abstract class for implementing Units and Modules.
Definition: BasicNodes.h:82
Patchable & in_reseed()
Sets "reseed" as selected input and returns this Unit ready to be patched. You need to patch an out_t...
Definition: WhiteNoise.cpp:28
Input of a Unit, process all the connected Outputs and sum them. Has an internal variable state...
Definition: BasicNodes.h:536
Patchable & out_signal()
Sets "signal" as selected output and returns this Unit ready to be patched. This is the default outpu...
Thread-safe value control with smoothing.
Definition: SequencerBridge.h:11
Output of a Unit, contains a buffer of rendered floats and has a variable state flag.
Definition: BasicNodes.h:354