module.audio.la.php 9.1 KB

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