forked from frifox/cueParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cue_parser.php
170 lines (150 loc) · 4.57 KB
/
cue_parser.php
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
class cueParser{
function parse($cue=null){
if(!$cue) return false;
// UTF-ize if necessary
$cue = preg_replace("/^\xef\xbb\xbf/", '', $cue);
if(!$encoding = mb_detect_encoding($cue)){
$cue = mb_convert_encoding($cue, 'UTF-8', 'ISO-8859-1');
}else{
$cue = mb_convert_encoding($cue, 'UTF-8', $encoding);
}
// convert to array
$cue = str_replace("\r",'',$cue);
$cue = explode("\n",$cue);
// dump bad lines
$allowed = array(
'file',
'track',
'performer',
'title',
'index'
);
foreach($cue as $key => $value){
$line = preg_replace('/[^a-z]/i','',$value);
$valid = false;
foreach($allowed as $x){
if(preg_match("/^$x/i",$line)) $valid = $x;
}
if($valid){
unset($cue[$key]);
$cue[$key]['type'] = $valid;
$cue[$key]['line'] = $value;
}else{
unset($cue[$key]);
}
unset($line,$valid);
}
// remove line type identifier
foreach($cue as $key => $value){
$line = $value['line'];
$chars = preg_split('//', $value['type'], -1, PREG_SPLIT_NO_EMPTY);
foreach($chars as $char){
$line = preg_replace("/^.*?$char/i",'',$line);
}
$cue[$key]['line'] = $line;
unset($line,$chars);
}
// clean up lines & convert index->frames
foreach($cue as $key => $value){
$line = $value['line'];
switch($value['type']){
case 'file':
$line = preg_replace('/^[ |"|\t]*(.*?)[ |"|\t]*$/','$1',$line); // wrappers
$line = preg_replace('/\w+$/','',$line); // filetype identifier
$line = preg_replace('/^[ |"|\t]*(.*?)[ |"|\t]*$/','$1',$line); // wrappers
$line = preg_replace('/\t+/',' ',$line); // tabs
$line = preg_replace('/ +/',' ',$line); // multi-spaces
$cue[$key]['line'] = $line;
break;
case 'index':
$line = preg_replace('/^[ |"|\t]*(.*?)[ |"|\t]*$/','$1',$line); // wrappers
$line = preg_replace('/^\w+/','',$line); // leading identifier
$line = preg_replace('/[^\d|:]+/','',$line); // everyting non-digit or :
// convert to frames
$line = explode(':', $line);
$line = array_reverse($line);
@$frames = $line[0]; // frames
@$frames+= $line[1]*75; // seconds
@$frames+= $line[2]*75*60; // minutes
@$frames+= $line[3]*75*60*60; // hours (!)
$cue[$key]['line'] = $frames;
break;
default:
$line = preg_replace('/^[ |"|\t]*(.*?)[ |"|\t]*$/','$1',$line); // wrappers
$line = preg_replace('/\t+/',' ',$line); // tabs
$line = preg_replace('/ +/',' ',$line); // multi-spaces
$cue[$key]['line'] = $line;
break;
}
unset($line,$frames);
}
// retrieve cuesheet data
$allowed = array(
'performer',
'title',
'file'
);
foreach($cue as $key => $value){
if($value['type']=='track') break; // reached tracks, stop
if(in_array($value['type'],$allowed)){
$out['Cuesheet'][$value['type']] = $value['line'];
}
unset($cue[$key]);
}
// retrieve track data
$allowed = array(
'performer',
'title',
'index'
);
foreach($cue as $key => $value){
if($value['type']=='track'){
@$i++; // reached new track, increment
}else{
if(in_array($value['type'],$allowed)) $out['Track'][$i][$value['type']] = $value['line'];
}
unset($cue[$key]);
}
$out['Track'] = array_values($out['Track']);
// done, return it
return $out;
}
function compile($cue=null){
if(!$cue) return false;
// convert frames->index
if(count(@$cue['Track'])>0)
foreach($cue['Track'] as $key => $value){
$i = $value['index'];
$min = floor($i/75/60);
$sec = floor(($i - ($min*75*60)) /75);
$frm = floor(($i - ($min*75*60) - ($sec*75)));
$min = str_pad($min, 2, "0", STR_PAD_LEFT);
$sec = str_pad($sec, 2, "0", STR_PAD_LEFT);
$frm = str_pad($frm, 2, "0", STR_PAD_LEFT);
$cue['Track'][$key]['index'] = "$min:$sec:$frm";
unset($i,$min,$sec,$frm);
}
// compile cuesheet
$out = 'PERFORMER "'.@$cue['Cuesheet']['performer'].'"'.PHP_EOL;
$out.= 'TITLE "'.@$cue['Cuesheet']['title'].'"'.PHP_EOL;
$out.= 'FILE "'.@$cue['Cuesheet']['file'].'"';
if($ext = strtoupper(pathinfo(@$cue['Cuesheet']['file'],PATHINFO_EXTENSION))) $out.= " $ext".PHP_EOL;
if(count(@$cue['Track'])>0)
foreach($cue['Track'] as $key => $value){
$key++;
$out.= ' TRACK '.sprintf("%02d",$key).' AUDIO'.PHP_EOL;
$out.= ' PERFORMER "'.$value['performer'].'"'.PHP_EOL;
$out.= ' TITLE "'.$value['title'].'"'.PHP_EOL;
$out.= ' INDEX 01 '.$value['index'].PHP_EOL;
}
// done
return $out;
}
function clean($cue=null){
if(!$cue) return false;
$cue = $this->parse($cue);
$cue = $this->compile($cue);
return $cue;
}
}