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

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.monkey.php                                     //
0012 // module for analyzing Monkey's Audio files                   //
0013 // dependencies: NONE                                          //
0014 //                                                            ///
0015 /////////////////////////////////////////////////////////////////
0016 
0017 
0018 class getid3_monkey extends getid3_handler
0019 {
0020 
0021   public function Analyze() {
0022     $info = &$this->getid3->info;
0023 
0024     // based loosely on code from TMonkey by Jurgen Faul <jfaulØgmx*de>
0025     // http://jfaul.de/atl  or  http://j-faul.virtualave.net/atl/atl.html
0026 
0027     $info['fileformat']            = 'mac';
0028     $info['audio']['dataformat']   = 'mac';
0029     $info['audio']['bitrate_mode'] = 'vbr';
0030     $info['audio']['lossless']     = true;
0031 
0032     $info['monkeys_audio']['raw'] = array();
0033     $thisfile_monkeysaudio                = &$info['monkeys_audio'];
0034     $thisfile_monkeysaudio_raw            = &$thisfile_monkeysaudio['raw'];
0035 
0036     $this->fseek($info['avdataoffset']);
0037     $MACheaderData = $this->fread(74);
0038 
0039     $thisfile_monkeysaudio_raw['magic'] = substr($MACheaderData, 0, 4);
0040     $magic = 'MAC ';
0041     if ($thisfile_monkeysaudio_raw['magic'] != $magic) {
0042       $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($thisfile_monkeysaudio_raw['magic']).'"';
0043       unset($info['fileformat']);
0044       return false;
0045     }
0046     $thisfile_monkeysaudio_raw['nVersion']             = getid3_lib::LittleEndian2Int(substr($MACheaderData, 4, 2)); // appears to be uint32 in 3.98+
0047 
0048     if ($thisfile_monkeysaudio_raw['nVersion'] < 3980) {
0049       $thisfile_monkeysaudio_raw['nCompressionLevel']    = getid3_lib::LittleEndian2Int(substr($MACheaderData, 6, 2));
0050       $thisfile_monkeysaudio_raw['nFormatFlags']         = getid3_lib::LittleEndian2Int(substr($MACheaderData, 8, 2));
0051       $thisfile_monkeysaudio_raw['nChannels']            = getid3_lib::LittleEndian2Int(substr($MACheaderData, 10, 2));
0052       $thisfile_monkeysaudio_raw['nSampleRate']          = getid3_lib::LittleEndian2Int(substr($MACheaderData, 12, 4));
0053       $thisfile_monkeysaudio_raw['nHeaderDataBytes']     = getid3_lib::LittleEndian2Int(substr($MACheaderData, 16, 4));
0054       $thisfile_monkeysaudio_raw['nWAVTerminatingBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 20, 4));
0055       $thisfile_monkeysaudio_raw['nTotalFrames']         = getid3_lib::LittleEndian2Int(substr($MACheaderData, 24, 4));
0056       $thisfile_monkeysaudio_raw['nFinalFrameSamples']   = getid3_lib::LittleEndian2Int(substr($MACheaderData, 28, 4));
0057       $thisfile_monkeysaudio_raw['nPeakLevel']           = getid3_lib::LittleEndian2Int(substr($MACheaderData, 32, 4));
0058       $thisfile_monkeysaudio_raw['nSeekElements']        = getid3_lib::LittleEndian2Int(substr($MACheaderData, 38, 2));
0059       $offset = 8;
0060     } else {
0061       $offset = 8;
0062       // APE_DESCRIPTOR
0063       $thisfile_monkeysaudio_raw['nDescriptorBytes']       = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset,  4));
0064       $offset += 4;
0065       $thisfile_monkeysaudio_raw['nHeaderBytes']           = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset,  4));
0066       $offset += 4;
0067       $thisfile_monkeysaudio_raw['nSeekTableBytes']        = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset,  4));
0068       $offset += 4;
0069       $thisfile_monkeysaudio_raw['nHeaderDataBytes']       = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset,  4));
0070       $offset += 4;
0071       $thisfile_monkeysaudio_raw['nAPEFrameDataBytes']     = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset,  4));
0072       $offset += 4;
0073       $thisfile_monkeysaudio_raw['nAPEFrameDataBytesHigh'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset,  4));
0074       $offset += 4;
0075       $thisfile_monkeysaudio_raw['nTerminatingDataBytes']  = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset,  4));
0076       $offset += 4;
0077       $thisfile_monkeysaudio_raw['cFileMD5']               =                              substr($MACheaderData, $offset, 16);
0078       $offset += 16;
0079 
0080       // APE_HEADER
0081       $thisfile_monkeysaudio_raw['nCompressionLevel']    = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2));
0082       $offset += 2;
0083       $thisfile_monkeysaudio_raw['nFormatFlags']         = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2));
0084       $offset += 2;
0085       $thisfile_monkeysaudio_raw['nBlocksPerFrame']      = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
0086       $offset += 4;
0087       $thisfile_monkeysaudio_raw['nFinalFrameBlocks']    = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
0088       $offset += 4;
0089       $thisfile_monkeysaudio_raw['nTotalFrames']         = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
0090       $offset += 4;
0091       $thisfile_monkeysaudio_raw['nBitsPerSample']       = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2));
0092       $offset += 2;
0093       $thisfile_monkeysaudio_raw['nChannels']            = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2));
0094       $offset += 2;
0095       $thisfile_monkeysaudio_raw['nSampleRate']          = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
0096       $offset += 4;
0097     }
0098 
0099     $thisfile_monkeysaudio['flags']['8-bit']         = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0001);
0100     $thisfile_monkeysaudio['flags']['crc-32']        = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0002);
0101     $thisfile_monkeysaudio['flags']['peak_level']    = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0004);
0102     $thisfile_monkeysaudio['flags']['24-bit']        = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0008);
0103     $thisfile_monkeysaudio['flags']['seek_elements'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0010);
0104     $thisfile_monkeysaudio['flags']['no_wav_header'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0020);
0105     $thisfile_monkeysaudio['version']                = $thisfile_monkeysaudio_raw['nVersion'] / 1000;
0106     $thisfile_monkeysaudio['compression']            = $this->MonkeyCompressionLevelNameLookup($thisfile_monkeysaudio_raw['nCompressionLevel']);
0107     if ($thisfile_monkeysaudio_raw['nVersion'] < 3980) {
0108       $thisfile_monkeysaudio['samples_per_frame']      = $this->MonkeySamplesPerFrame($thisfile_monkeysaudio_raw['nVersion'], $thisfile_monkeysaudio_raw['nCompressionLevel']);
0109     }
0110     $thisfile_monkeysaudio['bits_per_sample']        = ($thisfile_monkeysaudio['flags']['24-bit'] ? 24 : ($thisfile_monkeysaudio['flags']['8-bit'] ? 8 : 16));
0111     $thisfile_monkeysaudio['channels']               = $thisfile_monkeysaudio_raw['nChannels'];
0112     $info['audio']['channels']               = $thisfile_monkeysaudio['channels'];
0113     $thisfile_monkeysaudio['sample_rate']            = $thisfile_monkeysaudio_raw['nSampleRate'];
0114     if ($thisfile_monkeysaudio['sample_rate'] == 0) {
0115       $info['error'][] = 'Corrupt MAC file: frequency == zero';
0116       return false;
0117     }
0118     $info['audio']['sample_rate']            = $thisfile_monkeysaudio['sample_rate'];
0119     if ($thisfile_monkeysaudio['flags']['peak_level']) {
0120       $thisfile_monkeysaudio['peak_level']         = $thisfile_monkeysaudio_raw['nPeakLevel'];
0121       $thisfile_monkeysaudio['peak_ratio']         = $thisfile_monkeysaudio['peak_level'] / pow(2, $thisfile_monkeysaudio['bits_per_sample'] - 1);
0122     }
0123     if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) {
0124       $thisfile_monkeysaudio['samples']            = (($thisfile_monkeysaudio_raw['nTotalFrames'] - 1) * $thisfile_monkeysaudio_raw['nBlocksPerFrame']) + $thisfile_monkeysaudio_raw['nFinalFrameBlocks'];
0125     } else {
0126       $thisfile_monkeysaudio['samples']            = (($thisfile_monkeysaudio_raw['nTotalFrames'] - 1) * $thisfile_monkeysaudio['samples_per_frame']) + $thisfile_monkeysaudio_raw['nFinalFrameSamples'];
0127     }
0128     $thisfile_monkeysaudio['playtime']               = $thisfile_monkeysaudio['samples'] / $thisfile_monkeysaudio['sample_rate'];
0129     if ($thisfile_monkeysaudio['playtime'] == 0) {
0130       $info['error'][] = 'Corrupt MAC file: playtime == zero';
0131       return false;
0132     }
0133     $info['playtime_seconds']                = $thisfile_monkeysaudio['playtime'];
0134     $thisfile_monkeysaudio['compressed_size']        = $info['avdataend'] - $info['avdataoffset'];
0135     $thisfile_monkeysaudio['uncompressed_size']      = $thisfile_monkeysaudio['samples'] * $thisfile_monkeysaudio['channels'] * ($thisfile_monkeysaudio['bits_per_sample'] / 8);
0136     if ($thisfile_monkeysaudio['uncompressed_size'] == 0) {
0137       $info['error'][] = 'Corrupt MAC file: uncompressed_size == zero';
0138       return false;
0139     }
0140     $thisfile_monkeysaudio['compression_ratio']      = $thisfile_monkeysaudio['compressed_size'] / ($thisfile_monkeysaudio['uncompressed_size'] + $thisfile_monkeysaudio_raw['nHeaderDataBytes']);
0141     $thisfile_monkeysaudio['bitrate']                = (($thisfile_monkeysaudio['samples'] * $thisfile_monkeysaudio['channels'] * $thisfile_monkeysaudio['bits_per_sample']) / $thisfile_monkeysaudio['playtime']) * $thisfile_monkeysaudio['compression_ratio'];
0142     $info['audio']['bitrate']                = $thisfile_monkeysaudio['bitrate'];
0143 
0144     // add size of MAC header to avdataoffset
0145     if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) {
0146       $info['avdataoffset'] += $thisfile_monkeysaudio_raw['nDescriptorBytes'];
0147       $info['avdataoffset'] += $thisfile_monkeysaudio_raw['nHeaderBytes'];
0148       $info['avdataoffset'] += $thisfile_monkeysaudio_raw['nSeekTableBytes'];
0149       $info['avdataoffset'] += $thisfile_monkeysaudio_raw['nHeaderDataBytes'];
0150 
0151       $info['avdataend'] -= $thisfile_monkeysaudio_raw['nTerminatingDataBytes'];
0152     } else {
0153       $info['avdataoffset'] += $offset;
0154     }
0155 
0156     if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) {
0157       if ($thisfile_monkeysaudio_raw['cFileMD5'] === str_repeat("\x00", 16)) {
0158         //$info['warning'][] = 'cFileMD5 is null';
0159       } else {
0160         $info['md5_data_source'] = '';
0161         $md5 = $thisfile_monkeysaudio_raw['cFileMD5'];
0162         for ($i = 0; $i < strlen($md5); $i++) {
0163           $info['md5_data_source'] .= str_pad(dechex(ord($md5{$i})), 2, '00', STR_PAD_LEFT);
0164         }
0165         if (!preg_match('/^[0-9a-f]{32}$/', $info['md5_data_source'])) {
0166           unset($info['md5_data_source']);
0167         }
0168       }
0169     }
0170 
0171 
0172 
0173     $info['audio']['bits_per_sample'] = $thisfile_monkeysaudio['bits_per_sample'];
0174     $info['audio']['encoder']         = 'MAC v'.number_format($thisfile_monkeysaudio['version'], 2);
0175     $info['audio']['encoder_options'] = ucfirst($thisfile_monkeysaudio['compression']).' compression';
0176 
0177     return true;
0178   }
0179 
0180   public function MonkeyCompressionLevelNameLookup($compressionlevel) {
0181     static $MonkeyCompressionLevelNameLookup = array(
0182       0     => 'unknown',
0183       1000  => 'fast',
0184       2000  => 'normal',
0185       3000  => 'high',
0186       4000  => 'extra-high',
0187       5000  => 'insane'
0188     );
0189     return (isset($MonkeyCompressionLevelNameLookup[$compressionlevel]) ? $MonkeyCompressionLevelNameLookup[$compressionlevel] : 'invalid');
0190   }
0191 
0192   public function MonkeySamplesPerFrame($versionid, $compressionlevel) {
0193     if ($versionid >= 3950) {
0194       return 73728 * 4;
0195     } elseif ($versionid >= 3900) {
0196       return 73728;
0197     } elseif (($versionid >= 3800) && ($compressionlevel == 4000)) {
0198       return 73728;
0199     } else {
0200       return 9216;
0201     }
0202   }
0203 
0204 }