File: filter.cpp

package info (click to toggle)
musescore2 2.3.2%2Bdfsg4-15~bpo10%2B1
  • links: PTS, VCS
  • area: main
  • in suites: buster-backports
  • size: 168,212 kB
  • sloc: cpp: 262,317; xml: 176,707; sh: 3,377; ansic: 1,384; python: 356; makefile: 228; perl: 82; pascal: 78
file content (288 lines) | stat: -rw-r--r-- 11,068 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//=============================================================================
//  Zerberus
//  Filters implementation
//
//  Copyright (C) 2018 Werner Schweer
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2
//  as published by the Free Software Foundation and appearing in
//  the file LICENCE.GPL
//=============================================================================

#include "filter.h"
#include "zerberus.h"
#include "zone.h"

#include <math.h>
#include <functional>

static constexpr int INTERP_MAX = 256;
static float interpCoeff[INTERP_MAX][4];

//---------------------------------------------------------
//   FilterBQ
//---------------------------------------------------------

ZFilter::ZFilter()
      {
      constexpr double ff = 1.0 / 32768.0;
      for (int i = 0; i < INTERP_MAX; i++) {
            double x = (double) i / (double) INTERP_MAX;
            interpCoeff[i][0] = (x * (-0.5 + x * (1 - 0.5 * x)))  * ff;
            interpCoeff[i][1] = (1.0 + x * x * (1.5 * x - 2.5))   * ff;
            interpCoeff[i][2] = (x * (0.5 + x * (2.0 - 1.5 * x))) * ff;
            interpCoeff[i][3] = (0.5 * x * x * (x - 1.0))         * ff;
            }
      }

//---------------------------------------------------------
//   initialize
//---------------------------------------------------------

void ZFilter::initialize(const Zerberus* zerberus, const Zone* z, int velocity)
      {
      this->zerberus = zerberus;
      this->sampleZone = z;

      resonanceF = zerberus->ct2hz(13500.0);
      if (z->isCutoffDefined) {
            //calculate current cutoff value
            float cutoffHz = z->cutoff;
            //Formula for converting the interval frequency ratio f2 / f1 to cents (c or ¢).
            //¢ or c = 1200 × log2 (f2 / f1)
            cutoffHz *= pow(2.0, velocity / 127.0f * z->fil_veltrack / 1200.0);
            resonanceF = cutoffHz;
            }

      last_resonanceF   = -1.0;
      float q_db  = 0; //no resonance by default
      q_lin       = pow(10.0f, q_db / 20.0f);
      gain = 1.0 / sqrt(q_lin);

      firstRun = true;
      }

//---------------------------------------------------------
//   update
//---------------------------------------------------------

void ZFilter::update()
      {
      float adaptedFrequency = resonanceF;
      const float sr = zerberus->sampleRate();
      if (adaptedFrequency > 0.45f * sr)
            adaptedFrequency = 0.45f * sr;
      else if (adaptedFrequency < 5.f)
            adaptedFrequency = 5.f;

      const bool freqWasUpdated = fabs(adaptedFrequency - last_resonanceF) > 0.01f;
      if (!freqWasUpdated)
            return;

      last_resonanceF = adaptedFrequency;

      // The filter coefficients have to be recalculated (filter
      // parameters have changed). Recalculation for various reasons is
      // forced by setting last_fres to -1.  The flag filter_startup
      // indicates, that the DSP loop runs for the first time, in this
      // case, the filter is set directly, instead of smoothly fading
      // between old and new settings.
      //
      // Those equations from Robert Bristow-Johnson's `Cookbook
      // formulae for audio EQ biquad filter coefficients', obtained
      // from Harmony-central.com / Computer / Programming. They are
      // the result of the bilinear transform on an analogue filter
      // prototype. To quote, `BLT frequency warping has been taken
      // into account for both significant frequency relocation and for
      // bandwidth readjustment'.

      const float omega       = (float) 2.0f * M_PI * (adaptedFrequency / sr);
      const float sin_coeff   = sin(omega);
      const float cos_coeff   = cos(omega);
      const float alpha_coeff = sin_coeff / (2.0f * q_lin);
      const float a0_inv      = 1.0f / (1.0f + alpha_coeff);
      const float linCosCoeff = 2.f - cos_coeff;

      float a1_temp = 0.f;
      float a2_temp = 0.f;
      float b1_temp = 0.f;
      float b0_temp = 0.f;
      float b2_temp = 0.f;
      switch (sampleZone->fil_type) {
            case FilterType::lpf_2p: {
                  a1_temp = 2.0f * cos_coeff * a0_inv;
                  a2_temp = (alpha_coeff - 1.f) * a0_inv;
                  b1_temp = (1.0f - cos_coeff) * a0_inv;
                  b0_temp = b2_temp = b1_temp * 0.5f;
                  break;
                  }
            case FilterType::hpf_2p: {
                  a1_temp = 2.0f * cos_coeff * a0_inv;
                  a2_temp = (alpha_coeff - 1.f) * a0_inv;
                  b1_temp = -(1.0f + cos_coeff) * a0_inv;
                  b0_temp = b2_temp = -b1_temp * 0.5f;
                  break;
                  }
            case FilterType::bpf_2p: {
                  a1_temp = 2.0f * cos_coeff * a0_inv;
                  a2_temp = (alpha_coeff - 1.f) * a0_inv;
                  b0_temp = alpha_coeff * a0_inv;
                  b1_temp = 0.f;
                  b2_temp = -b0_temp;
                  break;
                  }
            case FilterType::brf_2p: {
                  a1_temp = 2.0f * cos_coeff * a0_inv;
                  a2_temp = (alpha_coeff - 1.f) * a0_inv;
                  b1_temp = a0_inv * (-2.f * cos_coeff);
                  b0_temp = b2_temp = a0_inv;
                  break;
                  }
            case FilterType::lpf_1p: {
                  a1_temp = -(linCosCoeff - sqrt(linCosCoeff * linCosCoeff - 1));
                  b0_temp = 1 + a1_temp;
                  break;
                  }
            case FilterType::hpf_1p: {
                  a1_temp = -(linCosCoeff - sqrt(linCosCoeff * linCosCoeff - 1));
                  b0_temp = -a1_temp;
                  b1_temp = a1_temp;
                  break;
                  }
            default:
                  qWarning() << "fil_type is not implemented: " << (int)sampleZone->fil_type;
            }

      if (firstRun) {
            /* The filter is calculated, because the voice was started up.
             * In this case set the filter coefficients without delay.
             */
            a1 = a1_temp;
            a2 = a2_temp;
            b0 = b0_temp;
            b2 = b2_temp;
            b1 = b1_temp;
            filter_coeff_incr_count = 0;
            firstRun = false;
            }
      else {
            /* The filter frequency is changed.  Calculate an increment
            * factor, so that the new setting is reached after some time.
            */

            static const int FILTER_TRANSITION_SAMPLES = 64;

            a1_incr = (a1_temp - a1) / FILTER_TRANSITION_SAMPLES;
            a2_incr = (a2_temp - a2) / FILTER_TRANSITION_SAMPLES;
            b0_incr = (b0_temp - b0) / FILTER_TRANSITION_SAMPLES;
            b1_incr = (b1_temp - b1) / FILTER_TRANSITION_SAMPLES;
            b2_incr = (b2_temp - b2) / FILTER_TRANSITION_SAMPLES;
            /* Have to add the increments filter_coeff_incr_count times. */
            filter_coeff_incr_count = FILTER_TRANSITION_SAMPLES;
            }
      }

