Class: Hallon::AudioQueue
- Inherits:
-
Object
- Object
- Hallon::AudioQueue
- Defined in:
- lib/hallon/audio_queue.rb
Overview
Hallon::AudioQueue is a non-blocking (well, not entirely) sized FIFO queue.
You initialize the queue with a max_size
, and then push data to it.
For every push operation, the AudioQueue will tell you how much of your data
it could consume. If the queue becomes full, it won’t accept any more
data (and will return 0 on the #push operation) until you pull some data
out of it with #pop.
Hallon::AudioQueue is useful for handling Observable::Session#music_delivery_callback.
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the format previously set by #format=.
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all data from the AudioQueue.
-
#empty? ⇒ Boolean
True if the queue has a #size of 0.
-
#initialize(max_size) ⇒ AudioQueue
constructor
A new instance of AudioQueue.
-
#new_cond ⇒ MonitorMixin::ConditionVariable
Create a condition variable bound to this AudioQueue.
-
#pop(format, num_frames = max_size) ⇒ Array?
Array of data, but no longer than
num_frames
. -
#push(format, frames) ⇒ Integer
How much of the data that was added to the queue.
-
#size ⇒ Integer
Number of frames in buffer.
-
#synchronize { ... } ⇒ Object
Use this if you wish to perform multiple operations on the AudioQueue atomicly.
Constructor Details
#initialize(max_size) ⇒ AudioQueue
Returns a new instance of AudioQueue.
41 42 43 44 45 46 47 48 |
# File 'lib/hallon/audio_queue.rb', line 41 def initialize(max_size) @max_size = max_size @frames = [] @format = nil @frames.extend(MonitorMixin) @condvar = @frames.new_cond end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the format previously set by #format=.
104 105 106 |
# File 'lib/hallon/audio_queue.rb', line 104 def format @format end |
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
38 39 40 |
# File 'lib/hallon/audio_queue.rb', line 38 def max_size @max_size end |
Instance Method Details
#clear ⇒ Object
Clear all data from the AudioQueue.
99 100 101 |
# File 'lib/hallon/audio_queue.rb', line 99 def clear synchronize { @frames.clear } end |
#empty? ⇒ Boolean
Returns true if the queue has a #size of 0.
94 95 96 |
# File 'lib/hallon/audio_queue.rb', line 94 def empty? size.zero? end |
#new_cond ⇒ MonitorMixin::ConditionVariable
Create a condition variable bound to this AudioQueue. Should be used if you want to wait inside #synchronize.
121 122 123 |
# File 'lib/hallon/audio_queue.rb', line 121 def new_cond @frames.new_cond end |
#pop(format, num_frames = max_size) ⇒ Array?
If the queue is empty, this operation will block until data is available.
When data is available, if it’s not in the same format as the format requested the return value will be nil. This is to avoid the format changing during wait.
Returns array of data, but no longer than num_frames
.
81 82 83 84 85 86 |
# File 'lib/hallon/audio_queue.rb', line 81 def pop(format, num_frames = max_size) synchronize do @condvar.wait_while { empty? } @frames.shift(num_frames) if format == @format end end |
#push(format, frames) ⇒ Integer
If the format is not the same as the current format, the queue is emptied before appending the new data. In this case, #format will be assigned to the new format as well.
Returns how much of the data that was added to the queue.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/hallon/audio_queue.rb', line 57 def push(format, frames) synchronize do unless format == @format @format = format clear end can_accept = max_size - size new_frames = frames.take(can_accept) @frames.concat(new_frames) @condvar.signal new_frames.size end end |
#size ⇒ Integer
Returns number of frames in buffer.
89 90 91 |
# File 'lib/hallon/audio_queue.rb', line 89 def size synchronize { @frames.size } end |
#synchronize { ... } ⇒ Object
this lock is re-entrant, you can nest it in itself
Use this if you wish to perform multiple operations on the AudioQueue atomicly.
112 113 114 |
# File 'lib/hallon/audio_queue.rb', line 112 def synchronize @frames.synchronize { return yield } end |