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

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.



50
51
52
# File 'lib/wavefile/chunk_readers/data_chunk_reader.rb', line 50

def current_sample_frame
  @current_sample_frame
end

#formatObject (readonly)

Returns the value of attribute format.



50
51
52
# File 'lib/wavefile/chunk_readers/data_chunk_reader.rb', line 50

def format
  @format
end

#raw_native_formatObject (readonly)

Returns the value of attribute raw_native_format.



50
51
52
# File 'lib/wavefile/chunk_readers/data_chunk_reader.rb', line 50

def raw_native_format
  @raw_native_format
end

#readable_formatObject (readonly)

Returns the value of attribute readable_format.



50
51
52
# File 'lib/wavefile/chunk_readers/data_chunk_reader.rb', line 50

def readable_format
  @readable_format
end

#total_sample_framesObject (readonly)

Returns the value of attribute total_sample_frames.



50
51
52
# File 'lib/wavefile/chunk_readers/data_chunk_reader.rb', line 50

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
# 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
    #FIXME: Do something different here, because the end of the file has not actually necessarily been reached
    raise EOFError
  elsif sample_frame_count > sample_frames_remaining
    sample_frame_count = sample_frames_remaining
  end

  samples = @io.sysread(sample_frame_count * @native_format.block_align).unpack(@pack_code)
  @current_sample_frame += sample_frame_count

  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

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