jeudi 1 janvier 2015

How to play audio as a stream in Ruby

I'm trying to figure out a good way to play a WAV file as a stream using Ruby. I found this CoreAudio gem but can't seem to get audio to play properly. When I run this code it just makes a very choppy, stuttery sound.



require 'coreaudio'
require 'thread'

BUFF_SIZE = 1024

Thread.abort_on_exception = true

song = CoreAudio::AudioFile.new("bleh.wav", :read)
outbuf = CoreAudio.default_output_device.output_buffer(BUFF_SIZE)

queue = Queue.new

read_song = Thread.start do
loop do
segment = song.read(BUFF_SIZE)
queue.push(segment)
end
end

play_song = Thread.start do
while segment = queue.pop do
outbuf << segment
end
end

outbuf.start

sleep 10

read_song.kill.join
play_song.kill.join


Any advice would be greatly appreciated, thanks!


Aucun commentaire:

Enregistrer un commentaire