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.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();
41 public function Analyze() {
42 $info = &$this->getid3
->info
;
44 $info['fileformat'] = 'cue';
45 $this->readCueSheetFilename($info['filenamepath']);
46 $info['cue'] = $this->cuesheet
;
51 * @param string $filename
55 public function readCueSheetFilename($filename)
57 $filedata = file_get_contents($filename);
58 return $this->readCueSheet($filedata);
62 * Parses a cue sheet file.
64 * @param string $filedata
68 public function readCueSheet(&$filedata)
71 foreach (explode("\n", str_replace("\r", null, $filedata)) as $line)
73 if ( (strlen($line) > 0) && ($line[0] != '#'))
75 $cue_lines[] = trim($line);
78 $this->parseCueSheet($cue_lines);
80 return $this->cuesheet
;
84 * Parses the cue sheet array.
86 * @param array $file - The cuesheet as an array of each line.
88 public function parseCueSheet($file)
90 //-1 means still global, all others are track specific
93 for ($i=0; $i < count($file); $i++
)
95 list($key) = explode(' ', strtolower($file[$i]), 2);
104 $this->parseString($file[$i], $track_on);
107 $currentFile = $this->parseFile($file[$i]);
110 $this->parseFlags($file[$i], $track_on);
115 $this->parseIndex($file[$i], $track_on);
118 $this->parseComment($file[$i], $track_on);
122 $this->parseTrack($file[$i], $track_on);
123 if (isset($currentFile)) // if there's a file
125 $this->cuesheet
['tracks'][$track_on]['datafile'] = $currentFile;
129 //save discarded junk and place string[] with track it was found in
130 $this->parseGarbage($file[$i], $track_on);
137 * Parses the REM command.
139 * @param string $line - The line in the cue file that contains the TRACK command.
140 * @param integer $track_on - The track currently processing.
142 public function parseComment($line, $track_on)
144 $explodedline = explode(' ', $line, 3);
145 $comment_REM = (isset($explodedline[0]) ?
$explodedline[0] : '');
146 $comment_type = (isset($explodedline[1]) ?
$explodedline[1] : '');
147 $comment_data = (isset($explodedline[2]) ?
$explodedline[2] : '');
148 if (($comment_REM == 'REM') && $comment_type) {
149 $comment_type = strtolower($comment_type);
150 $commment_data = trim($comment_data, ' "');
151 if ($track_on != -1) {
152 $this->cuesheet
['tracks'][$track_on]['comments'][$comment_type][] = $comment_data;
154 $this->cuesheet
['comments'][$comment_type][] = $comment_data;
160 * Parses the FILE command.
162 * @param string $line - The line in the cue file that contains the FILE command.
164 * @return array - Array of FILENAME and TYPE of file..
166 public function parseFile($line)
168 $line = substr($line, strpos($line, ' ') +
1);
169 $type = strtolower(substr($line, strrpos($line, ' ')));
172 $line = substr($line, 0, strrpos($line, ' ') - 1);
174 //if quotes around it, remove them.
175 $line = trim($line, '"');
177 return array('filename'=>$line, 'type'=>$type);
181 * Parses the FLAG command.
183 * @param string $line - The line in the cue file that contains the TRACK command.
184 * @param integer $track_on - The track currently processing.
186 public function parseFlags($line, $track_on)
190 foreach (explode(' ', strtolower($line)) as $type)
195 // first entry in this line
196 $this->cuesheet
['tracks'][$track_on]['flags'] = array(
209 $this->cuesheet
['tracks'][$track_on]['flags'][$type] = true;
219 * Collect any unidentified data.
221 * @param string $line - The line in the cue file that contains the TRACK command.
222 * @param integer $track_on - The track currently processing.
224 public function parseGarbage($line, $track_on)
226 if ( strlen($line) > 0 )
230 $this->cuesheet
['garbage'][] = $line;
234 $this->cuesheet
['tracks'][$track_on]['garbage'][] = $line;
240 * Parses the INDEX command of a TRACK.
242 * @param string $line - The line in the cue file that contains the TRACK command.
243 * @param integer $track_on - The track currently processing.
245 public function parseIndex($line, $track_on)
247 $type = strtolower(substr($line, 0, strpos($line, ' ')));
248 $line = substr($line, strpos($line, ' ') +
1);
251 if ($type == 'index')
253 //read the index number
254 $number = intval(substr($line, 0, strpos($line, ' ')));
255 $line = substr($line, strpos($line, ' ') +
1);
258 //extract the minutes, seconds, and frames
259 $explodedline = explode(':', $line);
260 $minutes = (isset($explodedline[0]) ?
$explodedline[0] : '');
261 $seconds = (isset($explodedline[1]) ?
$explodedline[1] : '');
262 $frames = (isset($explodedline[2]) ?
$explodedline[2] : '');
266 $this->cuesheet
['tracks'][$track_on][$type][$number] = array('minutes'=>intval($minutes), 'seconds'=>intval($seconds), 'frames'=>intval($frames));
270 $this->cuesheet
['tracks'][$track_on][$type] = array('minutes'=>intval($minutes), 'seconds'=>intval($seconds), 'frames'=>intval($frames));
276 * @param string $line
277 * @param int $track_on
279 public function parseString($line, $track_on)
281 $category = strtolower(substr($line, 0, strpos($line, ' ')));
282 $line = substr($line, strpos($line, ' ') +
1);
284 //get rid of the quotes
285 $line = trim($line, '"');
297 $this->cuesheet
[$category] = $line;
301 $this->cuesheet
['tracks'][$track_on][$category] = $line;
310 * Parses the TRACK command.
312 * @param string $line - The line in the cue file that contains the TRACK command.
313 * @param integer $track_on - The track currently processing.
315 public function parseTrack($line, $track_on)
317 $line = substr($line, strpos($line, ' ') +
1);
318 $track = ltrim(substr($line, 0, strpos($line, ' ')), '0');
320 //find the data type.
321 $datatype = strtolower(substr($line, strpos($line, ' ') +
1));
323 $this->cuesheet
['tracks'][$track_on] = array('track_number'=>$track, 'datatype'=>$datatype);