Class: WaveFile::ChunkReaders::DataChunkReader

Inherits:
BaseChunkReader show all
Defined in:
lib/wavefile/chunk_readers/data_chunk_reader.rb

Overview

Internal

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseChunkReader

#raise_error, #read_entire_chunk_body

Constructor Details

#initialize(io, chunk_size, raw_native_format, format = nil) ⇒ DataChunkReader

:nodoc:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/wavefile/chunk_readers/data_chunk_reader.rb', line 5

def initialize(io, chunk_size, raw_native_format, format=nil)
  @io = io
  @raw_native_format = raw_native_format

  @total_sample_frames = chunk_size / @raw_native_format.block_align
  @current_sample_frame = 0

  @readable_format = true
  begin
    @native_format = @raw_native_format.to_validated_format
    @pack_code = PACK_CODES[@native_format.sample_format][@native_format.bits_per_sample]
  rescue FormatError
    @readable_format = false
    @native_format = nil
    @pack_code = nil
  end

  @format = (format == nil) ? (@native_format || @raw_native_format) : format
end

Instance Attribute Details

#current_sample_frameObject (readonly)

Returns the value of attribute current_sample_frame.



59
60
61
# File 'lib/wavefile/chunk_readers/data_chunk_reader.rb', line 59

def current_sample_frame
  @current_sample_frame
end

#formatObject (readonly)

Returns the value of attribute format.



59
60
61
# File 'lib/wavefile/chunk_readers/data_chunk_reader.rb', line 59

def format
  @format
end

#raw_native_formatObject (readonly)

Returns the value of attribute raw_native_format.



59
60
61
# File 'lib/wavefile/chunk_readers/data_chunk_reader.rb', line 59

def raw_native_format
  @raw_native_format
end

#readable_formatObject (readonly)

Returns the value of attribute readable_format.



59
60
61
# File 'lib/wavefile/chunk_readers/data_chunk_reader.rb', line 59

def readable_format
  @readable_format
end

#total_sample_framesObject (readonly)

Returns the value of attribute total_sample_frames.



59
60
61
# File 'lib/wavefile/chunk_readers/data_chunk_reader.rb', line 59

def total_sample_frames
  @total_sample_frames
end

Instance Method Details

#read(sample_frame_count) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/wavefile/chunk_readers/data_chunk_reader.rb', line 25

def read(sample_frame_count)
  raise UnsupportedFormatError unless @readable_format

  if @current_sample_frame >= @total_sample_frames
    # The end of the file has not necessarily been reached if there is another chunk after
    # the data chunk, but EOFError is raised for backwards compatibility with older versions
    # of the gem, and because it is also generally semantically correct that the "relevant"
    # end of the file has been reached.
    raise EOFError
  elsif sample_frame_count > sample_frames_remaining
    sample_frame_count = sample_frames_remaining
  end

  byte_count = sample_frame_count * @native_format.block_align
  samples = @io.read(byte_count)
  if samples.nil?
    raise EOFError
  end
  samples = samples.unpack(@pack_code)

  if @native_format.bits_per_sample == 24
    samples = convert_24_bit_samples(samples)
  end

  if @native_format.channels > 1
    samples = samples.each_slice(@native_format.channels).to_a
  end

  @current_sample_frame += samples.length

  buffer = Buffer.new(samples, @native_format)
  buffer.convert(@format)
end