module.audio.la.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. /////////////////////////////////////////////////////////////////
  7. // See readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.audio.la.php //
  11. // module for analyzing LA audio files //
  12. // dependencies: module.audio.riff.php //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
  16. class getid3_la
  17. {
  18. function getid3_la(&$fd, &$ThisFileInfo) {
  19. $offset = 0;
  20. fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
  21. $rawdata = fread($fd, GETID3_FREAD_BUFFER_SIZE);
  22. switch (substr($rawdata, $offset, 4)) {
  23. case 'LA02':
  24. case 'LA03':
  25. case 'LA04':
  26. $ThisFileInfo['fileformat'] = 'la';
  27. $ThisFileInfo['audio']['dataformat'] = 'la';
  28. $ThisFileInfo['audio']['lossless'] = true;
  29. $ThisFileInfo['la']['version_major'] = (int) substr($rawdata, $offset + 2, 1);
  30. $ThisFileInfo['la']['version_minor'] = (int) substr($rawdata, $offset + 3, 1);
  31. $ThisFileInfo['la']['version'] = (float) $ThisFileInfo['la']['version_major'] + ($ThisFileInfo['la']['version_minor'] / 10);
  32. $offset += 4;
  33. $ThisFileInfo['la']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  34. $offset += 4;
  35. if ($ThisFileInfo['la']['uncompressed_size'] == 0) {
  36. $ThisFileInfo['error'][] = 'Corrupt LA file: uncompressed_size == zero';
  37. return false;
  38. }
  39. $WAVEchunk = substr($rawdata, $offset, 4);
  40. if ($WAVEchunk !== 'WAVE') {
  41. $ThisFileInfo['error'][] = 'Expected "WAVE" ('.getid3_lib::PrintHexBytes('WAVE').') at offset '.$offset.', found "'.$WAVEchunk.'" ('.getid3_lib::PrintHexBytes($WAVEchunk).') instead.';
  42. return false;
  43. }
  44. $offset += 4;
  45. $ThisFileInfo['la']['fmt_size'] = 24;
  46. if ($ThisFileInfo['la']['version'] >= 0.3) {
  47. $ThisFileInfo['la']['fmt_size'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  48. $ThisFileInfo['la']['header_size'] = 49 + $ThisFileInfo['la']['fmt_size'] - 24;
  49. $offset += 4;
  50. } else {
  51. // version 0.2 didn't support additional data blocks
  52. $ThisFileInfo['la']['header_size'] = 41;
  53. }
  54. $fmt_chunk = substr($rawdata, $offset, 4);
  55. if ($fmt_chunk !== 'fmt ') {
  56. $ThisFileInfo['error'][] = 'Expected "fmt " ('.getid3_lib::PrintHexBytes('fmt ').') at offset '.$offset.', found "'.$fmt_chunk.'" ('.getid3_lib::PrintHexBytes($fmt_chunk).') instead.';
  57. return false;
  58. }
  59. $offset += 4;
  60. $fmt_size = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  61. $offset += 4;
  62. $ThisFileInfo['la']['raw']['format'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
  63. $offset += 2;
  64. $ThisFileInfo['la']['channels'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
  65. $offset += 2;
  66. if ($ThisFileInfo['la']['channels'] == 0) {
  67. $ThisFileInfo['error'][] = 'Corrupt LA file: channels == zero';
  68. return false;
  69. }
  70. $ThisFileInfo['la']['sample_rate'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  71. $offset += 4;
  72. if ($ThisFileInfo['la']['sample_rate'] == 0) {
  73. $ThisFileInfo['error'][] = 'Corrupt LA file: sample_rate == zero';
  74. return false;
  75. }
  76. $ThisFileInfo['la']['bytes_per_second'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  77. $offset += 4;
  78. $ThisFileInfo['la']['bytes_per_sample'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
  79. $offset += 2;
  80. $ThisFileInfo['la']['bits_per_sample'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
  81. $offset += 2;
  82. $ThisFileInfo['la']['samples'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  83. $offset += 4;
  84. $ThisFileInfo['la']['raw']['flags'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 1));
  85. $offset += 1;
  86. $ThisFileInfo['la']['flags']['seekable'] = (bool) ($ThisFileInfo['la']['raw']['flags'] & 0x01);
  87. if ($ThisFileInfo['la']['version'] >= 0.4) {
  88. $ThisFileInfo['la']['flags']['high_compression'] = (bool) ($ThisFileInfo['la']['raw']['flags'] & 0x02);
  89. }
  90. $ThisFileInfo['la']['original_crc'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  91. $offset += 4;
  92. // mikeØbevin*de
  93. // Basically, the blocksize/seekevery are 61440/19 in La0.4 and 73728/16
  94. // in earlier versions. A seekpoint is added every blocksize * seekevery
  95. // samples, so 4 * int(totalSamples / (blockSize * seekEvery)) should
  96. // give the number of bytes used for the seekpoints. Of course, if seeking
  97. // is disabled, there are no seekpoints stored.
  98. if ($ThisFileInfo['la']['version'] >= 0.4) {
  99. $ThisFileInfo['la']['blocksize'] = 61440;
  100. $ThisFileInfo['la']['seekevery'] = 19;
  101. } else {
  102. $ThisFileInfo['la']['blocksize'] = 73728;
  103. $ThisFileInfo['la']['seekevery'] = 16;
  104. }
  105. $ThisFileInfo['la']['seekpoint_count'] = 0;
  106. if ($ThisFileInfo['la']['flags']['seekable']) {
  107. $ThisFileInfo['la']['seekpoint_count'] = floor($ThisFileInfo['la']['samples'] / ($ThisFileInfo['la']['blocksize'] * $ThisFileInfo['la']['seekevery']));
  108. for ($i = 0; $i < $ThisFileInfo['la']['seekpoint_count']; $i++) {
  109. $ThisFileInfo['la']['seekpoints'][] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  110. $offset += 4;
  111. }
  112. }
  113. if ($ThisFileInfo['la']['version'] >= 0.3) {
  114. // Following the main header information, the program outputs all of the
  115. // seekpoints. Following these is what I called the 'footer start',
  116. // i.e. the position immediately after the La audio data is finished.
  117. $ThisFileInfo['la']['footerstart'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  118. $offset += 4;
  119. if ($ThisFileInfo['la']['footerstart'] > $ThisFileInfo['filesize']) {
  120. $ThisFileInfo['warning'][] = 'FooterStart value points to offset '.$ThisFileInfo['la']['footerstart'].' which is beyond end-of-file ('.$ThisFileInfo['filesize'].')';
  121. $ThisFileInfo['la']['footerstart'] = $ThisFileInfo['filesize'];
  122. }
  123. } else {
  124. // La v0.2 didn't have FooterStart value
  125. $ThisFileInfo['la']['footerstart'] = $ThisFileInfo['avdataend'];
  126. }
  127. if ($ThisFileInfo['la']['footerstart'] < $ThisFileInfo['avdataend']) {
  128. if ($RIFFtempfilename = tempnam('*', 'id3')) {
  129. if ($RIFF_fp = fopen($RIFFtempfilename, 'w+b')) {
  130. $RIFFdata = 'WAVE';
  131. if ($ThisFileInfo['la']['version'] == 0.2) {
  132. $RIFFdata .= substr($rawdata, 12, 24);
  133. } else {
  134. $RIFFdata .= substr($rawdata, 16, 24);
  135. }
  136. if ($ThisFileInfo['la']['footerstart'] < $ThisFileInfo['avdataend']) {
  137. fseek($fd, $ThisFileInfo['la']['footerstart'], SEEK_SET);
  138. $RIFFdata .= fread($fd, $ThisFileInfo['avdataend'] - $ThisFileInfo['la']['footerstart']);
  139. }
  140. $RIFFdata = 'RIFF'.getid3_lib::LittleEndian2String(strlen($RIFFdata), 4, false).$RIFFdata;
  141. fwrite($RIFF_fp, $RIFFdata, strlen($RIFFdata));
  142. $dummy = $ThisFileInfo;
  143. $dummy['filesize'] = strlen($RIFFdata);
  144. $dummy['avdataoffset'] = 0;
  145. $dummy['avdataend'] = $dummy['filesize'];
  146. $riff = new getid3_riff($RIFF_fp, $dummy);
  147. if (empty($dummy['error'])) {
  148. $ThisFileInfo['riff'] = $dummy['riff'];
  149. } else {
  150. $ThisFileInfo['warning'][] = 'Error parsing RIFF portion of La file: '.implode($dummy['error']);
  151. }
  152. unset($riff);
  153. unset($dummy);
  154. fclose($RIFF_fp);
  155. }
  156. unlink($RIFFtempfilename);
  157. }
  158. }
  159. // $ThisFileInfo['avdataoffset'] should be zero to begin with, but just in case it's not, include the addition anyway
  160. $ThisFileInfo['avdataend'] = $ThisFileInfo['avdataoffset'] + $ThisFileInfo['la']['footerstart'];
  161. $ThisFileInfo['avdataoffset'] = $ThisFileInfo['avdataoffset'] + $offset;
  162. //$ThisFileInfo['la']['codec'] = RIFFwFormatTagLookup($ThisFileInfo['la']['raw']['format']);
  163. $ThisFileInfo['la']['compression_ratio'] = (float) (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) / $ThisFileInfo['la']['uncompressed_size']);
  164. $ThisFileInfo['playtime_seconds'] = (float) ($ThisFileInfo['la']['samples'] / $ThisFileInfo['la']['sample_rate']) / $ThisFileInfo['la']['channels'];
  165. if ($ThisFileInfo['playtime_seconds'] == 0) {
  166. $ThisFileInfo['error'][] = 'Corrupt LA file: playtime_seconds == zero';
  167. return false;
  168. }
  169. $ThisFileInfo['audio']['bitrate'] = ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8 / $ThisFileInfo['playtime_seconds'];
  170. //$ThisFileInfo['audio']['codec'] = $ThisFileInfo['la']['codec'];
  171. $ThisFileInfo['audio']['bits_per_sample'] = $ThisFileInfo['la']['bits_per_sample'];
  172. break;
  173. default:
  174. if (substr($rawdata, $offset, 2) == 'LA') {
  175. $ThisFileInfo['error'][] = 'This version of getID3() (v'.GETID3_VERSION.') doesn\'t support LA version '.substr($rawdata, $offset + 2, 1).'.'.substr($rawdata, $offset + 3, 1).' which this appears to be - check http://getid3.sourceforge.net for updates.';
  176. } else {
  177. $ThisFileInfo['error'][] = 'Not a LA (Lossless-Audio) file';
  178. }
  179. return false;
  180. break;
  181. }
  182. $ThisFileInfo['audio']['channels'] = $ThisFileInfo['la']['channels'];
  183. $ThisFileInfo['audio']['sample_rate'] = (int) $ThisFileInfo['la']['sample_rate'];
  184. $ThisFileInfo['audio']['encoder'] = 'LA v'.$ThisFileInfo['la']['version'];
  185. return true;
  186. }
  187. }
  188. ?>