view e2gallerypro/e2upload/Backend/Assets/getid3/module.audio.monkey.php @ 11:ed6ee381b8fd judyates

[svn r12] compressed photos and assembled them into a working web gallery
author rlm
date Mon, 12 Apr 2010 02:31:43 -0400
parents 3f6b44aa6b35
children
line wrap: on
line source
1 <?php
2 // +----------------------------------------------------------------------+
3 // | PHP version 5 |
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 2 of the GPL license, |
8 // | that is bundled with this package in the file license.txt and is |
9 // | available through the world-wide-web at the following url: |
10 // | http://www.gnu.org/copyleft/gpl.html |
11 // +----------------------------------------------------------------------+
12 // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
13 // +----------------------------------------------------------------------+
14 // | Authors: James Heinrich <infoØgetid3*org> |
15 // | Allan Hansen <ahØartemis*dk> |
16 // +----------------------------------------------------------------------+
17 // | module.audio.monkey.php |
18 // | Module for analyzing Monkey's Audio files |
19 // | dependencies: NONE |
20 // +----------------------------------------------------------------------+
21 //
22 // $Id: module.audio.monkey.php,v 1.2 2006/11/02 10:48:01 ah Exp $
26 class getid3_monkey extends getid3_handler
27 {
29 public function Analyze() {
31 $getid3 = $this->getid3;
33 // based loosely on code from TMonkey by Jurgen Faul <jfaulØgmx*de>
34 // http://jfaul.de/atl or http://j-faul.virtualave.net/atl/atl.html
36 $getid3->info['fileformat'] = 'mac';
37 $getid3->info['audio']['dataformat'] = 'mac';
38 $getid3->info['audio']['bitrate_mode'] = 'vbr';
39 $getid3->info['audio']['lossless'] = true;
41 $getid3->info['monkeys_audio']['raw'] = array ();
42 $info_monkeys_audio = &$getid3->info['monkeys_audio'];
43 $info_monkeys_audio_raw = &$info_monkeys_audio['raw'];
45 // Read file header
46 fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
47 $mac_header_data = fread($getid3->fp, 74);
49 $info_monkeys_audio_raw['magic'] = 'MAC '; // Magic bytes
51 // Read MAC version
52 $info_monkeys_audio_raw['nVersion'] = getid3_lib::LittleEndian2Int(substr($mac_header_data, 4, 2)); // appears to be uint32 in 3.98+
54 // Parse MAC Header < v3980
55 if ($info_monkeys_audio_raw['nVersion'] < 3980) {
57 getid3_lib::ReadSequence("LittleEndian2Int", $info_monkeys_audio_raw, $mac_header_data, 6,
58 array (
59 'nCompressionLevel' => 2,
60 'nFormatFlags' => 2,
61 'nChannels' => 2,
62 'nSampleRate' => 4,
63 'nHeaderDataBytes' => 4,
64 'nWAVTerminatingBytes' => 4,
65 'nTotalFrames' => 4,
66 'nFinalFrameSamples' => 4,
67 'nPeakLevel' => 4,
68 'IGNORE-1' => 2,
69 'nSeekElements' => 2
70 )
71 );
72 }
74 // Parse MAC Header >= v3980
75 else {
77 getid3_lib::ReadSequence("LittleEndian2Int", $info_monkeys_audio_raw, $mac_header_data, 8,
78 array (
79 // APE_DESCRIPTOR
80 'nDescriptorBytes' => 4,
81 'nHeaderBytes' => 4,
82 'nSeekTableBytes' => 4,
83 'nHeaderDataBytes' => 4,
84 'nAPEFrameDataBytes' => 4,
85 'nAPEFrameDataBytesHigh'=> 4,
86 'nTerminatingDataBytes' => 4,
88 // MD5 - string
89 'cFileMD5' => -16,
91 // APE_HEADER
92 'nCompressionLevel' => 2,
93 'nFormatFlags' => 2,
94 'nBlocksPerFrame' => 4,
95 'nFinalFrameBlocks' => 4,
96 'nTotalFrames' => 4,
97 'nBitsPerSample' => 2,
98 'nChannels' => 2,
99 'nSampleRate' => 4
100 )
101 );
102 }
104 // Process data
105 $info_monkeys_audio['flags']['8-bit'] = (bool)($info_monkeys_audio_raw['nFormatFlags'] & 0x0001);
106 $info_monkeys_audio['flags']['crc-32'] = (bool)($info_monkeys_audio_raw['nFormatFlags'] & 0x0002);
107 $info_monkeys_audio['flags']['peak_level'] = (bool)($info_monkeys_audio_raw['nFormatFlags'] & 0x0004);
108 $info_monkeys_audio['flags']['24-bit'] = (bool)($info_monkeys_audio_raw['nFormatFlags'] & 0x0008);
109 $info_monkeys_audio['flags']['seek_elements'] = (bool)($info_monkeys_audio_raw['nFormatFlags'] & 0x0010);
110 $info_monkeys_audio['flags']['no_wav_header'] = (bool)($info_monkeys_audio_raw['nFormatFlags'] & 0x0020);
112 $info_monkeys_audio['version'] = $info_monkeys_audio_raw['nVersion'] / 1000;
114 $info_monkeys_audio['compression'] = getid3_monkey::MonkeyCompressionLevelNameLookup($info_monkeys_audio_raw['nCompressionLevel']);
116 $info_monkeys_audio['bits_per_sample'] = ($info_monkeys_audio['flags']['24-bit'] ? 24 : ($info_monkeys_audio['flags']['8-bit'] ? 8 : 16));
118 $info_monkeys_audio['channels'] = $info_monkeys_audio_raw['nChannels'];
120 $getid3->info['audio']['channels'] = $info_monkeys_audio['channels'];
122 $info_monkeys_audio['sample_rate'] = $info_monkeys_audio_raw['nSampleRate'];
124 $getid3->info['audio']['sample_rate'] = $info_monkeys_audio['sample_rate'];
126 if ($info_monkeys_audio['flags']['peak_level']) {
127 $info_monkeys_audio['peak_level'] = $info_monkeys_audio_raw['nPeakLevel'];
128 $info_monkeys_audio['peak_ratio'] = $info_monkeys_audio['peak_level'] / pow(2, $info_monkeys_audio['bits_per_sample'] - 1);
129 }
131 // MAC >= v3980
132 if ($info_monkeys_audio_raw['nVersion'] >= 3980) {
133 $info_monkeys_audio['samples'] = (($info_monkeys_audio_raw['nTotalFrames'] - 1) * $info_monkeys_audio_raw['nBlocksPerFrame']) + $info_monkeys_audio_raw['nFinalFrameBlocks'];
134 }
136 // MAC < v3980
137 else {
138 $info_monkeys_audio['samples_per_frame'] = getid3_monkey::MonkeySamplesPerFrame($info_monkeys_audio_raw['nVersion'], $info_monkeys_audio_raw['nCompressionLevel']);
139 $info_monkeys_audio['samples'] = (($info_monkeys_audio_raw['nTotalFrames'] - 1) * $info_monkeys_audio['samples_per_frame']) + $info_monkeys_audio_raw['nFinalFrameSamples'];
140 }
142 $info_monkeys_audio['playtime'] = $info_monkeys_audio['samples'] / $info_monkeys_audio['sample_rate'];
144 $getid3->info['playtime_seconds'] = $info_monkeys_audio['playtime'];
146 $info_monkeys_audio['compressed_size'] = $getid3->info['avdataend'] - $getid3->info['avdataoffset'];
147 $info_monkeys_audio['uncompressed_size'] = $info_monkeys_audio['samples'] * $info_monkeys_audio['channels'] * ($info_monkeys_audio['bits_per_sample'] / 8);
148 $info_monkeys_audio['compression_ratio'] = $info_monkeys_audio['compressed_size'] / ($info_monkeys_audio['uncompressed_size'] + $info_monkeys_audio_raw['nHeaderDataBytes']);
149 $info_monkeys_audio['bitrate'] = (($info_monkeys_audio['samples'] * $info_monkeys_audio['channels'] * $info_monkeys_audio['bits_per_sample']) / $info_monkeys_audio['playtime']) * $info_monkeys_audio['compression_ratio'];
151 $getid3->info['audio']['bitrate'] = $info_monkeys_audio['bitrate'];
153 $getid3->info['audio']['bits_per_sample'] = $info_monkeys_audio['bits_per_sample'];
154 $getid3->info['audio']['encoder'] = 'MAC v'.number_format($info_monkeys_audio['version'], 2);
155 $getid3->info['audio']['encoder_options'] = ucfirst($info_monkeys_audio['compression']).' compression';
157 // MAC >= v3980 - get avdataoffsets from MAC header
158 if ($info_monkeys_audio_raw['nVersion'] >= 3980) {
159 $getid3->info['avdataoffset'] += $info_monkeys_audio_raw['nDescriptorBytes'] + $info_monkeys_audio_raw['nHeaderBytes'] + $info_monkeys_audio_raw['nSeekTableBytes'] + $info_monkeys_audio_raw['nHeaderDataBytes'];
160 $getid3->info['avdataend'] -= $info_monkeys_audio_raw['nTerminatingDataBytes'];
161 }
163 // MAC < v3980 Add size of MAC header to avdataoffset
164 else {
165 $getid3->info['avdataoffset'] += 8;
166 }
168 // Convert md5sum to 32 byte string
169 if (@$info_monkeys_audio_raw['cFileMD5']) {
170 if ($info_monkeys_audio_raw['cFileMD5'] !== str_repeat("\x00", 16)) {
171 $getid3->info['md5_data_source'] = '';
172 $md5 = $info_monkeys_audio_raw['cFileMD5'];
173 for ($i = 0; $i < strlen($md5); $i++) {
174 $getid3->info['md5_data_source'] .= str_pad(dechex(ord($md5{$i})), 2, '00', STR_PAD_LEFT);
175 }
176 if (!preg_match('/^[0-9a-f]{32}$/', $getid3->info['md5_data_source'])) {
177 unset($getid3->info['md5_data_source']);
178 }
179 }
180 }
183 return true;
184 }
188 public static function MonkeyCompressionLevelNameLookup($compression_level) {
190 static $lookup = array (
191 0 => 'unknown',
192 1000 => 'fast',
193 2000 => 'normal',
194 3000 => 'high',
195 4000 => 'extra-high',
196 5000 => 'insane'
197 );
198 return (isset($lookup[$compression_level]) ? $lookup[$compression_level] : 'invalid');
199 }
203 public static function MonkeySamplesPerFrame($version_id, $compression_level) {
205 if ($version_id >= 3950) {
206 return 73728 * 4;
207 }
208 if (($version_id >= 3900) || (($version_id >= 3800) && ($compression_level == 4000))) {
209 return 73728;
210 }
211 return 9216;
212 }
214 }
216 ?>