1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (tremolo.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>
29 #include <string/stdstring.h>
31 #define sqr(a) ((a) * (a))
44 struct tremolo_core left, right;
47 static void tremolo_free(void *data)
49 struct tremolo *tre = (struct tremolo*)data;
50 free(tre->left.wavetable);
51 free(tre->right.wavetable);
55 static void tremolocore_init(struct tremolo_core *core,float depth,int samplerate,float freq)
59 const double offset = 1. - depth / 2.;
61 core->maxindex = samplerate / freq;
62 core->wavetable = malloc(core->maxindex * sizeof(float));
63 memset(core->wavetable, 0, core->maxindex * sizeof(float));
64 for (i = 0; i < core->maxindex; i++)
66 env = freq * i / samplerate;
67 env = sin((M_PI*2) * fmod(env + 0.25, 1.0));
68 core->wavetable[i] = env * (1 - fabs(offset)) + offset;
72 float tremolocore_core(struct tremolo_core *core,float in)
74 core->index = core->index % core->maxindex;
75 return in * core->wavetable[core->index++];
78 static void tremolo_process(void *data, struct dspfilter_output *output,
79 const struct dspfilter_input *input)
83 struct tremolo *tre = (struct tremolo*)data;
85 output->samples = input->samples;
86 output->frames = input->frames;
87 out = output->samples;
89 for (i = 0; i < input->frames; i++, out += 2)
91 float in[2] = { out[0], out[1] };
92 out[0] = tremolocore_core(&tre->left, in[0]);
93 out[1] = tremolocore_core(&tre->right, in[1]);
97 static void *tremolo_init(const struct dspfilter_info *info,
98 const struct dspfilter_config *config, void *userdata)
101 struct tremolo *tre = (struct tremolo*)calloc(1, sizeof(*tre));
105 config->get_float(userdata, "freq", &freq,4.0f);
106 config->get_float(userdata, "depth", &depth, 0.9f);
107 tremolocore_init(&tre->left,depth,info->input_rate,freq);
108 tremolocore_init(&tre->right,depth,info->input_rate,freq);
112 static const struct dspfilter_implementation tremolo_plug = {
117 DSPFILTER_API_VERSION,
122 #ifdef HAVE_FILTERS_BUILTIN
123 #define dspfilter_get_implementation tremolo_dspfilter_get_implementation
126 const struct dspfilter_implementation *dspfilter_get_implementation(dspfilter_simd_mask_t mask)
128 return &tremolo_plug;
131 #undef dspfilter_get_implementation