git subrepo clone https://github.com/libretro/libretro-common.git deps/libretro-common
[pcsx_rearmed.git] / deps / libretro-common / audio / dsp_filters / chorus.c
1 /* Copyright  (C) 2010-2020 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (chorus.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 CHORUS_MAX_DELAY 4096
31 #define CHORUS_DELAY_MASK (CHORUS_MAX_DELAY - 1)
32
33 struct chorus_data
34 {
35    float old[2][CHORUS_MAX_DELAY];
36    float delay;
37    float depth;
38    float input_rate;
39    float mix_dry;
40    float mix_wet;
41    unsigned old_ptr;
42    unsigned lfo_ptr;
43    unsigned lfo_period;
44 };
45
46 static void chorus_free(void *data)
47 {
48    if (data)
49       free(data);
50 }
51
52 static void chorus_process(void *data, struct dspfilter_output *output,
53       const struct dspfilter_input *input)
54 {
55    unsigned i;
56    float *out             = NULL;
57    struct chorus_data *ch = (struct chorus_data*)data;
58
59    output->samples        = input->samples;
60    output->frames         = input->frames;
61    out                    = output->samples;
62
63    for (i = 0; i < input->frames; i++, out += 2)
64    {
65       unsigned delay_int;
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);
70
71       delay                  *= ch->input_rate;
72       if (ch->lfo_ptr >= ch->lfo_period)
73          ch->lfo_ptr          = 0;
74
75       delay_int               = (unsigned)delay;
76
77       if (delay_int >= CHORUS_MAX_DELAY - 1)
78          delay_int            = CHORUS_MAX_DELAY - 2;
79
80       delay_frac              = delay - delay_int;
81
82       ch->old[0][ch->old_ptr] = in[0];
83       ch->old[1][ch->old_ptr] = in[1];
84
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];
89
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;
94
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;
97
98       ch->old_ptr             = (ch->old_ptr + 1) & CHORUS_DELAY_MASK;
99    }
100 }
101
102 static void *chorus_init(const struct dspfilter_info *info,
103       const struct dspfilter_config *config, void *userdata)
104 {
105    float delay, depth, lfo_freq, drywet;
106    struct chorus_data *ch = (struct chorus_data*)calloc(1, sizeof(*ch));
107    if (!ch)
108       return NULL;
109
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);
114
115    delay            /= 1000.0f;
116    depth            /= 1000.0f;
117
118    if (depth > delay)
119       depth          = delay;
120
121    if (drywet < 0.0f)
122       drywet         = 0.0f;
123    else if (drywet > 1.0f)
124       drywet         = 1.0f;
125
126    ch->mix_dry       = 1.0f - 0.5f * drywet;
127    ch->mix_wet       = 0.5f * drywet;
128
129    ch->delay         = delay;
130    ch->depth         = depth;
131    ch->lfo_period    = (1.0f / lfo_freq) * info->input_rate;
132    ch->input_rate    = info->input_rate;
133    if (!ch->lfo_period)
134       ch->lfo_period = 1;
135    return ch;
136 }
137
138 static const struct dspfilter_implementation chorus_plug = {
139    chorus_init,
140    chorus_process,
141    chorus_free,
142
143    DSPFILTER_API_VERSION,
144    "Chorus",
145    "chorus",
146 };
147
148 #ifdef HAVE_FILTERS_BUILTIN
149 #define dspfilter_get_implementation chorus_dspfilter_get_implementation
150 #endif
151
152 const struct dspfilter_implementation *
153 dspfilter_get_implementation(dspfilter_simd_mask_t mask) { return &chorus_plug; }
154
155 #undef dspfilter_get_implementation