Player.hx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // WAV/AU Flash player with resampler
  3. //
  4. // Copyright (c) 2009, Anton Fedorov <datacompboy@call2ru.com>
  5. //
  6. /* This code is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 only, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This code is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * version 2 for more details (a copy is included in the LICENSE file that
  14. * accompanied this code).
  15. */
  16. import flash.media.SoundTransform;
  17. // Main player class: loads stream, process it by appropriate file decoder,
  18. // that will initialize correct sound decoder. Decoded audio samples
  19. // resample to 44100 and play via AudioSink
  20. class Player extends flash.events.EventDispatcher, implements IPlayer {
  21. var File : flash.net.URLStream;
  22. var Sound : fmt.File;
  23. var Resampler : com.sun.media.sound.SoftAbstractResampler;
  24. var pitch : Array<Float>;
  25. var asink : org.xiph.system.AudioSink;
  26. var buffer : Array<Array<Float>>;
  27. var padding : Array<Float>;
  28. var in_off : Array<Float>;
  29. var fname : String;
  30. var first : Bool;
  31. var timer : flash.utils.Timer;
  32. var trigger : Null<Float>;
  33. var pos : Null<Float>;
  34. var schtr: SoundTransform;
  35. public var volume(get_volume, set_volume): Float;
  36. public var pan(get_pan, set_pan): Float;
  37. public var soundTransform(get_soundTransform, set_soundTransform): SoundTransform;
  38. public function new(?path : String) {
  39. super();
  40. schtr = new SoundTransform();
  41. fname = path;
  42. asink = null;
  43. File = null;
  44. }
  45. public function play(?path : String, ?trigger_buffer : Float) {
  46. if (trigger_buffer != null) trigger = trigger_buffer;
  47. if (path != null) fname = path;
  48. if (fname == null) throw "No sound URL given";
  49. // To-do: re-play already loaded stream
  50. pitch = new Array<Float>();
  51. trace("Player for "+fname);
  52. var slnrx = ~/[.](sln(\d{1,3}))$/i;
  53. if ((~/[.]au$/i).match(fname)) {
  54. Sound = new fmt.FileAu();
  55. } else
  56. if ((~/[.]wav(49)?$/i).match(fname)) {
  57. Sound = new fmt.FileWav();
  58. } else
  59. if ((~/[.](sln|raw)$/i).match(fname)) {
  60. Sound = new fmt.FileSln();
  61. } else
  62. if (slnrx.match(fname)) {
  63. Sound = new fmt.FileSln(Std.parseInt(slnrx.matched(2)) * 1000);
  64. } else
  65. if ((~/[.](alaw|al)$/i).match(fname)) {
  66. Sound = new fmt.FileAlaw();
  67. } else
  68. if ((~/[.](ulaw|ul|pcm|mu)$/i).match(fname)) {
  69. Sound = new fmt.FileUlaw();
  70. } else
  71. if ((~/[.]la$/i).match(fname)) {
  72. Sound = new fmt.FileAlawInv();
  73. } else
  74. if ((~/[.]lu$/i).match(fname)) {
  75. Sound = new fmt.FileUlawInv();
  76. } else
  77. if ((~/[.]gsm$/i).match(fname)) {
  78. Sound = new fmt.FileGsm();
  79. } else {
  80. trace("Unsupported file type");
  81. throw "Unsupported file type";
  82. }
  83. Resampler = new com.sun.media.sound.SoftLanczosResampler();
  84. initAsink();
  85. try {
  86. File = new flash.net.URLStream();
  87. var Req = new flash.net.URLRequest(fname);
  88. File.addEventListener(flash.events.Event.COMPLETE, completeHandler);
  89. File.addEventListener(flash.events.ProgressEvent.PROGRESS, progressHandler);
  90. File.addEventListener(flash.events.IOErrorEvent.IO_ERROR, errorHandler);
  91. trace("Load begin!");
  92. first = true;
  93. File.load(Req);
  94. dispatchEvent(new PlayerEvent(PlayerEvent.BUFFERING, 0));
  95. timer = new flash.utils.Timer(100);
  96. timer.addEventListener( flash.events.TimerEvent.TIMER, timeout );
  97. timer.start();
  98. }
  99. catch (error : Dynamic) {
  100. trace("Unable to load: "+error);
  101. throw error;
  102. }
  103. }
  104. function initAsink() {
  105. try {
  106. asink = new org.xiph.system.AudioSink(8192, true, 44100*5, trigger==null?null:Math.round(trigger*44100), schtr);
  107. asink.addEventListener(PlayerEvent.PLAYING, playingEvent);
  108. asink.addEventListener(PlayerEvent.STOPPED, stoppedEvent);
  109. } catch (error : Dynamic) {
  110. trace("Unable to load: "+error);
  111. //trace(haxe.Stack.exceptionStack());
  112. throw error;
  113. }
  114. }
  115. public function set_volume(volume: Float): Float {
  116. this.schtr.volume=volume;
  117. trace("set_volume("+volume+")");
  118. this.soundTransform = this.soundTransform; // Apply changes
  119. return volume;
  120. }
  121. public function get_volume(): Float {
  122. return this.schtr.volume;
  123. }
  124. public function set_pan(pan: Float): Float {
  125. this.schtr.pan=pan;
  126. this.soundTransform = this.soundTransform; // Apply changes
  127. return this.schtr.pan;
  128. }
  129. public function get_pan(): Float {
  130. return this.schtr.pan;
  131. }
  132. public function set_soundTransform(st: SoundTransform): SoundTransform {
  133. this.schtr = st;
  134. if (this.asink!=null) {
  135. this.asink.soundTransform = this.schtr;
  136. }
  137. return this.schtr;
  138. }
  139. public function get_soundTransform(): SoundTransform {
  140. return this.schtr;
  141. }
  142. function playingEvent(event:PlayerEvent) {
  143. dispatchEvent(new PlayerEvent(PlayerEvent.PLAYING, event.position));
  144. }
  145. function stoppedEvent(event:PlayerEvent) {
  146. dispatchEvent(new PlayerEvent(PlayerEvent.STOPPED, event.position));
  147. }
  148. public function pause() {
  149. if (asink != null) {
  150. pos = asink.pause();
  151. trace("Paused pos = "+pos);
  152. dispatchEvent(new PlayerEvent(PlayerEvent.PAUSED, pos));
  153. }
  154. else stop();
  155. }
  156. public function resume() {
  157. trace("Try to resume from"+pos);
  158. if (pos!=null) {
  159. asink.play(pos);
  160. }
  161. else play();
  162. }
  163. public function seek(pos: Float) {
  164. if (asink != null && Sound != null && Sound.ready()==1) {
  165. asink.pause();
  166. if (!asink.play(pos)) { // If we seek outside prepared buffer, need to re-setup resampler buffer
  167. asink = null;
  168. initAsink();
  169. initResampler();
  170. asink.pos += Sound.seek(pos);
  171. dispatchEvent(new PlayerEvent(PlayerEvent.BUFFERING, asink.pos));
  172. // timeout handler will populate from new pos when ready
  173. }
  174. }
  175. }
  176. public function stop() {
  177. if (asink != null) {
  178. var pos = asink.stop();
  179. trace("Stopped position = "+pos);
  180. asink = null;
  181. }
  182. if (File != null) {
  183. File.close();
  184. File = null;
  185. dispatchEvent(new PlayerEvent(PlayerEvent.STOPPED, 0.0));
  186. }
  187. if (timer != null) {
  188. timer = null;
  189. }
  190. }
  191. function completeHandler(event:flash.events.Event) {
  192. trace("completeHandler: " + event);
  193. timeout(null);
  194. dispatchEvent(event);
  195. }
  196. function progressHandler(event:flash.events.ProgressEvent) {
  197. trace("progressHandler: " + event);
  198. if (first) {
  199. first = false;
  200. if (event.bytesTotal>0)
  201. Sound.setSize(Std.int(event.bytesTotal));
  202. }
  203. dispatchEvent(event); // here we fire byte progress
  204. }
  205. function errorHandler(event:flash.events.IOErrorEvent) {
  206. trace("ERROR ERROR");
  207. dispatchEvent(event);
  208. }
  209. function timeout(event:Null<flash.events.Event>) {
  210. if (asink.available < 44100*5) {
  211. read(event == null);
  212. Sound.populate( Math.ceil(Math.min(Sound.getRate(), Sound.getRate()*((44100*5-asink.available)/44100.0) )) );
  213. populate();
  214. }
  215. }
  216. function read(last: Bool) {
  217. if (File.bytesAvailable > 0) {
  218. Sound.push( File, last );
  219. dispatchEvent( new PlayerLoadEvent(PlayerLoadEvent.LOAD, false, false, Sound.getLoadedLength(), Sound.getEtaLength()) );
  220. //trace("Sound ready = "+Sound.ready()+"; rate="+Sound.getRate()+"; channels="+Sound.getChannels()+"; samples="+Sound.samplesAvailable());
  221. }
  222. }
  223. function initResampler() {
  224. if (Sound.getRate() != 44100) {
  225. pitch[0] = Sound.getRate() / 44100.0;
  226. trace("Resample with "+pitch[0]+" pitch");
  227. buffer = new Array<Array<Float>>();
  228. if (padding == null) {
  229. padding = new Array<Float>();
  230. for( k in 0...Resampler.getPadding() )
  231. padding.push( 0.0 ); // Fill startup padding
  232. }
  233. for( c in 0...Sound.getChannels() ) {
  234. // Fill with double padding
  235. buffer.push( padding.copy() );
  236. buffer[c] = buffer[c].concat( padding );
  237. }
  238. in_off = new Array<Float>();
  239. in_off[0] = Resampler.getPadding();
  240. asink.pos -= (padding.length / Sound.getRate());
  241. }
  242. }
  243. function populate() {
  244. if (Sound.samplesAvailable()>0) {
  245. if (Sound.getRate() == 44100) {
  246. var Samples = Sound.getSamples();
  247. var ind = new Array<Int>(); ind[0] = 0;
  248. var cnt = Samples[0].length;
  249. asink.write(Samples, ind, cnt, Sound.last);
  250. } else {
  251. var Samples = Sound.getSamples();
  252. if (pitch.length != 1)
  253. initResampler();
  254. var Res = new Array<Array<Float>>();
  255. var out_off = new Array<Int>();
  256. var inOff = in_off[0];
  257. // Conversion needs padding samples before and padding samples after
  258. // So, for last pack we need to add one more padding zone
  259. for( c in 0...Sound.getChannels() ) {
  260. buffer[c] = buffer[c].concat( Samples[c] );
  261. if (Sound.last) {
  262. buffer[c] = buffer[c].concat( padding );
  263. buffer[c] = buffer[c].concat( padding );
  264. }
  265. Res.push( new Array<Float>() );
  266. in_off[0] = inOff;
  267. out_off[0] = 0;
  268. // Note: number of last element, not count!
  269. // Always hold 1 padding left and 1 padding right
  270. var in_end: Float = buffer[c].length-padding.length;
  271. var out_end: Int = (Std.int( buffer[0].length / pitch[0] + 1 )+Resampler.getPadding())*5;
  272. Resampler.interpolate(buffer[c], in_off, in_end, pitch, 0, Res[c], out_off, out_end);
  273. }
  274. // Write resampled sound
  275. var ind = new Array<Int>(); ind[0] = 0;
  276. asink.write(Res, ind, out_off[0], Sound.last);
  277. // Shift buffers
  278. for( c in 0...Sound.getChannels() ) {
  279. buffer[c].splice(0, Std.int( in_off[0] )-2*padding.length );
  280. }
  281. in_off[0] -= Std.int( in_off[0]-2*padding.length );
  282. }
  283. }
  284. }
  285. }