Class: DiscId::Track

Inherits:
Object
  • Object
show all
Defined in:
lib/discid/track.rb

Overview

This class holds information about a single track.

Currently this includes the following fields:

  • number: The number of the track on the disc.
  • sectors: Length of the track in sectors.
  • offset: Start position of the track on the disc in sectors.
  • end_sector: End position of the track on the disc in sectors.
  • seconds: Length of the track in seconds.
  • start_time: Start position of the track on the disc in seconds.
  • end_time: End position of the track on the disc in seconds.
  • isrc: The track's ISRC (International Standard Recordings Code) if available.

You can access all fields either directly or with the square bracket notation:

track = Track.new(1, 150, 16007)
puts track.sectors   # 16007
puts track[:sectors] # 16007

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, offset, sectors, isrc) ⇒ Track

Initializes a new Track object.



68
69
70
71
72
73
# File 'lib/discid/track.rb', line 68

def initialize(number, offset, sectors, isrc)
  @number = number
  @offset = offset
  @sectors = sectors
  @isrc = isrc
end

Instance Attribute Details

#isrcString (readonly)

Note:

libdiscid >= 0.3.0 required. Older versions will always return nil. Not available on all platforms, see http://musicbrainz.org/doc/libdiscid#Feature_Matrix.

ISRC number of the track.

Returns:

  • (String)


65
66
67
# File 'lib/discid/track.rb', line 65

def isrc
  @isrc
end

#numberInteger (readonly)

The number of the track on the disc.

Returns:

  • (Integer)


46
47
48
# File 'lib/discid/track.rb', line 46

def number
  @number
end

#offsetInteger (readonly)

Start position of the track on the disc in sectors.

Returns:

  • (Integer)


56
57
58
# File 'lib/discid/track.rb', line 56

def offset
  @offset
end

#sectorsInteger (readonly)

Length of the track in sectors.

Returns:

  • (Integer)


51
52
53
# File 'lib/discid/track.rb', line 51

def sectors
  @sectors
end

Instance Method Details

#[](key) ⇒ Object

Allows access to all fields similar to accessing values in a hash.

Example:

track = Track.new(1, 150, 16007)
puts track.sectors   # 16007
puts track[:sectors] # 16007


110
111
112
113
114
115
# File 'lib/discid/track.rb', line 110

def [](key)
  if [:number, :sectors, :offset, :end_sector,
      :seconds, :start_time, :end_time, :isrc].include?(key.to_sym)
    method(key).call
  end
end

#end_sectorInteger

End position of the track on the disc in sectors.

Returns:

  • (Integer)


78
79
80
# File 'lib/discid/track.rb', line 78

def end_sector
  offset + sectors
end

#end_timeInteger

End position of the track on the disc in seconds.

Returns:

  • (Integer)


99
100
101
# File 'lib/discid/track.rb', line 99

def end_time
  DiscId.sectors_to_seconds(end_sector)
end

#secondsInteger

Length of the track in seconds.

Returns:

  • (Integer)


85
86
87
# File 'lib/discid/track.rb', line 85

def seconds
  DiscId.sectors_to_seconds(sectors)
end

#start_timeInteger

Start position of the track on the disc in seconds.

Returns:

  • (Integer)


92
93
94
# File 'lib/discid/track.rb', line 92

def start_time
  DiscId.sectors_to_seconds(offset)
end

#to_hHash Also known as: to_hash

Converts the Track into a Hash.

Returns:

  • (Hash)


120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/discid/track.rb', line 120

def to_h
  {
    :number       => number,
    :sectors      => sectors,
    :offset       => offset,
    :end_sector   => end_sector,
    :seconds      => seconds,
    :start_time   => start_time,
    :end_time     => end_time,
    :isrc         => isrc,
  }
end