From 1b5e63f024da6f49851386b4f8fd4a184a5e8da4 Mon Sep 17 00:00:00 2001 From: Themaister Date: Sun, 6 Feb 2011 12:57:12 +0100 Subject: [PATCH] Add hermite resampler. --- audio/hermite.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++ audio/hermite.h | 42 +++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 audio/hermite.c create mode 100644 audio/hermite.h diff --git a/audio/hermite.c b/audio/hermite.c new file mode 100644 index 0000000000..64f4105008 --- /dev/null +++ b/audio/hermite.c @@ -0,0 +1,97 @@ +/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes. + * Copyright (C) 2010-2011 - Hans-Kristian Arntzen + * + * Some code herein may be based on code found in BSNES. + * + * SSNES is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with SSNES. + * If not, see . + */ + +// Hermite resampler based on bsnes' audio library. + +#include "hermite.h" +#include + +struct hermite_resampler +{ + float chan_data[2][4]; + double r_frac; +}; + +static inline float hermite_kernel(float mu1, float a, float b, float c, float d) +{ + float mu2, mu3, m0, m1, a0, a1, a2, a3; + + mu2 = mu1 * mu1; + mu3 = mu2 * mu1; + + m0 = (c - a) * 0.5; + m1 = (d - b) * 0.5; + + a0 = +2 * mu3 - 3 * mu2 + 1; + a1 = mu3 - 2 * mu2 + mu1; + a2 = mu3 - mu2; + a3 = -2 * mu3 + 3 * mu2; + + return (a0 * b) + (a1 * m0) + (a2 * m1) + (a3 * c); +} + +hermite_resampler_t *hermite_new(void) +{ + hermite_resampler_t *re = calloc(1, sizeof(*re)); + if (!re) + return NULL; + return re; +} + +size_t hermite_process(hermite_resampler_t *re, const struct hermite_data *data) +{ + double r_step = 1.0 / data->ratio; + size_t processed = 0; + + size_t in_frames = data->in_frames; + const float *in_data = data->in_data; + float *out_data = data->out_data; + + while (in_frames > 0) + { + while (re->r_frac <= 1.0) + { + re->r_frac += r_step; + for (unsigned i = 0; i < 2; i++) + { + float res = hermite_kernel(re->r_frac, + re->chan_data[i][0], re->chan_data[i][1], re->chan_data[i][2], re->chan_data[i][3]); + *out_data++ = res; + } + processed++; + } + + re->r_frac -= 1.0; + + for (unsigned i = 0; i < 2; i++) + { + re->chan_data[i][0] = re->chan_data[i][1]; + re->chan_data[i][1] = re->chan_data[i][2]; + re->chan_data[i][2] = re->chan_data[i][3]; + re->chan_data[i][3] = *in_data++; + } + + in_frames--; + } + return processed; +} + +void hermite_free(hermite_resampler_t *re) +{ + free(re); +} + diff --git a/audio/hermite.h b/audio/hermite.h new file mode 100644 index 0000000000..11cc9dc4e7 --- /dev/null +++ b/audio/hermite.h @@ -0,0 +1,42 @@ +/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes. + * Copyright (C) 2010-2011 - Hans-Kristian Arntzen + * + * Some code herein may be based on code found in BSNES. + * + * SSNES is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with SSNES. + * If not, see . + */ + +// Hermite resampler based on bsnes' audio library. + +#ifndef __SSNES_HERMITE_H +#define __SSNES_HERMITE_H + +#include + +typedef struct hermite_resampler hermite_resampler_t; + +hermite_resampler_t *hermite_new(void); + +struct hermite_data +{ + const float *in_data; + float *out_data; // We make it really simple and assume that there is always enough space. :) + + size_t in_frames; + double ratio; +}; + +size_t hermite_process(hermite_resampler_t *re, const struct hermite_data *data); +void hermite_free(hermite_resampler_t *re); + +#endif +