FileWav.hx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // FileWav: stream WAV file reader
  17. // Currently able to read only files with one <data> block
  18. package fmt;
  19. class FileWav extends fmt.File {
  20. var State: Int;
  21. var fileSize: Int;
  22. var format: Int;
  23. var bps : Int;
  24. public function new() {
  25. super();
  26. fileSize = 0;
  27. format = 0;
  28. State = 0;
  29. bps = 0;
  30. }
  31. // Set known full file length
  32. public override function setSize(size: Int): Void {
  33. fileSize = size;
  34. }
  35. // Get estimated sound length
  36. public override function getEtaLength(): Null<Float> {
  37. if (Readed < 0 || State<4 || rate==0 || chunkSize==0 || dataSize==0) return null;
  38. return super.getEtaLength();
  39. }
  40. // Read file header
  41. public override function readHeader() {
  42. var i = Readed;
  43. while (i<bufsize) {
  44. switch (State) {
  45. case 0: // Read RIFF header
  46. if (i < 12) {
  47. if (bufsize-i < 4) break;
  48. var DW = Buffer[i+3]*16777216+Buffer[i+2]*65536+Buffer[i+1]*256+Buffer[i];
  49. switch( i ) {
  50. case 0:
  51. if (DW != 0x46464952) {
  52. trace("Wrong RIFF magic! Got "+DW+" instead of 0x46464952");
  53. Readed = -1;
  54. return;
  55. }
  56. case 4:
  57. dataSize = DW;
  58. trace("dataSize = "+dataSize);
  59. case 8:
  60. if (DW != 0x45564157) {
  61. trace("Wrong WAVE magic! Got "+DW+" instead of 0x45564157");
  62. Readed = -1;
  63. return;
  64. }
  65. }
  66. i += 4;
  67. }
  68. if (i == 12) { // RIFF header skipped, go to WAVE blocks
  69. State++;
  70. dataOff = i;
  71. }
  72. case 1: // Read fmt block
  73. if (i-dataOff < 24 || i-dataOff < dataSize+8) {
  74. if (bufsize-i < 4) break;
  75. var W1 = Buffer[i+1]*256+Buffer[i];
  76. var W2 = Buffer[i+3]*256+Buffer[i+2];
  77. var DW = W2*65536+W1;
  78. switch( i-dataOff ) {
  79. case 0:
  80. if (DW != 0x20746D66) {
  81. trace("Wrong 'fmt ' magic! Got "+DW+" instead of 0x20746D66");
  82. Readed = -1;
  83. return;
  84. }
  85. case 4:
  86. dataSize = DW;
  87. trace("dataSize2 = "+dataSize);
  88. case 8:
  89. channels = W2;
  90. format = W1;
  91. trace("format = "+format+"; channels = "+channels);
  92. case 12:
  93. rate = DW;
  94. trace("rate = "+rate);
  95. case 20:
  96. bps = W2;
  97. align = W1;
  98. trace("align = "+align+"; bps="+bps);
  99. case 24:
  100. trace("Appendix W1="+W1+", W2="+W2+", DW="+DW);
  101. }
  102. i += 4;
  103. if (i-dataOff >= dataSize+8) {
  104. i = dataSize+8 + dataOff; // Workaround for non-round fmt section
  105. if (channels < 1 || channels > 2) {
  106. trace("Wrong number of channels: "+channels);
  107. Readed = -1;
  108. return;
  109. }
  110. switch ( format ) {
  111. case 1:
  112. trace("File in PCM");
  113. sndDecoder = new DecoderPCM(bps);
  114. case 65534:
  115. trace("File in (Bad?) PCM");
  116. sndDecoder = new DecoderPCM(bps);
  117. case 2:
  118. trace("File in MS ADPCM");
  119. Readed = -1; return;
  120. //sndDecoder = new DecoderMSADPCM(bps);
  121. case 6:
  122. trace("File in 8-bit G.711 a-law format");
  123. sndDecoder = new DecoderG711a(bps, false);
  124. case 7:
  125. trace("File in 8-bit G.711 mu-law format");
  126. sndDecoder = new DecoderG711u(bps, false);
  127. case 17:
  128. trace("File in IMA ADPCM");
  129. sndDecoder = new DecoderIMAADPCM(bps, channels, align, W2);
  130. align = 0; // hack :(
  131. //Readed = -1; return;
  132. case 20:
  133. trace("File in G.723 ADPCM");
  134. Readed = -1; return;
  135. //sndDecoder = new DecoderG723ADPCM(bps);
  136. case 49:
  137. trace("File in GSM 6.10");
  138. sndDecoder = new DecoderGSM(bps, align);
  139. case 64:
  140. trace("File in G.721 ADPCM");
  141. Readed = -1; return;
  142. //sndDecoder = new DecoderG721ADPCM(bps);
  143. case 80:
  144. trace("File in MPEG");
  145. //sndDecoder = new DecoderMPEG(bps);
  146. Readed = -1; return;
  147. default:
  148. trace("File in unknown/unsupported format #"+format);
  149. Readed = -1;
  150. return;
  151. }
  152. chunkSize = sndDecoder.sampleSize*channels;
  153. init();
  154. if (align > chunkSize) align -= chunkSize; else align = 0;
  155. if (i-dataOff == dataSize+8) {
  156. State++;
  157. dataOff = i;
  158. }
  159. trace("chunkSize = "+chunkSize+"; sampleSize="+sndDecoder.sampleSize+"; align="+align);
  160. }
  161. }
  162. else if (i-dataOff < dataSize+8) {
  163. var NeedSkip = (dataSize+8) - (i-dataOff);
  164. trace("dataOff = "+dataOff+"; dataSize = "+dataSize+"; Readed="+Readed+"; i="+i+"; bufsize="+bufsize+"; NeedSkip="+NeedSkip);
  165. if (NeedSkip > bufsize-i) {
  166. i = bufsize;
  167. } else {
  168. i += NeedSkip;
  169. }
  170. if (i-dataOff == dataSize+8) {
  171. State++;
  172. dataOff = i;
  173. }
  174. }
  175. case 2: // Read any misc header
  176. if (i-dataOff < 8) {
  177. if (bufsize-i < 4) break;
  178. var DW = Buffer[i+3]*16777216+Buffer[i+2]*65536+Buffer[i+1]*256+Buffer[i];
  179. switch( i-dataOff ) {
  180. case 0:
  181. if (DW == 0x61746164) {
  182. trace("Data block!");
  183. State++;
  184. } else
  185. trace("Unknown block, skipping ("+DW+")");
  186. case 4:
  187. dataSize = DW;
  188. trace("dataSize3 = "+dataSize);
  189. }
  190. i += 4;
  191. }
  192. if (i-dataOff >= 8 && i-dataOff <= dataSize+8) {
  193. var NeedSkip = (dataSize+8) - (i-dataOff);
  194. trace("dataOff = "+dataOff+"; dataSize = "+dataSize+"; Readed="+Readed+"; i="+i+"; bufsize="+bufsize+"; NeedSkip="+NeedSkip);
  195. if (NeedSkip > bufsize-i) {
  196. i = bufsize;
  197. } else {
  198. i += NeedSkip;
  199. }
  200. if (i-dataOff == dataSize+8) {
  201. dataOff = i;
  202. }
  203. }
  204. case 3: // Read data header
  205. if (i-dataOff < 8) {
  206. if (bufsize-i < 4) break;
  207. var DW = Buffer[i+3]*16777216+Buffer[i+2]*65536+Buffer[i+1]*256+Buffer[i];
  208. switch( i-dataOff ) {
  209. case 0:
  210. if (DW != 0x61746164) {
  211. trace("Wrong 'data' magic! Got "+DW+" instead of 0x61746164");
  212. Readed = -1;
  213. return;
  214. }
  215. case 4:
  216. dataSize = DW;
  217. trace("dataSize(data) = "+dataSize);
  218. if (dataSize <= 0 && fileSize > 0)
  219. dataSize = fileSize - dataOff; // Try to get data size
  220. // Todo: support multiple "DATA" chunks in file, skipping unknown blocks
  221. }
  222. i += 4;
  223. }
  224. if (i-dataOff == 8) {
  225. State++;
  226. dataOff = i;
  227. trace("Get data block begin (dataOff="+dataOff+"; State="+State+"; bufsize="+bufsize+")");
  228. }
  229. default: // Read sound stream
  230. break;
  231. }
  232. }
  233. // Remove processed bytes
  234. Readed = i;
  235. }
  236. // Returns is stream ready to operate: header readed (1), not ready (0), error(-1)
  237. public override function ready(): Int
  238. {
  239. if (Readed < 0) return -1;
  240. if (State < 4) return 0;
  241. if (channels == 0 || chunkSize == 0 || rate == 0 || sndDecoder==null) return -1; // if we got audio state without info -- error
  242. return 1;
  243. }
  244. }