-
Notifications
You must be signed in to change notification settings - Fork 3
/
buffered-ts.js
104 lines (83 loc) · 2.48 KB
/
buffered-ts.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
/*
* A helper class to use OLATS with the Web Audio API.
* Just pass an AudioBuffer with the "set_audio_buffer" method.
* Then, for example, at each cycle of ScriptProcessor.onaudioprocess,
* change the "alpha" and "position" fields to change the stretching
* factor and the audio buffer position pointer. After changing one
* or both parameters, call the "process" method.
*
*/
function BufferedTS(frameSize) {
var _frameSize = frameSize || 4096;
var _olaL = new OLATS(_frameSize);
var _olaR = new OLATS(_frameSize);
var _buffer;
var _position = 0;
var _newAlpha = 1;
var _midBufL = new CBuffer(Math.round(_frameSize * 1.2));
var _midBufR = new CBuffer(Math.round(_frameSize * 1.2));
var _zeros = new Float32Array(frameSize);
this.process = function(outputAudioBuffer) {
if (!_buffer)
return;
var sampleCounter = 0;
var il = _buffer.getChannelData(0);
var ir = _buffer.getChannelData(0);
var ol = outputAudioBuffer.getChannelData(0);
var or = outputAudioBuffer.getChannelData(1);
while (_midBufR.size > 0 && sampleCounter < outputAudioBuffer.length) {
var i = sampleCounter++;
ol[i] = _midBufL.shift();
or[i] = _midBufR.shift();
}
if (sampleCounter == outputAudioBuffer.length)
return;
do {
var frameL = il.subarray(_position, _position + _frameSize);
var frameR = ir.subarray(_position, _position + _frameSize);
if (_newAlpha != undefined && _newAlpha != _olaL.get_alpha()) {
_olaL.set_alpha(_newAlpha);
_olaR.set_alpha(_newAlpha);
_newAlpha = undefined;
}
if (frameL.length < _frameSize) {
_zeros.set(0, frameL);
_olaL.process(_zeros, _midBufL);
_zeros.set(0, frameR);
_olaR.process(_zeros, _midBufR);
_zeros.fill(0);
} else {
_olaL.process(frameL, _midBufL);
_olaR.process(frameR, _midBufR);
}
for (var i=sampleCounter; _midBufL.size > 0 && i < outputAudioBuffer.length; i++) {
ol[i] = _midBufL.shift();
or[i] = _midBufR.shift();
}
sampleCounter += _olaL.get_hs();
_position
+= _olaL.get_ha();
} while (sampleCounter < outputAudioBuffer.length);
}
this.set_audio_buffer = function(newBuffer) {
_buffer = newBuffer;
}
Object.defineProperties(this, {
'position' : {
get : function() {
return _position;
},
set : function(newPosition) {
_position = new Number(newPosition);
}
},
'alpha' : {
get : function() {
return _olaL.get_alpha();
},
set : function(newAlpha) {
_newAlpha = new Number(newAlpha);
}
}
});
}