FileAu.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // FileAu: stream AU file reader
  18. // Reads any implemented codecs from it
  19. class FileAu extends fmt.File {
  20. var fileSize: Int;
  21. var format: Int;
  22. public function new() {
  23. super();
  24. fileSize = 0;
  25. format = 0;
  26. }
  27. // Set known full file length
  28. public override function setSize(size: Int): Void {
  29. fileSize = size;
  30. }
  31. // Get estimated sound length
  32. public override function getEtaLength(): Null<Float> {
  33. if (Readed < 0 || dataOff == 0 || Readed<dataOff) return null;
  34. return super.getEtaLength();
  35. }
  36. // Read file header
  37. public override function readHeader() {
  38. var i = Readed;
  39. while (i<bufsize) {
  40. if (dataOff == 0 || i < dataOff) { // Read header
  41. if (i < 24) {
  42. if (bufsize-i < 4) break;
  43. var DW = Buffer[i]*16777216+Buffer[i+1]*65536+Buffer[i+2]*256+Buffer[i+3];
  44. switch( i ) {
  45. case 0:
  46. if (DW != 0x2E736E64) {
  47. trace("Wrong file magic! Got "+DW+" instead of 779316836");
  48. Readed = -1;
  49. return;
  50. }
  51. case 4:
  52. dataOff = DW;
  53. if (dataOff < 24) dataOff = 24;
  54. trace("dataOff = "+dataOff);
  55. case 8:
  56. dataSize = DW;
  57. if (dataSize == 0) dataSize = -1; // Fix for incorrect AU file
  58. if (fileSize > 0 && dataSize == -1)
  59. dataSize = fileSize - dataOff; // Calc length if we can
  60. trace("dataSize = "+dataSize);
  61. case 12:
  62. format = DW;
  63. trace("format = "+format);
  64. case 16:
  65. rate = DW;
  66. trace("rate = "+rate);
  67. case 20:
  68. channels = DW;
  69. trace("channels = "+channels);
  70. }
  71. i += 4;
  72. if (i==24) { // Header complete: check consistency
  73. if (channels < 1 || channels > 2) {
  74. trace("Wrong number of channels: "+channels);
  75. Readed = -1;
  76. return;
  77. }
  78. switch ( format ) {
  79. case 1:
  80. trace("File in 8-bit G.711 mu-law format");
  81. sndDecoder = new DecoderG711u(8, false);
  82. case 27: // Really 27!
  83. trace("File in 8-bit G.711 a-law format");
  84. sndDecoder = new DecoderG711a(8, false);
  85. case 2:
  86. trace("File in 8-bit PCM format");
  87. sndDecoder = new DecoderPCM(8);
  88. case 3:
  89. trace("File in 16-bit PCM format");
  90. sndDecoder = new DecoderPCM(16);
  91. case 4:
  92. trace("File in 24-bit PCM format");
  93. sndDecoder = new DecoderPCM(24);
  94. case 5:
  95. trace("File in 32-bit PCM format");
  96. sndDecoder = new DecoderPCM(32);
  97. default:
  98. trace("File in unknown format #"+format);
  99. Readed = -1;
  100. return;
  101. }
  102. chunkSize = sndDecoder.sampleSize*channels;
  103. init();
  104. }
  105. } else {
  106. var NeedSkip = dataOff - (Readed+i);
  107. trace("dataOff = "+dataOff+"; Readed="+Readed+"; i="+i+"; bufsize="+bufsize+"; NeedSkip="+NeedSkip);
  108. if (NeedSkip > bufsize-i) {
  109. i = bufsize;
  110. } else {
  111. i += NeedSkip;
  112. }
  113. }
  114. } else { // Read sound
  115. break;
  116. }
  117. }
  118. Readed = i;
  119. }
  120. // Returns is stream ready to operate: header readed (1), not ready (0), error(-1)
  121. public override function ready(): Int {
  122. if (Readed < 0) return -1;
  123. if (channels == 0 || chunkSize == 0 || rate == 0 || sndDecoder==null || Readed < dataOff) return 0;
  124. return 1;
  125. }
  126. }