Class: AudioStream::RingBuffer

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/audio_stream/ring_buffer.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ RingBuffer

Returns a new instance of RingBuffer.



5
6
7
8
# File 'lib/audio_stream/ring_buffer.rb', line 5

def initialize(*args, &block)
  @array = Array.new(*args, &block)
  @seek = 0
end

Instance Method Details

#[](idx) ⇒ Object



19
20
21
# File 'lib/audio_stream/ring_buffer.rb', line 19

def [](idx)
  @array[(idx+@seek) % @array.size]
end

#[]=(idx, val) ⇒ Object



23
24
25
# File 'lib/audio_stream/ring_buffer.rb', line 23

def []=(idx, val)
  @array[(idx+@seek) % @array.size] = val
end

#currentObject



27
28
29
# File 'lib/audio_stream/ring_buffer.rb', line 27

def current
  self[0]
end

#current=(val) ⇒ Object



31
32
33
# File 'lib/audio_stream/ring_buffer.rb', line 31

def current=(val)
  self[0] = val
end

#each(&block) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/audio_stream/ring_buffer.rb', line 10

def each(&block)
  Enumerator.new do|y|
    start = @seek
    @array.size.times {|i|
      y << self[i]
    }
  end.each(&block)
end

#rotate(step = 1) ⇒ Object



35
36
37
# File 'lib/audio_stream/ring_buffer.rb', line 35

def rotate(step=1)
  @seek = (@seek + step) % @array.size
end