PageRenderTime 37ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/haxe/org/xiph/fvorbis/Info.hx

http://github.com/TooTallNate/SoundJS
Haxe | 466 lines | 369 code | 21 blank | 76 comment | 114 complexity | 7d3a339026d467e22b3c85862aaf33b1 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. package org.xiph.fvorbis;
  2. import org.xiph.system.Bytes;
  3. import flash.Vector;
  4. import org.xiph.fogg.Buffer;
  5. import org.xiph.fogg.Packet;
  6. class Info {
  7. /*
  8. * generated source for Info
  9. */
  10. inline static private var OV_EBADPACKET : Int = -136;
  11. inline static private var OV_ENOTAUDIO : Int = -135;
  12. static private var _vorbis : Bytes = System.fromString("vorbis");
  13. inline static private var VI_TIMEB : Int = 1;
  14. inline static private var VI_FLOORB : Int = 2;
  15. inline static private var VI_RESB : Int = 3;
  16. inline static private var VI_MAPB : Int = 1;
  17. inline static private var VI_WINDOWB : Int = 1;
  18. public var version : Int;
  19. public var channels : Int;
  20. public var rate : Int;
  21. var bitrate_upper : Int;
  22. var bitrate_nominal : Int;
  23. var bitrate_lower : Int;
  24. // discarded initializer: 'new int[2]';
  25. public var blocksizes : Vector<Int>;
  26. public var modes : Int;
  27. var maps : Int;
  28. public var times : Int;
  29. public var floors : Int;
  30. public var residues : Int;
  31. public var books : Int;
  32. public var psys : Int;
  33. // discarded initializer: 'null';
  34. public var mode_param : Array<InfoMode>;
  35. // discarded initializer: 'null';
  36. public var map_type : Vector<Int>;
  37. // discarded initializer: 'null';
  38. public var map_param : Array<Dynamic>;
  39. // discarded initializer: 'null';
  40. public var time_type : Vector<Int>;
  41. // discarded initializer: 'null';
  42. public var time_param : Array<Dynamic>;
  43. // discarded initializer: 'null';
  44. public var floor_type : Vector<Int>;
  45. // discarded initializer: 'null';
  46. public var floor_param : Array<Dynamic>;
  47. // discarded initializer: 'null';
  48. public var residue_type : Vector<Int>;
  49. // discarded initializer: 'null';
  50. public var residue_param : Array<Dynamic>;
  51. // discarded initializer: 'null';
  52. public var book_param : Array<StaticCodeBook>;
  53. // discarded initializer: 'new PsyInfo[64]';
  54. // psy_param : Vector<PsyInfo>;
  55. var envelopesa : Int;
  56. var preecho_thresh : Float;
  57. var preecho_clamp : Float;
  58. // modifiers: public
  59. public function init() : Void {
  60. rate = 0;
  61. }
  62. // modifiers: public
  63. public function clear() : Void {
  64. // for-while;
  65. var i : Int = 0;
  66. while (i < modes) {
  67. mode_param[i] = null;
  68. i++;
  69. };
  70. mode_param = null;
  71. // for-while;
  72. var i : Int = 0;
  73. while (i < maps) {
  74. FuncMapping.mapping_P[map_type[i]].free_info(map_param[i]);
  75. i++;
  76. };
  77. map_param = null;
  78. // for-while;
  79. var i : Int = 0;
  80. while (i < times) {
  81. FuncTime.time_P[time_type[i]].free_info(time_param[i]);
  82. i++;
  83. };
  84. time_param = null;
  85. // for-while;
  86. var i : Int = 0;
  87. while (i < floors) {
  88. FuncFloor.floor_P[floor_type[i]].free_info(floor_param[i]);
  89. i++;
  90. };
  91. floor_param = null;
  92. // for-while;
  93. var i : Int = 0;
  94. while (i < residues) {
  95. FuncResidue.residue_P[residue_type[i]].free_info(residue_param[i]);
  96. i++;
  97. };
  98. residue_param = null;
  99. // for-while;
  100. var i : Int = 0;
  101. while (i < books) {
  102. if (book_param[i] != null) {
  103. book_param[i].clear();
  104. book_param[i] = null;
  105. };
  106. i++;
  107. };
  108. book_param = null;
  109. /*
  110. // for-while;
  111. var i : Int = 0;
  112. while (i < psys) {
  113. psy_param[i].free();
  114. i++;
  115. };
  116. */
  117. }
  118. // modifiers:
  119. function unpack_info(opb : Buffer) : Int {
  120. version = opb.read(32);
  121. if (version != 0) {
  122. return -1;
  123. };
  124. channels = opb.read(8);
  125. rate = opb.read(32);
  126. bitrate_upper = opb.read(32);
  127. bitrate_nominal = opb.read(32);
  128. bitrate_lower = opb.read(32);
  129. blocksizes[0] = (1 << opb.read(4));
  130. blocksizes[1] = (1 << opb.read(4));
  131. if (((((rate < 1) || (channels < 1)) || (blocksizes[0] < 8)) || (blocksizes[1] < blocksizes[0])) || (opb.read(1) != 1)) {
  132. clear();
  133. return -1;
  134. };
  135. return 0;
  136. }
  137. // modifiers:
  138. function unpack_books(opb : Buffer) : Int {
  139. books = (opb.read(8) + 1);
  140. if ((book_param == null) || (book_param.length != books)) {
  141. //book_param = new StaticCodeBook[books];
  142. //book_param = new Vector(books, true);
  143. book_param = ArrayTools.alloc(books);
  144. };
  145. // for-while;
  146. var i : Int = 0;
  147. while (i < books) {
  148. book_param[i] = new StaticCodeBook();
  149. if (book_param[i].unpack(opb) != 0) {
  150. clear();
  151. return -1;
  152. };
  153. i++;
  154. };
  155. times = (opb.read(6) + 1);
  156. if ((time_type == null) || (cast(time_type.length, Int) != times)) {
  157. //time_type = new int[times];
  158. time_type = new Vector(times, true);
  159. };
  160. if ((time_param == null) || (time_param.length != times)) {
  161. //time_param = new Object[times];
  162. //time_param = new Vector(times, true);
  163. time_param = ArrayTools.alloc(times);
  164. };
  165. // for-while;
  166. var i : Int = 0;
  167. while (i < times) {
  168. time_type[i] = opb.read(16);
  169. if ((time_type[i] < 0) || (time_type[i] >= Info.VI_TIMEB)) {
  170. clear();
  171. return -1;
  172. };
  173. time_param[i] = FuncTime.time_P[time_type[i]].unpack(this, opb);
  174. if (time_param[i] == null) {
  175. clear();
  176. return -1;
  177. };
  178. i++;
  179. };
  180. floors = (opb.read(6) + 1);
  181. if ((floor_type == null) || (cast(floor_type.length, Int) != floors)) {
  182. //floor_type = new int[floors];
  183. floor_type = new Vector(floors, true);
  184. };
  185. if ((floor_param == null) || (floor_param.length != floors)) {
  186. //floor_param = new Object[floors];
  187. //floor_param = new Vector(floors, true);
  188. floor_param = ArrayTools.alloc(floors, true);
  189. };
  190. // for-while;
  191. var i : Int = 0;
  192. while (i < floors) {
  193. floor_type[i] = opb.read(16);
  194. if ((floor_type[i] < 0) || (floor_type[i] >= Info.VI_FLOORB)) {
  195. clear();
  196. return -1;
  197. };
  198. floor_param[i] = FuncFloor.floor_P[floor_type[i]].unpack(this, opb);
  199. if (floor_param[i] == null) {
  200. clear();
  201. return -1;
  202. };
  203. i++;
  204. };
  205. residues = (opb.read(6) + 1);
  206. if ((residue_type == null) || (cast(residue_type.length, Int) != residues)) {
  207. //residue_type = new int[residues];
  208. residue_type = new Vector(residues, true);
  209. };
  210. if ((residue_param == null) || (residue_param.length != residues)) {
  211. //residue_param = new Object[residues];
  212. //residue_param = new Vector(residues, true);
  213. residue_param = ArrayTools.alloc(residues);
  214. };
  215. // for-while;
  216. var i : Int = 0;
  217. while (i < residues) {
  218. residue_type[i] = opb.read(16);
  219. if ((residue_type[i] < 0) || (residue_type[i] >= Info.VI_RESB)) {
  220. clear();
  221. return -1;
  222. };
  223. residue_param[i] = FuncResidue.residue_P[residue_type[i]].unpack(this, opb);
  224. if (residue_param[i] == null) {
  225. clear();
  226. return -1;
  227. };
  228. i++;
  229. };
  230. maps = (opb.read(6) + 1);
  231. if ((map_type == null) || (cast(map_type.length, Int) != maps)) {
  232. //map_type = new int[maps];
  233. map_type = new Vector(maps, true);
  234. };
  235. if ((map_param == null) || (map_param.length != maps)) {
  236. //map_param = new Object[maps];
  237. //map_param = new Vector(maps, true);
  238. map_param = ArrayTools.alloc(maps);
  239. };
  240. // for-while;
  241. var i : Int = 0;
  242. while (i < maps) {
  243. map_type[i] = opb.read(16);
  244. if ((map_type[i] < 0) || (map_type[i] >= Info.VI_MAPB)) {
  245. clear();
  246. return -1;
  247. };
  248. map_param[i] = FuncMapping.mapping_P[map_type[i]].unpack(this, opb);
  249. if (map_param[i] == null) {
  250. clear();
  251. return -1;
  252. };
  253. i++;
  254. };
  255. modes = (opb.read(6) + 1);
  256. if ((mode_param == null) || (mode_param.length != modes)) {
  257. //mode_param = new InfoMode[modes];
  258. //mode_param = new Vector(modes, true);
  259. mode_param = ArrayTools.alloc(modes);
  260. };
  261. // for-while;
  262. var i : Int = 0;
  263. while (i < modes) {
  264. mode_param[i] = new InfoMode();
  265. mode_param[i].blockflag = opb.read(1);
  266. mode_param[i].windowtype = opb.read(16);
  267. mode_param[i].transformtype = opb.read(16);
  268. mode_param[i].mapping = opb.read(8);
  269. if (((mode_param[i].windowtype >= Info.VI_WINDOWB) || (mode_param[i].transformtype >= Info.VI_WINDOWB)) || (mode_param[i].mapping >= maps)) {
  270. clear();
  271. return -1;
  272. };
  273. i++;
  274. };
  275. if (opb.read(1) != 1) {
  276. clear();
  277. return -1;
  278. };
  279. return 0;
  280. }
  281. // modifiers: public
  282. public function synthesis_headerin(vc : Comment, op : Packet) : Int {
  283. var opb : Buffer = new Buffer();
  284. if (op != null) {
  285. opb.readinit(op.packet_base, op.packet, op.bytes);
  286. var buffer : Bytes = System.alloc(6);
  287. var packtype : Int = opb.read(8);
  288. opb.readBytes(buffer, 6);
  289. /*
  290. if (buffer[0] != 'v' || buffer[1] != 'o' || buffer[2] != 'r' ||
  291. buffer[3] != 'b' || buffer[4] != 'i' || buffer[5] != 's') {
  292. return -1;
  293. };
  294. */
  295. if (buffer.toString() != "vorbis") {
  296. return -1;
  297. };
  298. switch (packtype) {
  299. case 0x01:
  300. if (op.b_o_s == 0) {
  301. return -1;
  302. };
  303. if (rate != 0) {
  304. return -1;
  305. };
  306. return unpack_info(opb);
  307. case 0x03:
  308. if (rate == 0) {
  309. return -1;
  310. };
  311. return vc.unpack(opb);
  312. case 0x05:
  313. if ((rate == 0) || (vc.vendor == null)) {
  314. return -1;
  315. };
  316. return unpack_books(opb);
  317. default:
  318. };
  319. };
  320. return -1;
  321. }
  322. // modifiers:
  323. function pack_info(opb : Buffer) : Int {
  324. opb.write(0x01, 8);
  325. opb.writeBytes(Info._vorbis);
  326. opb.write(0x00, 32);
  327. opb.write(channels, 8);
  328. opb.write(rate, 32);
  329. opb.write(bitrate_upper, 32);
  330. opb.write(bitrate_nominal, 32);
  331. opb.write(bitrate_lower, 32);
  332. opb.write(Info.ilog2(blocksizes[0]), 4);
  333. opb.write(Info.ilog2(blocksizes[1]), 4);
  334. opb.write(1, 1);
  335. return 0;
  336. }
  337. // modifiers:
  338. function pack_books(opb : Buffer) : Int {
  339. opb.write(0x05, 8);
  340. opb.writeBytes(Info._vorbis);
  341. opb.write(books - 1, 8);
  342. // for-while;
  343. var i : Int = 0;
  344. while (i < books) {
  345. if (book_param[i].pack(opb) != 0) {
  346. return -1;
  347. };
  348. i++;
  349. };
  350. opb.write(times - 1, 6);
  351. // for-while;
  352. var i : Int = 0;
  353. while (i < times) {
  354. opb.write(time_type[i], 16);
  355. FuncTime.time_P[time_type[i]].pack(this.time_param[i], opb);
  356. i++;
  357. };
  358. opb.write(floors - 1, 6);
  359. // for-while;
  360. var i : Int = 0;
  361. while (i < floors) {
  362. opb.write(floor_type[i], 16);
  363. FuncFloor.floor_P[floor_type[i]].pack(floor_param[i], opb);
  364. i++;
  365. };
  366. opb.write(residues - 1, 6);
  367. // for-while;
  368. var i : Int = 0;
  369. while (i < residues) {
  370. opb.write(residue_type[i], 16);
  371. FuncResidue.residue_P[residue_type[i]].pack(residue_param[i], opb);
  372. i++;
  373. };
  374. opb.write(maps - 1, 6);
  375. // for-while;
  376. var i : Int = 0;
  377. while (i < maps) {
  378. opb.write(map_type[i], 16);
  379. FuncMapping.mapping_P[map_type[i]].pack(this, map_param[i], opb);
  380. i++;
  381. };
  382. opb.write(modes - 1, 6);
  383. // for-while;
  384. var i : Int = 0;
  385. while (i < modes) {
  386. opb.write(mode_param[i].blockflag, 1);
  387. opb.write(mode_param[i].windowtype, 16);
  388. opb.write(mode_param[i].transformtype, 16);
  389. opb.write(mode_param[i].mapping, 8);
  390. i++;
  391. };
  392. opb.write(1, 1);
  393. return 0;
  394. }
  395. // modifiers: public
  396. public function blocksize(op : Packet) : Int {
  397. var opb : Buffer = new Buffer();
  398. var mode : Int;
  399. opb.readinit(op.packet_base, op.packet, op.bytes);
  400. if (opb.read(1) != 0) {
  401. return Info.OV_ENOTAUDIO;
  402. };
  403. var modebits : Int = 0;
  404. var v : Int = modes;
  405. while (v > 1) {
  406. modebits++;
  407. v >>>= 1;
  408. };
  409. mode = opb.read(modebits);
  410. if (mode == -1) {
  411. return Info.OV_EBADPACKET;
  412. };
  413. return blocksizes[mode_param[mode].blockflag];
  414. }
  415. // modifiers: static, private
  416. static private function ilog2(v : Int) : Int {
  417. var ret : Int = 0;
  418. while (v > 1) {
  419. ret++;
  420. v >>>= 1;
  421. };
  422. return ret;
  423. }
  424. // modifiers: public
  425. public function toString() : String {
  426. return "version:" + version + ", channels:" + channels +
  427. ", rate:" + rate + ", bitrate:" + bitrate_upper + "," +
  428. bitrate_nominal + "," + bitrate_lower;
  429. }
  430. public function new() {
  431. // collected discarded initializers:
  432. blocksizes = new Vector(2, true);
  433. mode_param = null;
  434. map_type = null;
  435. map_param = null;
  436. time_type = null;
  437. time_param = null;
  438. floor_type = null;
  439. floor_param = null;
  440. residue_type = null;
  441. residue_param = null;
  442. book_param = null;
  443. //psy_param = new Vector(64, true);
  444. }
  445. }