-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPU.as
More file actions
207 lines (190 loc) · 6.19 KB
/
Copy pathAPU.as
File metadata and controls
207 lines (190 loc) · 6.19 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
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
package
{
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.SampleDataEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.sensors.Accelerometer;
public class APU
{
public var initialized:Boolean;
public var channels:int;
public var mono:Boolean;
public var sampleRate:Number;
public var minBufferSize:uint;
public var maxBufferSize:uint;
public var bufferSize:uint;
public var volume:Number;
public var samplesPerCallback:int;
public var outputConvert:Function;
public var audioContextSampleBuffer:Vector.<Number>;
public var resampled:Vector.<Number>;
public var resampleControl:Resampler;
public var resampleBufferStart:int;
public var resampleBufferEnd:int;
public var resampleBufferSize:uint;
public var channel:SoundChannel;
public var sound:Sound = null;
public function APU(channels:int, sampleRate:int, minBufferSize:int, maxBufferSize:int, volume:Number)
{
this.initialized = false;
this.samplesPerCallback = 2048;
this.outputConvert = null;
this.bufferSize = 0;
this.resampleBufferStart = 0;
this.resampleBufferEnd = 0;
this.resampleBufferSize = 2;
this.channels = (channels == 2) ? 2 : 1;
this.mono = (this.channels == 1);
this.sampleRate = (sampleRate > 0 && sampleRate <= 0xFFFFFF) ? sampleRate : 44100;
this.minBufferSize = (minBufferSize >= (samplesPerCallback << 1) && minBufferSize < maxBufferSize) ? (minBufferSize & ((mono) ? 0xFFFFFFFF : 0xFFFFFFFE)) : (samplesPerCallback << 1);
this.maxBufferSize = (Math.floor(maxBufferSize) > minBufferSize + this.channels) ? (maxBufferSize & ((mono) ? 0xFFFFFFFF : 0xFFFFFFFE)) : (minBufferSize << 1);
this.volume = (volume >= 0 && volume <= 1) ? volume : 1;
}
public function callbackBasedWriteAudioNoCallback(buffer:Vector.<Number>):void
{
var length:int = buffer.length;
for (var bufferCounter:int = 0; bufferCounter < length && bufferSize < maxBufferSize;) {
audioContextSampleBuffer[bufferSize++] = buffer[bufferCounter++];
}
}
public function writeAudioNoCallback(buffer:Vector.<Number>):void
{
if (this.checkInit()) {
this.callbackBasedWriteAudioNoCallback(buffer);
}
}
public function remainingBuffer():Number
{
if (this.checkInit()) {
return (((resampledSamplesLeft() * resampleControl.ratioWeight) >> (this.channels - 1)) << (this.channels - 1)) + bufferSize;
}
return 0;
}
public function changeVolume(value:Number):void
{
if (value >= 0 && value <= 1) {
volume = value;
this.checkInit();
}
}
public function checkInit():Boolean
{
if (this.initialized!=true) {
if (sound!=null) {
sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, soundCallback);
if (channel!=null) {
channel.stop();
}
}
sound = new Sound();
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, soundCallback);
channel = sound.play();
resetCallbackAPIAudioBuffer(44100, samplesPerCallback);
this.initialized = true;
}
return this.initialized;
}
public function generateStereo(event:SampleDataEvent):void
{
for (var index:int = 0; index < samplesPerCallback && resampleBufferStart != resampleBufferEnd; ++index) {
event.data.writeFloat(resampled[resampleBufferStart++]);
event.data.writeFloat(resampled[resampleBufferStart++]);
if (resampleBufferStart == resampleBufferSize) {
resampleBufferStart = 0;
}
}
while (++index < 2048) {
event.data.writeFloat(0);
event.data.writeFloat(0);
}
}
public function generateMono(event:SampleDataEvent):void
{
for (var index:int = 0; index < samplesPerCallback && resampleBufferStart != resampleBufferEnd; ++index) {
event.data.writeFloat(resampled[resampleBufferStart++]);
if (resampleBufferStart == resampleBufferSize) {
resampleBufferStart = 0;
}
}
while (++index < 2048) {
event.data.writeFloat(0);
event.data.writeFloat(0);
}
}
public function resampleRefill():void
{
if (bufferSize > 0) {
var resampleLength:int = resampleControl.resampler(getBufferSamples());
var resampledResult:Vector.<Number> = resampleControl.outputBuffer;
for (var index:int = 0; index < resampleLength; ++index) {
resampled[resampleBufferEnd++] = resampledResult[index];
if (resampleBufferEnd == resampleBufferSize) {
resampleBufferEnd = 0;
}
if (resampleBufferStart == resampleBufferEnd) {
++resampleBufferStart;
if (resampleBufferStart == resampleBufferSize) {
resampleBufferStart = 0;
}
}
}
bufferSize = 0;
}
}
public function resampledSamplesLeft():int
{
return ((resampleBufferStart <= resampleBufferEnd) ? 0 : resampleBufferSize) + resampleBufferEnd - resampleBufferStart;
}
public function getBufferSamples():Vector.<Number>
{
try {
return audioContextSampleBuffer.slice(0, bufferSize);
}
catch (error:Error) {
try {
audioContextSampleBuffer.length = bufferSize;
return audioContextSampleBuffer;
}
catch (error:Error) {
return audioContextSampleBuffer.slice(0, bufferSize);
}
}
return new Vector.<Number>();
}
public function resetCallbackAPIAudioBuffer(APISampleRate:int, bufferAlloc:int):void
{
audioContextSampleBuffer = new Vector.<Number>(maxBufferSize);
bufferSize = maxBufferSize;
resampleBufferStart = 0;
resampleBufferEnd = 0;
resampleBufferSize = Math.max(maxBufferSize * Math.ceil(sampleRate / APISampleRate), samplesPerCallback) << 1;
if (mono) {
resampled = new Vector.<Number>(resampleBufferSize);
resampleControl = new Resampler(sampleRate, APISampleRate, 1, resampleBufferSize, true);
outputConvert = generateMono;
}
else {
resampleBufferSize <<= 1;
resampled = new Vector.<Number>(resampleBufferSize);
resampleControl = new Resampler(sampleRate, APISampleRate, 2, resampleBufferSize, true);
outputConvert = generateStereo;
}
}
public function soundCallback(event:SampleDataEvent):void {
var index:int = 0;
resampleRefill();
if (outputConvert == null) {
while (++index <= 2048) {
event.data.writeFloat(0);
event.data.writeFloat(0);
}
return;
}
else {
outputConvert(event);
}
}
}
}