3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
8 // see readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
11 // module.audio.la.php //
12 // module for analyzing BONK audio files //
13 // dependencies: module.tag.id3v2.php (optional) //
15 /////////////////////////////////////////////////////////////////
18 class getid3_bonk
extends getid3_handler
23 public function Analyze() {
24 $info = &$this->getid3
->info
;
27 $info['bonk'] = array();
28 $thisfile_bonk = &$info['bonk'];
30 $thisfile_bonk['dataoffset'] = $info['avdataoffset'];
31 $thisfile_bonk['dataend'] = $info['avdataend'];
33 if (!getid3_lib
::intValueSupported($thisfile_bonk['dataend'])) {
35 $this->warning('Unable to parse BONK file from end (v0.6+ preferred method) because PHP filesystem functions only support up to '.round(PHP_INT_MAX
/ 1073741824).'GB');
39 // scan-from-end method, for v0.6 and higher
40 $this->fseek($thisfile_bonk['dataend'] - 8);
41 $PossibleBonkTag = $this->fread(8);
42 while ($this->BonkIsValidTagName(substr($PossibleBonkTag, 4, 4), true)) {
43 $BonkTagSize = getid3_lib
::LittleEndian2Int(substr($PossibleBonkTag, 0, 4));
44 $this->fseek(0 - $BonkTagSize, SEEK_CUR
);
45 $BonkTagOffset = $this->ftell();
46 $TagHeaderTest = $this->fread(5);
47 if (($TagHeaderTest{0} != "\x00") ||
(substr($PossibleBonkTag, 4, 4) != strtolower(substr($PossibleBonkTag, 4, 4)))) {
48 $this->error('Expecting "'.getid3_lib
::PrintHexBytes("\x00".strtoupper(substr($PossibleBonkTag, 4, 4))).'" at offset '.$BonkTagOffset.', found "'.getid3_lib
::PrintHexBytes($TagHeaderTest).'"');
51 $BonkTagName = substr($TagHeaderTest, 1, 4);
53 $thisfile_bonk[$BonkTagName]['size'] = $BonkTagSize;
54 $thisfile_bonk[$BonkTagName]['offset'] = $BonkTagOffset;
55 $this->HandleBonkTags($BonkTagName);
56 $NextTagEndOffset = $BonkTagOffset - 8;
57 if ($NextTagEndOffset < $thisfile_bonk['dataoffset']) {
58 if (empty($info['audio']['encoder'])) {
59 $info['audio']['encoder'] = 'Extended BONK v0.9+';
63 $this->fseek($NextTagEndOffset);
64 $PossibleBonkTag = $this->fread(8);
69 // seek-from-beginning method for v0.4 and v0.5
70 if (empty($thisfile_bonk['BONK'])) {
71 $this->fseek($thisfile_bonk['dataoffset']);
73 $TagHeaderTest = $this->fread(5);
74 switch ($TagHeaderTest) {
76 if (empty($info['audio']['encoder'])) {
77 $info['audio']['encoder'] = 'BONK v0.4';
82 $info['audio']['encoder'] = 'Extended BONK v0.5';
88 $BonkTagName = substr($TagHeaderTest, 1, 4);
89 $thisfile_bonk[$BonkTagName]['size'] = $thisfile_bonk['dataend'] - $thisfile_bonk['dataoffset'];
90 $thisfile_bonk[$BonkTagName]['offset'] = $thisfile_bonk['dataoffset'];
91 $this->HandleBonkTags($BonkTagName);
96 // parse META block for v0.6 - v0.8
97 if (empty($thisfile_bonk['INFO']) && isset($thisfile_bonk['META']['tags']['info'])) {
98 $this->fseek($thisfile_bonk['META']['tags']['info']);
99 $TagHeaderTest = $this->fread(5);
100 if ($TagHeaderTest == "\x00".'INFO') {
101 $info['audio']['encoder'] = 'Extended BONK v0.6 - v0.8';
103 $BonkTagName = substr($TagHeaderTest, 1, 4);
104 $thisfile_bonk[$BonkTagName]['size'] = $thisfile_bonk['dataend'] - $thisfile_bonk['dataoffset'];
105 $thisfile_bonk[$BonkTagName]['offset'] = $thisfile_bonk['dataoffset'];
106 $this->HandleBonkTags($BonkTagName);
110 if (empty($info['audio']['encoder'])) {
111 $info['audio']['encoder'] = 'Extended BONK v0.9+';
113 if (empty($thisfile_bonk['BONK'])) {
114 unset($info['bonk']);
121 * @param string $BonkTagName
123 public function HandleBonkTags($BonkTagName) {
124 $info = &$this->getid3
->info
;
125 switch ($BonkTagName) {
128 $thisfile_bonk_BONK = &$info['bonk']['BONK'];
130 $BonkData = "\x00".'BONK'.$this->fread(17);
131 $thisfile_bonk_BONK['version'] = getid3_lib
::LittleEndian2Int(substr($BonkData, 5, 1));
132 $thisfile_bonk_BONK['number_samples'] = getid3_lib
::LittleEndian2Int(substr($BonkData, 6, 4));
133 $thisfile_bonk_BONK['sample_rate'] = getid3_lib
::LittleEndian2Int(substr($BonkData, 10, 4));
135 $thisfile_bonk_BONK['channels'] = getid3_lib
::LittleEndian2Int(substr($BonkData, 14, 1));
136 $thisfile_bonk_BONK['lossless'] = (bool) getid3_lib
::LittleEndian2Int(substr($BonkData, 15, 1));
137 $thisfile_bonk_BONK['joint_stereo'] = (bool) getid3_lib
::LittleEndian2Int(substr($BonkData, 16, 1));
138 $thisfile_bonk_BONK['number_taps'] = getid3_lib
::LittleEndian2Int(substr($BonkData, 17, 2));
139 $thisfile_bonk_BONK['downsampling_ratio'] = getid3_lib
::LittleEndian2Int(substr($BonkData, 19, 1));
140 $thisfile_bonk_BONK['samples_per_packet'] = getid3_lib
::LittleEndian2Int(substr($BonkData, 20, 2));
142 $info['avdataoffset'] = $thisfile_bonk_BONK['offset'] +
5 +
17;
143 $info['avdataend'] = $thisfile_bonk_BONK['offset'] +
$thisfile_bonk_BONK['size'];
145 $info['fileformat'] = 'bonk';
146 $info['audio']['dataformat'] = 'bonk';
147 $info['audio']['bitrate_mode'] = 'vbr'; // assumed
148 $info['audio']['channels'] = $thisfile_bonk_BONK['channels'];
149 $info['audio']['sample_rate'] = $thisfile_bonk_BONK['sample_rate'];
150 $info['audio']['channelmode'] = ($thisfile_bonk_BONK['joint_stereo'] ?
'joint stereo' : 'stereo');
151 $info['audio']['lossless'] = $thisfile_bonk_BONK['lossless'];
152 $info['audio']['codec'] = 'bonk';
154 $info['playtime_seconds'] = $thisfile_bonk_BONK['number_samples'] / ($thisfile_bonk_BONK['sample_rate'] * $thisfile_bonk_BONK['channels']);
155 if ($info['playtime_seconds'] > 0) {
156 $info['audio']['bitrate'] = (($info['bonk']['dataend'] - $info['bonk']['dataoffset']) * 8) / $info['playtime_seconds'];
162 $thisfile_bonk_INFO = &$info['bonk']['INFO'];
164 $thisfile_bonk_INFO['version'] = getid3_lib
::LittleEndian2Int($this->fread(1));
165 $thisfile_bonk_INFO['entries_count'] = 0;
166 $NextInfoDataPair = $this->fread(5);
167 if (!$this->BonkIsValidTagName(substr($NextInfoDataPair, 1, 4))) {
168 while (!feof($this->getid3
->fp
)) {
169 //$CurrentSeekInfo['offset'] = getid3_lib::LittleEndian2Int(substr($NextInfoDataPair, 0, 4));
170 //$CurrentSeekInfo['nextbit'] = getid3_lib::LittleEndian2Int(substr($NextInfoDataPair, 4, 1));
171 //$thisfile_bonk_INFO[] = $CurrentSeekInfo;
173 $NextInfoDataPair = $this->fread(5);
174 if ($this->BonkIsValidTagName(substr($NextInfoDataPair, 1, 4))) {
175 $this->fseek(-5, SEEK_CUR
);
178 $thisfile_bonk_INFO['entries_count']++
;
184 $BonkData = "\x00".'META'.$this->fread($info['bonk']['META']['size'] - 5);
185 $info['bonk']['META']['version'] = getid3_lib
::LittleEndian2Int(substr($BonkData, 5, 1));
187 $MetaTagEntries = floor(((strlen($BonkData) - 8) - 6) / 8); // BonkData - xxxxmeta - ØMETA
189 for ($i = 0; $i < $MetaTagEntries; $i++
) {
190 $MetaEntryTagName = substr($BonkData, $offset, 4);
192 $MetaEntryTagOffset = getid3_lib
::LittleEndian2Int(substr($BonkData, $offset, 4));
194 $info['bonk']['META']['tags'][$MetaEntryTagName] = $MetaEntryTagOffset;
199 $info['audio']['encoder'] = 'Extended BONK v0.9+';
201 // ID3v2 checking is optional
202 if (class_exists('getid3_id3v2')) {
203 $getid3_temp = new getID3();
204 $getid3_temp->openfile($this->getid3
->filename
);
205 $getid3_id3v2 = new getid3_id3v2($getid3_temp);
206 $getid3_id3v2->StartingOffset
= $info['bonk'][' ID3']['offset'] +
2;
207 $info['bonk'][' ID3']['valid'] = $getid3_id3v2->Analyze();
208 if ($info['bonk'][' ID3']['valid']) {
209 $info['id3v2'] = $getid3_temp->info
['id3v2'];
211 unset($getid3_temp, $getid3_id3v2);
216 $this->warning('Unexpected Bonk tag "'.$BonkTagName.'" at offset '.$info['bonk'][$BonkTagName]['offset']);
223 * @param string $PossibleBonkTag
224 * @param bool $ignorecase
228 public static function BonkIsValidTagName($PossibleBonkTag, $ignorecase=false) {
229 static $BonkIsValidTagName = array('BONK', 'INFO', ' ID3', 'META');
230 foreach ($BonkIsValidTagName as $validtagname) {
231 if ($validtagname == $PossibleBonkTag) {
233 } elseif ($ignorecase && (strtolower($validtagname) == strtolower($PossibleBonkTag))) {