1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (wahwah.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * 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 THE SOFTWARE.
27 #include <retro_miscellaneous.h>
28 #include <libretro_dspfilter.h>
30 #define WAHWAH_LFO_SKIP_SAMPLES 30
36 float b0, b1, b2, a0, a1, a2;
37 float freq, startphase;
38 float depth, freqofs, res;
39 unsigned long skipcount;
43 float xn1, xn2, yn1, yn2;
47 static void wahwah_free(void *data)
53 static void wahwah_process(void *data, struct dspfilter_output *output,
54 const struct dspfilter_input *input)
57 struct wahwah_data *wah = (struct wahwah_data*)data;
58 float *out = output->samples;
60 output->samples = input->samples;
61 output->frames = input->frames;
63 for (i = 0; i < input->frames; i++, out += 2)
66 float in[2] = { out[0], out[1] };
68 if ((wah->skipcount++ % WAHWAH_LFO_SKIP_SAMPLES) == 0)
70 float omega, sn, cs, alpha;
71 float frequency = (1.0f + cos(wah->skipcount * wah->lfoskip + wah->phase)) / 2.0f;
73 frequency = frequency * wah->depth * (1.0f - wah->freqofs) + wah->freqofs;
74 frequency = exp((frequency - 1.0f) * 6.0f);
76 omega = M_PI * frequency;
79 alpha = sn / (2.0f * wah->res);
81 wah->b0 = (1.0f - cs) / 2.0f;
83 wah->b2 = (1.0f - cs) / 2.0f;
84 wah->a0 = 1.0f + alpha;
86 wah->a2 = 1.0f - alpha;
89 out_l = (wah->b0 * in[0] + wah->b1 * wah->l.xn1 + wah->b2 * wah->l.xn2 - wah->a1 * wah->l.yn1 - wah->a2 * wah->l.yn2) / wah->a0;
90 out_r = (wah->b0 * in[1] + wah->b1 * wah->r.xn1 + wah->b2 * wah->r.xn2 - wah->a1 * wah->r.yn1 - wah->a2 * wah->r.yn2) / wah->a0;
92 wah->l.xn2 = wah->l.xn1;
94 wah->l.yn2 = wah->l.yn1;
97 wah->r.xn2 = wah->r.xn1;
99 wah->r.yn2 = wah->r.yn1;
107 static void *wahwah_init(const struct dspfilter_info *info,
108 const struct dspfilter_config *config, void *userdata)
110 struct wahwah_data *wah = (struct wahwah_data*)calloc(1, sizeof(*wah));
114 config->get_float(userdata, "lfo_freq", &wah->freq, 1.5f);
115 config->get_float(userdata, "lfo_start_phase", &wah->startphase, 0.0f);
116 config->get_float(userdata, "freq_offset", &wah->freqofs, 0.3f);
117 config->get_float(userdata, "depth", &wah->depth, 0.7f);
118 config->get_float(userdata, "resonance", &wah->res, 2.5f);
120 wah->lfoskip = wah->freq * 2.0f * M_PI / info->input_rate;
121 wah->phase = wah->startphase * M_PI / 180.0f;
126 static const struct dspfilter_implementation wahwah_plug = {
131 DSPFILTER_API_VERSION,
136 #ifdef HAVE_FILTERS_BUILTIN
137 #define dspfilter_get_implementation wahwah_dspfilter_get_implementation
140 const struct dspfilter_implementation *
141 dspfilter_get_implementation(dspfilter_simd_mask_t mask)
147 #undef dspfilter_get_implementation