-
Notifications
You must be signed in to change notification settings - Fork 26
/
Player.hx
305 lines (285 loc) · 11.4 KB
/
Player.hx
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
295
296
297
298
299
300
301
302
303
304
305
//
// WAV/AU Flash player with resampler
//
// Copyright (c) 2009, Anton Fedorov <[email protected]>
//
/* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*/
import flash.media.SoundTransform;
// Main player class: loads stream, process it by appropriate file decoder,
// that will initialize correct sound decoder. Decoded audio samples
// resample to 44100 and play via AudioSink
class Player extends flash.events.EventDispatcher, implements IPlayer {
var File : flash.net.URLStream;
var Sound : fmt.File;
var Resampler : com.sun.media.sound.SoftAbstractResampler;
var pitch : Array<Float>;
var asink : org.xiph.system.AudioSink;
var buffer : Array<Array<Float>>;
var padding : Array<Float>;
var in_off : Array<Float>;
var fname : String;
var first : Bool;
var timer : flash.utils.Timer;
var trigger : Null<Float>;
var pos : Null<Float>;
var schtr: SoundTransform;
public var volume(get_volume, set_volume): Float;
public var pan(get_pan, set_pan): Float;
public var soundTransform(get_soundTransform, set_soundTransform): SoundTransform;
public function new(?path : String) {
super();
schtr = new SoundTransform();
fname = path;
asink = null;
File = null;
}
public function play(?path : String, ?trigger_buffer : Float) {
if (trigger_buffer != null) trigger = trigger_buffer;
if (path != null) fname = path;
if (fname == null) throw "No sound URL given";
// To-do: re-play already loaded stream
pitch = new Array<Float>();
trace("Player for "+fname);
var slnrx = ~/[.](sln(\d{1,3}))$/i;
if ((~/[.]au$/i).match(fname)) {
Sound = new fmt.FileAu();
} else
if ((~/[.]wav(49)?$/i).match(fname)) {
Sound = new fmt.FileWav();
} else
if ((~/[.](sln|raw)$/i).match(fname)) {
Sound = new fmt.FileSln();
} else
if (slnrx.match(fname)) {
Sound = new fmt.FileSln(Std.parseInt(slnrx.matched(2)) * 1000);
} else
if ((~/[.](alaw|al)$/i).match(fname)) {
Sound = new fmt.FileAlaw();
} else
if ((~/[.](ulaw|ul|pcm|mu)$/i).match(fname)) {
Sound = new fmt.FileUlaw();
} else
if ((~/[.]la$/i).match(fname)) {
Sound = new fmt.FileAlawInv();
} else
if ((~/[.]lu$/i).match(fname)) {
Sound = new fmt.FileUlawInv();
} else
if ((~/[.]gsm$/i).match(fname)) {
Sound = new fmt.FileGsm();
} else {
trace("Unsupported file type");
throw "Unsupported file type";
}
Resampler = new com.sun.media.sound.SoftLanczosResampler();
initAsink();
try {
File = new flash.net.URLStream();
var Req = new flash.net.URLRequest(fname);
File.addEventListener(flash.events.Event.COMPLETE, completeHandler);
File.addEventListener(flash.events.ProgressEvent.PROGRESS, progressHandler);
File.addEventListener(flash.events.IOErrorEvent.IO_ERROR, errorHandler);
trace("Load begin!");
first = true;
File.load(Req);
dispatchEvent(new PlayerEvent(PlayerEvent.BUFFERING, 0));
timer = new flash.utils.Timer(100);
timer.addEventListener( flash.events.TimerEvent.TIMER, timeout );
timer.start();
}
catch (error : Dynamic) {
trace("Unable to load: "+error);
throw error;
}
}
function initAsink() {
try {
asink = new org.xiph.system.AudioSink(8192, true, 44100*5, trigger==null?null:Math.round(trigger*44100), schtr);
asink.addEventListener(PlayerEvent.PLAYING, playingEvent);
asink.addEventListener(PlayerEvent.STOPPED, stoppedEvent);
} catch (error : Dynamic) {
trace("Unable to load: "+error);
//trace(haxe.Stack.exceptionStack());
throw error;
}
}
public function set_volume(volume: Float): Float {
this.schtr.volume=volume;
trace("set_volume("+volume+")");
this.soundTransform = this.soundTransform; // Apply changes
return volume;
}
public function get_volume(): Float {
return this.schtr.volume;
}
public function set_pan(pan: Float): Float {
this.schtr.pan=pan;
this.soundTransform = this.soundTransform; // Apply changes
return this.schtr.pan;
}
public function get_pan(): Float {
return this.schtr.pan;
}
public function set_soundTransform(st: SoundTransform): SoundTransform {
this.schtr = st;
if (this.asink!=null) {
this.asink.soundTransform = this.schtr;
}
return this.schtr;
}
public function get_soundTransform(): SoundTransform {
return this.schtr;
}
function playingEvent(event:PlayerEvent) {
dispatchEvent(new PlayerEvent(PlayerEvent.PLAYING, event.position));
}
function stoppedEvent(event:PlayerEvent) {
dispatchEvent(new PlayerEvent(PlayerEvent.STOPPED, event.position));
}
public function pause() {
if (asink != null) {
pos = asink.pause();
trace("Paused pos = "+pos);
dispatchEvent(new PlayerEvent(PlayerEvent.PAUSED, pos));
}
else stop();
}
public function resume() {
trace("Try to resume from"+pos);
if (pos!=null) {
asink.play(pos);
}
else play();
}
public function seek(pos: Float) {
if (asink != null && Sound != null && Sound.ready()==1) {
asink.pause();
if (!asink.play(pos)) { // If we seek outside prepared buffer, need to re-setup resampler buffer
asink = null;
initAsink();
initResampler();
asink.pos += Sound.seek(pos);
dispatchEvent(new PlayerEvent(PlayerEvent.BUFFERING, asink.pos));
// timeout handler will populate from new pos when ready
}
}
}
public function stop() {
if (asink != null) {
var pos = asink.stop();
trace("Stopped position = "+pos);
asink = null;
}
if (File != null) {
File.close();
File = null;
dispatchEvent(new PlayerEvent(PlayerEvent.STOPPED, 0.0));
}
if (timer != null) {
timer = null;
}
}
function completeHandler(event:flash.events.Event) {
trace("completeHandler: " + event);
timeout(null);
dispatchEvent(event);
}
function progressHandler(event:flash.events.ProgressEvent) {
trace("progressHandler: " + event);
if (first) {
first = false;
if (event.bytesTotal>0)
Sound.setSize(Std.int(event.bytesTotal));
}
dispatchEvent(event); // here we fire byte progress
}
function errorHandler(event:flash.events.IOErrorEvent) {
trace("ERROR ERROR");
dispatchEvent(event);
}
function timeout(event:Null<flash.events.Event>) {
if (asink.available < 44100*5) {
read(event == null);
Sound.populate( Math.ceil(Math.min(Sound.getRate(), Sound.getRate()*((44100*5-asink.available)/44100.0) )) );
populate();
}
}
function read(last: Bool) {
if (File.bytesAvailable > 0) {
Sound.push( File, last );
dispatchEvent( new PlayerLoadEvent(PlayerLoadEvent.LOAD, false, false, Sound.getLoadedLength(), Sound.getEtaLength()) );
//trace("Sound ready = "+Sound.ready()+"; rate="+Sound.getRate()+"; channels="+Sound.getChannels()+"; samples="+Sound.samplesAvailable());
}
}
function initResampler() {
if (Sound.getRate() != 44100) {
pitch[0] = Sound.getRate() / 44100.0;
trace("Resample with "+pitch[0]+" pitch");
buffer = new Array<Array<Float>>();
if (padding == null) {
padding = new Array<Float>();
for( k in 0...Resampler.getPadding() )
padding.push( 0.0 ); // Fill startup padding
}
for( c in 0...Sound.getChannels() ) {
// Fill with double padding
buffer.push( padding.copy() );
buffer[c] = buffer[c].concat( padding );
}
in_off = new Array<Float>();
in_off[0] = Resampler.getPadding();
asink.pos -= (padding.length / Sound.getRate());
}
}
function populate() {
if (Sound.samplesAvailable()>0) {
if (Sound.getRate() == 44100) {
var Samples = Sound.getSamples();
var ind = new Array<Int>(); ind[0] = 0;
var cnt = Samples[0].length;
asink.write(Samples, ind, cnt, Sound.last);
} else {
var Samples = Sound.getSamples();
if (pitch.length != 1)
initResampler();
var Res = new Array<Array<Float>>();
var out_off = new Array<Int>();
var inOff = in_off[0];
// Conversion needs padding samples before and padding samples after
// So, for last pack we need to add one more padding zone
for( c in 0...Sound.getChannels() ) {
buffer[c] = buffer[c].concat( Samples[c] );
if (Sound.last) {
buffer[c] = buffer[c].concat( padding );
buffer[c] = buffer[c].concat( padding );
}
Res.push( new Array<Float>() );
in_off[0] = inOff;
out_off[0] = 0;
// Note: number of last element, not count!
// Always hold 1 padding left and 1 padding right
var in_end: Float = buffer[c].length-padding.length;
var out_end: Int = (Std.int( buffer[0].length / pitch[0] + 1 )+Resampler.getPadding())*5;
Resampler.interpolate(buffer[c], in_off, in_end, pitch, 0, Res[c], out_off, out_end);
}
// Write resampled sound
var ind = new Array<Int>(); ind[0] = 0;
asink.write(Res, ind, out_off[0], Sound.last);
// Shift buffers
for( c in 0...Sound.getChannels() ) {
buffer[c].splice(0, Std.int( in_off[0] )-2*padding.length );
}
in_off[0] -= Std.int( in_off[0]-2*padding.length );
}
}
}
}