internal representation of a cddb file
:dartist |
[R] |
|
:discid |
[R] |
|
:dsecs |
[R] |
|
:dsongs |
[R] |
|
:dtitle |
[R] |
|
:dyear |
[R] |
|
Read and parse the file.
- fn
- file/path name
# File db/cddb-cddbfile.rb, line 53
def initialize(fn)
@dartist = nil
@discid = nil
@dsecs = 0
@dsongs = Hash.new
@dtitle = ""
@dyear = 1900
# work vars
fstart = 0
song_frames = Array.new
song_secs = Hash.new
song_artists = Hash.new
song_titles = Hash.new
# for each line of the file
IO.foreach(fn) {
|ln|
ln.chomp!
# song start frame
if }^#\s+([0-9]+)} =~ ln
f = fstart
fstart = $1.to_i
song_frames.push(fstart - f) if f > 0
# disc length (seconds)
elsif }\s+([0-9]+)\s+seconds} =~ ln
@dsecs = $1.to_i
song_frames.push(sec2frames(dsecs) - fstart)
# disc id
elsif }^DISCID=(.+)$} =~ ln
@discid = $1.strip.downcase
# disc title (artist / title)
elsif }^DTITLE=(.+)$} =~ ln
@dtitle += $1.strip
# disc year (optional)
elsif }^DYEAR=(.+)$} =~ ln
@dyear = $1.to_i
# track title
elsif }^TTITLE([0-9]+)=(.+)$} =~ ln
trknum = $1.to_i + 1
song_titles[trknum] = $2.strip
if song_titles[trknum] =~ /^(data|null|missing)(_?\d+)?/i
song_titles[trknum] = nil
end
# track artist
elsif }^TARTIST([0-9]+)=(.+)$} =~ ln
trknum = $1.to_i + 1
song_artists[trknum] = $2.strip
end
}
tmp = @dtitle.split("/")
@dartist = tmp.first.strip
@dtitle = tmp.last.strip
song_trknum = Range.new(1,song_frames.size).to_a
song_secs =
Hash.zip(song_trknum.zip(song_frames.map { |f| frames2sec(f) }))
songcnt = 0
1.upto(song_trknum.last) {
|i|
if song_titles[i].nil?
@dsongs[i] = nil
else
songcnt += 1
@dsongs[i] = DiscSong.new(song_artists[i] || @dartist,
song_titles[i], song_secs[i])
end
}
STDERR.puts("#{@dartist}/#{dtitle} has no songs!") if songcnt == 0
end
Convert frames to seconds (rounded).
# File db/cddb-cddbfile.rb, line 126
def frames2sec(f)
(f + FramesPerSec / 2) / FramesPerSec
end
Convert seconds to frames (exact conversion).
# File db/cddb-cddbfile.rb, line 131
def sec2frames(s)
s * FramesPerSec
end