Class: WaveFile::ChunkReaders::RiffReader::FormatChunkReader

Inherits:
BaseChunkReader
  • Object
show all
Defined in:
lib/wavefile/chunk_readers/header_reader.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from BaseChunkReader

#raise_error, #read_chunk_size

Constructor Details

#initialize(file) ⇒ FormatChunkReader

Returns a new instance of FormatChunkReader.



107
108
109
# File 'lib/wavefile/chunk_readers/header_reader.rb', line 107

def initialize(file)
  @file = file
end

Instance Method Details

#readObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/wavefile/chunk_readers/header_reader.rb', line 111

def read
  chunk_size = read_chunk_size

  if chunk_size < MINIMUM_CHUNK_SIZE
    raise_error InvalidFormatError, "The format chunk is incomplete."
  end

  raw_bytes = read_chunk_body(CHUNK_IDS[:format], chunk_size)

  format_chunk = {}
  format_chunk[:audio_format],
  format_chunk[:channels],
  format_chunk[:sample_rate],
  format_chunk[:byte_rate],
  format_chunk[:block_align],
  format_chunk[:bits_per_sample] = raw_bytes.slice!(0...MINIMUM_CHUNK_SIZE).unpack("vvVVvv")

  if chunk_size > MINIMUM_CHUNK_SIZE
    format_chunk[:extension_size] = raw_bytes.slice!(0...2).unpack(UNSIGNED_INT_16).first

    if format_chunk[:extension_size] == nil
      raise_error InvalidFormatError, "The format chunk is missing an expected extension."
    end

    if format_chunk[:extension_size] != raw_bytes.length
      raise_error InvalidFormatError, "The format chunk extension is shorter than expected."
    end

    # TODO: Parse the extension
  end

  UnvalidatedFormat.new(format_chunk)
end