RetroArch/audio/hermite.c

111 lines
3.0 KiB
C
Raw Normal View History

2011-02-06 11:57:12 +00:00
/* 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 <http://www.gnu.org/licenses/>.
*/
// Hermite resampler based on bsnes' audio library.
#include "hermite.h"
#include <stdlib.h>
#define MAX_CHANS 8
2011-02-06 11:57:12 +00:00
struct hermite_resampler
{
float chan_data[MAX_CHANS][4];
2011-02-06 11:57:12 +00:00
double r_frac;
unsigned chans;
2011-02-06 11:57:12 +00:00
};
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(unsigned channels)
2011-02-06 11:57:12 +00:00
{
if (channels > MAX_CHANS)
return NULL;
2011-02-06 11:57:12 +00:00
hermite_resampler_t *re = calloc(1, sizeof(*re));
if (!re)
return NULL;
re->chans = channels;
2011-02-06 11:57:12 +00:00
return re;
}
void hermite_process(hermite_resampler_t *re, struct hermite_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;
size_t processed_in = 0;
2011-02-06 11:57:12 +00:00
size_t in_frames = data->input_frames;
size_t out_frames = data->output_frames;
const float *in_data = data->data_in;
float *out_data = data->data_out;
2011-02-06 11:57:12 +00:00
while (processed_in < in_frames && processed_out < out_frames)
2011-02-06 11:57:12 +00:00
{
while (re->r_frac <= 1.0 && processed_out < out_frames)
2011-02-06 11:57:12 +00:00
{
re->r_frac += r_step;
for (unsigned i = 0; i < re->chans; i++)
2011-02-06 11:57:12 +00:00
{
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_out++;
2011-02-06 11:57:12 +00:00
}
if (re->r_frac >= 1.0)
2011-02-06 11:57:12 +00:00
{
re->r_frac -= 1.0;
2011-02-06 11:57:12 +00:00
for (unsigned i = 0; i < re->chans; 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++;
}
processed_in++;
}
2011-02-06 11:57:12 +00:00
}
data->input_frames_used = processed_in;
data->output_frames_gen = processed_out;
2011-02-06 11:57:12 +00:00
}
void hermite_free(hermite_resampler_t *re)
{
free(re);
}