Debian Codegraph @

src/engine/filter.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _USE_MATH_DEFINES
21 #include <math.h>
22 #include "filter.h"
23 #include "../ta-log.h"
24
25 float* DivFilterTables::cubicTable=NULL;
26 float* DivFilterTables::sincTable=NULL;
27 float* DivFilterTables::sincTable8=NULL;
28 float* DivFilterTables::sincIntegralTable=NULL;
29 float* DivFilterTables::sincIntegralSmallTable=NULL;
30
31 // portions from Schism Tracker (scripts/lutgen.c)
32 // licensed under same license as this program.
33 float* DivFilterTables::getCubicTable() {
34 if (cubicTable==NULL) {
35 logD("initializing cubic spline table.");
36 cubicTable=new float[4096];
37
38 for (int i=0; i<1024; i++) {
39 float x=(float)i/1024.0;
40 cubicTable[(i<<2)]=-0.5*pow(x,3)+1.0*pow(x,2)-0.5*x;
41 cubicTable[1+(i<<2)]=1.5*pow(x,3)-2.5*pow(x,2)+1.0;
42 cubicTable[2+(i<<2)]=-1.5*pow(x,3)+2.0*pow(x,2)+0.5*x;
43 cubicTable[3+(i<<2)]=0.5*pow(x,3)-0.5*pow(x,2);
44 }
45 }
46 return cubicTable;
47 }
48
49 float* DivFilterTables::getSincTable() {
50 if (sincTable==NULL) {
51 logD("initializing sinc table.");
52 sincTable=new float[65536];
53
54 sincTable[0]=1.0f;
55 for (int i=1; i<65536; i++) {
56 int mapped=((i&8191)<<3)|(i>>13);
57 double x=(double)i*M_PI/8192.0;
58 sincTable[mapped]=sin(x)/x;
59 }
60
61 for (int i=0; i<65536; i++) {
62 int mapped=((i&8191)<<3)|(i>>13);
63 sincTable[mapped]*=pow(cos(M_PI*(double)i/131072.0),2.0);
64 }
65 }
66 return sincTable;
67 }
68
69 float* DivFilterTables::getSincTable8() {
70 if (sincTable8==NULL) {
71 logD("initializing sinc table (8).");
72 sincTable8=new float[32768];
73
74 sincTable8[0]=1.0f;
75 for (int i=1; i<32768; i++) {
76 int mapped=((i&8191)<<2)|(i>>13);
77 double x=(double)i*M_PI/8192.0;
78 sincTable8[mapped]=sin(x)/x;
79 }
80
81 for (int i=0; i<32768; i++) {
82 int mapped=((i&8191)<<2)|(i>>13);
83 sincTable8[mapped]*=pow(cos(M_PI*(double)i/65536.0),2.0);
84 }
85 }
86 return sincTable8;
87 }
88
89 float* DivFilterTables::getSincIntegralTable() {
90 if (sincIntegralTable==NULL) {
91 logD("initializing sinc integral table.");
92 sincIntegralTable=new float[65536];
93
94 sincIntegralTable[0]=-0.5f;
95 for (int i=1; i<65536; i++) {
96 int mapped=((i&8191)<<3)|(i>>13);
97 int mappedPrev=(((i-1)&8191)<<3)|((i-1)>>13);
98 double x=(double)i*M_PI/8192.0;
99 double sinc=sin(x)/x;
100 sincIntegralTable[mapped]=sincIntegralTable[mappedPrev]+(sinc/8192.0);
101 }
102
103 for (int i=0; i<65536; i++) {
104 int mapped=((i&8191)<<3)|(i>>13);
105 sincIntegralTable[mapped]*=pow(cos(M_PI*(double)i/131072.0),2.0);
106 }
107 }
108 return sincIntegralTable;
109 }
110
111 float* DivFilterTables::getSincIntegralSmallTable() {
112 if (sincIntegralSmallTable==NULL) {
113 logD("initializing small sinc integral table.");
114 sincIntegralSmallTable=new float[512];
115
116 sincIntegralSmallTable[0]=-0.5f;
117 for (int i=1; i<512; i++) {
118 int mapped=((i&63)<<3)|(i>>6);
119 int mappedPrev=(((i-1)&63)<<3)|((i-1)>>6);
120 double x=(double)i*M_PI/64.0;
121 double sinc=sin(x)/x;
122 sincIntegralSmallTable[mapped]=sincIntegralSmallTable[mappedPrev]+(sinc/64.0);
123 }
124
125 for (int i=0; i<512; i++) {
126 int mapped=((i&63)<<3)|(i>>6);
127 sincIntegralSmallTable[mapped]*=pow(cos(M_PI*(double)i/1024.0),2.0);
128 }
129 }
130 return sincIntegralSmallTable;
131 }