Blame view

sources/3rdparty/getid3/module.misc.cue.php 8.36 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  <?php
  /////////////////////////////////////////////////////////////////
  /// getID3() by James Heinrich <info@getid3.org>               //
  //  available at http://getid3.sourceforge.net                 //
  //            or http://www.getid3.org                         //
  /////////////////////////////////////////////////////////////////
  // See readme.txt for more details                             //
  /////////////////////////////////////////////////////////////////
  //                                                             //
  // module.misc.cue.php                                         //
  // module for analyzing CUEsheet files                         //
  // dependencies: NONE                                          //
  //                                                             //
  /////////////////////////////////////////////////////////////////
  //                                                             //
  // Module originally written [2009-Mar-25] by                  //
  //      Nigel Barnes <ngbarnesØhotmail*com>                    //
  // Minor reformatting and similar small changes to integrate   //
  //   into getID3 by James Heinrich <info@getid3.org>           //
  //                                                            ///
  /////////////////////////////////////////////////////////////////
  
  /*
   * CueSheet parser by Nigel Barnes.
   *
   * This is a PHP conversion of CueSharp 0.5 by Wyatt O'Day (wyday.com/cuesharp)
   */
  
  /**
   * A CueSheet class used to open and parse cuesheets.
   *
   */
  class getid3_cue extends getid3_handler
  {
31b7f2792   Kload   Upgrade to ownclo...
35
  	public $cuesheet = array();
03e52840d   Kload   Init
36

31b7f2792   Kload   Upgrade to ownclo...
37
  	public function Analyze() {
03e52840d   Kload   Init
38
39
40
41
42
43
44
  		$info = &$this->getid3->info;
  
  		$info['fileformat'] = 'cue';
  		$this->readCueSheetFilename($info['filenamepath']);
  		$info['cue'] = $this->cuesheet;
  		return true;
  	}
31b7f2792   Kload   Upgrade to ownclo...
45
  	public function readCueSheetFilename($filename)
03e52840d   Kload   Init
46
47
48
49
50
51
52
53
54
  	{
  		$filedata = file_get_contents($filename);
  		return $this->readCueSheet($filedata);
  	}
  	/**
  	* Parses a cue sheet file.
  	*
  	* @param string $filename - The filename for the cue sheet to open.
  	*/
31b7f2792   Kload   Upgrade to ownclo...
55
  	public function readCueSheet(&$filedata)
03e52840d   Kload   Init
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  	{
  		$cue_lines = array();
  		foreach (explode("
  ", str_replace("\r", null, $filedata)) as $line)
  		{
  			if ( (strlen($line) > 0) && ($line[0] != '#'))
  			{
  				$cue_lines[] = trim($line);
  			}
  		}
  		$this->parseCueSheet($cue_lines);
  
  		return $this->cuesheet;
  	}
  
  	/**
  	* Parses the cue sheet array.
  	*
  	* @param array $file - The cuesheet as an array of each line.
  	*/
31b7f2792   Kload   Upgrade to ownclo...
76
  	public function parseCueSheet($file)
03e52840d   Kload   Init
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  	{
  		//-1 means still global, all others are track specific
  		$track_on = -1;
  
  		for ($i=0; $i < count($file); $i++)
  		{
  			list($key) = explode(' ', strtolower($file[$i]), 2);
  			switch ($key)
  			{
  				case 'catalog':
  				case 'cdtextfile':
  				case 'isrc':
  				case 'performer':
  				case 'songwriter':
  				case 'title':
  					$this->parseString($file[$i], $track_on);
  					break;
  				case 'file':
  					$currentFile = $this->parseFile($file[$i]);
  					break;
  				case 'flags':
  					$this->parseFlags($file[$i], $track_on);
  					break;
  				case 'index':
  				case 'postgap':
  				case 'pregap':
  					$this->parseIndex($file[$i], $track_on);
  					break;
  				case 'rem':
  					$this->parseComment($file[$i], $track_on);
  					break;
  				case 'track':
  					$track_on++;
  					$this->parseTrack($file[$i], $track_on);
  					if (isset($currentFile)) // if there's a file
  					{
  						$this->cuesheet['tracks'][$track_on]['datafile'] = $currentFile;
  					}
  					break;
  				default:
  					//save discarded junk and place string[] with track it was found in
  					$this->parseGarbage($file[$i], $track_on);
  					break;
  			}
  		}
  	}
  
  	/**
  	* Parses the REM command.
  	*
  	* @param string $line - The line in the cue file that contains the TRACK command.
  	* @param integer $track_on - The track currently processing.
  	*/
31b7f2792   Kload   Upgrade to ownclo...
130
  	public function parseComment($line, $track_on)
03e52840d   Kload   Init
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  	{
  		$explodedline = explode(' ', $line, 3);
  		$comment_REM  = (isset($explodedline[0]) ? $explodedline[0] : '');
  		$comment_type = (isset($explodedline[1]) ? $explodedline[1] : '');
  		$comment_data = (isset($explodedline[2]) ? $explodedline[2] : '');
  		if (($comment_REM == 'REM') && $comment_type) {
  			$comment_type  = strtolower($comment_type);
  			$commment_data = trim($comment_data, ' "');
  			if ($track_on != -1) {
  				$this->cuesheet['tracks'][$track_on]['comments'][$comment_type][] = $comment_data;
  			} else {
  				$this->cuesheet['comments'][$comment_type][] = $comment_data;
  			}
  		}
  	}
  
  	/**
  	* Parses the FILE command.
  	*
  	* @param string $line - The line in the cue file that contains the FILE command.
  	* @return array - Array of FILENAME and TYPE of file..
  	*/
31b7f2792   Kload   Upgrade to ownclo...
153
  	public function parseFile($line)
03e52840d   Kload   Init
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  	{
  		$line =            substr($line, strpos($line, ' ') + 1);
  		$type = strtolower(substr($line, strrpos($line, ' ')));
  
  		//remove type
  		$line = substr($line, 0, strrpos($line, ' ') - 1);
  
  		//if quotes around it, remove them.
  		$line = trim($line, '"');
  
  		return array('filename'=>$line, 'type'=>$type);
  	}
  
  	/**
  	* Parses the FLAG command.
  	*
  	* @param string $line - The line in the cue file that contains the TRACK command.
  	* @param integer $track_on - The track currently processing.
  	*/
31b7f2792   Kload   Upgrade to ownclo...
173
  	public function parseFlags($line, $track_on)
03e52840d   Kload   Init
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  	{
  		if ($track_on != -1)
  		{
  			foreach (explode(' ', strtolower($line)) as $type)
  			{
  				switch ($type)
  				{
  					case 'flags':
  						// first entry in this line
  						$this->cuesheet['tracks'][$track_on]['flags'] = array(
  							'4ch'  => false,
  							'data' => false,
  							'dcp'  => false,
  							'pre'  => false,
  							'scms' => false,
  						);
  						break;
  					case 'data':
  					case 'dcp':
  					case '4ch':
  					case 'pre':
  					case 'scms':
  						$this->cuesheet['tracks'][$track_on]['flags'][$type] = true;
  						break;
  					default:
  						break;
  				}
  			}
  		}
  	}
  
  	/**
  	* Collect any unidentified data.
  	*
  	* @param string $line - The line in the cue file that contains the TRACK command.
  	* @param integer $track_on - The track currently processing.
  	*/
31b7f2792   Kload   Upgrade to ownclo...
211
  	public function parseGarbage($line, $track_on)
03e52840d   Kload   Init
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  	{
  		if ( strlen($line) > 0 )
  		{
  			if ($track_on == -1)
  			{
  				$this->cuesheet['garbage'][] = $line;
  			}
  			else
  			{
  				$this->cuesheet['tracks'][$track_on]['garbage'][] = $line;
  			}
  		}
  	}
  
  	/**
  	* Parses the INDEX command of a TRACK.
  	*
  	* @param string $line - The line in the cue file that contains the TRACK command.
  	* @param integer $track_on - The track currently processing.
  	*/
31b7f2792   Kload   Upgrade to ownclo...
232
  	public function parseIndex($line, $track_on)
03e52840d   Kload   Init
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
  	{
  		$type = strtolower(substr($line, 0, strpos($line, ' ')));
  		$line =            substr($line, strpos($line, ' ') + 1);
  
  		if ($type == 'index')
  		{
  			//read the index number
  			$number = intval(substr($line, 0, strpos($line, ' ')));
  			$line   =        substr($line, strpos($line, ' ') + 1);
  		}
  
  		//extract the minutes, seconds, and frames
  		$explodedline = explode(':', $line);
  		$minutes = (isset($explodedline[0]) ? $explodedline[0] : '');
  		$seconds = (isset($explodedline[1]) ? $explodedline[1] : '');
  		$frames  = (isset($explodedline[2]) ? $explodedline[2] : '');
  
  		switch ($type) {
  			case 'index':
  				$this->cuesheet['tracks'][$track_on][$type][$number] = array('minutes'=>intval($minutes), 'seconds'=>intval($seconds), 'frames'=>intval($frames));
  				break;
  			case 'pregap':
  			case 'postgap':
  				$this->cuesheet['tracks'][$track_on][$type]          = array('minutes'=>intval($minutes), 'seconds'=>intval($seconds), 'frames'=>intval($frames));
  				break;
  		}
  	}
31b7f2792   Kload   Upgrade to ownclo...
260
  	public function parseString($line, $track_on)
03e52840d   Kload   Init
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  	{
  		$category = strtolower(substr($line, 0, strpos($line, ' ')));
  		$line     =            substr($line, strpos($line, ' ') + 1);
  
  		//get rid of the quotes
  		$line = trim($line, '"');
  
  		switch ($category)
  		{
  			case 'catalog':
  			case 'cdtextfile':
  			case 'isrc':
  			case 'performer':
  			case 'songwriter':
  			case 'title':
  				if ($track_on == -1)
  				{
  					$this->cuesheet[$category] = $line;
  				}
  				else
  				{
  					$this->cuesheet['tracks'][$track_on][$category] = $line;
  				}
  				break;
  			default:
  				break;
  		}
  	}
  
  	/**
  	* Parses the TRACK command.
  	*
  	* @param string $line - The line in the cue file that contains the TRACK command.
  	* @param integer $track_on - The track currently processing.
  	*/
31b7f2792   Kload   Upgrade to ownclo...
296
  	public function parseTrack($line, $track_on)
03e52840d   Kload   Init
297
298
299
300
301
302
303
304
305
306
307
  	{
  		$line = substr($line, strpos($line, ' ') + 1);
  		$track = ltrim(substr($line, 0, strpos($line, ' ')), '0');
  
  		//find the data type.
  		$datatype = strtolower(substr($line, strpos($line, ' ') + 1));
  
  		$this->cuesheet['tracks'][$track_on] = array('track_number'=>$track, 'datatype'=>$datatype);
  	}
  
  }