9c42799e6ec65ec1438bb596e7e2b118f7676938
2 /////////////////////////////////////////////////////////////////
3 /// getID3() by James Heinrich <info@getid3.org> //
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // also https://github.com/JamesHeinrich/getID3 //
7 /////////////////////////////////////////////////////////////////
8 // See readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
11 // module.misc.cue.php //
12 // module for analyzing CUEsheet files //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
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> //
22 /////////////////////////////////////////////////////////////////
25 * CueSheet parser by Nigel Barnes.
27 * This is a PHP conversion of CueSharp 0.5 by Wyatt O'Day (wyday.com/cuesharp)
31 * A CueSheet class used to open and parse cuesheets.
34 class getid3_cue
extends getid3_handler
36 public $cuesheet = array();
38 public function Analyze() {
39 $info = &$this->getid3
->info
;
41 $info['fileformat'] = 'cue';
42 $this->readCueSheetFilename($info['filenamepath']);
43 $info['cue'] = $this->cuesheet
;
49 public function readCueSheetFilename($filename)
51 $filedata = file_get_contents($filename);
52 return $this->readCueSheet($filedata);
55 * Parses a cue sheet file.
57 * @param string $filename - The filename for the cue sheet to open.
59 public function readCueSheet(&$filedata)
62 foreach (explode("\n", str_replace("\r", null, $filedata)) as $line)
64 if ( (strlen($line) > 0) && ($line[0] != '#'))
66 $cue_lines[] = trim($line);
69 $this->parseCueSheet($cue_lines);
71 return $this->cuesheet
;
75 * Parses the cue sheet array.
77 * @param array $file - The cuesheet as an array of each line.
79 public function parseCueSheet($file)
81 //-1 means still global, all others are track specific
84 for ($i=0; $i < count($file); $i++
)
86 list($key) = explode(' ', strtolower($file[$i]), 2);
95 $this->parseString($file[$i], $track_on);
98 $currentFile = $this->parseFile($file[$i]);
101 $this->parseFlags($file[$i], $track_on);
106 $this->parseIndex($file[$i], $track_on);
109 $this->parseComment($file[$i], $track_on);
113 $this->parseTrack($file[$i], $track_on);
114 if (isset($currentFile)) // if there's a file
116 $this->cuesheet
['tracks'][$track_on]['datafile'] = $currentFile;
120 //save discarded junk and place string[] with track it was found in
121 $this->parseGarbage($file[$i], $track_on);
128 * Parses the REM command.
130 * @param string $line - The line in the cue file that contains the TRACK command.
131 * @param integer $track_on - The track currently processing.
133 public function parseComment($line, $track_on)
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;
145 $this->cuesheet
['comments'][$comment_type][] = $comment_data;
151 * Parses the FILE command.
153 * @param string $line - The line in the cue file that contains the FILE command.
154 * @return array - Array of FILENAME and TYPE of file..
156 public function parseFile($line)
158 $line = substr($line, strpos($line, ' ') +
1);
159 $type = strtolower(substr($line, strrpos($line, ' ')));
162 $line = substr($line, 0, strrpos($line, ' ') - 1);
164 //if quotes around it, remove them.
165 $line = trim($line, '"');
167 return array('filename'=>$line, 'type'=>$type);
171 * Parses the FLAG command.
173 * @param string $line - The line in the cue file that contains the TRACK command.
174 * @param integer $track_on - The track currently processing.
176 public function parseFlags($line, $track_on)
180 foreach (explode(' ', strtolower($line)) as $type)
185 // first entry in this line
186 $this->cuesheet
['tracks'][$track_on]['flags'] = array(
199 $this->cuesheet
['tracks'][$track_on]['flags'][$type] = true;
209 * Collect any unidentified data.
211 * @param string $line - The line in the cue file that contains the TRACK command.
212 * @param integer $track_on - The track currently processing.
214 public function parseGarbage($line, $track_on)
216 if ( strlen($line) > 0 )
220 $this->cuesheet
['garbage'][] = $line;
224 $this->cuesheet
['tracks'][$track_on]['garbage'][] = $line;
230 * Parses the INDEX command of a TRACK.
232 * @param string $line - The line in the cue file that contains the TRACK command.
233 * @param integer $track_on - The track currently processing.
235 public function parseIndex($line, $track_on)
237 $type = strtolower(substr($line, 0, strpos($line, ' ')));
238 $line = substr($line, strpos($line, ' ') +
1);
240 if ($type == 'index')
242 //read the index number
243 $number = intval(substr($line, 0, strpos($line, ' ')));
244 $line = substr($line, strpos($line, ' ') +
1);
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] : '');
255 $this->cuesheet
['tracks'][$track_on][$type][$number] = array('minutes'=>intval($minutes), 'seconds'=>intval($seconds), 'frames'=>intval($frames));
259 $this->cuesheet
['tracks'][$track_on][$type] = array('minutes'=>intval($minutes), 'seconds'=>intval($seconds), 'frames'=>intval($frames));
264 public function parseString($line, $track_on)
266 $category = strtolower(substr($line, 0, strpos($line, ' ')));
267 $line = substr($line, strpos($line, ' ') +
1);
269 //get rid of the quotes
270 $line = trim($line, '"');
282 $this->cuesheet
[$category] = $line;
286 $this->cuesheet
['tracks'][$track_on][$category] = $line;
295 * Parses the TRACK command.
297 * @param string $line - The line in the cue file that contains the TRACK command.
298 * @param integer $track_on - The track currently processing.
300 public function parseTrack($line, $track_on)
302 $line = substr($line, strpos($line, ' ') +
1);
303 $track = ltrim(substr($line, 0, strpos($line, ' ')), '0');
305 //find the data type.
306 $datatype = strtolower(substr($line, strpos($line, ' ') +
1));
308 $this->cuesheet
['tracks'][$track_on] = array('track_number'=>$track, 'datatype'=>$datatype);