5 Copyright (C) 2009 Laurent Aimar
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 if( not string.match( string.upper( vlc.path ), ".CUE$" ) ) then
27 header = vlc.peek( 2048 )
28 return string.match( header, "FILE.*WAVE%s[\r\n]+" ) or
29 string.match( header, "FILE.*AIFF%s[\r\n]+" ) or
30 string.match( header, "FILE.*MP3%s[\r\n]+" )
34 function cue_string( src )
35 local sub = string.match( src, "^\"(.*)\".*$" );
39 return string.match( src, "^(%S+).*$" )
42 function cue_path( src )
43 if( string.match( src, "^/" ) or
44 string.match( src, "^\\" ) or
45 string.match( src, "^[%l%u]:\\" ) ) then
49 local path = string.gsub( vlc.path, '\\', '/' )
50 local slash = string.find( string.reverse( path ), '/' )
51 if( path == nil ) then
54 return string.sub( path, 1, -slash-1 ) .. '/' .. src
57 function cue_track( global, track )
58 if( track.index01 == nil ) then
63 t.path = cue_path( track.file or global.file )
65 t.album = global.title
66 t.artist = track.performer or global.performer
67 t.genre = track.genre or global.genre
68 t.date = track.date or global.date
69 t.tracknum = track.num
70 t.options = { ":start-time=" .. math.floor(track.index01) }
75 function cue_append( tracks, global, track )
76 local t = cue_track( global, track )
78 if( #tracks > 0 ) then
79 local prev = tracks[#tracks]
80 table.insert( prev.options, ":stop-time=" .. math.floor(track.index01) )
82 table.insert( tracks, t )
96 if not line then break end
98 cmd, arg = string.match( line, "^%s*(%S+)%s*(.*)$" )
100 if( cmd == "REM" and arg ) then
101 subcmd, value = string.match( arg, "^(%S+)%s*(.*)$" )
102 if( subcmd == "GENRE" and value ) then
103 data.genre = cue_string( value )
104 elseif( subcmd == "DATE" and value ) then
105 data.date = cue_string( value )
107 elseif( cmd == "PERFORMER" and arg ) then
108 data.performer = cue_string( arg )
109 elseif( cmd == "TITLE" and arg ) then
110 data.title = cue_string( arg )
111 elseif( cmd == "FILE" ) then
112 file = cue_string( arg )
113 elseif( cmd == "TRACK" ) then
114 if( not global_data ) then
117 cue_append( p, global_data, data )
119 data = { file = file, num = string.match( arg, "^(%d+)" ) }
120 elseif( cmd == "INDEX" ) then
121 local idx, m, s, f = string.match( arg, "(%d+)%s+(%d+):(%d+):(%d+)" )
122 if( idx == "01" and m ~= nil and s ~= nil and f ~= nil ) then
123 data.index01 = m * 60 + s + f / 75
128 cue_append( p, global_data, data )