Class: Cdio::Device

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

Overview

class Device

CD Input and control class for discs/devices

SYNOPSIS

require "cdio"
d = Cdio::Device.new("", Rubycdio::DRIVER_UNKNOWN)
drive_name = d.device()
hw  = d.hwinfo()
if hw then
   puts "drive: %s, vendor: %s, model: %s, revision: %s" %
     [drive_name, hw["vendor"], hw["model"], hw["revision"]]
end

drivers.each_pair { |driver_name, driver_id|
begin 
    if driver?(driver_id):
        puts "Driver %s is installed." % driver_name
    end
rescue ValueError
end

Instance Method Summary collapse

Constructor Details

#initialize(source = nil, driver_id = nil, access_mode = nil) ⇒ Device

Returns a new instance of Device.



433
434
435
436
437
438
439
# File 'lib/cdio.rb', line 433

def initialize(source=nil, driver_id=nil,
               access_mode=nil)
  @cd = nil
  if source or driver_id
    open(source, driver_id, access_mode)
  end
end

Instance Method Details

#arg(key) ⇒ Object

Get the value associatied with key.



515
516
517
# File 'lib/cdio.rb', line 515

def arg(key)
  return Rubycdio::get_arg(@cd, key)
end

#ATAPI?Boolean

Returns: bool

return true if CD-ROM understand ATAPI commands.

Returns:

  • (Boolean)


444
445
446
# File 'lib/cdio.rb', line 444

def ATAPI?()
  return Rubycdio::ATAPI?(@cd)
end

#audio_pauseObject

Returns: status

Pause playing CD through analog output. A DeviceError exception may be raised.



452
453
454
455
# File 'lib/cdio.rb', line 452

def audio_pause()
  drc=Rubycdio::audio_pause(@cd)
  possibly_raise_exception__(drc)
end

#audio_play_lsn(start_lsn, end_lsn) ⇒ Object

Returns: status

Playing CD through analog output from start_lsn to ending_lsn A DeviceError exception may be raised.



461
462
463
464
# File 'lib/cdio.rb', line 461

def audio_play_lsn(start_lsn, end_lsn)
  drc=Rubycdio::audio_play_lsn(@cd, start_lsn, end_lsn)
  possibly_raise_exception__(drc)
end

#audio_resumeObject

Returns: status

Resume playing an audio CD through the analog interface. A DeviceError exception may be raised.



470
471
472
473
# File 'lib/cdio.rb', line 470

def audio_resume()
  drc=Rubycdio::audio_resume(@cd)
  possibly_raise_exception__(drc)
end

#audio_stopObject

Returns: status

Stop playing an audio CD through the analog interface. A DeviceError exception may be raised.



479
480
481
482
# File 'lib/cdio.rb', line 479

def audio_stop()
  drc=Rubycdio::audio_stop(@cd)
  possibly_raise_exception__(drc)
end

#blocksize=(blocksize) ⇒ Object

Set the blocksize for subsequent reads. An exception is thrown on error.



824
825
826
827
# File 'lib/cdio.rb', line 824

def blocksize=(blocksize)
  drc = Rubycdio::set_blocksize(@cd, blocksize)
  possibly_raise_exception__(drc)
end

#closeObject

Free (C memory) resources associated with the object. Call this when done using using CD reading/control operations for the current device.



487
488
489
490
491
492
493
494
# File 'lib/cdio.rb', line 487

def close()
  if @cd 
    Rubycdio::close(@cd)
  else
    puts "***No object to close"
  end
  @cd=nil
end

#deviceObject

Returns: String

Get the default CD device. If we haven’t initialized a specific device driver), then find a suitable one and return the default device for that. In some situations of drivers or OS’s we can’t find a CD device if there is no media in it and it is possible for this routine to return nil even though there may be a hardware CD-ROM.



527
528
529
530
531
532
# File 'lib/cdio.rb', line 527

def device()
  if @cd 
    return Rubycdio::get_arg(@cd, "source")
  end
  return Rubycdio::get_device(@cd)
end

#disc_last_lsnObject

Returns: Fixnum

Get the LSN of the end of the CD

DriverError and IOError may raised on error.



539
540
541
542
543
544
545
# File 'lib/cdio.rb', line 539

def disc_last_lsn()
  lsn = Rubycdio::get_disc_last_lsn(@cd)
  if lsn == Rubycdio::INVALID_LSN
    raise DriverError 
  end
  return lsn
end

#disc_modeObject

Returns: String

Get disc mode - the kind of CD (CD-DA, CD-ROM mode 1, CD-MIXED, …) that we’ve got. The notion of ‘CD’ is extended a little to include DVD’s.



552
553
554
555
# File 'lib/cdio.rb', line 552

def disc_mode()
  if not @cd then return "Uninitialized Device" end
  return Rubycdio::get_disc_mode(@cd)
end

#drive_capObject

Get drive capabilities of device.

In some situations of drivers or OS’s we can’t find a CD device if there is no media in it. In this situation capabilities will show up as empty even though there is a hardware CD-ROM. get_drive_cap_dev()->(read_cap, write_cap, misc_cap)

