Class: Vizcore::Audio::RingBuffer
- Inherits:
-
Object
- Object
- Vizcore::Audio::RingBuffer
- Defined in:
- lib/vizcore/audio/ring_buffer.rb
Overview
Thread-safe circular buffer for recent audio samples.
Instance Attribute Summary collapse
-
#capacity ⇒ Object
readonly
Returns the value of attribute capacity.
Instance Method Summary collapse
- #clear ⇒ void
-
#initialize(capacity) ⇒ RingBuffer
constructor
A new instance of RingBuffer.
-
#latest(count = nil) ⇒ Array<Float>
Newest values first-in-order.
-
#metrics ⇒ Hash
Buffer health counters for runtime diagnostics.
- #push(sample) ⇒ void
- #size ⇒ Integer
- #write(samples) ⇒ void
Constructor Details
#initialize(capacity) ⇒ RingBuffer
Returns a new instance of RingBuffer.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/vizcore/audio/ring_buffer.rb', line 12 def initialize(capacity) raise ArgumentError, "capacity must be positive" unless capacity.to_i.positive? @capacity = Integer(capacity) @buffer = Array.new(@capacity, 0.0) @write_index = 0 @size = 0 @write_count = 0 @overrun_count = 0 @underrun_count = 0 @mutex = Mutex.new end |
Instance Attribute Details
#capacity ⇒ Object (readonly)
Returns the value of attribute capacity.
9 10 11 |
# File 'lib/vizcore/audio/ring_buffer.rb', line 9 def capacity @capacity end |
Instance Method Details
#clear ⇒ void
This method returns an undefined value.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/vizcore/audio/ring_buffer.rb', line 84 def clear @mutex.synchronize do @buffer.fill(0.0) @write_index = 0 @size = 0 @write_count = 0 @overrun_count = 0 @underrun_count = 0 end end |
#latest(count = nil) ⇒ Array<Float>
Returns newest values first-in-order.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/vizcore/audio/ring_buffer.rb', line 50 def latest(count = nil) @mutex.synchronize do return [] if @size.zero? requested = count ? Integer(count) : @size return [] if requested <= 0 @underrun_count += requested - @size if requested > @size length = [requested, @size].min start = (@write_index - length) % @capacity extract_range(start, length) end end |
#metrics ⇒ Hash
Returns buffer health counters for runtime diagnostics.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/vizcore/audio/ring_buffer.rb', line 71 def metrics @mutex.synchronize do { capacity: @capacity, size: @size, write_count: @write_count, overrun_count: @overrun_count, underrun_count: @underrun_count } end end |
#push(sample) ⇒ void
This method returns an undefined value.
44 45 46 |
# File 'lib/vizcore/audio/ring_buffer.rb', line 44 def push(sample) write([sample]) end |
#size ⇒ Integer
66 67 68 |
# File 'lib/vizcore/audio/ring_buffer.rb', line 66 def size @mutex.synchronize { @size } end |
#write(samples) ⇒ void
This method returns an undefined value.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/vizcore/audio/ring_buffer.rb', line 27 def write(samples) normalized = normalize_samples(samples) return if normalized.empty? @mutex.synchronize do normalized.each do |sample| @overrun_count += 1 if @size == @capacity @buffer[@write_index] = sample @write_index = (@write_index + 1) % @capacity @write_count += 1 @size += 1 if @size < @capacity end end end |