ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
module.audio.bonk.php
Go to the documentation of this file.
1 <?php
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
7 // See readme.txt for more details //
9 // //
10 // module.audio.la.php //
11 // module for analyzing BONK audio files //
12 // dependencies: module.tag.id3v2.php (optional) //
13 // ///
15 
16 
18 {
19  function getid3_bonk(&$fd, &$ThisFileInfo) {
20 
21  // shortcut
22  $ThisFileInfo['bonk'] = array();
23  $thisfile_bonk = &$ThisFileInfo['bonk'];
24 
25  $thisfile_bonk['dataoffset'] = $ThisFileInfo['avdataoffset'];
26  $thisfile_bonk['dataend'] = $ThisFileInfo['avdataend'];
27 
28  // scan-from-end method, for v0.6 and higher
29  fseek($fd, $thisfile_bonk['dataend'] - 8, SEEK_SET);
30  $PossibleBonkTag = fread($fd, 8);
31  while ($this->BonkIsValidTagName(substr($PossibleBonkTag, 4, 4), true)) {
32  $BonkTagSize = getid3_lib::LittleEndian2Int(substr($PossibleBonkTag, 0, 4));
33  fseek($fd, 0 - $BonkTagSize, SEEK_CUR);
34  $BonkTagOffset = ftell($fd);
35  $TagHeaderTest = fread($fd, 5);
36  if (($TagHeaderTest{0} != "\x00") || (substr($PossibleBonkTag, 4, 4) != strtolower(substr($PossibleBonkTag, 4, 4)))) {
37  $ThisFileInfo['error'][] = 'Expecting "Ø'.strtoupper(substr($PossibleBonkTag, 4, 4)).'" at offset '.$BonkTagOffset.', found "'.$TagHeaderTest.'"';
38  return false;
39  }
40  $BonkTagName = substr($TagHeaderTest, 1, 4);
41 
42  $thisfile_bonk[$BonkTagName]['size'] = $BonkTagSize;
43  $thisfile_bonk[$BonkTagName]['offset'] = $BonkTagOffset;
44  $this->HandleBonkTags($fd, $BonkTagName, $ThisFileInfo);
45  $NextTagEndOffset = $BonkTagOffset - 8;
46  if ($NextTagEndOffset < $thisfile_bonk['dataoffset']) {
47  if (empty($ThisFileInfo['audio']['encoder'])) {
48  $ThisFileInfo['audio']['encoder'] = 'Extended BONK v0.9+';
49  }
50  return true;
51  }
52  fseek($fd, $NextTagEndOffset, SEEK_SET);
53  $PossibleBonkTag = fread($fd, 8);
54  }
55 
56  // seek-from-beginning method for v0.4 and v0.5
57  if (empty($thisfile_bonk['BONK'])) {
58  fseek($fd, $thisfile_bonk['dataoffset'], SEEK_SET);
59  do {
60  $TagHeaderTest = fread($fd, 5);
61  switch ($TagHeaderTest) {
62  case "\x00".'BONK':
63  if (empty($ThisFileInfo['audio']['encoder'])) {
64  $ThisFileInfo['audio']['encoder'] = 'BONK v0.4';
65  }
66  break;
67 
68  case "\x00".'INFO':
69  $ThisFileInfo['audio']['encoder'] = 'Extended BONK v0.5';
70  break;
71 
72  default:
73  break 2;
74  }
75  $BonkTagName = substr($TagHeaderTest, 1, 4);
76  $thisfile_bonk[$BonkTagName]['size'] = $thisfile_bonk['dataend'] - $thisfile_bonk['dataoffset'];
77  $thisfile_bonk[$BonkTagName]['offset'] = $thisfile_bonk['dataoffset'];
78  $this->HandleBonkTags($fd, $BonkTagName, $ThisFileInfo);
79 
80  } while (true);
81  }
82 
83  // parse META block for v0.6 - v0.8
84  if (empty($thisfile_bonk['INFO']) && isset($thisfile_bonk['META']['tags']['info'])) {
85  fseek($fd, $thisfile_bonk['META']['tags']['info'], SEEK_SET);
86  $TagHeaderTest = fread($fd, 5);
87  if ($TagHeaderTest == "\x00".'INFO') {
88  $ThisFileInfo['audio']['encoder'] = 'Extended BONK v0.6 - v0.8';
89 
90  $BonkTagName = substr($TagHeaderTest, 1, 4);
91  $thisfile_bonk[$BonkTagName]['size'] = $thisfile_bonk['dataend'] - $thisfile_bonk['dataoffset'];
92  $thisfile_bonk[$BonkTagName]['offset'] = $thisfile_bonk['dataoffset'];
93  $this->HandleBonkTags($fd, $BonkTagName, $ThisFileInfo);
94  }
95  }
96 
97  if (empty($ThisFileInfo['audio']['encoder'])) {
98  $ThisFileInfo['audio']['encoder'] = 'Extended BONK v0.9+';
99  }
100  if (empty($thisfile_bonk['BONK'])) {
101  unset($ThisFileInfo['bonk']);
102  }
103  return true;
104 
105  }
106 
107  function HandleBonkTags(&$fd, &$BonkTagName, &$ThisFileInfo) {
108 
109  switch ($BonkTagName) {
110  case 'BONK':
111  // shortcut
112  $thisfile_bonk_BONK = &$ThisFileInfo['bonk']['BONK'];
113 
114  $BonkData = "\x00".'BONK'.fread($fd, 17);
115  $thisfile_bonk_BONK['version'] = getid3_lib::LittleEndian2Int(substr($BonkData, 5, 1));
116  $thisfile_bonk_BONK['number_samples'] = getid3_lib::LittleEndian2Int(substr($BonkData, 6, 4));
117  $thisfile_bonk_BONK['sample_rate'] = getid3_lib::LittleEndian2Int(substr($BonkData, 10, 4));
118 
119  $thisfile_bonk_BONK['channels'] = getid3_lib::LittleEndian2Int(substr($BonkData, 14, 1));
120  $thisfile_bonk_BONK['lossless'] = (bool) getid3_lib::LittleEndian2Int(substr($BonkData, 15, 1));
121  $thisfile_bonk_BONK['joint_stereo'] = (bool) getid3_lib::LittleEndian2Int(substr($BonkData, 16, 1));
122  $thisfile_bonk_BONK['number_taps'] = getid3_lib::LittleEndian2Int(substr($BonkData, 17, 2));
123  $thisfile_bonk_BONK['downsampling_ratio'] = getid3_lib::LittleEndian2Int(substr($BonkData, 19, 1));
124  $thisfile_bonk_BONK['samples_per_packet'] = getid3_lib::LittleEndian2Int(substr($BonkData, 20, 2));
125 
126  $ThisFileInfo['avdataoffset'] = $thisfile_bonk_BONK['offset'] + 5 + 17;
127  $ThisFileInfo['avdataend'] = $thisfile_bonk_BONK['offset'] + $thisfile_bonk_BONK['size'];
128 
129  $ThisFileInfo['fileformat'] = 'bonk';
130  $ThisFileInfo['audio']['dataformat'] = 'bonk';
131  $ThisFileInfo['audio']['bitrate_mode'] = 'vbr'; // assumed
132  $ThisFileInfo['audio']['channels'] = $thisfile_bonk_BONK['channels'];
133  $ThisFileInfo['audio']['sample_rate'] = $thisfile_bonk_BONK['sample_rate'];
134  $ThisFileInfo['audio']['channelmode'] = ($thisfile_bonk_BONK['joint_stereo'] ? 'joint stereo' : 'stereo');
135  $ThisFileInfo['audio']['lossless'] = $thisfile_bonk_BONK['lossless'];
136  $ThisFileInfo['audio']['codec'] = 'bonk';
137 
138  $ThisFileInfo['playtime_seconds'] = $thisfile_bonk_BONK['number_samples'] / ($thisfile_bonk_BONK['sample_rate'] * $thisfile_bonk_BONK['channels']);
139  if ($ThisFileInfo['playtime_seconds'] > 0) {
140  $ThisFileInfo['audio']['bitrate'] = (($ThisFileInfo['bonk']['dataend'] - $ThisFileInfo['bonk']['dataoffset']) * 8) / $ThisFileInfo['playtime_seconds'];
141  }
142  break;
143 
144  case 'INFO':
145  // shortcut
146  $thisfile_bonk_INFO = &$ThisFileInfo['bonk']['INFO'];
147 
148  $thisfile_bonk_INFO['version'] = getid3_lib::LittleEndian2Int(fread($fd, 1));
149  $thisfile_bonk_INFO['entries_count'] = 0;
150  $NextInfoDataPair = fread($fd, 5);
151  if (!$this->BonkIsValidTagName(substr($NextInfoDataPair, 1, 4))) {
152  while (!feof($fd)) {
153  //$CurrentSeekInfo['offset'] = getid3_lib::LittleEndian2Int(substr($NextInfoDataPair, 0, 4));
154  //$CurrentSeekInfo['nextbit'] = getid3_lib::LittleEndian2Int(substr($NextInfoDataPair, 4, 1));
155  //$thisfile_bonk_INFO[] = $CurrentSeekInfo;
156 
157  $NextInfoDataPair = fread($fd, 5);
158  if ($this->BonkIsValidTagName(substr($NextInfoDataPair, 1, 4))) {
159  fseek($fd, -5, SEEK_CUR);
160  break;
161  }
162  $thisfile_bonk_INFO['entries_count']++;
163  }
164  }
165  break;
166 
167  case 'META':
168  $BonkData = "\x00".'META'.fread($fd, $ThisFileInfo['bonk']['META']['size'] - 5);
169  $ThisFileInfo['bonk']['META']['version'] = getid3_lib::LittleEndian2Int(substr($BonkData, 5, 1));
170 
171  $MetaTagEntries = floor(((strlen($BonkData) - 8) - 6) / 8); // BonkData - xxxxmeta - ØMETA
172  $offset = 6;
173  for ($i = 0; $i < $MetaTagEntries; $i++) {
174  $MetaEntryTagName = substr($BonkData, $offset, 4);
175  $offset += 4;
176  $MetaEntryTagOffset = getid3_lib::LittleEndian2Int(substr($BonkData, $offset, 4));
177  $offset += 4;
178  $ThisFileInfo['bonk']['META']['tags'][$MetaEntryTagName] = $MetaEntryTagOffset;
179  }
180  break;
181 
182  case ' ID3':
183  $ThisFileInfo['audio']['encoder'] = 'Extended BONK v0.9+';
184 
185  // ID3v2 checking is optional
186  if (class_exists('getid3_id3v2')) {
187  $ThisFileInfo['bonk'][' ID3']['valid'] = new getid3_id3v2($fd, $ThisFileInfo, $ThisFileInfo['bonk'][' ID3']['offset'] + 2);
188  }
189  break;
190 
191  default:
192  $ThisFileInfo['warning'][] = 'Unexpected Bonk tag "'.$BonkTagName.'" at offset '.$ThisFileInfo['bonk'][$BonkTagName]['offset'];
193  break;
194 
195  }
196  }
197 
198  function BonkIsValidTagName($PossibleBonkTag, $ignorecase=false) {
199  static $BonkIsValidTagName = array('BONK', 'INFO', ' ID3', 'META');
200  foreach ($BonkIsValidTagName as $validtagname) {
201  if ($validtagname == $PossibleBonkTag) {
202  return true;
203  } elseif ($ignorecase && (strtolower($validtagname) == strtolower($PossibleBonkTag))) {
204  return true;
205  }
206  }
207  return false;
208  }
209 
210 }
211 
212 
213 ?>