This repository has been archived by the owner on Nov 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
pitchshift.js
294 lines (222 loc) · 10.8 KB
/
pitchshift.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
function Pitchshift(fftFrameSize, sampleRate, algo) {
if( arguments.length ) { this.getready(fftFrameSize, sampleRate, algo); }
}
Pitchshift.prototype.getready = function (fftFrameSize, sampleRate, algo) {
this.fftFrameSize_ = fftFrameSize;
this.sampleRate_= sampleRate;
this.hannWindow_ = []
this.gRover_ = false;
this.algo = algo || "FFT";
// This has to go.
this.MAX_FRAME_LENGTH = 8192
function newFilledArray(length, val) {
var intLength = Math.floor(length);
var array = [];
for (var i = 0; i < intLength; i++) {
array[i] = val;
}
return array;
}
this.gInFIFO = newFilledArray(this.MAX_FRAME_LENGTH, 0);
this.gOutFIFO = newFilledArray(this.MAX_FRAME_LENGTH, 0);
this.gLastPhase = newFilledArray(this.MAX_FRAME_LENGTH / 2 + 1, 0);
this.gSumPhase = newFilledArray(this.MAX_FRAME_LENGTH / 2 + 1, 0);
this.gOutputAccum = newFilledArray(2 * this.MAX_FRAME_LENGTH, 0);
this.gAnaFreq = newFilledArray(this.MAX_FRAME_LENGTH, 0);
this.gAnaMagn = newFilledArray(this.MAX_FRAME_LENGTH, 0);
this.gSynFreq = newFilledArray(this.MAX_FRAME_LENGTH, 0);
this.gSynMagn = newFilledArray(this.MAX_FRAME_LENGTH, 0);
//this.gFFTworksp = newFilledArray(2 * this.MAX_FRAME_LENGTH, 0);
// Not two, 'cos we haven't to fill phases with 0's.
this.gFFTworksp = newFilledArray(this.fftFrameSize_,0);
// Real and imaginary parts of the resynthesized signal
this.real_ = [];
this.imag_ = [];
// Output data.
this.outdata = [];
this.hannWindow_ = [];
for (k = 0; k < fftFrameSize; k++) {
//Pre-generating Hann wavetable
this.hannWindow_[k]= WindowFunction.Hann(fftFrameSize, k);
}
// Init once, use always.
if (this.algo === "FFT") {
this.fft = new FFT(this.fftFrameSize_, this.sampleRate_);
}
else if (this.algo === "RFFT" ) {
this.fft = new RFFT(this.fftFrameSize_, this.sampleRate_);
}
else {
throw new Error("Invalid DFT algorithm selected " + this.algo);
}
//Probably we don't need this.
this.invFFT = new FFT(this.fftFrameSize_, this.sampleRate_);
console.log ("Pitchshift.prototype.getready returns back");
};
Pitchshift.prototype.process = function (pitchShift, numSampsToProcess, osamp, indata) {
function setArray(array, length, val) {
var intLength = Math.floor(length);
for (var i = 0; i < intLength; i++) {
array[i] = val;
}
}
/* pitchShift: factor value which is between 0.5 (one octave down) and 2. (one octave up). */
var fftFrameSize2 = this.fftFrameSize_/2,
stepSize = this.fftFrameSize_/osamp,
freqPerBin = this.sampleRate_ / this.fftFrameSize_,
expct = 2.* Math.PI * stepSize / this.fftFrameSize_,
inFifoLatency = this.fftFrameSize_ - stepSize,
j, k = 0, magn, phase, tmp, qpd, index, signal;
if (this.gRover_ === false) {
this.gRover_ = inFifoLatency;
}
/* main processing loop */
for (j = 0; j < numSampsToProcess; j++){
/* As long as we have not yet collected enough data just read in */
this.gInFIFO[this.gRover_] = indata[j];
this.outdata[j] = this.gOutFIFO[this.gRover_ - inFifoLatency];
this.gRover_++;
/* now we have enough data for processing */
if (this.gRover_ >= this.fftFrameSize_) {
this.gRover_ = inFifoLatency;
/* Do the windowing */
for (k = 0 ; k < this.fftFrameSize_ ; k++) {
//Need the signal for the FFT.
this.gFFTworksp[k] = this.gInFIFO[k] * this.hannWindow_[k];
//this.gFFTworksp[k][1] = 0.;
}
this.fft.forward(this.gFFTworksp);
/* this is the analysis step */
for (k = 0; k <= fftFrameSize2; k++) {
//These ifs make the pitchshifter code dependent on the DFT implementation; we should decorate DFTs instead.
if (this.algo === "FFT") {
//Taking some "private" member out of fft here.
magn = 2 * Math.sqrt (this.fft.real[k] * this.fft.real[k] + this.fft.imag[k] * this.fft.imag[k]);
//aka magn = spectrum[k];
phase = Math.atan2 (this.fft.imag[k], this.fft.real[k]);
}
else if (this.algo === "RFFT") {
//Because having the same interface but a different output schema
//in the same library is a great fucking idea!
// Ordering of output:
//
// trans[0] = re[0] (==zero frequency, purely real)
// trans[1] = re[1]
// ...
// trans[n/2-1] = re[n/2-1]
// trans[n/2] = re[n/2] (==nyquist frequency, purely real)
//
// trans[n/2+1] = im[n/2-1]
// trans[n/2+2] = im[n/2-2]
// ...
// trans[n-1] = im[1]
var imaginary, real;
real = this.fft.trans[k];
if (k == 0) {
imaginary = 0;
}
else {
imaginary = this.fft.trans[this.fftFrameSize_ - k];
}
magn = 2 * Math.sqrt (real * real + imaginary * imaginary);
phase = Math.atan2 (imaginary, real);
}
else {
//If we used the constructor, we can't be here.
throw new Error("Invalid DFT algorithm selected " + this.algo);
}
/* compute phase difference */
tmp = phase - this.gLastPhase[k];
this.gLastPhase[k] = phase;
/* subtract expected phase difference */
tmp -= k * expct;
/* map delta phase into +/- Pi interval */
/* Floor and ceil should emulate the behaviour
* of a C float -> long int conversion
* "Truncating conversion means that any
* fractional part is discarded, so that e.g.
* 3.9 is converted to 3".
* (http://www.cs.tut.fi/~jkorpela/round.html)*/
qpd = tmp / Math.PI;
if (qpd >= 0) {
qpd = Math.floor(qpd);
/* This probably won't work like in C */
qpd += qpd & 1;
}
else {
qpd = Math.ceil(qpd);
qpd -= qpd & 1;
}
tmp -= Math.PI * qpd;
/* get deviation from bin frequency from the +/- Pi interval */
tmp = osamp * tmp /(2 * Math.PI);
/* compute the k-th partials' true frequency */
tmp = k * freqPerBin + tmp * freqPerBin;
/* store magnitude and true frequency in analysis arrays */
this.gAnaMagn[k] = magn;
this.gAnaFreq[k] = tmp;
}
/* ***************** PROCESSING ******************* */
/* this does the actual pitch shifting */
//memset(gSynMagn, 0, fftFrameSize*sizeof(float));
//memset(gSynFreq, 0, fftFrameSize*sizeof(float));
setArray(this.gSynMagn, this.fftFrameSize_, 0);
setArray(this.gSynFreq, this.fftFrameSize_, 0);
for (k = 0; k <= fftFrameSize2; k++) {
//This is an int multiplication in C.
index = Math.floor(k * pitchShift);
if (index <= fftFrameSize2) {
this.gSynMagn[index] += this.gAnaMagn[k];
this.gSynFreq[index] = this.gAnaFreq[k] * pitchShift;
}
}
/* ***************** SYNTHESIS ******************* */
/* this is the synthesis step */
for (k = 0; k <= fftFrameSize2; k++) {
/* get magnitude and true frequency from synthesis arrays */
magn = this.gSynMagn[k];
tmp = this.gSynFreq[k];
/* subtract bin mid frequency */
tmp -= k * freqPerBin;
/* get bin deviation from freq deviation */
tmp /= freqPerBin;
/* take osamp into account */
tmp = 2.* Math.PI * tmp / osamp;
/* add the overlap phase advance back in */
tmp += k * expct;
/* accumulate delta phase to get bin phase */
this.gSumPhase[k] += tmp;
phase = this.gSumPhase[k];
// Get real and imag part
this.real_[k] = magn* Math.cos(phase);
this.imag_[k] = magn* Math.sin(phase);
}
// zero negative frequencies
for (k = ((fftFrameSize2)+1); (k < this.fftFrameSize_); k++) {
//That's ok, otherwise inverse fft has a fit.
this.real_[k] = 0;
this.imag_[k] = 0;
}
// Do the Inverse transform
signal = this.invFFT.inverse(this.real_, this.imag_);
// Do inverse windowing and add to output accumulator
for(k=0; k < this.fftFrameSize_; k++) {
this.gOutputAccum[k] += this.hannWindow_[k] * signal[k];
}
for (k = 0; k < stepSize; k++) {
this.gOutFIFO[k] = this.gOutputAccum[k];
}
// Shift the output accumulator.
// Rough memmove implementation.
var tempArray = this.gOutputAccum.slice (stepSize, stepSize + this.fftFrameSize_);
for (k = 0; k < this.fftFrameSize_; k++) {
this.gOutputAccum[k] = tempArray[k];
}
// Shift the input FIFO
// These memory shifts have to be optimized.
for (k = 0; k < inFifoLatency; k++) {
this.gInFIFO[k] = this.gInFIFO[k + stepSize];
}
}
}
}