nagachika/ruby-coreaudio — Issue #7: output buffer needs extra value to be ignored
Author: dturnbull
Created: 2012-02-23 01:17:05+00:00
State: open

I took a quick look at the C but didn't see any obvious off by one bugs. I have a Ruby example that seems to indicate otherwise. The comments in the following code should explain better than my prose.

require "coreaudio"
require "thread"

dev = CoreAudio.default_output_device
buf = dev.output_buffer(8192)
q = Queue.new

th1 = Thread.start do
loop do
buf << q.pop
end
end

th2 = Thread.start do
buf.start
siz = 1500
next_at = Time.now - 0.1
phase = Math::PI * 2.0 * 440.0 / dev.nominal_rate
i = 0
loop do
wav = NArray.sint(siz+1) # +1 for never used value
wav.collect! do |x|
i+= 1
(0.5 * Math.sin(phase*i) * 0x7FFF).round
end
i-=1 # back off for the unused value
# wav[-2] = 0x7FFF # you will hear this
wav[-1] = 0x7FFF # you should hear this but won't
next_at += siz/dev.nominal_rate
snooze = [0, next_at - Time.now].max
sleep snooze unless snooze == 0
q << wav
end
end

buf.start
sleep 10
buf.stop
puts "#{buf.dropped_frame} frame dropped."
th1.kill
th2.kill.join
th1.join