1 /* Copyright  (C) 2010-2017 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (echo.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 <stdlib.h>
24 
25 #include <retro_miscellaneous.h>
26 #include <libretro_dspfilter.h>
27 
28 struct echo_channel
29 {
30    float *buffer;
31    unsigned ptr;
32    unsigned frames;
33    float feedback;
34 };
35 
36 struct echo_data
37 {
38    struct echo_channel *channels;
39    unsigned num_channels;
40    float amp;
41 };
42 
echo_free(void * data)43 static void echo_free(void *data)
44 {
45    unsigned i;
46    struct echo_data *echo = (struct echo_data*)data;
47 
48    for (i = 0; i < echo->num_channels; i++)
49       free(echo->channels[i].buffer);
50    free(echo->channels);
51    free(echo);
52 }
53 
echo_process(void * data,struct dspfilter_output * output,const struct dspfilter_input * input)54 static void echo_process(void *data, struct dspfilter_output *output,
55       const struct dspfilter_input *input)
56 {
57    unsigned i, c;
58    float *out             = NULL;
59    struct echo_data *echo = (struct echo_data*)data;
60 
61    output->samples        = input->samples;
62    output->frames         = input->frames;
63 
64    out                    = output->samples;
65 
66    for (i = 0; i < input->frames; i++, out += 2)
67    {
68       float left, right;
69       float echo_left  = 0.0f;
70       float echo_right = 0.0f;
71 
72       for (c = 0; c < echo->num_channels; c++)
73       {
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];
76       }
77 
78       echo_left  *= echo->amp;
79       echo_right *= echo->amp;
80 
81       left        = out[0] + echo_left;
82       right       = out[1] + echo_right;
83 
84       for (c = 0; c < echo->num_channels; c++)
85       {
86          float feedback_left  = out[0] + echo->channels[c].feedback * echo_left;
87          float feedback_right = out[1] + echo->channels[c].feedback * echo_right;
88 
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;
91 
92          echo->channels[c].ptr = (echo->channels[c].ptr + 1) % echo->channels[c].frames;
93       }
94 
95       out[0] = left;
96       out[1] = right;
97    }
98 }
99 
echo_init(const struct dspfilter_info * info,const struct dspfilter_config * config,void * userdata)100 static void *echo_init(const struct dspfilter_info *info,
101       const struct dspfilter_config *config, void *userdata)
102 {
103    unsigned i, channels;
104    struct echo_channel *echo_channels    = NULL;
105    float *delay                          = NULL;
106    float *feedback                       = NULL;
107    unsigned num_delay                    = 0;
108    unsigned num_feedback                 = 0;
109 
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));
114 
115    if (!echo)
116       return NULL;
117 
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);
123 
124    channels       = num_feedback = num_delay = MIN(num_delay, num_feedback);
125 
126    echo_channels = (struct echo_channel*)calloc(channels,
127          sizeof(*echo_channels));
128 
129    if (!echo_channels)
130       goto error;
131 
132    echo->channels     = echo_channels;
133    echo->num_channels = channels;
134 
135    for (i = 0; i < channels; i++)
136    {
137       unsigned frames = (unsigned)(delay[i] * info->input_rate / 1000.0f + 0.5f);
138       if (!frames)
139          goto error;
140 
141       echo->channels[i].buffer = (float*)calloc(frames, 2 * sizeof(float));
142       if (!echo->channels[i].buffer)
143          goto error;
144 
145       echo->channels[i].frames = frames;
146       echo->channels[i].feedback = feedback[i];
147    }
148 
149    config->free(delay);
150    config->free(feedback);
151    return echo;
152 
153 error:
154    config->free(delay);
155    config->free(feedback);
156    echo_free(echo);
157    return NULL;
158 }
159 
160 static const struct dspfilter_implementation echo_plug = {
161    echo_init,
162    echo_process,
163    echo_free,
164 
165    DSPFILTER_API_VERSION,
166    "Multi-Echo",
167    "echo",
168 };
169 
170 #ifdef HAVE_FILTERS_BUILTIN
171 #define dspfilter_get_implementation echo_dspfilter_get_implementation
172 #endif
173 
dspfilter_get_implementation(dspfilter_simd_mask_t mask)174 const struct dspfilter_implementation *dspfilter_get_implementation(dspfilter_simd_mask_t mask)
175 {
176    (void)mask;
177    return &echo_plug;
178 }
179 
180 #undef dspfilter_get_implementation
181