1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (echo.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.
25 #include <retro_miscellaneous.h>
26 #include <libretro_dspfilter.h>
38 struct echo_channel *channels;
39 unsigned num_channels;
43 static void echo_free(void *data)
46 struct echo_data *echo = (struct echo_data*)data;
48 for (i = 0; i < echo->num_channels; i++)
49 free(echo->channels[i].buffer);
54 static void echo_process(void *data, struct dspfilter_output *output,
55 const struct dspfilter_input *input)
59 struct echo_data *echo = (struct echo_data*)data;
61 output->samples = input->samples;
62 output->frames = input->frames;
64 out = output->samples;
66 for (i = 0; i < input->frames; i++, out += 2)
69 float echo_left = 0.0f;
70 float echo_right = 0.0f;
72 for (c = 0; c < echo->num_channels; c++)
74 echo_left += echo->channels[c].buffer[(echo->channels[c].ptr << 1) + 0];
75 echo_right += echo->channels[c].buffer[(echo->channels[c].ptr << 1) + 1];
78 echo_left *= echo->amp;
79 echo_right *= echo->amp;
81 left = out[0] + echo_left;
82 right = out[1] + echo_right;
84 for (c = 0; c < echo->num_channels; c++)
86 float feedback_left = out[0] + echo->channels[c].feedback * echo_left;
87 float feedback_right = out[1] + echo->channels[c].feedback * echo_right;
89 echo->channels[c].buffer[(echo->channels[c].ptr << 1) + 0] = feedback_left;
90 echo->channels[c].buffer[(echo->channels[c].ptr << 1) + 1] = feedback_right;
92 echo->channels[c].ptr = (echo->channels[c].ptr + 1) % echo->channels[c].frames;
100 static void *echo_init(const struct dspfilter_info *info,
101 const struct dspfilter_config *config, void *userdata)
103 unsigned i, channels;
104 struct echo_channel *echo_channels = NULL;
106 float *feedback = NULL;
107 unsigned num_delay = 0;
108 unsigned num_feedback = 0;
110 static const float default_delay[] = { 200.0f };
111 static const float default_feedback[] = { 0.5f };
112 struct echo_data *echo = (struct echo_data*)
113 calloc(1, sizeof(*echo));
118 config->get_float_array(userdata, "delay", &delay,
119 &num_delay, default_delay, 1);
120 config->get_float_array(userdata, "feedback", &feedback,
121 &num_feedback, default_feedback, 1);
122 config->get_float(userdata, "amp", &echo->amp, 0.2f);
124 channels = num_feedback = num_delay = MIN(num_delay, num_feedback);
126 if (!(echo_channels = (struct echo_channel*)calloc(channels,
127 sizeof(*echo_channels))))
130 echo->channels = echo_channels;
131 echo->num_channels = channels;
133 for (i = 0; i < channels; i++)
135 unsigned frames = (unsigned)(delay[i] * info->input_rate / 1000.0f + 0.5f);
139 if (!(echo->channels[i].buffer = (float*)calloc(frames, 2 * sizeof(float))))
142 echo->channels[i].frames = frames;
143 echo->channels[i].feedback = feedback[i];
147 config->free(feedback);
152 config->free(feedback);
157 static const struct dspfilter_implementation echo_plug = {
162 DSPFILTER_API_VERSION,
167 #ifdef HAVE_FILTERS_BUILTIN
168 #define dspfilter_get_implementation echo_dspfilter_get_implementation
171 const struct dspfilter_implementation *dspfilter_get_implementation(dspfilter_simd_mask_t mask)
176 #undef dspfilter_get_implementation