//---------------------------------------------------------
//   apply
//---------------------------------------------------------

float ZFilter::apply(float inputValue, bool leftChannel)
      {
      float value = 0.f;
      const auto applyBqEquation = [this, &inputValue](float& histX1, float& histX2, float& histY1, float& histY2) {
            //apply filter
            /*
                  float y = d.b0 * x + d.b1 * d.x1 + d.b2 * d.x2 +
                            d.a1 * d.y1 + d.a2 * d.y2;
                  d.x2 = d.x1;
                  d.x1 = x;
                  d.y2 = d.y1;
                  d.y1 = y;
                  return y;
             */
            float value = b0 * inputValue + b1 * histX1 + b2 * histX2 + a1 * histY1 + a2 * histY2;
            histX2 = histX1;
            histX1 = inputValue;
            histY2 = histY1;
            histY1 = value;
            return value;
            };
      const auto applyHPF1PEquation = [this, &inputValue](float& histX1, float& histY1) {
            float value = b0 * inputValue + b1 * histX1 - a1 * histY1;
            histX1 = inputValue;
            histY1 = value;
            return value;
            };
      const auto applyLPF1PEquation = [this, &inputValue](float& histY1) {
            float value = b0 * inputValue - a1 * histY1;
            histY1 = value;
            return value;
            };
      if (leftChannel) {
            switch (sampleZone->fil_type) {
                  case FilterType::hpf_2p:
                  case FilterType::lpf_2p:
                  case FilterType::bpf_2p:
                  case FilterType::brf_2p: {
                        value = applyBqEquation(monoL.histX1, monoL.histX2, monoL.histY1, monoL.histY2);
                        break;
                        }
                  case FilterType::hpf_1p: {
                        value = applyHPF1PEquation(monoL.histX1, monoL.histY1);
                        break;
                        }
                  case FilterType::lpf_1p: {
                        value = applyLPF1PEquation(monoL.histY1);
                        break;
                        }
                  default:
                        qWarning() << "this equation is not implemented" << (int)sampleZone->fil_type;
                  }
            }
      else { //right channel in stereo samples
            switch (sampleZone->fil_type) {
                  case FilterType::hpf_2p:
                  case FilterType::lpf_2p:
                  case FilterType::bpf_2p:
                  case FilterType::brf_2p: {
                        value = applyBqEquation(monoR.histX1, monoR.histX2, monoR.histY1, monoR.histY2);
                        break;
                        }
                  case FilterType::hpf_1p: {
                        value = applyHPF1PEquation(monoR.histX1, monoR.histY1);
                        break;
                        }
                  case FilterType::lpf_1p: {
                        value = applyLPF1PEquation(monoR.histY1);
                        break;
                        }
                  default:
                        qWarning() << "this equation is not implemented" << (int)sampleZone->fil_type;
                  }
            }

      if (filter_coeff_incr_count) {
            --filter_coeff_incr_count;
            a1 += a1_incr;
            a2 += a2_incr;
            b0 += b0_incr;
            b1 += b1_incr;
            b2 += b2_incr;
            }

      return value;
      }

//---------------------------------------------------------
//   interpolate
//---------------------------------------------------------

float ZFilter::interpolate(unsigned phase, short prevVal, short currVal, short nextVal, short nextNextVal) const
      {
      const auto& interpValTable = interpCoeff[phase];
      return (interpValTable[0] * prevVal
          + interpValTable[1] * currVal
          + interpValTable[2] * nextVal
          + interpValTable[3] * nextNextVal);
      }