Module: DiscId

Defined in:
lib/discid.rb,
lib/discid/lib.rb,
lib/discid/disc.rb,
lib/discid/track.rb,
lib/discid/version.rb,
lib/discid/disc_error.rb

Overview

The DiscId module allows calculating DiscIDs (MusicBrainz and freedb) for Audio CDs. Additionally the library can extract the MCN/UPC/EAN and the ISRCs from disc.

The main interface for using this module are the DiscId.read and DiscId.put methods which both return an instance of Disc. DiscId.read allows you to read the data from an actual CD drive while DiscId.put allows you to calculate the DiscID for a previously read CD TOC.

Depending on the version of libdiscid and the operating system used additional features like reading the MCN or ISRCs from the disc might be available. You can check for supported features with DiscId.has_feature?.

Examples:

Read the TOC, MCN and ISRCs

require 'discid'

device = "/dev/cdrom"
disc = DiscId.read(device, :mcn, :isrc)

# Print information about the disc:
puts "DiscID      : #{disc.id}"
puts "FreeDB ID   : #{disc.freedb_id}"
puts "Total length: #{disc.seconds} seconds"
puts "MCN         : #{disc.mcn}"

# Print information about individual tracks:
disc.tracks do |track|
  puts "Track ##{track.number}"
  puts "  Length: %02d:%02d (%i sectors)" %
      [track.seconds / 60, track.seconds % 60, track.sectors]
  puts "  ISRC  : %s" % track.isrc
end

Get the DiscID for an existing TOC

require 'discid'

first_track = 1
sectors = 82255
offsets = [150, 16157, 35932, 57527]
disc = DiscId.put(first_track, sectors, offsets)
puts disc.id # Output: E5VLOkhodzhvsMlK8LSNVioYOgY-

Check for supported MCN feature

disc = DiscId.read(nil, :mcn)
puts "MCN: #{disc.mcn}" if DiscId.has_feature?(:mcn)

See Also:

Defined Under Namespace

Classes: Disc, DiscError, Track

Constant Summary collapse

LIBDISCID_VERSION =
Note:

This will only give meaningful results for libdiscid 0.4.0 and higher. For lower versions this constant will always have the value "libdiscid < 0.4.0".

The libdiscid version.

Lib.get_version_string
VERSION =

The version of ruby-discid.

"1.3.2"

Class Method Summary collapse

Class Method Details

.default_deviceString

Return the name of the default disc drive for this operating system.

Returns:

  • (String)

    An operating system dependent device identifier


126
127
128
# File 'lib/discid.rb', line 126

def self.default_device
  Lib.default_device
end

.feature_listArray<Symbol>

Note:

libdiscid >= 0.5.0 required. Older versions will return only [:read].

A list of features supported by the current platform.

Currently the following features are available:

  • :read
  • :mcn
  • :isrc

Returns:

  • (Array<Symbol>)

155
156
157
# File 'lib/discid.rb', line 155

def self.feature_list
  return Lib::Features.symbols.select {|f| Lib.has_feature(f) == 1}
end

.has_feature?(feature) ⇒ Boolean

Note:

libdiscid >= 0.5.0 required. Older versions will return true for :read and false for anything else.

Check if a certain feature is implemented on the current platform.

You can obtain a list of supported features with feature_list.

Parameters:

  • feature (:read, :mcn, :isrc)

Returns:

  • (Boolean)

    True if the feature is implemented and false if not.


139
140
141
142
# File 'lib/discid.rb', line 139

def self.has_feature?(feature)
  feature = feature.to_sym if feature.respond_to? :to_sym
  return self.feature_list.include? feature
end

.put(first_track, sectors, offsets) ⇒ Disc

Provides the TOC of a known CD.

This function may be used if the TOC has been read earlier and you want to calculate the disc ID afterwards, without accessing the disc drive.

Parameters:

  • first_track (Integer)

    The number of the first audio track on the disc (usually one).

  • sectors (Integer)

    The total number of sectors on the disc.

  • offsets (Array)

    An array with track offsets (sectors) for each track.

Returns:

Raises:

  • (DiscError)

    The TOC could not be set. Exception#messagecontains error details.


117
118
119
120
121
# File 'lib/discid.rb', line 117

def self.put(first_track, sectors, offsets)
  disc = Disc.new
  disc.put first_track, sectors, offsets
  return disc
end

.read(device = nil, *features) ⇒ Disc

Note:

libdiscid >= 0.5.0 is required for the feature selection to work. Older versions will allways read MCN and ISRCs when supported. See http://musicbrainz.org/doc/libdiscid#Feature_Matrix for a list of supported features by version and platform.

Read the disc in the given CD-ROM/DVD-ROM drive extracting only the TOC and additionally specified features.

This function reads the disc in the drive specified by the given device identifier. If the device is nil, the default device, as returned by default_device, is used.

This function will always read the TOC, but additional features like :mcn and :isrc can be set using the features parameter. You can set multiple features.

Examples:

Read only the TOC:

disc = DiscId.read(device)

Read the TOC, MCN and ISRCs:

disc = DiscId.read(device, :mcn, :isrc)

Parameters:

  • device (String) (defaults to: nil)

    The device identifier. If set to nil default_device will be used.

  • features (:mcn, :isrc)

    List of features to use. :read is always implied.

Returns:

Raises:

  • (TypeError)

    device can not be converted to a String.

  • (DiscError)

    Error reading from device. Exception#message contains error details.


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

def self.read(device = nil, *features)
  disc = Disc.new
  disc.read device, *features
  return disc
end