rlm@3: <?php
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | PHP version 5                                                        |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen                 |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | This source file is subject to version 2 of the GPL license,         |
rlm@3: // | that is bundled with this package in the file license.txt and is     |
rlm@3: // | available through the world-wide-web at the following url:           |
rlm@3: // | http://www.gnu.org/copyleft/gpl.html                                 |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org    |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | Authors: James Heinrich <infoØgetid3*org>                            |
rlm@3: // |          Allan Hansen <ahØartemis*dk>                                |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | module.audio.la.php                                                  |
rlm@3: // | Module for analyzing LA udio files                                   |
rlm@3: // | dependencies: module.audio-video.riff.php                            |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: //
rlm@3: // $Id: module.audio.la.php,v 1.2 2006/11/02 10:48:01 ah Exp $
rlm@3: 
rlm@3:         
rlm@3:         
rlm@3: class getid3_la extends getid3_handler
rlm@3: {
rlm@3: 
rlm@3:     public function Analyze() {
rlm@3:         
rlm@3:         $getid3 = $this->getid3;
rlm@3:         
rlm@3:         $getid3->include_module('audio-video.riff');
rlm@3:         
rlm@3:         fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
rlm@3:         $raw_data = fread($getid3->fp, getid3::FREAD_BUFFER_SIZE);
rlm@3:     
rlm@3:         $getid3->info['fileformat']          = 'la';
rlm@3:         $getid3->info['audio']['dataformat'] = 'la';
rlm@3:         $getid3->info['audio']['lossless']   = true;
rlm@3: 
rlm@3:         $getid3->info['la']['version_major'] = (int)$raw_data{2};
rlm@3:         $getid3->info['la']['version_minor'] = (int)$raw_data{3};
rlm@3:         $getid3->info['la']['version']       = (float)$getid3->info['la']['version_major'] + ($getid3->info['la']['version_minor'] / 10);
rlm@3: 
rlm@3:         $getid3->info['la']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($raw_data, 4, 4));
rlm@3:         
rlm@3:         $wave_chunk = substr($raw_data, 8, 4);
rlm@3:         if ($wave_chunk !== 'WAVE') {
rlm@3:             throw new getid3_exception('Expected "WAVE" ('.getid3_lib::PrintHexBytes('WAVE').') at offset 8, found "'.$wave_chunk.'" ('.getid3_lib::PrintHexBytes($wave_chunk).') instead.');
rlm@3:         }
rlm@3:         
rlm@3:         $offset = 12;
rlm@3: 
rlm@3:         $getid3->info['la']['fmt_size'] = 24;
rlm@3:         if ($getid3->info['la']['version'] >= 0.3) {
rlm@3: 
rlm@3:             $getid3->info['la']['fmt_size']    = getid3_lib::LittleEndian2Int(substr($raw_data, $offset, 4));
rlm@3:             $getid3->info['la']['header_size'] = 49 + $getid3->info['la']['fmt_size'] - 24;
rlm@3:             $offset += 4;
rlm@3: 
rlm@3:         } else {
rlm@3: 
rlm@3:             // version 0.2 didn't support additional data blocks
rlm@3:             $getid3->info['la']['header_size'] = 41;
rlm@3:         }
rlm@3: 
rlm@3:         $fmt_chunk = substr($raw_data, $offset, 4);
rlm@3:         if ($fmt_chunk !== 'fmt ') {
rlm@3:             throw new getid3_exception('Expected "fmt " ('.getid3_lib::PrintHexBytes('fmt ').') at offset '.$offset.', found "'.$fmt_chunk.'" ('.getid3_lib::PrintHexBytes($fmt_chunk).') instead.');
rlm@3:         }
rlm@3:         $offset += 4;
rlm@3:         
rlm@3:         $fmt_size = getid3_lib::LittleEndian2Int(substr($raw_data, $offset, 4));
rlm@3:         $offset += 4;
rlm@3: 
rlm@3:         $getid3->info['la']['raw']['format'] = getid3_lib::LittleEndian2Int(substr($raw_data, $offset, 2));
rlm@3:         $offset += 2;
rlm@3:         
rlm@3:         getid3_lib::ReadSequence('LittleEndian2Int', $getid3->info['la'], $raw_data, $offset,
rlm@3:             array (
rlm@3:                 'channels'         => 2,
rlm@3:                 'sample_rate'      => 4,
rlm@3:                 'bytes_per_second' => 4,
rlm@3:                 'bytes_per_sample' => 2,
rlm@3:                 'bits_per_sample'  => 2,
rlm@3:                 'samples'          => 4
rlm@3:             )
rlm@3:         );
rlm@3:         $offset += 18;
rlm@3:         
rlm@3:         $getid3->info['la']['raw']['flags'] = getid3_lib::LittleEndian2Int($raw_data{$offset++});
rlm@3:         
rlm@3:         $getid3->info['la']['flags']['seekable']             = (bool)($getid3->info['la']['raw']['flags'] & 0x01);
rlm@3:         if ($getid3->info['la']['version'] >= 0.4) {
rlm@3:             $getid3->info['la']['flags']['high_compression'] = (bool)($getid3->info['la']['raw']['flags'] & 0x02);
rlm@3:         }
rlm@3: 
rlm@3:         $getid3->info['la']['original_crc'] = getid3_lib::LittleEndian2Int(substr($raw_data, $offset, 4));
rlm@3:         $offset += 4;
rlm@3: 
rlm@3:         // mikeØbevin*de
rlm@3:         // Basically, the blocksize/seekevery are 61440/19 in La0.4 and 73728/16
rlm@3:         // in earlier versions. A seekpoint is added every blocksize * seekevery
rlm@3:         // samples, so 4 * int(totalSamples / (blockSize * seekEvery)) should
rlm@3:         // give the number of bytes used for the seekpoints. Of course, if seeking
rlm@3:         // is disabled, there are no seekpoints stored.
rlm@3:         
rlm@3:         if ($getid3->info['la']['version'] >= 0.4) {
rlm@3:             $getid3->info['la']['blocksize'] = 61440;
rlm@3:             $getid3->info['la']['seekevery'] = 19;
rlm@3:         } else {
rlm@3:             $getid3->info['la']['blocksize'] = 73728;
rlm@3:             $getid3->info['la']['seekevery'] = 16;
rlm@3:         }
rlm@3: 
rlm@3:         $getid3->info['la']['seekpoint_count'] = 0;
rlm@3:         if ($getid3->info['la']['flags']['seekable']) {
rlm@3:             $getid3->info['la']['seekpoint_count'] = floor($getid3->info['la']['samples'] / ($getid3->info['la']['blocksize'] * $getid3->info['la']['seekevery']));
rlm@3: 
rlm@3:             for ($i = 0; $i < $getid3->info['la']['seekpoint_count']; $i++) {
rlm@3:                 $getid3->info['la']['seekpoints'][] = getid3_lib::LittleEndian2Int(substr($raw_data, $offset, 4));
rlm@3:                 $offset += 4;
rlm@3:             }
rlm@3:         }
rlm@3: 
rlm@3:         if ($getid3->info['la']['version'] >= 0.3) {
rlm@3: 
rlm@3:             // Following the main header information, the program outputs all of the
rlm@3:             // seekpoints. Following these is what I called the 'footer start',
rlm@3:             // i.e. the position immediately after the La audio data is finished.
rlm@3:         
rlm@3:             $getid3->info['la']['footerstart'] = getid3_lib::LittleEndian2Int(substr($raw_data, $offset, 4));
rlm@3:             $offset += 4;
rlm@3: 
rlm@3:             if ($getid3->info['la']['footerstart'] > $getid3->info['filesize']) {
rlm@3:                 $getid3->warning('FooterStart value points to offset '.$getid3->info['la']['footerstart'].' which is beyond end-of-file ('.$getid3->info['filesize'].')');
rlm@3:                 $getid3->info['la']['footerstart'] = $getid3->info['filesize'];
rlm@3:             }
rlm@3: 
rlm@3:         } else {
rlm@3: 
rlm@3:             // La v0.2 didn't have FooterStart value
rlm@3:             $getid3->info['la']['footerstart'] = $getid3->info['avdataend'];
rlm@3: 
rlm@3:         }
rlm@3: 
rlm@3:         if ($getid3->info['la']['footerstart'] < $getid3->info['avdataend']) {
rlm@3:         
rlm@3:             // Create riff header
rlm@3:             $riff_data = 'WAVE';
rlm@3:             if ($getid3->info['la']['version'] == 0.2) {
rlm@3:                 $riff_data .= substr($raw_data, 12, 24);
rlm@3:             } else {
rlm@3:                 $riff_data .= substr($raw_data, 16, 24);
rlm@3:             }
rlm@3:             if ($getid3->info['la']['footerstart'] < $getid3->info['avdataend']) {
rlm@3:                 fseek($getid3->fp, $getid3->info['la']['footerstart'], SEEK_SET);
rlm@3:                 $riff_data .= fread($getid3->fp, $getid3->info['avdataend'] - $getid3->info['la']['footerstart']);
rlm@3:             }
rlm@3:             $riff_data = 'RIFF'.getid3_lib::LittleEndian2String(strlen($riff_data), 4, false).$riff_data;
rlm@3:             
rlm@3:             // Clone getid3 - messing with offsets - better safe than sorry
rlm@3:             $clone = clone $getid3;
rlm@3:             
rlm@3:             // Analyze clone by string
rlm@3:             $riff = new getid3_riff($clone);
rlm@3:             $riff->AnalyzeString($riff_data);
rlm@3:             
rlm@3:             // Import from clone and destroy
rlm@3:             $getid3->info['riff']   = $clone->info['riff'];
rlm@3:             $getid3->warnings($clone->warnings());
rlm@3:             unset($clone);
rlm@3:         }
rlm@3: 
rlm@3:         // $getid3->info['avdataoffset'] should be zero to begin with, but just in case it's not, include the addition anyway
rlm@3:         $getid3->info['avdataend']    = $getid3->info['avdataoffset'] + $getid3->info['la']['footerstart'];
rlm@3:         $getid3->info['avdataoffset'] = $getid3->info['avdataoffset'] + $offset;
rlm@3: 
rlm@3:         $getid3->info['la']['compression_ratio']  = (float)(($getid3->info['avdataend'] - $getid3->info['avdataoffset']) / $getid3->info['la']['uncompressed_size']);
rlm@3:         $getid3->info['playtime_seconds']         = (float)($getid3->info['la']['samples'] / $getid3->info['la']['sample_rate']) / $getid3->info['la']['channels'];
rlm@3: 
rlm@3:         $getid3->info['audio']['bitrate']         = ($getid3->info['avdataend'] - $getid3->info['avdataoffset']) * 8 / $getid3->info['playtime_seconds'];
rlm@3:         $getid3->info['audio']['bits_per_sample'] = $getid3->info['la']['bits_per_sample'];
rlm@3: 
rlm@3:         $getid3->info['audio']['channels']        = $getid3->info['la']['channels'];
rlm@3:         $getid3->info['audio']['sample_rate']     = (int)$getid3->info['la']['sample_rate'];
rlm@3:         $getid3->info['audio']['encoder']         = 'LA v'.$getid3->info['la']['version'];
rlm@3: 
rlm@3:         return true;
rlm@3:     }
rlm@3: 
rlm@3: }
rlm@3: 
rlm@3: 
rlm@3: ?>