/
wavetable.cpp
152 lines (138 loc) · 4.39 KB
/
wavetable.cpp
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "wavetable.h"
#include <stdio.h>
#include "soundprocessor.h"
#include <cmath>
#define EPSILON 0.001
using namespace std;
WaveTable::WaveTable() {
buildSawtooth();
buildSquare();
buildTriangle();
}
WaveTable::~WaveTable() {
int i;
for(i = 0; i < NUM_NOTES; i++) {
delete[] triangleTable[i];
delete[] sawtoothTable[i];
delete[] squareTable[i];
}
}
double WaveTable::triangle(double phase, double freq) {
if(freq < ALIASING_THRESHOLD) {
double t = phase*freq/SAMPLE_RATE;
return pow(-1, (int)floor(t+0.5))*((t*2)-1);
}
return getSample(phase, freq, triangleTable);
}
double WaveTable::square(double phase, double freq) {
if(freq < ALIASING_THRESHOLD) {
return (phase*freq/SAMPLE_RATE) < 0.5 ? 0 : 1;
}
return getSample(phase, freq, squareTable);
}
double WaveTable::sawtooth(double phase, double freq) {
if(freq < ALIASING_THRESHOLD) {
double t = phase*freq/SAMPLE_RATE;
return (t - floor(t+0.5))*2;
}
else
return getSample(phase, freq, sawtoothTable);
}
double WaveTable::getSample(double phase, double freq, double **table) {
double closestFreq, ratio, sample, cursor, period, amountRead, amountLeft;
int closest, index;
sample = 0;
freq = roundf(freq * 100.0f) / 100.0f;
closest = (int)floor(12*(log(freq)/440)/log(2))+MIDDLE_A_INDEX;
closest = closest >= NUM_NOTES ? NUM_NOTES - 1 : (closest < 0 ? 0 : closest);
closestFreq = 440 * pow(2,(double(closest-MIDDLE_A_INDEX))/12);
period = SAMPLE_RATE/closestFreq;
if (fabs((closestFreq - freq)/closestFreq) < EPSILON) {
ratio = 1;
}
else
ratio = freq/closestFreq;
amountRead = 0;
cursor = ratio*phase;
while(fabs((amountRead-ratio))/ratio > EPSILON && amountRead < ratio) {
cursor = fmod(cursor, period);
amountLeft = min(min(1.0, ratio-amountRead), min(ratio-amountRead, min(period-cursor, (fabs((fmod(cursor, 1)) < EPSILON)?cursor+1:ceil(cursor))-cursor)));
index = (int)floor(cursor);
amountRead += amountLeft;
cursor += amountLeft;
sample += amountLeft*table[closest][index];
}
return sample/ratio;
}
double WaveTable::sine(double phase, double freq) {
return sin(((phase*freq)/SAMPLE_RATE)*2*PI);
}
void WaveTable::buildTriangle() {
int i, j;
double freq, period;
for(i = 0; i < NUM_NOTES; i++) {
freq = 440 * pow(2,(double(i-MIDDLE_A_INDEX))/12); // sawtoothTable[MIDDLE_A_INDEX] = A440
period = SAMPLE_RATE/freq;
triangleTable[i] = new double[(int)ceil(period)];
for(j = 0; j < ceil(period); j++) {
triangleTable[i][j] = triangleFunction(j, freq);
}
}
}
void WaveTable::buildSquare() {
int i, j;
double freq, period;
for(i = 0; i < NUM_NOTES; i++) {
freq = 440 * pow(2,(double(i-MIDDLE_A_INDEX))/12); // sawtoothTable[MIDDLE_A_INDEX] = A440
period = SAMPLE_RATE/freq;
squareTable[i] = new double[(int)ceil(period)];
for(j = 0; j < ceil(period); j++) {
squareTable[i][j] = squareFunction(j, freq);
}
}
}
void WaveTable::buildSawtooth() {
int i, j;
double freq, period;
for(i = 0; i < NUM_NOTES; i++) {
freq = 440 * pow(2,(double(i-MIDDLE_A_INDEX))/12); // sawtoothTable[MIDDLE_A_INDEX] = A440
period = SAMPLE_RATE/freq;
sawtoothTable[i] = new double[(int)ceil(period)];
for(j = 0; j < (int)ceil(period); j++) {
sawtoothTable[i][j] = sawtoothFunction(j, freq);
}
}
}
double WaveTable::sawtoothFunction(double phase, double f) {
double t, sum, sample;
int i;
t = phase/SAMPLE_RATE;
sum = 0;
for(i = 1; i < SAMPLE_RATE/(2*f); i++) {
sum += pow(-1, i+1)*sin(2*PI*i*f*t)/i;
}
sample = (2*sum)/(PI);
return sample;
}
double WaveTable::squareFunction(double phase, double f) {
double t, sum, sample;
int i;
t = phase/SAMPLE_RATE;
sum = 0;
for(i = 1; i < (SAMPLE_RATE/4)/f; i++) {
sum += sin(2*PI*(2*i-1)*f*t)/(2*i-1);
}
sample = (4*sum)/PI;
return sample;
}
double WaveTable::triangleFunction(double phase, double f) {
double t, sum, sample;
int i;
t = phase/SAMPLE_RATE;
sum = 0;
for(i = 0; i < (SAMPLE_RATE/4)/f; i++) {
sum += pow(-1, i)*sin((2*i+1)*2*PI*f*t)/pow(2*i+1, 2);
}
sample = (8*sum)/(pow(PI, 2));
return sample;
}