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 // Quantize voltages by sampling from a discrete distribution.
28
29 #include "marbles/random/discrete_distribution_quantizer.h"
30
31 #include "stmlib/dsp/dsp.h"
32
33 #include <cmath>
34 #include <algorithm>
35
36 namespace marbles {
37
38 using namespace stmlib;
39 using namespace std;
40
Init(const Scale & scale)41 void DiscreteDistributionQuantizer::Init(const Scale& scale) {
42 int n = scale.num_degrees;
43
44 // We don't want garbage scale data here...
45 if (!n || n > kMaxDegrees || scale.base_interval == 0.0f) {
46 return;
47 }
48
49 base_interval_ = scale.base_interval;
50 base_interval_reciprocal_ = 1.0f / scale.base_interval;
51 num_cells_ = n + 1;
52 for (int i = 0; i <= n; ++i) {
53 float previous_voltage = scale.cell_voltage(i == 0 ? 0 : i - 1);
54 float next_voltage = scale.cell_voltage(i == n ? n : i + 1);
55 cells_[i].center = scale.cell_voltage(i);
56 cells_[i].width = 0.5f * (next_voltage - previous_voltage);
57 cells_[i].weight = static_cast<float>(scale.degree[i % n].weight) / 256.0f;
58 }
59 }
60
Process(float value,float amount)61 float DiscreteDistributionQuantizer::Process(float value, float amount) {
62 if (amount < 0.0f) {
63 return value;
64 }
65
66 float raw_value = value;
67
68 // Assuming 1V/Octave and a scale repeating every octave, note_integral
69 // will store the octave number, and note_fractional the fractional
70 // pitch class.
71 const float note = value * base_interval_reciprocal_;
72 MAKE_INTEGRAL_FRACTIONAL(note);
73 if (value < 0.0f) {
74 note_integral -= 1;
75 note_fractional += 1.0f;
76 }
77
78 // For amount ranging between 0 and 0.25, do not remove notes from the scale
79 // just crossfade from the unquantized output to the quantized output.
80 const float scaled_amount = amount < 0.25f ? 0.0f : (amount - 0.25f) * 1.333f;
81
82 distribution_.Init();
83 for (int i = 0; i < num_cells_ - 1; ++i) {
84 distribution_.AddToken(i, cells_[i].scaled_width(scaled_amount));
85 }
86 distribution_.NoMoreTokens();
87 Distribution::Result r = distribution_.Sample(note_fractional);
88
89 float quantized_value = cells_[r.token_id].center;
90 float offset = static_cast<float>(note_integral) * base_interval_;
91 quantized_value += offset;
92
93 r.start *= base_interval_;
94 r.start += offset;
95
96 if (amount < 0.25f) {
97 amount *= 4.0f;
98
99 float x;
100 if (r.token_id == 0) {
101 x = r.fraction - 1.0f;
102 } else if (r.token_id == num_cells_ - 1) {
103 x = -r.fraction;
104 } else {
105 x = 2.0f * (fabs(r.fraction - 0.5f) - 0.5f);
106 }
107 const float slope = amount / (1.01f - amount);
108 const float y = max(x * slope + 1.0f, 0.0f);
109 quantized_value -= y * (quantized_value - raw_value);
110 }
111
112 return quantized_value;
113 }
114
115 } // namespace marbles