1 // Copyright 2014 Olivier Gillet.
2 //
3 // Author: Olivier Gillet (ol.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 // Filter bank.
28 
29 #include "warps/dsp/filter_bank.h"
30 
31 #include <algorithm>
32 
33 #include "warps/resources.h"
34 
35 namespace warps {
36 
37 using namespace std;
38 using namespace stmlib;
39 
Init(float sample_rate)40 void FilterBank::Init(float sample_rate) {
41   low_src_down_.Init();
42   low_src_up_.Init();
43   mid_src_down_.Init();
44   mid_src_up_.Init();
45 
46   int32_t max_delay = 0;
47   float* samples = &samples_[0];
48 
49   int32_t group = -1;
50   int32_t decimation_factor = -1;
51   for (int32_t i = 0; i < kNumBands; ++i) {
52     const float* coefficients = filter_bank_table[i];
53 
54     Band& b = band_[i];
55 
56     b.decimation_factor = static_cast<int32_t>(coefficients[0]);
57 
58     if (b.decimation_factor != decimation_factor) {
59       decimation_factor = b.decimation_factor;
60       ++group;
61     }
62 
63     b.group = group;
64     b.sample_rate = sample_rate / static_cast<float>(b.decimation_factor);
65     b.samples = samples;
66     samples += kMaxFilterBankBlockSize / b.decimation_factor;
67 
68     b.delay = static_cast<int32_t>(coefficients[1]);
69     b.delay *= b.decimation_factor;
70     b.post_gain = coefficients[2];
71 
72     max_delay = max(max_delay, b.delay);
73     for (int32_t pass = 0; pass < 2; ++pass) {
74       b.svf[pass].Init();
75       b.svf[pass].set_f_fq(
76           coefficients[pass * 2 + 3],
77           coefficients[pass * 2 + 4]);
78     }
79   }
80   band_[kNumBands].group = band_[kNumBands - 1].group + 1;
81   max_delay = min(max_delay, int32_t(256));
82   float* delay_ptr = &delay_buffer_[0];
83   for (int32_t i = 0; i < kNumBands; ++i) {
84     Band& b = band_[i];
85     int32_t compensation = max_delay - b.delay;
86     if (b.group == 0) {
87       compensation -= kLowFactor * \
88           (low_src_down_.delay() + low_src_up_.delay());
89       compensation -= mid_src_down_.delay();
90       compensation -= mid_src_up_.delay();
91     } else if (b.group == 1) {
92       compensation -= mid_src_down_.delay();
93       compensation -= mid_src_up_.delay();
94     }
95     compensation = max(compensation - b.decimation_factor / 2, int32_t(0));
96     b.delay_line.Init(delay_ptr, compensation / b.decimation_factor);
97     delay_ptr += b.delay_line.size();
98   }
99 }
100 
Analyze(const float * in,size_t size)101 void FilterBank::Analyze(const float* in, size_t size) {
102   mid_src_down_.Process(in, tmp_[0], size);
103   low_src_down_.Process(tmp_[0], tmp_[1], size / kMidFactor);
104 
105   const float* sources[3] = { tmp_[1], tmp_[0], in };
106   for (int32_t i = 0; i < kNumBands; ++i) {
107     Band& b = band_[i];
108     const size_t band_size = size / b.decimation_factor;
109     const float* input = sources[b.group];
110 
111     for (int32_t pass = 0; pass < 2; ++pass) {
112       const float* source = pass == 0 ? input : b.samples;
113       float* destination = b.samples;
114       if (i == 0) {
115         b.svf[pass].Process<FILTER_MODE_LOW_PASS>(
116             source, destination, band_size);
117       } else if (i == kNumBands - 1) {
118         b.svf[pass].Process<FILTER_MODE_HIGH_PASS>(
119             source, destination, band_size);
120       } else {
121         b.svf[pass].Process<FILTER_MODE_BAND_PASS_NORMALIZED>(
122             source, destination, band_size);
123       }
124     }
125     // Apply post-gain
126     const float gain = b.post_gain;
127     float* output = b.samples;
128     for (size_t i = 0; i < band_size; ++i) {
129       output[i] *= gain;
130     }
131   }
132 }
133 
Synthesize(float * out,size_t size)134 void FilterBank::Synthesize(float* out, size_t size) {
135   float* buffers[3] = { tmp_[1], tmp_[0], out };
136 
137   fill(&buffers[0][0], &buffers[0][size / band_[0].decimation_factor], 0.0f);
138   for (int32_t i = 0; i < kNumBands; ++i) {
139     Band& b = band_[i];
140 
141     size_t band_size = size / b.decimation_factor;
142     float* s = buffers[b.group];
143     for (size_t j = 0; j < band_size; ++j) {
144       s[j] += b.delay_line.ReadWrite(b.samples[j]);
145     }
146 
147     if (band_[i + 1].group != b.group) {
148       if (b.group == 0) {
149         low_src_up_.Process(tmp_[1], tmp_[0], band_size);
150       } else if (b.group == 1) {
151         mid_src_up_.Process(tmp_[0], out, band_size);
152       }
153     }
154   }
155 }
156 
157 }  // namespace warps
158