Commit | Line | Data |
---|---|---|
3719602c PC |
1 | /* Copyright (C) 2010-2020 The RetroArch team |
2 | * | |
3 | * --------------------------------------------------------------------------------------- | |
4 | * The following license statement only applies to this file (wahwah.c). | |
5 | * --------------------------------------------------------------------------------------- | |
6 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
14 | * | |
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. | |
21 | */ | |
22 | ||
23 | #include <math.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | ||
27 | #include <retro_miscellaneous.h> | |
28 | #include <libretro_dspfilter.h> | |
29 | ||
30 | #define WAHWAH_LFO_SKIP_SAMPLES 30 | |
31 | ||
32 | struct wahwah_data | |
33 | { | |
34 | float phase; | |
35 | float lfoskip; | |
36 | float b0, b1, b2, a0, a1, a2; | |
37 | float freq, startphase; | |
38 | float depth, freqofs, res; | |
39 | unsigned long skipcount; | |
40 | ||
41 | struct | |
42 | { | |
43 | float xn1, xn2, yn1, yn2; | |
44 | } l, r; | |
45 | }; | |
46 | ||
47 | static void wahwah_free(void *data) | |
48 | { | |
49 | if (data) | |
50 | free(data); | |
51 | } | |
52 | ||
53 | static void wahwah_process(void *data, struct dspfilter_output *output, | |
54 | const struct dspfilter_input *input) | |
55 | { | |
56 | unsigned i; | |
57 | struct wahwah_data *wah = (struct wahwah_data*)data; | |
58 | float *out = output->samples; | |
59 | ||
60 | output->samples = input->samples; | |
61 | output->frames = input->frames; | |
62 | ||
63 | for (i = 0; i < input->frames; i++, out += 2) | |
64 | { | |
65 | float out_l, out_r; | |
66 | float in[2] = { out[0], out[1] }; | |
67 | ||
68 | if ((wah->skipcount++ % WAHWAH_LFO_SKIP_SAMPLES) == 0) | |
69 | { | |
70 | float omega, sn, cs, alpha; | |
71 | float frequency = (1.0f + cos(wah->skipcount * wah->lfoskip + wah->phase)) / 2.0f; | |
72 | ||
73 | frequency = frequency * wah->depth * (1.0f - wah->freqofs) + wah->freqofs; | |
74 | frequency = exp((frequency - 1.0f) * 6.0f); | |
75 | ||
76 | omega = M_PI * frequency; | |
77 | sn = sin(omega); | |
78 | cs = cos(omega); | |
79 | alpha = sn / (2.0f * wah->res); | |
80 | ||
81 | wah->b0 = (1.0f - cs) / 2.0f; | |
82 | wah->b1 = 1.0f - cs; | |
83 | wah->b2 = (1.0f - cs) / 2.0f; | |
84 | wah->a0 = 1.0f + alpha; | |
85 | wah->a1 = -2.0f * cs; | |
86 | wah->a2 = 1.0f - alpha; | |
87 | } | |
88 | ||
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; | |
91 | ||
92 | wah->l.xn2 = wah->l.xn1; | |
93 | wah->l.xn1 = in[0]; | |
94 | wah->l.yn2 = wah->l.yn1; | |
95 | wah->l.yn1 = out_l; | |
96 | ||
97 | wah->r.xn2 = wah->r.xn1; | |
98 | wah->r.xn1 = in[1]; | |
99 | wah->r.yn2 = wah->r.yn1; | |
100 | wah->r.yn1 = out_r; | |
101 | ||
102 | out[0] = out_l; | |
103 | out[1] = out_r; | |
104 | } | |
105 | } | |
106 | ||
107 | static void *wahwah_init(const struct dspfilter_info *info, | |
108 | const struct dspfilter_config *config, void *userdata) | |
109 | { | |
110 | struct wahwah_data *wah = (struct wahwah_data*)calloc(1, sizeof(*wah)); | |
111 | if (!wah) | |
112 | return NULL; | |
113 | ||
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); | |
119 | ||
120 | wah->lfoskip = wah->freq * 2.0f * M_PI / info->input_rate; | |
121 | wah->phase = wah->startphase * M_PI / 180.0f; | |
122 | ||
123 | return wah; | |
124 | } | |
125 | ||
126 | static const struct dspfilter_implementation wahwah_plug = { | |
127 | wahwah_init, | |
128 | wahwah_process, | |
129 | wahwah_free, | |
130 | ||
131 | DSPFILTER_API_VERSION, | |
132 | "Wah-Wah", | |
133 | "wahwah", | |
134 | }; | |
135 | ||
136 | #ifdef HAVE_FILTERS_BUILTIN | |
137 | #define dspfilter_get_implementation wahwah_dspfilter_get_implementation | |
138 | #endif | |
139 | ||
140 | const struct dspfilter_implementation * | |
141 | dspfilter_get_implementation(dspfilter_simd_mask_t mask) | |
142 | { | |
143 | (void)mask; | |
144 | return &wahwah_plug; | |
145 | } | |
146 | ||
147 | #undef dspfilter_get_implementation |