File indexing completed on 2024-05-12 05:58:13

0001 <?php
0002 /////////////////////////////////////////////////////////////////
0003 /// getID3() by James Heinrich <info@getid3.org>               //
0004 //  available at http://getid3.sourceforge.net                 //
0005 //            or http://www.getid3.org                         //
0006 //          also https://github.com/JamesHeinrich/getID3       //
0007 /////////////////////////////////////////////////////////////////
0008 // See readme.txt for more details                             //
0009 /////////////////////////////////////////////////////////////////
0010 //                                                             //
0011 // module.audio.lpac.php                                       //
0012 // module for analyzing LPAC Audio files                       //
0013 // dependencies: module.audio-video.riff.php                   //
0014 //                                                            ///
0015 /////////////////////////////////////////////////////////////////
0016 
0017 getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
0018 
0019 class getid3_lpac extends getid3_handler
0020 {
0021 
0022   public function Analyze() {
0023     $info = &$this->getid3->info;
0024 
0025     $this->fseek($info['avdataoffset']);
0026     $LPACheader = $this->fread(14);
0027     if (substr($LPACheader, 0, 4) != 'LPAC') {
0028       $info['error'][] = 'Expected "LPAC" at offset '.$info['avdataoffset'].', found "'.$StreamMarker.'"';
0029       return false;
0030     }
0031     $info['avdataoffset'] += 14;
0032 
0033     $info['fileformat']            = 'lpac';
0034     $info['audio']['dataformat']   = 'lpac';
0035     $info['audio']['lossless']     = true;
0036     $info['audio']['bitrate_mode'] = 'vbr';
0037 
0038     $info['lpac']['file_version'] = getid3_lib::BigEndian2Int(substr($LPACheader,  4, 1));
0039     $flags['audio_type']                  = getid3_lib::BigEndian2Int(substr($LPACheader,  5, 1));
0040     $info['lpac']['total_samples']= getid3_lib::BigEndian2Int(substr($LPACheader,  6, 4));
0041     $flags['parameters']                  = getid3_lib::BigEndian2Int(substr($LPACheader, 10, 4));
0042 
0043     $info['lpac']['flags']['is_wave'] = (bool) ($flags['audio_type'] & 0x40);
0044     $info['lpac']['flags']['stereo']  = (bool) ($flags['audio_type'] & 0x04);
0045     $info['lpac']['flags']['24_bit']  = (bool) ($flags['audio_type'] & 0x02);
0046     $info['lpac']['flags']['16_bit']  = (bool) ($flags['audio_type'] & 0x01);
0047 
0048     if ($info['lpac']['flags']['24_bit'] && $info['lpac']['flags']['16_bit']) {
0049       $info['warning'][] = '24-bit and 16-bit flags cannot both be set';
0050     }
0051 
0052     $info['lpac']['flags']['fast_compress']             =  (bool) ($flags['parameters'] & 0x40000000);
0053     $info['lpac']['flags']['random_access']             =  (bool) ($flags['parameters'] & 0x08000000);
0054     $info['lpac']['block_length']                       = pow(2, (($flags['parameters'] & 0x07000000) >> 24)) * 256;
0055     $info['lpac']['flags']['adaptive_prediction_order'] =  (bool) ($flags['parameters'] & 0x00800000);
0056     $info['lpac']['flags']['adaptive_quantization']     =  (bool) ($flags['parameters'] & 0x00400000);
0057     $info['lpac']['flags']['joint_stereo']              =  (bool) ($flags['parameters'] & 0x00040000);
0058     $info['lpac']['quantization']                       =         ($flags['parameters'] & 0x00001F00) >> 8;
0059     $info['lpac']['max_prediction_order']               =         ($flags['parameters'] & 0x0000003F);
0060 
0061     if ($info['lpac']['flags']['fast_compress'] && ($info['lpac']['max_prediction_order'] != 3)) {
0062       $info['warning'][] = 'max_prediction_order expected to be "3" if fast_compress is true, actual value is "'.$info['lpac']['max_prediction_order'].'"';
0063     }
0064     switch ($info['lpac']['file_version']) {
0065       case 6:
0066         if ($info['lpac']['flags']['adaptive_quantization']) {
0067           $info['warning'][] = 'adaptive_quantization expected to be false in LPAC file stucture v6, actually true';
0068         }
0069         if ($info['lpac']['quantization'] != 20) {
0070           $info['warning'][] = 'Quantization expected to be 20 in LPAC file stucture v6, actually '.$info['lpac']['flags']['Q'];
0071         }
0072         break;
0073 
0074       default:
0075         //$info['warning'][] = 'This version of getID3() ['.$this->getid3->version().'] only supports LPAC file format version 6, this file is version '.$info['lpac']['file_version'].' - please report to info@getid3.org';
0076         break;
0077     }
0078 
0079     $getid3_temp = new getID3();
0080     $getid3_temp->openfile($this->getid3->filename);
0081     $getid3_temp->info = $info;
0082     $getid3_riff = new getid3_riff($getid3_temp);
0083     $getid3_riff->Analyze();
0084     $info['avdataoffset']                = $getid3_temp->info['avdataoffset'];
0085     $info['riff']                        = $getid3_temp->info['riff'];
0086     $info['error']                       = $getid3_temp->info['error'];
0087     $info['warning']                     = $getid3_temp->info['warning'];
0088     $info['lpac']['comments']['comment'] = $getid3_temp->info['comments'];
0089     $info['audio']['sample_rate']        = $getid3_temp->info['audio']['sample_rate'];
0090     unset($getid3_temp, $getid3_riff);
0091 
0092     $info['audio']['channels']    = ($info['lpac']['flags']['stereo'] ? 2 : 1);
0093 
0094     if ($info['lpac']['flags']['24_bit']) {
0095       $info['audio']['bits_per_sample'] = $info['riff']['audio'][0]['bits_per_sample'];
0096     } elseif ($info['lpac']['flags']['16_bit']) {
0097       $info['audio']['bits_per_sample'] = 16;
0098     } else {
0099       $info['audio']['bits_per_sample'] = 8;
0100     }
0101 
0102     if ($info['lpac']['flags']['fast_compress']) {
0103        // fast
0104       $info['audio']['encoder_options'] = '-1';
0105     } else {
0106       switch ($info['lpac']['max_prediction_order']) {
0107         case 20: // simple
0108           $info['audio']['encoder_options'] = '-2';
0109           break;
0110         case 30: // medium
0111           $info['audio']['encoder_options'] = '-3';
0112           break;
0113         case 40: // high
0114           $info['audio']['encoder_options'] = '-4';
0115           break;
0116         case 60: // extrahigh
0117           $info['audio']['encoder_options'] = '-5';
0118           break;
0119       }
0120     }
0121 
0122     $info['playtime_seconds'] = $info['lpac']['total_samples'] / $info['audio']['sample_rate'];
0123     $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
0124 
0125     return true;
0126   }
0127 
0128 }