Get drive capabilities of device.

In some situations of drivers or OS’s we can’t find a CD device if there is no media in it. In this situation capabilities will show up as empty even though there is a hardware CD-ROM.



571
572
573
574
575
576
# File 'lib/cdio.rb', line 571

def drive_cap()
b_read_cap, b_write_cap, b_misc_cap = Rubycdio::get_drive_cap(@cd)
return [convert_drive_cap_read(b_read_cap),
        convert_drive_cap_write(b_write_cap),
        convert_drive_cap_misc(b_misc_cap)]
end

#drive_cap_dev(device = nil) ⇒ Object



578
579
580
581
582
583
584
585
586
587
# File 'lib/cdio.rb', line 578

def drive_cap_dev(device=nil)
  #---
  ### FIXME: combine into above by testing on the type of device.
  #+++
  b_read_cap, b_write_cap, b_misc_cap = 
  Rubycdio::get_drive_cap_dev(device);
  return [convert_drive_cap_read(b_read_cap), 
          convert_drive_cap_write(b_write_cap), 
          convert_drive_cap_misc(b_misc_cap)]
end

#driver_idObject

Returns: Fixnum

Return the driver id of the driver in use. if object has not been initialized or is nil, return Rubycdio::DRIVER_UNKNOWN.



604
605
606
# File 'lib/cdio.rb', line 604

def driver_id()
  return Rubycdio::get_driver_id(@cd)
end

#driver_nameObject

Returns: String

return a string containing the name of the driver in use.

An IOError exception is raised on error.



594
595
596
597
# File 'lib/cdio.rb', line 594

def driver_name()
  if not @cd then raise DriverUninitError end
  return Rubycdio::get_driver_name(@cd)
end

#eject_mediaObject

Eject media in CD drive if there is a routine to do so. A DeviceError exception may be raised.



498
499
500
501
502
# File 'lib/cdio.rb', line 498

def eject_media()
  drc=Rubycdio::eject_media(@cd)
  @cd = nil
  possibly_raise_exception__(drc)
end

#eject_media_drive(drive = nil) ⇒ Object

Eject media in CD drive if there is a routine to do so. An exception is thrown on error.



506
507
508
509
510
# File 'lib/cdio.rb', line 506

def eject_media_drive(drive=nil)
  ### FIXME: combine into above by testing if drive is the string
  ### nil versus drive = Rubycdio::DRIVER_UNKNOWN
  Rubycdio::eject_media_drive(drive)
end

#first_trackObject

Returns: Track

return a Track object of the first track. nil is returned if there was a problem.



612
613
614
615
616
617
618
# File 'lib/cdio.rb', line 612

def first_track()
  track = Rubycdio::get_first_track_num(@cd)
  if track == Rubycdio::INVALID_TRACK
  return nil
  end
  return Track.new(@cd, track)
end

#hwinfoObject

Returns: “model”=>??, “release”=>??

Get the CD-ROM hardware info via a SCSI MMC INQUIRY command.



623
624
625
# File 'lib/cdio.rb', line 623

def hwinfo()
  return Rubycdio::get_hwinfo(@cd)
end

#joliet_levelObject

Return the Joliet level recognized for cdio. This only makes sense for something that has an ISO-9660 filesystem.



632
633
634
# File 'lib/cdio.rb', line 632

def joliet_level()
  return Rubycdio::get_joliet_level(@cd)
end

#last_sessionObject

Get the LSN of the first track of the last session of on the CD. An exception is thrown on error.



641
642
643
644
645
# File 'lib/cdio.rb', line 641

def last_session()
  drc, session = Rubycdio::get_last_session(@cd)
  possibly_raise_exception__(drc)
  return session
end

#last_trackObject

Returns: Track

return a Track object of the first track. nil is returned if there was a problem.



651
652
653
654
655
656
657
# File 'lib/cdio.rb', line 651

def last_track()
track = Rubycdio::get_last_track_num(@cd)
  if track == Rubycdio::INVALID_TRACK
    return nil
  end
  return Track.new(@cd, track)
end

#lseek(offset, whence) ⇒ Object

Returns: Fixnum Reposition read offset Similar to (if not the same as) libc’s fseek()

offset is amount to seek and whence is like corresponding parameter in libc’s lseek, e.g. it should be SEEK_SET or SEEK_END.

the offset is returned or -1 on error.



722
723
724
# File 'lib/cdio.rb', line 722

def lseek(offset, whence)
  return Rubycdio::lseek(@cd, offset, whence)
end

#mcnObject

Returns: String

Get the media catalog number (MCN) from the CD.



662
663
664
# File 'lib/cdio.rb', line 662

def mcn()
  return Rubycdio::get_mcn(@cd)
end

#media_changed?Boolean

Find out if media has changed since the last call. Return true if media has changed since last call. An exception Error is given on error.

Returns:

  • (Boolean)

Raises:



669
670
671
672
673
674
675
# File 'lib/cdio.rb', line 669

def media_changed?()
  drc = Rubycdio::get_media_changed(@cd)
  if drc == 0 then return false end
  if drc == 1 then return true end
  possibly_raise_exception__(drc)
  raise DeviceException
