1 // Copyright 2015 Emilie Gillet.
2 //
3 // Author: Emilie Gillet (emilie.o.gillet@gmail.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 // See http://creativecommons.org/licenses/MIT/ for more information.
24 //
25 // -----------------------------------------------------------------------------
26 //
27 // Random generation channel.
28 
29 #ifndef MARBLES_RANDOM_OUTPUT_CHANNEL_H_
30 #define MARBLES_RANDOM_OUTPUT_CHANNEL_H_
31 
32 #include "stmlib/stmlib.h"
33 
34 #include "marbles/random/lag_processor.h"
35 #include "marbles/random/quantizer.h"
36 
37 namespace marbles {
38 
39 class RandomSequence;
40 
41 struct ScaleOffset {
ScaleOffsetScaleOffset42   ScaleOffset(float s, float o) {
43     scale = s;
44     offset = o;
45   }
46 
ScaleOffsetScaleOffset47   ScaleOffset() { scale = 1.0f; offset = 0.0f; }
48 
49   float scale;
50   float offset;
operatorScaleOffset51   inline float operator()(float x) { return x * scale + offset; }
52 };
53 
54 class OutputChannel {
55  public:
OutputChannel()56   OutputChannel() { }
~OutputChannel()57   ~OutputChannel() { }
58 
59   void Init();
60 
LoadScale(int i,const Scale & scale)61   void LoadScale(int i, const Scale& scale) {
62     quantizer_[i].Init(scale);
63   }
64 
65   void Process(
66       RandomSequence* random_sequence,
67       const float* phase,
68       float* output,
69       size_t size,
70       size_t stride);
71 
set_spread(float spread)72   inline void set_spread(float spread) {
73     spread_ = spread;
74   }
75 
set_bias(float bias)76   inline void set_bias(float bias) {
77     bias_ = bias;
78   }
79 
set_scale_index(int i)80   inline void set_scale_index(int i) {
81     scale_index_ = i;
82   }
83 
set_steps(float steps)84   inline void set_steps(float steps) {
85     steps_ = steps;
86   }
87 
set_register_mode(bool register_mode)88   inline void set_register_mode(bool register_mode) {
89     register_mode_ = register_mode;
90   }
91 
set_register_value(float register_value)92   inline void set_register_value(float register_value) {
93     register_value_ = register_value;
94   }
95 
set_register_transposition(float register_transposition)96   inline void set_register_transposition(float register_transposition) {
97     register_transposition_ = register_transposition;
98   }
99 
set_scale_offset(const ScaleOffset & scale_offset)100   inline void set_scale_offset(const ScaleOffset& scale_offset) {
101     scale_offset_ = scale_offset;
102   }
103 
Quantize(float voltage,float amount)104   inline float Quantize(float voltage, float amount) {
105     return quantizer_[scale_index_].Process(voltage, amount, false);
106   }
107 
108  private:
109   float GenerateNewVoltage(RandomSequence* random_sequence);
110 
111   float spread_;
112   float bias_;
113   float steps_;
114   int scale_index_;
115 
116   bool register_mode_;
117   float register_value_;
118   float register_transposition_;
119 
120   float previous_steps_;
121   float previous_phase_;
122   uint32_t reacquisition_counter_;
123 
124   float previous_voltage_;
125   float voltage_;
126   float quantized_voltage_;
127 
128   ScaleOffset scale_offset_;
129 
130   LagProcessor lag_processor_;
131 
132   Quantizer quantizer_[6];
133 
134   DISALLOW_COPY_AND_ASSIGN(OutputChannel);
135 };
136 
137 }  // namespace marbles
138 
139 #endif  // MARBLES_RANDOM_OUTPUT_CHANNEL_H_
140