DecoderG711u.hx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // G711 mu-Law decoder
  17. // Dirty IF-based version removed in favor of more effective
  18. // and beautiful code from sox, licensed under GPL
  19. package fmt;
  20. class DecoderG711u extends fmt.Decoder {
  21. public var inverted : Bool;
  22. static var ulaw : Array<Float> = [ ];
  23. static var Inv : Array<Int> = [ ];
  24. public function new(bps : Int, inv : Bool = false) {
  25. if (bps != 8) throw "Unsupported BPS";
  26. sampleSize = 1;
  27. sampleLength = 1;
  28. inverted = inv;
  29. generate();
  30. }
  31. public override function decode( InBuf : haxe.io.BytesData, InOff: Int, Chan: Int, OutBuf : Array<Float>, OutOff: Int) : Int {
  32. if (inverted) {
  33. OutBuf[OutOff] = ulaw[Inv[InBuf[InOff]]];
  34. } else {
  35. OutBuf[OutOff] = ulaw[InBuf[InOff]];
  36. }
  37. return 1;
  38. }
  39. static var exp_lut : Array<Int> = [ 0, 132, 396, 924, 1980, 4092, 8316, 16764 ];
  40. public function generate() {
  41. ulaw = new Array<Float>();
  42. Inv = new Array<Int>();
  43. /*=================================================================================
  44. ** The following routines came from the sox-12.15 (Sound eXcahcnge) distribution.
  45. */
  46. /*
  47. ** This routine converts from ulaw to 16 bit linear.
  48. **
  49. ** Craig Reese: IDA/Supercomputing Research Center
  50. ** 29 September 1989
  51. **
  52. ** References:
  53. ** 1) CCITT Recommendation G.711 (very difficult to follow)
  54. ** 2) MIL-STD-188-113,"Interoperability and Performance Standards
  55. ** for Analog-to_Digital Conversion Techniques,"
  56. ** 17 February 1987
  57. **
  58. ** Input: 8 bit ulaw sample
  59. ** Output: signed 16 bit linear sample
  60. */
  61. var sign, exponent, mantissa, sample, ulawbyte;
  62. for (ii in 0...0xFF+1) {
  63. ulawbyte = ~ ii;
  64. sign = (ulawbyte & 0x80) ;
  65. exponent = (ulawbyte >> 4) & 0x07 ;
  66. mantissa = ulawbyte & 0x0F ;
  67. sample = exp_lut [exponent] + (mantissa << (exponent + 3)) ;
  68. if ( sign != 0 )
  69. sample = -sample ;
  70. ulaw[ii] = sample / 32768.0;
  71. var rev = 0;
  72. var norm = ii;
  73. for (i in 0...8) {
  74. rev = (rev<<1) | (norm & 0x01);
  75. norm >>= 1;
  76. }
  77. Inv[ii] = rev;
  78. }
  79. return;
  80. }
  81. }