end

#num_tracksObject

Returns: Fixnum

Return the number of tracks on the CD. A TrackError or IOError exception may be raised on error.



681
682
683
684
685
686
687
# File 'lib/cdio.rb', line 681

def num_tracks()
  track = Rubycdio::get_num_tracks(@cd)
  if track == Rubycdio::INVALID_TRACK
    raise TrackError
  end
  return track
end

#open(source = nil, driver_id = Rubycdio::DRIVER_UNKNOWN, access_mode = nil) ⇒ Object

Sets up to read from place specified by source, driver_id and access_mode. This should be called before using any other routine except those that act on a CD-ROM drive by name.

If nil is the value of source, we’ll use the default driver device. If nil is the value of driver_id, we’ll find a suitable device driver.

If device object was, previously opened it is closed first.

Device is opened so that subsequent operations can be performed.



736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'lib/cdio.rb', line 736

def open(source=nil, driver_id=Rubycdio::DRIVER_UNKNOWN,
         access_mode=nil)
  if not driver_id 
    driver_id=Rubycdio::DRIVER_UNKNOWN 
  end
  if not source
    source = '' 
  end
  if not access_mode 
    access_mode = '' 
  end
if @cd
  close()
end
  @cd = Rubycdio::open_cd(source, driver_id, access_mode)
end

#read(size) ⇒ Object

Returns: [size, data]

Reads the next size bytes. Similar to (if not the same as) libc’s read()

The number of bytes read and the data is returned. A DeviceError exception may be raised.



760
761
762
763
764
# File 'lib/cdio.rb', line 760

def read(size)
  size, data = Rubycdio::read_cd(@cd, size)
  possibly_raise_exception__(size)
  return [size, data]
end

#read_data_blocks(lsn, blocks = 1) ⇒ Object

return [size, data]

Reads a number of data sectors (AKA blocks).

lsn is the sector to read; bytes is the number of bytes to read. A DeviceError exception may be raised.



772
773
774
775
776
777
778
779
780
781
782
# File 'lib/cdio.rb', line 772

def read_data_blocks(lsn, blocks=1)
  size = Rubycdio::ISO_BLOCKSIZE*blocks
  triple  = Rubycdio::read_data_bytes(@cd, lsn,
                                      Rubycdio::ISO_BLOCKSIZE, size) 
  if not triple
    return [-1, nil]
  end
data, size, drc = triple
  possibly_raise_exception__(drc)
  return [size, data]
end

#read_sectors(lsn, read_mode, blocks = 1) ⇒ Object

return [blocks, data]

Reads a number of sectors (AKA blocks).

lsn is the sector to read, bytes is the number of bytes to read.

If read_mode is Rubycdio::MODE_AUDIO, the return buffer size will be truncated to multiple of Rubycdio::CDIO_FRAMESIZE_RAW i_blocks bytes.

If read_mode is Rubycdio::MODE_DATA, buffer will be truncated to a multiple of Rubycdio::ISO_BLOCKSIZE, Rubycdio::M1RAW_SECTOR_SIZE or Rubycdio::M2F2_SECTOR_SIZE bytes depending on what mode the data is in.

If read_mode is Rubycdio::MODE_M2F1, buffer will be truncated to a multiple of Rubycdio::M2RAW_SECTOR_SIZE bytes.

If read_mode is Rubycdio::MODE_M2F2, the return buffer size will be truncated to a multiple of Rubycdio::CD_FRAMESIZE bytes.

The number of bytes read and the data is returned. A DeviceError exception may be raised.



805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# File 'lib/cdio.rb', line 805

def read_sectors(lsn, read_mode, blocks=1)
  begin
    blocksize = read_mode2blocksize()[read_mode]
    size = blocks * blocksize
  rescue
    return [-1, nil]
  end
  triple = Rubycdio::read_sectors(@cd, lsn, read_mode, size)
  if not triple
    return [-1, nil]
  end
  data, size, drc = triple
  possibly_raise_exception__(drc)
  blocks = size / blocksize
  return [blocks, data]
end

#speed=(speed) ⇒ Object

Set the drive speed. An exception is thrown on error.



830
831
832
833
# File 'lib/cdio.rb', line 830

def speed=(speed)
  drc = Rubycdio::set_speed(@cd, speed)
  possibly_raise_exception__(drc)
end

#track(track_num) ⇒ Object

Returns: track

Return a track object for the given track number.



692
693
694
# File 'lib/cdio.rb', line 692

def track(track_num)
  return Track.new(@cd, track_num)
end

#track_for_lsn(lsn) ⇒ Object

Returns: track

Find the track which contains lsn. nil is returned if the lsn outside of the CD or if there was some error.

If lsn is before the pregap of the first track, A track object with a 0 track is returned. Otherwise we return the track that spans the LSN.



705
706
707
708
709
710
711
# File 'lib/cdio.rb', line 705

def track_for_lsn(lsn)
  track = Rubycdio::get_last_track_num(@cd)
  if track == Rubycdio::INVALID_TRACK
    return nil
  end
  return Track.new(@cd, track)
end