| 1 | /* Copyright (C) 2010-2020 The RetroArch team |
| 2 | * |
| 3 | * --------------------------------------------------------------------------------------- |
| 4 | * The following license statement only applies to this file (panning.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 <libretro_dspfilter.h> |
| 28 | |
| 29 | struct panning_data |
| 30 | { |
| 31 | float left[2]; |
| 32 | float right[2]; |
| 33 | }; |
| 34 | |
| 35 | static void panning_free(void *data) |
| 36 | { |
| 37 | free(data); |
| 38 | } |
| 39 | |
| 40 | static void panning_process(void *data, struct dspfilter_output *output, |
| 41 | const struct dspfilter_input *input) |
| 42 | { |
| 43 | unsigned i; |
| 44 | struct panning_data *pan = (struct panning_data*)data; |
| 45 | float *out = output->samples; |
| 46 | |
| 47 | output->samples = input->samples; |
| 48 | output->frames = input->frames; |
| 49 | |
| 50 | for (i = 0; i < input->frames; i++, out += 2) |
| 51 | { |
| 52 | float left = out[0]; |
| 53 | float right = out[1]; |
| 54 | out[0] = left * pan->left[0] + right * pan->left[1]; |
| 55 | out[1] = left * pan->right[0] + right * pan->right[1]; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | static void *panning_init(const struct dspfilter_info *info, |
| 60 | const struct dspfilter_config *config, void *userdata) |
| 61 | { |
| 62 | static const float default_left[] = { 1.0f, 0.0f }; |
| 63 | static const float default_right[] = { 0.0f, 1.0f }; |
| 64 | float *left = NULL; |
| 65 | float *right = NULL; |
| 66 | unsigned num_left = 0; |
| 67 | unsigned num_right = 0; |
| 68 | struct panning_data *pan = (struct panning_data*) |
| 69 | calloc(1, sizeof(*pan)); |
| 70 | |
| 71 | if (!pan) |
| 72 | return NULL; |
| 73 | |
| 74 | config->get_float_array(userdata, "left_mix", |
| 75 | &left, &num_left, default_left, 2); |
| 76 | config->get_float_array(userdata, "right_mix", |
| 77 | &right, &num_right, default_right, 2); |
| 78 | |
| 79 | memcpy(pan->left, (num_left == 2) ? |
| 80 | left : default_left, sizeof(pan->left)); |
| 81 | memcpy(pan->right, (num_right == 2) ? |
| 82 | right : default_right, sizeof(pan->right)); |
| 83 | |
| 84 | config->free(left); |
| 85 | config->free(right); |
| 86 | |
| 87 | return pan; |
| 88 | } |
| 89 | |
| 90 | static const struct dspfilter_implementation panning = { |
| 91 | panning_init, |
| 92 | panning_process, |
| 93 | panning_free, |
| 94 | |
| 95 | DSPFILTER_API_VERSION, |
| 96 | "Panning", |
| 97 | "panning", |
| 98 | }; |
| 99 | |
| 100 | #ifdef HAVE_FILTERS_BUILTIN |
| 101 | #define dspfilter_get_implementation panning_dspfilter_get_implementation |
| 102 | #endif |
| 103 | |
| 104 | const struct dspfilter_implementation * |
| 105 | dspfilter_get_implementation(dspfilter_simd_mask_t mask) |
| 106 | { |
| 107 | return &panning; |
| 108 | } |
| 109 | |
| 110 | #undef dspfilter_get_implementation |