view e2gallerypro/e2upload/Backend/Assets/getid3/module.audio.bonk.php @ 20:1038db2374ec judyates

change address
author Robert McIntyre <rlm@mit.edu>
date Sun, 08 Sep 2013 00:47:09 -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.bonk.php |
18 // | Module for analyzing BONK audio files |
19 // | dependencies: module.tag.id3v2.php (optional) |
20 // +----------------------------------------------------------------------+
21 //
22 // $Id: module.audio.bonk.php,v 1.3 2006/11/02 10:48:01 ah Exp $
26 class getid3_bonk extends getid3_handler
27 {
29 public function Analyze() {
31 $getid3 = $this->getid3;
33 $getid3->info['bonk'] = array ();
34 $info_bonk = &$getid3->info['bonk'];
36 $info_bonk['dataoffset'] = $getid3->info['avdataoffset'];
37 $info_bonk['dataend'] = $getid3->info['avdataend'];
40 // Scan-from-end method, for v0.6 and higher
41 fseek($getid3->fp, $info_bonk['dataend'] - 8, SEEK_SET);
42 $possible_bonk_tag = fread($getid3->fp, 8);
43 while (getid3_bonk::BonkIsValidTagName(substr($possible_bonk_tag, 4, 4), true)) {
44 $bonk_tag_size = getid3_lib::LittleEndian2Int(substr($possible_bonk_tag, 0, 4));
45 fseek($getid3->fp, 0 - $bonk_tag_size, SEEK_CUR);
46 $bonk_tag_offset = ftell($getid3->fp);
47 $tag_header_test = fread($getid3->fp, 5);
48 if (($tag_header_test{0} != "\x00") || (substr($possible_bonk_tag, 4, 4) != strtolower(substr($possible_bonk_tag, 4, 4)))) {
49 throw new getid3_exception('Expecting "Ø'.strtoupper(substr($possible_bonk_tag, 4, 4)).'" at offset '.$bonk_tag_offset.', found "'.$tag_header_test.'"');
50 }
51 $bonk_tag_name = substr($tag_header_test, 1, 4);
53 $info_bonk[$bonk_tag_name]['size'] = $bonk_tag_size;
54 $info_bonk[$bonk_tag_name]['offset'] = $bonk_tag_offset;
55 $this->HandleBonkTags($bonk_tag_name);
57 $next_tag_end_offset = $bonk_tag_offset - 8;
58 if ($next_tag_end_offset < $info_bonk['dataoffset']) {
59 if (empty($getid3->info['audio']['encoder'])) {
60 $getid3->info['audio']['encoder'] = 'Extended BONK v0.9+';
61 }
62 return true;
63 }
64 fseek($getid3->fp, $next_tag_end_offset, SEEK_SET);
65 $possible_bonk_tag = fread($getid3->fp, 8);
66 }
68 // Seek-from-beginning method for v0.4 and v0.5
69 if (empty($info_bonk['BONK'])) {
70 fseek($getid3->fp, $info_bonk['dataoffset'], SEEK_SET);
71 do {
72 $tag_header_test = fread($getid3->fp, 5);
73 switch ($tag_header_test) {
74 case "\x00".'BONK':
75 if (empty($getid3->info['audio']['encoder'])) {
76 $getid3->info['audio']['encoder'] = 'BONK v0.4';
77 }
78 break;
80 case "\x00".'INFO':
81 $getid3->info['audio']['encoder'] = 'Extended BONK v0.5';
82 break;
84 default:
85 break 2;
86 }
87 $bonk_tag_name = substr($tag_header_test, 1, 4);
88 $info_bonk[$bonk_tag_name]['size'] = $info_bonk['dataend'] - $info_bonk['dataoffset'];
89 $info_bonk[$bonk_tag_name]['offset'] = $info_bonk['dataoffset'];
90 $this->HandleBonkTags($bonk_tag_name);
92 } while (true);
93 }
96 // Parse META block for v0.6 - v0.8
97 if (!@$info_bonk['INFO'] && isset($info_bonk['META']['tags']['info'])) {
98 fseek($getid3->fp, $info_bonk['META']['tags']['info'], SEEK_SET);
99 $tag_header_test = fread($getid3->fp, 5);
100 if ($tag_header_test == "\x00".'INFO') {
101 $getid3->info['audio']['encoder'] = 'Extended BONK v0.6 - v0.8';
103 $bonk_tag_name = substr($tag_header_test, 1, 4);
104 $info_bonk[$bonk_tag_name]['size'] = $info_bonk['dataend'] - $info_bonk['dataoffset'];
105 $info_bonk[$bonk_tag_name]['offset'] = $info_bonk['dataoffset'];
106 $this->HandleBonkTags($bonk_tag_name);
107 }
108 }
110 if (empty($getid3->info['audio']['encoder'])) {
111 $getid3->info['audio']['encoder'] = 'Extended BONK v0.9+';
112 }
113 if (empty($info_bonk['BONK'])) {
114 unset($getid3->info['bonk']);
115 }
116 return true;
118 }
122 private function HandleBonkTags(&$bonk_tag_name) {
124 // Shortcut to getid3 pointer
125 $getid3 = $this->getid3;
126 $info_audio = &$getid3->info['audio'];
128 switch ($bonk_tag_name) {
130 case 'BONK':
131 // shortcut
132 $info_bonk_BONK = &$getid3->info['bonk']['BONK'];
134 $bonk_data = "\x00".'BONK'.fread($getid3->fp, 17);
136 getid3_lib::ReadSequence('LittleEndian2Int', $info_bonk_BONK, $bonk_data, 5,
137 array (
138 'version' => 1,
139 'number_samples' => 4,
140 'sample_rate' => 4,
141 'channels' => 1,
142 'lossless' => 1,
143 'joint_stereo' => 1,
144 'number_taps' => 2,
145 'downsampling_ratio' => 1,
146 'samples_per_packet' => 2
147 )
148 );
150 $info_bonk_BONK['lossless'] = (bool)$info_bonk_BONK['lossless'];
151 $info_bonk_BONK['joint_stereo'] = (bool)$info_bonk_BONK['joint_stereo'];
153 $getid3->info['avdataoffset'] = $info_bonk_BONK['offset'] + 5 + 17;
154 $getid3->info['avdataend'] = $info_bonk_BONK['offset'] + $info_bonk_BONK['size'];
156 $getid3->info['fileformat'] = 'bonk';
157 $info_audio['dataformat'] = 'bonk';
158 $info_audio['bitrate_mode'] = 'vbr'; // assumed
159 $info_audio['channels'] = $info_bonk_BONK['channels'];
160 $info_audio['sample_rate'] = $info_bonk_BONK['sample_rate'];
161 $info_audio['channelmode'] = $info_bonk_BONK['joint_stereo'] ? 'joint stereo' : 'stereo';
162 $info_audio['lossless'] = $info_bonk_BONK['lossless'];
163 $info_audio['codec'] = 'bonk';
165 $getid3->info['playtime_seconds'] = $info_bonk_BONK['number_samples'] / ($info_bonk_BONK['sample_rate'] * $info_bonk_BONK['channels']);
166 if ($getid3->info['playtime_seconds'] > 0) {
167 $info_audio['bitrate'] = (($getid3->info['bonk']['dataend'] - $getid3->info['bonk']['dataoffset']) * 8) / $getid3->info['playtime_seconds'];
168 }
169 break;
171 case 'INFO':
172 // shortcut
173 $info_bonk_INFO = &$getid3->info['bonk']['INFO'];
175 $info_bonk_INFO['version'] = getid3_lib::LittleEndian2Int(fread($getid3->fp, 1));
176 $info_bonk_INFO['entries_count'] = 0;
177 $next_info_data_pair = fread($getid3->fp, 5);
178 if (!getid3_bonk::BonkIsValidTagName(substr($next_info_data_pair, 1, 4))) {
179 while (!feof($getid3->fp)) {
180 $next_info_data_pair = fread($getid3->fp, 5);
181 if (getid3_bonk::BonkIsValidTagName(substr($next_info_data_pair, 1, 4))) {
182 fseek($getid3->fp, -5, SEEK_CUR);
183 break;
184 }
185 $info_bonk_INFO['entries_count']++;
186 }
187 }
188 break;
190 case 'META':
191 $bonk_data = "\x00".'META'.fread($getid3->fp, $getid3->info['bonk']['META']['size'] - 5);
192 $getid3->info['bonk']['META']['version'] = getid3_lib::LittleEndian2Int(substr($bonk_data, 5, 1));
194 $meta_tag_entries = floor(((strlen($bonk_data) - 8) - 6) / 8); // BonkData - xxxxmeta - ØMETA
195 $offset = 6;
196 for ($i = 0; $i < $meta_tag_entries; $i++) {
197 $meta_entry_tag_name = substr($bonk_data, $offset, 4);
198 $offset += 4;
199 $meta_entry_tag_offset = getid3_lib::LittleEndian2Int(substr($bonk_data, $offset, 4));
200 $offset += 4;
201 $getid3->info['bonk']['META']['tags'][$meta_entry_tag_name] = $meta_entry_tag_offset;
202 }
203 break;
205 case ' ID3':
206 $info_audio['encoder'] = 'Extended BONK v0.9+';
208 // ID3v2 checking is optional
209 if (class_exists('getid3_id3v2')) {
211 $id3v2 = new getid3_id3v2($getid3);
212 $id3v2->option_starting_offset = $getid3->info['bonk'][' ID3']['offset'] + 2;
213 $getid3->info['bonk'][' ID3']['valid'] = $id3v2->Analyze();
214 }
215 break;
217 default:
218 $getid3->warning('Unexpected Bonk tag "'.$bonk_tag_name.'" at offset '.$getid3->info['bonk'][$bonk_tag_name]['offset']);
219 break;
221 }
222 }
226 public static function BonkIsValidTagName($possible_bonk_tag, $ignore_case=false) {
228 $ignore_case = $ignore_case ? 'i' : '';
229 return preg_match('/^(BONK|INFO| ID3|META)$/'.$ignore_case, $possible_bonk_tag);
230 }
232 }
235 ?>