-
Notifications
You must be signed in to change notification settings - Fork 3
/
cheby.js
126 lines (112 loc) · 4 KB
/
cheby.js
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
// from http://stackoverflow.com/questions/22312841/waveshaper-node-in-webaudio-how-to-emulate-distortion
// via https://developer.mozilla.org/en-US/docs/Web/API/WaveShaperNode
// function makeDistortionCurve( amount ) {
// var k = typeof amount === 'number' ? amount : 50,
// n_samples = 44100,
// curve = new Float32Array(n_samples),
// deg = Math.PI / 180,
// i = 0,
// x;
// for ( ; i < n_samples; ++i ) {
// x = i * 2 / n_samples - 1;
// curve[i] = ( 3 + k ) * x * 20 * deg / ( Math.PI + k * Math.abs(x) );
// }
// return curve;
// };
const N_SAMPLES = 44100;
const DEGREE = 16
function cheby(k, x) {
if (k === 0) return 1.0;
if (k === 1) return x;
return 2.0 * x * cheby( (k-1), x) - cheby((k-2), x);
}
// TODO - work out caching here, we're computing the same values over and over
// again. if we start at (say) K=5 then we'll have to compute 4,3,2,1,0 so if we
// are clever we only ever compute each K once. right now we're doing more than
// we need to. s
const curves = []
// const curves = new Float32Array(require("json!./wavetables.json"))
function buildChebyCurves(k) {
for (let i = 1; i <=k ; i++) {
console.log(`building curve for degree=${i}`);
curves[i] = new Float32Array(N_SAMPLES);
for (let j=0; j<N_SAMPLES; ++j) {
let x = j * 2 / N_SAMPLES - 1;
curves[i][j] = cheby(i, x);
}
}
}
buildChebyCurves(DEGREE);
console.log(curves);
function writeChebyCurves() {
fs.appendFileSync('wavetables.json', JSON.stringify(curves), 'utf8');
}
module.exports = {
buildWaveshaper: function(opts={}) {
let context = opts.context;
let waveshaper = context.createWaveShaper();
waveshaper.curve = curves[opts.degree];
waveshaper.oversample = '4x';
return waveshaper;
},
DEGREE: DEGREE
}
// function writeChebyCurves(k) {
// fs.appendFileSync('wavetables.json', JSON.stringify(buildChebyCurves(k)), 'utf8');
// }
// TODO reimplement with http://stackoverflow.com/questions/20306750/what-is-a-compact-way-to-save-a-float32array-to-disk-on-node-js
// TODO - don't do this, it's slow as shit
// for posterity, the old way i was doing cheby stuff:
//
// var chebyFns = []
// for (let i=0; i<16; i++) {
// if (i == 0) {
// chebyFns[i] = function(x, cache) {
// cache[i] = 1.0;
// return 1.0;
// };
// } else if (i == 1) {
// chebyFns[i] = function(x, cache) {
// cache[i] = x;
// return x;
// };
// } else {
// chebyFns[i] = function(x, cache) {
// if (typeof(cache[i]) === "undefined") {
// cache[i] = 2.0 * x * chebyFns[i-1](x, cache) - chebyFns[i-2](x, cache);
// }
// return cache[i];
// };
// }
// }
//
// //console.log(chebyFns);
// const cheby = function(k, x, cache) {
// // if (k === 0) return 1.0;
// // if (k === 1) return x;
// // return 2.0 * x * cheby( (k-1), x) - cheby((k-2), x);
// return chebyFns[k](x, cache);
// }
//const bufferSize = 128;
//
// this.processor = this.context.createScriptProcessor(bufferSize, 1, 1)
// this.processor.onaudioprocess = function(e) {
// // The input buffer is the song we loaded earlier
// let inputBuffer = e.inputBuffer;
//
// // The output buffer contains the samples that will be modified and played
// let outputBuffer = e.outputBuffer;
//
// for (var channel = 0; channel < outputBuffer.numberOfChannels; channel++) {
// var inputData = inputBuffer.getChannelData(channel);
// var outputData = outputBuffer.getChannelData(channel);
//
// // Loop through the 4096 samples
// for (var sample = 0; sample < inputBuffer.length; sample++) {
// let scaled = inputData[sample] * that.amplitude;
// let cache = [];
// outputData[sample] = inputData[sample] + cheby(13, scaled, cache)+ cheby(11, scaled, cache)+ cheby(9, scaled, cache) + cheby(7, scaled, cache) + cheby(5, scaled, cache) + cheby(3, scaled, cache) + cheby(1, scaled, cache) //+ cheby(9, scaled) + cheby(11, scaled) + cheby(13, scaled) + cheby(15, scaled); //+ (1 - that.amplitude);
// }
// }
// };
//