forked from sparklewind/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hermite.c
106 lines (88 loc) · 2.75 KB
/
hermite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
*
* RetroArch 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.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
// Hermite resampler based on bsnes' audio library.
#include "resampler.h"
#include <stdlib.h>
#include <math.h>
#include "../boolean.h"
#include "../general.h"
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#define CHANNELS 2
struct rarch_resampler
{
float chan_data[CHANNELS][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.5f;
m1 = (d - b) * 0.5f;
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);
}
rarch_resampler_t *resampler_new(void)
{
RARCH_LOG("Hermite resampler [C]\n");
return (rarch_resampler_t*)calloc(1, sizeof(rarch_resampler_t));
}
void resampler_process(rarch_resampler_t *re, struct resampler_data *data)
{
double r_step = 1.0 / data->ratio;
size_t processed_out = 0;
size_t in_frames = data->input_frames;
const float *in_data = data->data_in;
float *out_data = data->data_out;
size_t i = 0;
while (i < in_frames)
{
while (re->r_frac >= 1.0 && i < in_frames)
{
re->r_frac -= 1.0;
for (unsigned c = 0; c < CHANNELS; c++)
{
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++;
}
i++;
}
while (re->r_frac <= 1.0)
{
re->r_frac += r_step;
for (unsigned c = 0; c < CHANNELS; c++)
{
float res = hermite_kernel((float)re->r_frac,
re->chan_data[c][0], re->chan_data[c][1],
re->chan_data[c][2], re->chan_data[c][3]);
*out_data++ = res;
}
processed_out++;
}
}
data->output_frames = processed_out;
}
void resampler_free(rarch_resampler_t *re)
{
free(re);
}