Annotation of /trunk/lib/getid3/module.audio.monkey.php
Parent Directory
|
Revision Log
Revision 2 - (view) (download)
| 1 : | andphe | 2 | <?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.monkey.php // | ||
| 11 : | // module for analyzing Monkey's Audio files // | ||
| 12 : | // dependencies: NONE // | ||
| 13 : | // /// | ||
| 14 : | ///////////////////////////////////////////////////////////////// | ||
| 15 : | // MOS Intruder Alerts | ||
| 16 : | defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); | ||
| 17 : | |||
| 18 : | class getid3_monkey | ||
| 19 : | { | ||
| 20 : | |||
| 21 : | function getid3_monkey(&$fd, &$ThisFileInfo) { | ||
| 22 : | // based loosely on code from TMonkey by Jurgen Faul <jfaul�gmx*de> | ||
| 23 : | // http://jfaul.de/atl or http://j-faul.virtualave.net/atl/atl.html | ||
| 24 : | |||
| 25 : | $ThisFileInfo['fileformat'] = 'mac'; | ||
| 26 : | $ThisFileInfo['audio']['dataformat'] = 'mac'; | ||
| 27 : | $ThisFileInfo['audio']['bitrate_mode'] = 'vbr'; | ||
| 28 : | $ThisFileInfo['audio']['lossless'] = true; | ||
| 29 : | |||
| 30 : | $ThisFileInfo['monkeys_audio']['raw'] = array(); | ||
| 31 : | $thisfile_monkeysaudio = &$ThisFileInfo['monkeys_audio']; | ||
| 32 : | $thisfile_monkeysaudio_raw = &$thisfile_monkeysaudio['raw']; | ||
| 33 : | |||
| 34 : | fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET); | ||
| 35 : | $MACheaderData = fread($fd, 74); | ||
| 36 : | |||
| 37 : | $thisfile_monkeysaudio_raw['magic'] = substr($MACheaderData, 0, 4); | ||
| 38 : | if ($thisfile_monkeysaudio_raw['magic'] != 'MAC ') { | ||
| 39 : | $ThisFileInfo['error'][] = 'Expecting "MAC" at offset '.$ThisFileInfo['avdataoffset'].', found "'.$thisfile_monkeysaudio_raw['magic'].'"'; | ||
| 40 : | unset($ThisFileInfo['fileformat']); | ||
| 41 : | return false; | ||
| 42 : | } | ||
| 43 : | $thisfile_monkeysaudio_raw['nVersion'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 4, 2)); // appears to be uint32 in 3.98+ | ||
| 44 : | |||
| 45 : | if ($thisfile_monkeysaudio_raw['nVersion'] < 3980) { | ||
| 46 : | $thisfile_monkeysaudio_raw['nCompressionLevel'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 6, 2)); | ||
| 47 : | $thisfile_monkeysaudio_raw['nFormatFlags'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 8, 2)); | ||
| 48 : | $thisfile_monkeysaudio_raw['nChannels'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 10, 2)); | ||
| 49 : | $thisfile_monkeysaudio_raw['nSampleRate'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 12, 4)); | ||
| 50 : | $thisfile_monkeysaudio_raw['nHeaderDataBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 16, 4)); | ||
| 51 : | $thisfile_monkeysaudio_raw['nWAVTerminatingBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 20, 4)); | ||
| 52 : | $thisfile_monkeysaudio_raw['nTotalFrames'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 24, 4)); | ||
| 53 : | $thisfile_monkeysaudio_raw['nFinalFrameSamples'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 28, 4)); | ||
| 54 : | $thisfile_monkeysaudio_raw['nPeakLevel'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 32, 4)); | ||
| 55 : | $thisfile_monkeysaudio_raw['nSeekElements'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 38, 2)); | ||
| 56 : | $offset = 8; | ||
| 57 : | } else { | ||
| 58 : | $offset = 8; | ||
| 59 : | // APE_DESCRIPTOR | ||
| 60 : | $thisfile_monkeysaudio_raw['nDescriptorBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4)); | ||
| 61 : | $offset += 4; | ||
| 62 : | $thisfile_monkeysaudio_raw['nHeaderBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4)); | ||
| 63 : | $offset += 4; | ||
| 64 : | $thisfile_monkeysaudio_raw['nSeekTableBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4)); | ||
| 65 : | $offset += 4; | ||
| 66 : | $thisfile_monkeysaudio_raw['nHeaderDataBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4)); | ||
| 67 : | $offset += 4; | ||
| 68 : | $thisfile_monkeysaudio_raw['nAPEFrameDataBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4)); | ||
| 69 : | $offset += 4; | ||
| 70 : | $thisfile_monkeysaudio_raw['nAPEFrameDataBytesHigh'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4)); | ||
| 71 : | $offset += 4; | ||
| 72 : | $thisfile_monkeysaudio_raw['nTerminatingDataBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4)); | ||
| 73 : | $offset += 4; | ||
| 74 : | $thisfile_monkeysaudio_raw['cFileMD5'] = substr($MACheaderData, $offset, 16); | ||
| 75 : | $offset += 16; | ||
| 76 : | |||
| 77 : | // APE_HEADER | ||
| 78 : | $thisfile_monkeysaudio_raw['nCompressionLevel'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2)); | ||
| 79 : | $offset += 2; | ||
| 80 : | $thisfile_monkeysaudio_raw['nFormatFlags'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2)); | ||
| 81 : | $offset += 2; | ||
| 82 : | $thisfile_monkeysaudio_raw['nBlocksPerFrame'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4)); | ||
| 83 : | $offset += 4; | ||
| 84 : | $thisfile_monkeysaudio_raw['nFinalFrameBlocks'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4)); | ||
| 85 : | $offset += 4; | ||
| 86 : | $thisfile_monkeysaudio_raw['nTotalFrames'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4)); | ||
| 87 : | $offset += 4; | ||
| 88 : | $thisfile_monkeysaudio_raw['nBitsPerSample'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2)); | ||
| 89 : | $offset += 2; | ||
| 90 : | $thisfile_monkeysaudio_raw['nChannels'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2)); | ||
| 91 : | $offset += 2; | ||
| 92 : | $thisfile_monkeysaudio_raw['nSampleRate'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4)); | ||
| 93 : | $offset += 4; | ||
| 94 : | } | ||
| 95 : | |||
| 96 : | $thisfile_monkeysaudio['flags']['8-bit'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0001); | ||
| 97 : | $thisfile_monkeysaudio['flags']['crc-32'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0002); | ||
| 98 : | $thisfile_monkeysaudio['flags']['peak_level'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0004); | ||
| 99 : | $thisfile_monkeysaudio['flags']['24-bit'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0008); | ||
| 100 : | $thisfile_monkeysaudio['flags']['seek_elements'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0010); | ||
| 101 : | $thisfile_monkeysaudio['flags']['no_wav_header'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0020); | ||
| 102 : | $thisfile_monkeysaudio['version'] = $thisfile_monkeysaudio_raw['nVersion'] / 1000; | ||
| 103 : | $thisfile_monkeysaudio['compression'] = $this->MonkeyCompressionLevelNameLookup($thisfile_monkeysaudio_raw['nCompressionLevel']); | ||
| 104 : | if ($thisfile_monkeysaudio_raw['nVersion'] < 3980) { | ||
| 105 : | $thisfile_monkeysaudio['samples_per_frame'] = $this->MonkeySamplesPerFrame($thisfile_monkeysaudio_raw['nVersion'], $thisfile_monkeysaudio_raw['nCompressionLevel']); | ||
| 106 : | } | ||
| 107 : | $thisfile_monkeysaudio['bits_per_sample'] = ($thisfile_monkeysaudio['flags']['24-bit'] ? 24 : ($thisfile_monkeysaudio['flags']['8-bit'] ? 8 : 16)); | ||
| 108 : | $thisfile_monkeysaudio['channels'] = $thisfile_monkeysaudio_raw['nChannels']; | ||
| 109 : | $ThisFileInfo['audio']['channels'] = $thisfile_monkeysaudio['channels']; | ||
| 110 : | $thisfile_monkeysaudio['sample_rate'] = $thisfile_monkeysaudio_raw['nSampleRate']; | ||
| 111 : | if ($thisfile_monkeysaudio['sample_rate'] == 0) { | ||
| 112 : | $ThisFileInfo['error'][] = 'Corrupt MAC file: frequency == zero'; | ||
| 113 : | return false; | ||
| 114 : | } | ||
| 115 : | $ThisFileInfo['audio']['sample_rate'] = $thisfile_monkeysaudio['sample_rate']; | ||
| 116 : | if ($thisfile_monkeysaudio['flags']['peak_level']) { | ||
| 117 : | $thisfile_monkeysaudio['peak_level'] = $thisfile_monkeysaudio_raw['nPeakLevel']; | ||
| 118 : | $thisfile_monkeysaudio['peak_ratio'] = $thisfile_monkeysaudio['peak_level'] / pow(2, $thisfile_monkeysaudio['bits_per_sample'] - 1); | ||
| 119 : | } | ||
| 120 : | if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) { | ||
| 121 : | $thisfile_monkeysaudio['samples'] = (($thisfile_monkeysaudio_raw['nTotalFrames'] - 1) * $thisfile_monkeysaudio_raw['nBlocksPerFrame']) + $thisfile_monkeysaudio_raw['nFinalFrameBlocks']; | ||
| 122 : | } else { | ||
| 123 : | $thisfile_monkeysaudio['samples'] = (($thisfile_monkeysaudio_raw['nTotalFrames'] - 1) * $thisfile_monkeysaudio['samples_per_frame']) + $thisfile_monkeysaudio_raw['nFinalFrameSamples']; | ||
| 124 : | } | ||
| 125 : | $thisfile_monkeysaudio['playtime'] = $thisfile_monkeysaudio['samples'] / $thisfile_monkeysaudio['sample_rate']; | ||
| 126 : | if ($thisfile_monkeysaudio['playtime'] == 0) { | ||
| 127 : | $ThisFileInfo['error'][] = 'Corrupt MAC file: playtime == zero'; | ||
| 128 : | return false; | ||
| 129 : | } | ||
| 130 : | $ThisFileInfo['playtime_seconds'] = $thisfile_monkeysaudio['playtime']; | ||
| 131 : | $thisfile_monkeysaudio['compressed_size'] = $ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']; | ||
| 132 : | $thisfile_monkeysaudio['uncompressed_size'] = $thisfile_monkeysaudio['samples'] * $thisfile_monkeysaudio['channels'] * ($thisfile_monkeysaudio['bits_per_sample'] / 8); | ||
| 133 : | if ($thisfile_monkeysaudio['uncompressed_size'] == 0) { | ||
| 134 : | $ThisFileInfo['error'][] = 'Corrupt MAC file: uncompressed_size == zero'; | ||
| 135 : | return false; | ||
| 136 : | } | ||
| 137 : | $thisfile_monkeysaudio['compression_ratio'] = $thisfile_monkeysaudio['compressed_size'] / ($thisfile_monkeysaudio['uncompressed_size'] + $thisfile_monkeysaudio_raw['nHeaderDataBytes']); | ||
| 138 : | $thisfile_monkeysaudio['bitrate'] = (($thisfile_monkeysaudio['samples'] * $thisfile_monkeysaudio['channels'] * $thisfile_monkeysaudio['bits_per_sample']) / $thisfile_monkeysaudio['playtime']) * $thisfile_monkeysaudio['compression_ratio']; | ||
| 139 : | $ThisFileInfo['audio']['bitrate'] = $thisfile_monkeysaudio['bitrate']; | ||
| 140 : | |||
| 141 : | // add size of MAC header to avdataoffset | ||
| 142 : | if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) { | ||
| 143 : | $ThisFileInfo['avdataoffset'] += $thisfile_monkeysaudio_raw['nDescriptorBytes']; | ||
| 144 : | $ThisFileInfo['avdataoffset'] += $thisfile_monkeysaudio_raw['nHeaderBytes']; | ||
| 145 : | $ThisFileInfo['avdataoffset'] += $thisfile_monkeysaudio_raw['nSeekTableBytes']; | ||
| 146 : | $ThisFileInfo['avdataoffset'] += $thisfile_monkeysaudio_raw['nHeaderDataBytes']; | ||
| 147 : | |||
| 148 : | $ThisFileInfo['avdataend'] -= $thisfile_monkeysaudio_raw['nTerminatingDataBytes']; | ||
| 149 : | } else { | ||
| 150 : | $ThisFileInfo['avdataoffset'] += $offset; | ||
| 151 : | } | ||
| 152 : | |||
| 153 : | if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) { | ||
| 154 : | if ($thisfile_monkeysaudio_raw['cFileMD5'] === str_repeat("\x00", 16)) { | ||
| 155 : | //$ThisFileInfo['warning'][] = 'cFileMD5 is null'; | ||
| 156 : | } else { | ||
| 157 : | $ThisFileInfo['md5_data_source'] = ''; | ||
| 158 : | $md5 = $thisfile_monkeysaudio_raw['cFileMD5']; | ||
| 159 : | for ($i = 0; $i < strlen($md5); $i++) { | ||
| 160 : | $ThisFileInfo['md5_data_source'] .= str_pad(dechex(ord($md5{$i})), 2, '00', STR_PAD_LEFT); | ||
| 161 : | } | ||
| 162 : | if (!preg_match('/^[0-9a-f]{32}$/', $ThisFileInfo['md5_data_source'])) { | ||
| 163 : | unset($ThisFileInfo['md5_data_source']); | ||
| 164 : | } | ||
| 165 : | } | ||
| 166 : | } | ||
| 167 : | |||
| 168 : | |||
| 169 : | |||
| 170 : | $ThisFileInfo['audio']['bits_per_sample'] = $thisfile_monkeysaudio['bits_per_sample']; | ||
| 171 : | $ThisFileInfo['audio']['encoder'] = 'MAC v'.number_format($thisfile_monkeysaudio['version'], 2); | ||
| 172 : | $ThisFileInfo['audio']['encoder_options'] = ucfirst($thisfile_monkeysaudio['compression']).' compression'; | ||
| 173 : | |||
| 174 : | return true; | ||
| 175 : | } | ||
| 176 : | |||
| 177 : | function MonkeyCompressionLevelNameLookup($compressionlevel) { | ||
| 178 : | static $MonkeyCompressionLevelNameLookup = array( | ||
| 179 : | 0 => 'unknown', | ||
| 180 : | 1000 => 'fast', | ||
| 181 : | 2000 => 'normal', | ||
| 182 : | 3000 => 'high', | ||
| 183 : | 4000 => 'extra-high', | ||
| 184 : | 5000 => 'insane' | ||
| 185 : | ); | ||
| 186 : | return (isset($MonkeyCompressionLevelNameLookup[$compressionlevel]) ? $MonkeyCompressionLevelNameLookup[$compressionlevel] : 'invalid'); | ||
| 187 : | } | ||
| 188 : | |||
| 189 : | function MonkeySamplesPerFrame($versionid, $compressionlevel) { | ||
| 190 : | if ($versionid >= 3950) { | ||
| 191 : | return 73728 * 4; | ||
| 192 : | } elseif ($versionid >= 3900) { | ||
| 193 : | return 73728; | ||
| 194 : | } elseif (($versionid >= 3800) && ($compressionlevel == 4000)) { | ||
| 195 : | return 73728; | ||
| 196 : | } else { | ||
| 197 : | return 9216; | ||
| 198 : | } | ||
| 199 : | } | ||
| 200 : | |||
| 201 : | } | ||
| 202 : | |||
| 203 : | ?> |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |
Web Hosting provided by Network Redux.

