DecoderG711a.hx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 a-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 DecoderG711a extends fmt.Decoder {
  21. public var inverted : Bool;
  22. static var aLaw : 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] = aLaw[Inv[InBuf[InOff]]];
  34. } else {
  35. OutBuf[OutOff] = aLaw[InBuf[InOff]];
  36. }
  37. return 1;
  38. }
  39. static var exp_lut : Array<Int> = [ 0, 264, 528, 1056, 2112, 4224, 8448, 16896 ];
  40. public function generate() {
  41. aLaw = 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, Alawbyte;
  62. for (ii in 0...0xFF+1) {
  63. Alawbyte = ii ^ 0x55;
  64. sign = ( Alawbyte & 0x80 ) ;
  65. Alawbyte &= 0x7f ; /* get magnitude */
  66. if (Alawbyte >= 16)
  67. { exponent = (Alawbyte >> 4 ) & 0x07 ;
  68. mantissa = Alawbyte & 0x0F ;
  69. sample = exp_lut[exponent] + (mantissa << ( exponent + 3 )) ;
  70. }
  71. else
  72. sample = (Alawbyte << 4) + 8 ;
  73. if (sign == 0)
  74. sample = -sample ;
  75. aLaw[ii] = sample / 32768.0;
  76. var rev = 0;
  77. var norm = ii;
  78. for (i in 0...8) {
  79. rev = (rev<<1) | (norm & 0x01);
  80. norm >>= 1;
  81. }
  82. Inv[ii] = rev;
  83. }
  84. return;
  85. }
  86. }