Class: Wavify::Codecs::Flac::BitReader
- Inherits:
-
Object
- Object
- Wavify::Codecs::Flac::BitReader
- Defined in:
- lib/wavify/codecs/flac.rb
Overview
Internal bit reader used by the FLAC decoder.
Instance Attribute Summary collapse
-
#captured_bytes ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#align_to_byte ⇒ Object
:nodoc:.
-
#initialize(io) ⇒ BitReader
constructor
A new instance of BitReader.
- #read_bits(count) ⇒ Object
-
#read_signed_bits(count) ⇒ Object
:nodoc:.
Constructor Details
#initialize(io) ⇒ BitReader
Returns a new instance of BitReader.
65 66 67 68 69 70 |
# File 'lib/wavify/codecs/flac.rb', line 65 def initialize(io) @io = io @buffer = 0 @bits_available = 0 @captured_bytes = +"".b end |
Instance Attribute Details
#captured_bytes ⇒ Object (readonly)
:nodoc:
63 64 65 |
# File 'lib/wavify/codecs/flac.rb', line 63 def captured_bytes @captured_bytes end |
Instance Method Details
#align_to_byte ⇒ Object
:nodoc:
99 100 101 102 |
# File 'lib/wavify/codecs/flac.rb', line 99 def align_to_byte # :nodoc: @buffer = 0 @bits_available = 0 end |
#read_bits(count) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/wavify/codecs/flac.rb', line 72 def read_bits(count) raise InvalidFormatError, "bit count must be non-negative" unless count.is_a?(Integer) && count >= 0 return 0 if count.zero? value = 0 remaining = count while remaining.positive? fill_buffer_if_needed! take = [remaining, @bits_available].min shift = @bits_available - take chunk = (@buffer >> shift) & ((1 << take) - 1) value = (value << take) | chunk @bits_available -= take @buffer &= ((1 << @bits_available) - 1) remaining -= take end value end |
#read_signed_bits(count) ⇒ Object
:nodoc:
93 94 95 96 97 |
# File 'lib/wavify/codecs/flac.rb', line 93 def read_signed_bits(count) # :nodoc: value = read_bits(count) sign_bit = 1 << (count - 1) value.nobits?(sign_bit) ? value : (value - (1 << count)) end |