1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (phaser.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 PHASER_LFO_SHAPE 4.0
31 #define PHASER_LFO_SKIP_SAMPLES 20
47 unsigned long skipcount;
50 static void phaser_free(void *data)
55 static void phaser_process(void *data, struct dspfilter_output *output,
56 const struct dspfilter_input *input)
61 struct phaser_data *ph = (struct phaser_data*)data;
62 float *out = output->samples;
64 output->samples = input->samples;
65 output->frames = input->frames;
67 for (i = 0; i < input->frames; i++, out += 2)
69 float in[2] = { out[0], out[1] };
71 for (c = 0; c < 2; c++)
72 m[c] = in[c] + ph->fbout[c] * ph->fb * 0.01f;
74 if ((ph->skipcount++ % PHASER_LFO_SKIP_SAMPLES) == 0)
76 ph->gain = 0.5 * (1.0 + cos(ph->skipcount * ph->lfoskip + ph->phase));
77 ph->gain = (exp(ph->gain * PHASER_LFO_SHAPE) - 1.0) / (exp(PHASER_LFO_SHAPE) - 1);
78 ph->gain = 1.0 - ph->gain * ph->depth;
81 for (s = 0; s < ph->stages; s++)
83 for (c = 0; c < 2; c++)
85 tmp[c] = ph->old[c][s];
86 ph->old[c][s] = ph->gain * tmp[c] + m[c];
87 m[c] = tmp[c] - ph->gain * ph->old[c][s];
91 for (c = 0; c < 2; c++)
94 out[c] = m[c] * ph->drywet + in[c] * (1.0f - ph->drywet);
99 static void *phaser_init(const struct dspfilter_info *info,
100 const struct dspfilter_config *config, void *userdata)
102 float lfo_freq, lfo_start_phase;
103 struct phaser_data *ph = (struct phaser_data*)calloc(1, sizeof(*ph));
107 config->get_float(userdata, "lfo_freq", &lfo_freq, 0.4f);
108 config->get_float(userdata, "lfo_start_phase", &lfo_start_phase, 0.0f);
109 config->get_float(userdata, "feedback", &ph->fb, 0.0f);
110 config->get_float(userdata, "depth", &ph->depth, 0.4f);
111 config->get_float(userdata, "dry_wet", &ph->drywet, 0.5f);
112 config->get_int(userdata, "stages", &ph->stages, 2);
116 else if (ph->stages > 24)
119 ph->lfoskip = lfo_freq * 2.0 * M_PI / info->input_rate;
120 ph->phase = lfo_start_phase * M_PI / 180.0;
125 static const struct dspfilter_implementation phaser_plug = {
130 DSPFILTER_API_VERSION,
135 #ifdef HAVE_FILTERS_BUILTIN
136 #define dspfilter_get_implementation phaser_dspfilter_get_implementation
139 const struct dspfilter_implementation *dspfilter_get_implementation(dspfilter_simd_mask_t mask)
144 #undef dspfilter_get_implementation