Class: Riff::Reader::Chunk

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

Overview

The Chunk object represents a chunk of a larger RIFF file. The chunk may be interrogated to reveal its fourcc and the size of its body. The chunk also provides the body method to access the stream of data within the chunk. Keep in mind that this bit of data can be very, very large in media files (like .wav files) and that reading them in a language like ruby will be quite slow.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fourccObject (readonly)

the four-character code of this chunk, always four characters long (shorter fourcc’s are padded with spaces)



124
125
126
# File 'lib/riff/reader.rb', line 124

def fourcc
  @fourcc
end

#lengthObject (readonly)

the length of the body of the chunk (the length of the chunk minus the the fourcc and length bytes; thus the length of the chunk - 8) This data is read directly from the chunk’s preamble and calling this is much, much faster than body.size



130
131
132
# File 'lib/riff/reader.rb', line 130

def length
  @length
end

#offsetObject (readonly)

the offset of this chunk from the start of its file



133
134
135
# File 'lib/riff/reader.rb', line 133

def offset
  @offset
end

Class Method Details

.read_chunk(fp) ⇒ Object

:nodoc:



136
137
138
139
140
141
142
143
144
# File 'lib/riff/reader.rb', line 136

def self.read_chunk(fp) #:nodoc:
  code = fp.read(4)  
  fp.pos -= 4
  if code == "RIFF" or code == "LIST" then
    ListChunk.new(fp)
  else
    Chunk.new(fp)
  end
end

Instance Method Details

#bodyObject

Reads the entire body of the chunk (all of the data following the fourcc and size)



147
148
149
150
# File 'lib/riff/reader.rb', line 147

def body
  @file.pos = @offset + 8
  @file.read @length
end

#each_byteObject

Yields each byte of the data chunk.



159
160
161
162
163
# File 'lib/riff/reader.rb', line 159

def each_byte
  while @file.pos < @offset + 8 + @length
    yield @file.getc  
  end
end

#is_list?Boolean

Returns true if this object is a ListChunk. A convenience to typing instance_of? Riff::ListChunk

Returns:

  • (Boolean)


154
155
156
# File 'lib/riff/reader.rb', line 154

def is_list?
  instance_of? Riff::Reader::ListChunk
end