Class: Raiff::Chunk::SoundData

Inherits:
Raiff::Chunk show all
Defined in:
lib/raiff/chunk/sound_data.rb

Instance Attribute Summary collapse

Attributes inherited from Raiff::Chunk

#id, #size

Instance Method Summary collapse

Methods inherited from Raiff::Chunk

#inspect

Constructor Details

#initialize(file, common) ⇒ SoundData

Instance Methods =====================================================



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/raiff/chunk/sound_data.rb', line 10

def initialize(file, common)
  super(file)
  
  @common = common

  @start_offset = file.offset
  @end_offset = file.offset + @size
  
  @offset, @block_size = file.unpack('NN')

  file.read(@offset)
  
  @sample_count = (@end_offset - file.offset) / @common.bytes_per_sample
  @bit_offset = @common.bytes_per_sample * 8 - @common.sample_size
  
  file.advance(@sample_count * @common.bytes_per_sample)
end

Instance Attribute Details

#block_sizeObject (readonly)

Properties ===========================================================



4
5
6
# File 'lib/raiff/chunk/sound_data.rb', line 4

def block_size
  @block_size
end

#offsetObject (readonly)

Properties ===========================================================



4
5
6
# File 'lib/raiff/chunk/sound_data.rb', line 4

def offset
  @offset
end

#start_offsetObject (readonly)

Properties ===========================================================



4
5
6
# File 'lib/raiff/chunk/sound_data.rb', line 4

def start_offset
  @start_offset
end

Instance Method Details

#decoderObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/raiff/chunk/sound_data.rb', line 47

def decoder
sample_size = @common.bytes_per_sample
channels = (1..@common.channels).to_a
negative = 1 << (@common.sample_size - 1)

@decoder =
  case (@bit_offset)
  when 0
    lambda {
      channels.collect {
        v = @file.read(sample_size).to_s.bytes.inject(0) do |a, b|
          a << 8 | b
        end
        
        (v & negative) ? ((v ^ negative) - negative) : v
      }
    }
  else
    lambda {
      channels.collect {
        v = @file.read(sample_size).to_s.bytes.inject(0) do |a, b|
          a << 8 | b
        end >> @bit_offset - @value_offset

        (v & negative) ? ((v ^ negative) - negative) : v
      }
    }
  end
end

#read_sampleObject



28
29
30
# File 'lib/raiff/chunk/sound_data.rb', line 28

def read_sample
  self.decoder.call
end

#read_samples(count) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/raiff/chunk/sound_data.rb', line 32

def read_samples(count)
  if (count + @file.offset > @end_offset)
    count = (@end_offset - @file.offset) / @common.bytes_per_sample
  end
  
  samples = [ ]
  _decoder = self.decoder

  count.times do
    samples << _decoder.call
  end
  
  samples
end