4 * Copyright (c) 2008 Lenny <leonardo.masoni@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
24 int *lanc_kernels = NULL;
26 float lanc(float x, float r) {
33 if (x <= -r || x >= r)
36 return r * sin(t) * sin(t / r) / (t * t);
39 float hann(float x, float d) {
44 return 0.5 * (1.0 - cos((M_PI * 2.0 * x) / (d - 1.0)));
47 int clamp(int a, int b, int c) {
58 void lopass(vc *vi, vc *vo, int l, int r) {
61 float *ck = (float *)malloc(d * sizeof(float));
66 for (i = 0; i < d; i ++)
67 cw += ck[i] = hann(i, d - 1);
69 for (i = 0; i < l; i ++) {
73 for (j = i - r; j <= i + r; j ++) {
75 int jc = clamp(j, 0, l - 1);
77 vc_mul_acc(&a, vi[jc], ck[j - i + r]);
80 vo[i] = vc_div(a, cw);
86 void hipass(vc *vi, vc *vo, int l, int r) {
92 for (i = 0; i < l; i ++)
93 vo[i] = vc_sub(vi[i], vo[i]);
96 void prepare_lanc_kernels() {
100 lanc_kernels = (int *)malloc(256 * 8 * sizeof(int));
102 for (i = 0; i < 256; i ++)
103 for (j = -3; j < 5; j ++)
104 lanc_kernels[i * 8 + j + 3] = lanc(j - i / 256.0, 4) * 1024.0;
107 int *select_lanc_kernel(float x) {
109 return lanc_kernels + (int)((x - floor(x)) * 256.0) * 8;
112 void free_lanc_kernels() {
117 vc interp(vc *vi, int l, float x) {
121 int *lk = select_lanc_kernel(x);
125 for (i = -3; i < 5; i ++) {
127 int ic = clamp(xd + i, 0, l - 1);
129 vc_mul_acc(&a, vi[ic], lk[i + 3]);
132 return vc_div(a, 1024.0);