Sha256: db30efc0e298e99bd3b70f4c3ae876a3d9d0f03c42c9ba79e6c44c17b4b6eff2

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require 'ostruct'
require 'taglib'
require 'fileutils'

module Ipod
  class Track < OpenStruct
    FinishedProgress = 0.97
    SecondsPerTick = 0.256

    def absolute_path
      File.join ipod_root, filename
    end

    def exists?
      @exists ||= File.exists? absolute_path
    end

    def finished?
      (playcount and playcount > 0) or progress > FinishedProgress
    end

    def progress
      pos / length
    end

    def pos
      Track.ticks_to_sec(bookmarktime)
    end

    def length
      return @length if @length
      # if file is writable TagLib opens it read-write and appears to write something when the file
      # gets closed, or in any case closing a file open read-write is slow on slow media.
      #
      # if file is readonly TagLib still opens it but closing is much faster.
      #
      old_stat = File::Stat.new(absolute_path)
      begin
        FileUtils.chmod('a-w', absolute_path)
        return @length = TagLib::FileRef.open(absolute_path){|file| file.audio_properties.length}
      ensure
        FileUtils.chmod(old_stat.mode, absolute_path)
      end
    end

    def self.sec_to_ticks(sec)
      sec / SecondsPerTick
    end

    def self.ticks_to_sec(ticks)
      ticks * SecondsPerTick
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ipod_db-0.2.10 lib/ipod/track.rb