File indexing completed on 2024-12-22 05:33: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.shorten.php                                    //
0012 // module for analyzing Shorten Audio files                    //
0013 // dependencies: NONE                                          //
0014 //                                                            ///
0015 /////////////////////////////////////////////////////////////////
0016 
0017 
0018 class getid3_rkau extends getid3_handler
0019 {
0020 
0021   public function Analyze() {
0022     $info = &$this->getid3->info;
0023 
0024     $this->fseek($info['avdataoffset']);
0025     $RKAUHeader = $this->fread(20);
0026     $magic = 'RKA';
0027     if (substr($RKAUHeader, 0, 3) != $magic) {
0028       $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($RKAUHeader, 0, 3)).'"';
0029       return false;
0030     }
0031 
0032     $info['fileformat']            = 'rkau';
0033     $info['audio']['dataformat']   = 'rkau';
0034     $info['audio']['bitrate_mode'] = 'vbr';
0035 
0036     $info['rkau']['raw']['version']   = getid3_lib::LittleEndian2Int(substr($RKAUHeader, 3, 1));
0037     $info['rkau']['version']          = '1.'.str_pad($info['rkau']['raw']['version'] & 0x0F, 2, '0', STR_PAD_LEFT);
0038     if (($info['rkau']['version'] > 1.07) || ($info['rkau']['version'] < 1.06)) {
0039       $info['error'][] = 'This version of getID3() ['.$this->getid3->version().'] can only parse RKAU files v1.06 and 1.07 (this file is v'.$info['rkau']['version'].')';
0040       unset($info['rkau']);
0041       return false;
0042     }
0043 
0044     $info['rkau']['source_bytes']     = getid3_lib::LittleEndian2Int(substr($RKAUHeader,  4, 4));
0045     $info['rkau']['sample_rate']      = getid3_lib::LittleEndian2Int(substr($RKAUHeader,  8, 4));
0046     $info['rkau']['channels']         = getid3_lib::LittleEndian2Int(substr($RKAUHeader, 12, 1));
0047     $info['rkau']['bits_per_sample']  = getid3_lib::LittleEndian2Int(substr($RKAUHeader, 13, 1));
0048 
0049     $info['rkau']['raw']['quality']   = getid3_lib::LittleEndian2Int(substr($RKAUHeader, 14, 1));
0050     $this->RKAUqualityLookup($info['rkau']);
0051 
0052     $info['rkau']['raw']['flags']            = getid3_lib::LittleEndian2Int(substr($RKAUHeader, 15, 1));
0053     $info['rkau']['flags']['joint_stereo']   = (bool) (!($info['rkau']['raw']['flags'] & 0x01));
0054     $info['rkau']['flags']['streaming']      =  (bool)  ($info['rkau']['raw']['flags'] & 0x02);
0055     $info['rkau']['flags']['vrq_lossy_mode'] =  (bool)  ($info['rkau']['raw']['flags'] & 0x04);
0056 
0057     if ($info['rkau']['flags']['streaming']) {
0058       $info['avdataoffset'] += 20;
0059       $info['rkau']['compressed_bytes']  = getid3_lib::LittleEndian2Int(substr($RKAUHeader, 16, 4));
0060     } else {
0061       $info['avdataoffset'] += 16;
0062       $info['rkau']['compressed_bytes'] = $info['avdataend'] - $info['avdataoffset'] - 1;
0063     }
0064     // Note: compressed_bytes does not always equal what appears to be the actual number of compressed bytes,
0065     // sometimes it's more, sometimes less. No idea why(?)
0066 
0067     $info['audio']['lossless']        = $info['rkau']['lossless'];
0068     $info['audio']['channels']        = $info['rkau']['channels'];
0069     $info['audio']['bits_per_sample'] = $info['rkau']['bits_per_sample'];
0070     $info['audio']['sample_rate']     = $info['rkau']['sample_rate'];
0071 
0072     $info['playtime_seconds']         = $info['rkau']['source_bytes'] / ($info['rkau']['sample_rate'] * $info['rkau']['channels'] * ($info['rkau']['bits_per_sample'] / 8));
0073     $info['audio']['bitrate']         = ($info['rkau']['compressed_bytes'] * 8) / $info['playtime_seconds'];
0074 
0075     return true;
0076 
0077   }
0078 
0079 
0080   public function RKAUqualityLookup(&$RKAUdata) {
0081     $level   = ($RKAUdata['raw']['quality'] & 0xF0) >> 4;
0082     $quality =  $RKAUdata['raw']['quality'] & 0x0F;
0083 
0084     $RKAUdata['lossless']          = (($quality == 0) ? true : false);
0085     $RKAUdata['compression_level'] = $level + 1;
0086     if (!$RKAUdata['lossless']) {
0087       $RKAUdata['quality_setting'] = $quality;
0088     }
0089 
0090     return true;
0091   }
0092 
0093 }