RetroArch/audio/hermite.c

105 lines
2.7 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2012-01-08 00:08:18 +00:00
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
2011-02-06 11:57:12 +00:00
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-02-06 11:57:12 +00:00
* 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.
*
2012-04-21 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-02-06 11:57:12 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-02-06 11:57:12 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
// Hermite resampler based on bsnes' audio library.
#include "resampler.h"
2011-02-06 11:57:12 +00:00
#include <stdlib.h>
2012-02-25 13:02:56 +00:00
#include <math.h>
#include "../boolean.h"
2011-02-06 11:57:12 +00:00
2012-07-06 15:36:37 +00:00
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
2011-10-20 20:26:41 +00:00
#define CHANNELS 2
2012-04-21 21:25:32 +00:00
struct rarch_resampler
2011-02-06 11:57:12 +00:00
{
2011-10-20 20:26:41 +00:00
float chan_data[CHANNELS][4];
2011-02-06 11:57:12 +00:00
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;
2011-12-24 23:59:46 +00:00
m0 = (c - a) * 0.5f;
m1 = (d - b) * 0.5f;
2011-02-06 11:57:12 +00:00
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);
}
2012-04-21 21:25:32 +00:00
rarch_resampler_t *resampler_new(void)
2011-02-06 11:57:12 +00:00
{
2012-04-21 21:25:32 +00:00
return (rarch_resampler_t*)calloc(1, sizeof(rarch_resampler_t));
2011-02-06 11:57:12 +00:00
}
2012-04-21 21:25:32 +00:00
void resampler_process(rarch_resampler_t *re, struct resampler_data *data)
2011-02-06 11:57:12 +00:00
{
2011-10-15 12:33:41 +00:00
double r_step = 1.0 / data->ratio;
size_t processed_out = 0;
2011-02-06 11:57:12 +00:00
size_t in_frames = data->input_frames;
const float *in_data = data->data_in;
float *out_data = data->data_out;
2011-02-06 11:57:12 +00:00
2012-11-25 11:32:24 +00:00
size_t i = 0;
while (i < in_frames)
2011-02-06 11:57:12 +00:00
{
2012-11-25 11:32:24 +00:00
while (re->r_frac >= 1.0 && i < in_frames)
2011-02-06 11:57:12 +00:00
{
2012-11-25 11:32:24 +00:00
re->r_frac -= 1.0;
2012-12-02 09:51:28 +00:00
for (unsigned c = 0; c < CHANNELS; c++)
2011-02-06 11:57:12 +00:00
{
2012-12-02 09:51:28 +00:00
re->chan_data[c][0] = re->chan_data[c][1];
re->chan_data[c][1] = re->chan_data[c][2];
re->chan_data[c][2] = re->chan_data[c][3];
re->chan_data[c][3] = *in_data++;
2011-02-06 11:57:12 +00:00
}
2012-11-25 11:32:24 +00:00
i++;
2011-02-06 11:57:12 +00:00
}
2012-11-25 11:32:24 +00:00
while (re->r_frac <= 1.0)
2011-02-06 11:57:12 +00:00
{
2012-11-25 11:32:24 +00:00
re->r_frac += r_step;
2012-12-02 09:51:28 +00:00
for (unsigned c = 0; c < CHANNELS; c++)
{
2012-11-25 11:32:24 +00:00
float res = hermite_kernel((float)re->r_frac,
2012-12-02 09:51:28 +00:00
re->chan_data[c][0], re->chan_data[c][1],
re->chan_data[c][2], re->chan_data[c][3]);
2012-11-25 11:32:24 +00:00
*out_data++ = res;
}
2012-11-25 11:32:24 +00:00
processed_out++;
}
2011-02-06 11:57:12 +00:00
}
2011-11-03 22:48:36 +00:00
data->output_frames = processed_out;
2011-02-06 11:57:12 +00:00
}
2012-04-21 21:25:32 +00:00
void resampler_free(rarch_resampler_t *re)
2011-02-06 11:57:12 +00:00
{
free(re);
}