DecoderPCM.hx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. package fmt;
  17. // PCM sound decoder. Supports any bitlength
  18. class DecoderPCM extends fmt.Decoder {
  19. var divisor : Int;
  20. var shift : Int;
  21. public function new(bps : Int, ?bs : Int) {
  22. sampleSize = Math.ceil(bps / 8);
  23. if (sampleSize == 0) throw "Unsupported BPS";
  24. divisor = 1 << (bps-1);
  25. shift = 1 << bps;
  26. sampleLength = 1;
  27. }
  28. public override function decode( InBuf : haxe.io.BytesData, Off: Int, Chan: Int, OutBuf: Array<Float>, OutOff: Int) : Int {
  29. var Sample: Int = 0;
  30. switch (sampleSize) {
  31. case 1: Sample = InBuf[Off];
  32. Sample -= divisor; // 1byte stored as unsigned
  33. case 2: Sample = Std.int(InBuf[Off] + InBuf[Off+1] * 256);
  34. if (Sample > divisor) Sample -= shift;
  35. case 3: Sample = Std.int(InBuf[Off] + InBuf[Off+1] * 256 + InBuf[Off+2] * 65536);
  36. if (Sample > divisor) Sample -= shift;
  37. case 4: Sample = Std.int(InBuf[Off] + InBuf[Off+1] * 256 + InBuf[Off+2] * 65536 + InBuf[Off+3] * 16777216);
  38. if (Sample > divisor) Sample -= shift;
  39. default: for(c in 0...sampleSize) Sample += InBuf[Off+c] << (8*c);
  40. if (Sample > divisor) Sample -= shift;
  41. }
  42. OutBuf[OutOff] = Sample / divisor;
  43. return 1;
  44. }
  45. }