-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResampler.as
More file actions
165 lines (158 loc) · 4.28 KB
/
Copy pathResampler.as
File metadata and controls
165 lines (158 loc) · 4.28 KB
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
package
{
public class Resampler
{
public var ratioWeight:Number;
public var resampler:Function;
public var outputBuffer:Vector.<Number>;
public var lastOutput:Vector.<Number>;
public var fromSampleRate:Number;
public var toSampleRate:Number;
public var channels:int;
public var outputBufferSize:uint;
public var noReturn:Boolean;
public var tailExists:Boolean;
public var lastWeight:Number;
public function Resampler(fromSampleRate:Number, toSampleRate:Number, channels:int, outputBufferSize:uint, noReturn:Boolean):void
{
this.fromSampleRate = fromSampleRate;
this.toSampleRate = toSampleRate;
this.channels = channels | 0;
this.outputBufferSize = outputBufferSize;
this.noReturn = noReturn;
this.initialize();
}
public function initialize():void
{
if (this.fromSampleRate > 0 && this.toSampleRate > 0 && this.channels > 0) {
if (this.fromSampleRate == this.toSampleRate) {
this.resampler = this.bypassResampler;
this.ratioWeight = 1;
}
else {
this.resampler = this.interpolate;
this.ratioWeight = this.fromSampleRate / this.toSampleRate;
this.tailExists = false;
this.lastWeight = 0;
this.initializeBuffers();
}
}
else {
throw(new Error("Invalid settings specified for the resampler."));
}
}
public function interpolate(buffer:Vector.<Number>):*
{
var bufferLength:uint = Math.min(buffer.length, this.outputBufferSize);
if ((bufferLength % this.channels) == 0) {
if (bufferLength > 0) {
var weight:Number = 0;
var output:Vector.<Number> = new Vector.<Number>(this.channels);
for (var i:int = 0 ; i<output.length ; i++) {
output[i] = 0;
}
var actualPosition:int = 0;
var amountToNext:int = 0;
var alreadyProcessedTail:Boolean = !this.tailExists;
this.tailExists = false;
var outputOffset:int = 0;
var currentPosition:int = 0;
do {
if (alreadyProcessedTail) {
weight = ratioWeight;
for (i = 0 ; i<output.length ; i++) {
output[i] = 0;
}
}
else {
weight = this.lastWeight;
for (i = 0 ; i<output.length ; i++) {
output[i] = this.lastOutput[i];
}
alreadyProcessedTail = true;
}
while (weight > 0 && actualPosition < bufferLength) {
amountToNext = 1 + actualPosition - currentPosition;
if (weight >= amountToNext) {
for (i = 0 ; i<output.length ; i++) {
output[i] += buffer[actualPosition++] * amountToNext;
}
currentPosition = actualPosition;
weight -= amountToNext;
}
else {
for (i = 0 ; i<output.length ; i++) {
output[i] += buffer[actualPosition+i] * weight;
}
currentPosition += weight;
weight = 0;
break;
}
}
if (weight == 0) {
for (i = 0 ; i<output.length ; i++) {
this.outputBuffer[outputOffset++] = output[i] / ratioWeight;
}
}
else {
this.lastWeight = weight;
for (i = 0 ; i<output.length ; i++) {
this.lastOutput[i] = output[i];
}
this.tailExists = true;
break;
}
} while (actualPosition < bufferLength);
return this.bufferSlice(outputOffset);
}
else {
return (this.noReturn) ? 0 : new Vector.<Number>;
}
}
else {
throw(new Error("Buffer was of incorrect sample length."));
}
}
public function bypassResampler(buffer:Vector.<Number>):*
{
if (this.noReturn) {
this.outputBuffer = buffer;
return buffer.length;
}
else {
return buffer;
}
}
public function bufferSlice(sliceAmount:int):*
{
if (this.noReturn) {
return sliceAmount;
}
else {
try {
return this.outputBuffer.slice(0, sliceAmount);
}
catch (error:Error) {
try {
this.outputBuffer.length = sliceAmount;
return this.outputBuffer;
}
catch (error:Error) {
return this.outputBuffer.slice(0, sliceAmount);
}
}
}
}
public function initializeBuffers ():void
{
try {
this.outputBuffer = new Vector.<Number>(this.outputBufferSize);
this.lastOutput = new Vector.<Number>(this.channels);
}
catch (error:Error) {
this.outputBuffer = new Vector.<Number>();
this.lastOutput = new Vector.<Number>();
}
}
}
}