Class: Raiff::Chunk::SoundData
- Inherits:
-
Raiff::Chunk
- Object
- Raiff::Chunk
- Raiff::Chunk::SoundData
- Defined in:
- lib/raiff/chunk/sound_data.rb
Instance Attribute Summary collapse
-
#block_size ⇒ Object
readonly
Properties ===========================================================.
-
#offset ⇒ Object
readonly
Properties ===========================================================.
-
#start_offset ⇒ Object
readonly
Properties ===========================================================.
Attributes inherited from Raiff::Chunk
Instance Method Summary collapse
- #decoder ⇒ Object
-
#initialize(file, common) ⇒ SoundData
constructor
Instance Methods =====================================================.
- #read_sample ⇒ Object
- #read_samples(count) ⇒ Object
Methods inherited from Raiff::Chunk
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_size ⇒ Object (readonly)
Properties ===========================================================
4 5 6 |
# File 'lib/raiff/chunk/sound_data.rb', line 4 def block_size @block_size end |
#offset ⇒ Object (readonly)
Properties ===========================================================
4 5 6 |
# File 'lib/raiff/chunk/sound_data.rb', line 4 def offset @offset end |
#start_offset ⇒ Object (readonly)
Properties ===========================================================
4 5 6 |
# File 'lib/raiff/chunk/sound_data.rb', line 4 def start_offset @start_offset end |
Instance Method Details
#decoder ⇒ Object
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_sample ⇒ Object
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 |