ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
module.misc.cue.php
Go to the documentation of this file.
1 <?php
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // also https://github.com/JamesHeinrich/getID3 //
8 // See readme.txt for more details //
10 // //
11 // module.misc.cue.php //
12 // module for analyzing CUEsheet files //
13 // dependencies: NONE //
14 // //
16 // //
17 // Module originally written [2009-Mar-25] by //
18 // Nigel Barnes <ngbarnesØhotmail*com> //
19 // Minor reformatting and similar small changes to integrate //
20 // into getID3 by James Heinrich <info@getid3.org> //
21 // ///
23 
24 /*
25  * CueSheet parser by Nigel Barnes.
26  *
27  * This is a PHP conversion of CueSharp 0.5 by Wyatt O'Day (wyday.com/cuesharp)
28  */
29 
35 {
36  public $cuesheet = array();
37 
38  public function Analyze() {
39  $info = &$this->getid3->info;
40 
41  $info['fileformat'] = 'cue';
42  $this->readCueSheetFilename($info['filenamepath']);
43  $info['cue'] = $this->cuesheet;
44  return true;
45  }
46 
47 
48 
50  {
51  $filedata = file_get_contents($filename);
52  return $this->readCueSheet($filedata);
53  }
59  public function readCueSheet(&$filedata)
60  {
61  $cue_lines = array();
62  foreach (explode("\n", str_replace("\r", null, $filedata)) as $line)
63  {
64  if ( (strlen($line) > 0) && ($line[0] != '#'))
65  {
66  $cue_lines[] = trim($line);
67  }
68  }
69  $this->parseCueSheet($cue_lines);
70 
71  return $this->cuesheet;
72  }
73 
79  public function parseCueSheet($file)
80  {
81  //-1 means still global, all others are track specific
82  $track_on = -1;
83 
84  for ($i=0; $i < count($file); $i++)
85  {
86  list($key) = explode(' ', strtolower($file[$i]), 2);
87  switch ($key)
88  {
89  case 'catalog':
90  case 'cdtextfile':
91  case 'isrc':
92  case 'performer':
93  case 'songwriter':
94  case 'title':
95  $this->parseString($file[$i], $track_on);
96  break;
97  case 'file':
98  $currentFile = $this->parseFile($file[$i]);
99  break;
100  case 'flags':
101  $this->parseFlags($file[$i], $track_on);
102  break;
103  case 'index':
104  case 'postgap':
105  case 'pregap':
106  $this->parseIndex($file[$i], $track_on);
107  break;
108  case 'rem':
109  $this->parseComment($file[$i], $track_on);
110  break;
111  case 'track':
112  $track_on++;
113  $this->parseTrack($file[$i], $track_on);
114  if (isset($currentFile)) // if there's a file
115  {
116  $this->cuesheet['tracks'][$track_on]['datafile'] = $currentFile;
117  }
118  break;
119  default:
120  //save discarded junk and place string[] with track it was found in
121  $this->parseGarbage($file[$i], $track_on);
122  break;
123  }
124  }
125  }
126 
133  public function parseComment($line, $track_on)
134  {
135  $explodedline = explode(' ', $line, 3);
136  $comment_REM = (isset($explodedline[0]) ? $explodedline[0] : '');
137  $comment_type = (isset($explodedline[1]) ? $explodedline[1] : '');
138  $comment_data = (isset($explodedline[2]) ? $explodedline[2] : '');
139  if (($comment_REM == 'REM') && $comment_type) {
140  $comment_type = strtolower($comment_type);
141  $commment_data = trim($comment_data, ' "');
142  if ($track_on != -1) {
143  $this->cuesheet['tracks'][$track_on]['comments'][$comment_type][] = $comment_data;
144  } else {
145  $this->cuesheet['comments'][$comment_type][] = $comment_data;
146  }
147  }
148  }
149 
156  public function parseFile($line)
157  {
158  $line = substr($line, strpos($line, ' ') + 1);
159  $type = strtolower(substr($line, strrpos($line, ' ')));
160 
161  //remove type
162  $line = substr($line, 0, strrpos($line, ' ') - 1);
163 
164  //if quotes around it, remove them.
165  $line = trim($line, '"');
166 
167  return array('filename'=>$line, 'type'=>$type);
168  }
169 
176  public function parseFlags($line, $track_on)
177  {
178  if ($track_on != -1)
179  {
180  foreach (explode(' ', strtolower($line)) as $type)
181  {
182  switch ($type)
183  {
184  case 'flags':
185  // first entry in this line
186  $this->cuesheet['tracks'][$track_on]['flags'] = array(
187  '4ch' => false,
188  'data' => false,
189  'dcp' => false,
190  'pre' => false,
191  'scms' => false,
192  );
193  break;
194  case 'data':
195  case 'dcp':
196  case '4ch':
197  case 'pre':
198  case 'scms':
199  $this->cuesheet['tracks'][$track_on]['flags'][$type] = true;
200  break;
201  default:
202  break;
203  }
204  }
205  }
206  }
207 
214  public function parseGarbage($line, $track_on)
215  {
216  if ( strlen($line) > 0 )
217  {
218  if ($track_on == -1)
219  {
220  $this->cuesheet['garbage'][] = $line;
221  }
222  else
223  {
224  $this->cuesheet['tracks'][$track_on]['garbage'][] = $line;
225  }
226  }
227  }
228 
235  public function parseIndex($line, $track_on)
236  {
237  $type = strtolower(substr($line, 0, strpos($line, ' ')));
238  $line = substr($line, strpos($line, ' ') + 1);
239 
240  if ($type == 'index')
241  {
242  //read the index number
243  $number = intval(substr($line, 0, strpos($line, ' ')));
244  $line = substr($line, strpos($line, ' ') + 1);
245  }
246 
247  //extract the minutes, seconds, and frames
248  $explodedline = explode(':', $line);
249  $minutes = (isset($explodedline[0]) ? $explodedline[0] : '');
250  $seconds = (isset($explodedline[1]) ? $explodedline[1] : '');
251  $frames = (isset($explodedline[2]) ? $explodedline[2] : '');
252 
253  switch ($type) {
254  case 'index':
255  $this->cuesheet['tracks'][$track_on][$type][$number] = array('minutes'=>intval($minutes), 'seconds'=>intval($seconds), 'frames'=>intval($frames));
256  break;
257  case 'pregap':
258  case 'postgap':
259  $this->cuesheet['tracks'][$track_on][$type] = array('minutes'=>intval($minutes), 'seconds'=>intval($seconds), 'frames'=>intval($frames));
260  break;
261  }
262  }
263 
264  public function parseString($line, $track_on)
265  {
266  $category = strtolower(substr($line, 0, strpos($line, ' ')));
267  $line = substr($line, strpos($line, ' ') + 1);
268 
269  //get rid of the quotes
270  $line = trim($line, '"');
271 
272  switch ($category)
273  {
274  case 'catalog':
275  case 'cdtextfile':
276  case 'isrc':
277  case 'performer':
278  case 'songwriter':
279  case 'title':
280  if ($track_on == -1)
281  {
282  $this->cuesheet[$category] = $line;
283  }
284  else
285  {
286  $this->cuesheet['tracks'][$track_on][$category] = $line;
287  }
288  break;
289  default:
290  break;
291  }
292  }
293 
300  public function parseTrack($line, $track_on)
301  {
302  $line = substr($line, strpos($line, ' ') + 1);
303  $track = ltrim(substr($line, 0, strpos($line, ' ')), '0');
304 
305  //find the data type.
306  $datatype = strtolower(substr($line, strpos($line, ' ') + 1));
307 
308  $this->cuesheet['tracks'][$track_on] = array('track_number'=>$track, 'datatype'=>$datatype);
309  }
310 
311 }
312 
$type
parseString($line, $track_on)
parseCueSheet($file)
Parses the cue sheet array.
parseFile($line)
Parses the FILE command.
getID3() by James Heinrich info@getid3.org //
parseIndex($line, $track_on)
Parses the INDEX command of a TRACK.
readCueSheetFilename($filename)
parseFlags($line, $track_on)
Parses the FLAG command.
$filename
Definition: buildRTE.php:89
parseComment($line, $track_on)
Parses the REM command.
parseGarbage($line, $track_on)
Collect any unidentified data.
$i
Definition: disco.tpl.php:19
readCueSheet(&$filedata)
Parses a cue sheet file.
parseTrack($line, $track_on)
Parses the TRACK command.
$info
Definition: index.php:5
$key
Definition: croninfo.php:18