Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.0.0
- Loading branch information
Showing
11 changed files
with
327 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
module AlacIt | ||
class Cuesheet | ||
attr_reader :cuesheet, :songs, :track_duration | ||
|
||
def initialize(cuesheet, track_duration = nil) | ||
@cuesheet = cuesheet | ||
@reg = { | ||
:track => %r(TRACK (\d{1,3}) AUDIO), | ||
:performer => %r(PERFORMER "(.*)"), | ||
:title => %r(TITLE "(.*)"), | ||
:index => %r(INDEX \d{1,3} (\d{1,3}):(\d{1,2}):(\d{1,2})) | ||
} | ||
@track_duration = AlacIt::Index.new(track_duration) if track_duration | ||
end | ||
|
||
def parse! | ||
@songs = parse_titles.map{ |title| {:title => title} } | ||
@songs.each_with_index do |song, i| | ||
song[:performer] = parse_performers[i] | ||
song[:track] = parse_tracks[i] | ||
song[:index] = parse_indices[i] | ||
end | ||
raise AlacIt::InvalidCuesheet.new('Cuesheet is malformed!') unless valid? | ||
calculate_song_durations! | ||
true | ||
end | ||
|
||
def position(value) | ||
index = Index.new(value) | ||
return @songs.first if index < @songs.first[:index] | ||
@songs.each_with_index do |song, i| | ||
return song if song == @songs.last | ||
return song if between(song[:index], @songs[i + 1][:index], index) | ||
end | ||
end | ||
|
||
def valid? | ||
@songs.all? do |song| | ||
[:performer, :track, :index, :title].all? do |key| | ||
song[key] != nil | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def calculate_song_durations! | ||
@songs.each_with_index do |song, i| | ||
if song == @songs.last | ||
song[:duration] = (@track_duration - song[:index]) if @track_duration | ||
return | ||
end | ||
song[:duration] = @songs[i + 1][:index] - song[:index] | ||
end | ||
end | ||
|
||
def between(a, b, position_index) | ||
(position_index > a) && (position_index < b) | ||
end | ||
|
||
def parse_titles | ||
unless @titles | ||
@titles = cuesheet_scan(:title).map{ |title| title.first } | ||
@titles.delete_at(0) | ||
end | ||
@titles | ||
end | ||
|
||
def parse_performers | ||
unless @performers | ||
@performers = cuesheet_scan(:performer).map{ |performer| performer.first } | ||
@performers.delete_at(0) | ||
end | ||
@performers | ||
end | ||
|
||
def parse_tracks | ||
@tracks ||= cuesheet_scan(:track).map{ |track| track.first.to_i } | ||
end | ||
|
||
def parse_indices | ||
@indices ||= cuesheet_scan(:index).map{ |index| AlacIt::Index.new([index[0].to_i, index[1].to_i, index[2].to_i]) } | ||
end | ||
|
||
def cuesheet_scan(field) | ||
scan = @cuesheet.scan(@reg[field]) | ||
raise InvalidCuesheet.new("No fields were found for #{field.to_s}") if scan.empty? | ||
scan | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
module AlacIt | ||
class Index | ||
SECONDS_PER_MINUTE = 60 | ||
FRAMES_PER_SECOND = 75 | ||
FRAMES_PER_MINUTE = FRAMES_PER_SECOND * 60 | ||
|
||
attr_reader :minutes, :seconds, :frames | ||
|
||
def initialize(value=nil) | ||
case value | ||
when Array | ||
set_from_array!(value) | ||
when Integer | ||
set_from_integer!(value) | ||
end | ||
end | ||
|
||
def to_f | ||
((@minutes * SECONDS_PER_MINUTE) + (@seconds) + (@frames.to_f / FRAMES_PER_SECOND)).to_f | ||
end | ||
|
||
def to_i | ||
to_f.floor | ||
end | ||
|
||
def to_a | ||
[@minutes, @seconds, @frames] | ||
end | ||
|
||
def to_s | ||
"#{'%02d' % @minutes}:#{'%02d' % @seconds}:#{'%02d' % @frames}" | ||
end | ||
|
||
def to_human_ms | ||
"00:#{'%02d' % @minutes}:#{'%02d' % @seconds}" + (@frames.to_f / 75).round(3).to_s[1..-1] | ||
end | ||
|
||
def +(other) | ||
self.class.new(carrying_addition(other)) | ||
end | ||
|
||
def -(other) | ||
self.class.new(carrying_subtraction(other)) | ||
end | ||
|
||
def >(other) | ||
self.to_f > other.to_f | ||
end | ||
|
||
def <(other) | ||
self.to_f < other.to_f | ||
end | ||
|
||
def ==(other) | ||
self.to_a == other.to_a | ||
end | ||
|
||
def each | ||
to_a.each { |value| yield value } | ||
end | ||
|
||
private | ||
|
||
def carrying_addition(other) | ||
minutes, seconds, frames = *[@minutes + other.minutes, | ||
@seconds + other.seconds, @frames + other.frames] | ||
|
||
seconds, frames = *convert_with_rate(frames, seconds, FRAMES_PER_SECOND) | ||
minutes, seconds = *convert_with_rate(seconds, minutes, SECONDS_PER_MINUTE) | ||
[minutes, seconds, frames] | ||
end | ||
|
||
def carrying_subtraction(other) | ||
seconds = minutes = 0 | ||
|
||
my_frames = @frames + (@seconds * FRAMES_PER_SECOND) + (@minutes * FRAMES_PER_MINUTE) | ||
other_frames = other.frames + (other.seconds * FRAMES_PER_SECOND) + (other.minutes * FRAMES_PER_MINUTE) | ||
frames = my_frames - other_frames | ||
|
||
seconds, frames = *convert_with_rate(frames, seconds, FRAMES_PER_SECOND) | ||
minutes, seconds = *convert_with_rate(seconds, minutes, SECONDS_PER_MINUTE) | ||
[minutes, seconds, frames] | ||
end | ||
|
||
def convert_with_rate(from, to, rate, step=1) | ||
while from >= rate | ||
to += step | ||
from -= rate | ||
end | ||
[to, from] | ||
end | ||
|
||
def set_from_array!(array) | ||
if array.size != 3 || array.any?{|element| !element.is_a?(Integer)} | ||
raise ArgumentError.new("Must be initialized with an array in the format of [minutes, seconds,frames], all integers") | ||
end | ||
@minutes, @seconds, @frames = *array | ||
end | ||
|
||
def set_from_integer!(seconds) | ||
@minutes = 0 | ||
@frames = 0 | ||
@seconds = seconds | ||
|
||
while @seconds >= SECONDS_PER_MINUTE | ||
@minutes += 1 | ||
@seconds -= SECONDS_PER_MINUTE | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module AlacIt | ||
VERSION = '0.1.1' | ||
VERSION = '1.0.0' | ||
end |
Binary file not shown.
Oops, something went wrong.