Class: Wavify::Codecs::Flac::BitReader

Inherits:
Object
  • Object
show all
Defined in:
lib/wavify/codecs/flac.rb

Overview

Internal bit reader used by the FLAC decoder.

Instance Attribute Summary collapse

Instance Method Summary collapse

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_bytesObject (readonly)

:nodoc:



63
64
65
# File 'lib/wavify/codecs/flac.rb', line 63

def captured_bytes
  @captured_bytes
end

Instance Method Details

#align_to_byteObject

: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

Raises:



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