1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 Christian Schoenebeck * |
7 |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
10 |
* the Free Software Foundation; either version 2 of the License, or * |
11 |
* (at your option) any later version. * |
12 |
* * |
13 |
* This program is distributed in the hope that it will be useful, * |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
* GNU General Public License for more details. * |
17 |
* * |
18 |
* You should have received a copy of the GNU General Public License * |
19 |
* along with this program; if not, write to the Free Software * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
23 |
|
24 |
#ifndef __LS_GIG_FILTER_H__ |
25 |
#define __LS_GIG_FILTER_H__ |
26 |
|
27 |
#include "../../common/global.h" |
28 |
|
29 |
#include <gig.h> |
30 |
|
31 |
#include "../common/BiquadFilter.h" |
32 |
|
33 |
// TODO: Gigasampler's "Turbo Lowpass" and "Bandreject" filters not implemented yet |
34 |
|
35 |
#define LSF_BW 0.9 |
36 |
#define LSF_FB 0.9f |
37 |
|
38 |
namespace LinuxSampler { namespace gig { |
39 |
|
40 |
/** |
41 |
* These are filters similar to the ones from Gigasampler. |
42 |
*/ |
43 |
class Filter { |
44 |
protected: |
45 |
BandpassFilter BasicBPFilter; |
46 |
HighpassFilter HPFilter; |
47 |
BandpassFilter BPFilter; |
48 |
LowpassFilter LPFilter; |
49 |
BiquadFilter* pFilter; |
50 |
bq_t scale; |
51 |
bq_t resonance; |
52 |
bq_t cutoff; |
53 |
::gig::vcf_type_t Type; |
54 |
static const float fFB = LSF_FB; |
55 |
public: |
56 |
|
57 |
Filter() { |
58 |
// set filter type to 'lowpass' by default |
59 |
pFilter = &LPFilter; |
60 |
Type = ::gig::vcf_type_lowpass; |
61 |
} |
62 |
|
63 |
inline bq_t Cutoff() { return cutoff; } |
64 |
|
65 |
inline bq_t Resonance() { return resonance; } |
66 |
|
67 |
inline void SetType(::gig::vcf_type_t FilterType) { |
68 |
switch (FilterType) { |
69 |
case ::gig::vcf_type_highpass: |
70 |
pFilter = &HPFilter; |
71 |
break; |
72 |
case ::gig::vcf_type_bandreject: //TODO: not implemented yet |
73 |
FilterType = ::gig::vcf_type_bandpass; |
74 |
case ::gig::vcf_type_bandpass: |
75 |
pFilter = &BPFilter; |
76 |
break; |
77 |
case ::gig::vcf_type_lowpassturbo: //TODO: not implemented yet |
78 |
default: |
79 |
FilterType = ::gig::vcf_type_lowpass; |
80 |
case ::gig::vcf_type_lowpass: |
81 |
pFilter = &LPFilter; |
82 |
|
83 |
} |
84 |
Type = FilterType; |
85 |
} |
86 |
|
87 |
inline void SetParameters(bq_t cutoff, bq_t resonance, bq_t fs) { |
88 |
BasicBPFilter.SetParameters(cutoff, 0.7, fs); |
89 |
switch (Type) { |
90 |
case ::gig::vcf_type_highpass: |
91 |
HPFilter.SetParameters(cutoff, 1.0 - resonance * LSF_BW, fs); |
92 |
break; |
93 |
case ::gig::vcf_type_bandpass: |
94 |
BPFilter.SetParameters(cutoff, 1.0 - resonance * LSF_BW, fs); |
95 |
break; |
96 |
case ::gig::vcf_type_lowpass: |
97 |
LPFilter.SetParameters(cutoff, 1.0 - resonance * LSF_BW, fs); |
98 |
break; |
99 |
} |
100 |
this->scale = 1.0f - resonance * 0.7f; |
101 |
this->resonance = resonance; |
102 |
this->cutoff = cutoff; |
103 |
} |
104 |
|
105 |
inline void SetParameters(biquad_param_t* base, biquad_param_t* main, bq_t cutoff, bq_t resonance, bq_t fs) { |
106 |
BasicBPFilter.SetParameters(base, cutoff, 0.7, fs); |
107 |
switch (Type) { |
108 |
case ::gig::vcf_type_highpass: |
109 |
HPFilter.SetParameters(main, cutoff, 1.0 - resonance * LSF_BW, fs); |
110 |
break; |
111 |
case ::gig::vcf_type_bandpass: |
112 |
BPFilter.SetParameters(main, cutoff, 1.0 - resonance * LSF_BW, fs); |
113 |
break; |
114 |
case ::gig::vcf_type_lowpass: |
115 |
LPFilter.SetParameters(main, cutoff, 1.0 - resonance * LSF_BW, fs); |
116 |
break; |
117 |
} |
118 |
this->scale = 1.0f - resonance * 0.7f; |
119 |
this->resonance = resonance; |
120 |
this->cutoff = cutoff; |
121 |
} |
122 |
|
123 |
void Reset() { |
124 |
BasicBPFilter.Reset(); |
125 |
HPFilter.Reset(); |
126 |
BPFilter.Reset(); |
127 |
LPFilter.Reset(); |
128 |
} |
129 |
|
130 |
inline bq_t Apply(const bq_t in) { |
131 |
return pFilter->Apply(in) * this->scale + |
132 |
BasicBPFilter.ApplyFB(in, this->resonance * LSF_FB) * this->resonance; |
133 |
} |
134 |
|
135 |
inline bq_t Apply(biquad_param_t* base, biquad_param_t* main, const bq_t in) { |
136 |
return pFilter->Apply(main, in) * this->scale + |
137 |
BasicBPFilter.ApplyFB(base, in, this->resonance * LSF_FB) * this->resonance; |
138 |
} |
139 |
|
140 |
#if CONFIG_ASM && ARCH_X86 |
141 |
// expects to find input in xmm0 and leaves output in xmm7 |
142 |
inline void Apply4StepsSSE(biquad_param_t* base, biquad_param_t* main) { |
143 |
float fb; |
144 |
__asm__ __volatile__ ( |
145 |
"movss %0, %%xmm4\n\t" |
146 |
"mulss %1, %%xmm4 # this->resonance * LSF_FB\n\t" |
147 |
"movss %%xmm4, %2\n\t" |
148 |
:: "m" (fFB), /* %0 */ |
149 |
"m" (resonance), /* %1 */ |
150 |
"m" (fb) /* %2 */ |
151 |
); |
152 |
BasicBPFilter.ApplyFB4StepsSSE(base, fb); // leaves output in xmm7 |
153 |
__asm__ __volatile__ ( |
154 |
"movss %0, %%xmm4\n\t" |
155 |
"shufps $0, %%xmm4, %%xmm4 # copy to other 3 cells\n\t" |
156 |
"mulps %%xmm4, %%xmm7 # ApplyFB() * this->resonance\n\t" |
157 |
:: "m" (resonance) /* %0 */ |
158 |
); |
159 |
pFilter->Apply4StepsSSE(main); // leaves output in xmm6 |
160 |
__asm__ __volatile__ ( |
161 |
"movss %0, %%xmm5\n\t" |
162 |
"shufps $0, %%xmm5, %%xmm5 # copy to other 3 cells\n\t" |
163 |
"mulps %%xmm5, %%xmm6 # Apply() * this->scale\n\t" |
164 |
"addps %%xmm6, %%xmm7 # xmm7 = result\n\t" |
165 |
:: "m" (scale) /* %0 */ |
166 |
); |
167 |
} |
168 |
#endif // CONFIG_ASM && ARCH_X86 |
169 |
|
170 |
}; |
171 |
|
172 |
}} //namespace LinuxSampler::gig |
173 |
|
174 |
#endif // __LS_GIG_FILTER_H__ |