1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (chorus.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 CHORUS_MAX_DELAY 4096
31 #define CHORUS_DELAY_MASK (CHORUS_MAX_DELAY - 1)
35 float old[2][CHORUS_MAX_DELAY];
46 static void chorus_free(void *data)
52 static void chorus_process(void *data, struct dspfilter_output *output,
53 const struct dspfilter_input *input)
57 struct chorus_data *ch = (struct chorus_data*)data;
59 output->samples = input->samples;
60 output->frames = input->frames;
61 out = output->samples;
63 for (i = 0; i < input->frames; i++, out += 2)
66 float delay_frac, l_a, l_b, r_a, r_b;
67 float chorus_l, chorus_r;
68 float in[2] = { out[0], out[1] };
69 float delay = ch->delay + ch->depth * sin((2.0 * M_PI * ch->lfo_ptr++) / ch->lfo_period);
71 delay *= ch->input_rate;
72 if (ch->lfo_ptr >= ch->lfo_period)
75 delay_int = (unsigned)delay;
77 if (delay_int >= CHORUS_MAX_DELAY - 1)
78 delay_int = CHORUS_MAX_DELAY - 2;
80 delay_frac = delay - delay_int;
82 ch->old[0][ch->old_ptr] = in[0];
83 ch->old[1][ch->old_ptr] = in[1];
85 l_a = ch->old[0][(ch->old_ptr - delay_int - 0) & CHORUS_DELAY_MASK];
86 l_b = ch->old[0][(ch->old_ptr - delay_int - 1) & CHORUS_DELAY_MASK];
87 r_a = ch->old[1][(ch->old_ptr - delay_int - 0) & CHORUS_DELAY_MASK];
88 r_b = ch->old[1][(ch->old_ptr - delay_int - 1) & CHORUS_DELAY_MASK];
90 /* Lerp introduces aliasing of the chorus component,
91 * but doing full polyphase here is probably overkill. */
92 chorus_l = l_a * (1.0f - delay_frac) + l_b * delay_frac;
93 chorus_r = r_a * (1.0f - delay_frac) + r_b * delay_frac;
95 out[0] = ch->mix_dry * in[0] + ch->mix_wet * chorus_l;
96 out[1] = ch->mix_dry * in[1] + ch->mix_wet * chorus_r;
98 ch->old_ptr = (ch->old_ptr + 1) & CHORUS_DELAY_MASK;
102 static void *chorus_init(const struct dspfilter_info *info,
103 const struct dspfilter_config *config, void *userdata)
105 float delay, depth, lfo_freq, drywet;
106 struct chorus_data *ch = (struct chorus_data*)calloc(1, sizeof(*ch));
110 config->get_float(userdata, "delay_ms", &delay, 25.0f);
111 config->get_float(userdata, "depth_ms", &depth, 1.0f);
112 config->get_float(userdata, "lfo_freq", &lfo_freq, 0.5f);
113 config->get_float(userdata, "drywet", &drywet, 0.8f);
123 else if (drywet > 1.0f)
126 ch->mix_dry = 1.0f - 0.5f * drywet;
127 ch->mix_wet = 0.5f * drywet;
131 ch->lfo_period = (1.0f / lfo_freq) * info->input_rate;
132 ch->input_rate = info->input_rate;
138 static const struct dspfilter_implementation chorus_plug = {
143 DSPFILTER_API_VERSION,
148 #ifdef HAVE_FILTERS_BUILTIN
149 #define dspfilter_get_implementation chorus_dspfilter_get_implementation
152 const struct dspfilter_implementation *
153 dspfilter_get_implementation(dspfilter_simd_mask_t mask) { return &chorus_plug; }
155 #undef dspfilter_get_implementation