Create a gist now

Instantly share code, notes, and snippets.

@nanki /olm.rb
Created Oct 21, 2011

One liner music player for Ruby. http://hitode909.appspot.com/one-liner-music/
require 'rubygems'
require 'ffi-portaudio'
def safe
r = nil
Thread.new {
$SAFE = 4
r = yield
}.join
r
end
class OLMStream < FFI::PortAudio::Stream
attr_accessor :generator
def initialize
@count = 0
@max = 1
end
def process(input, output, frameCount, timeInfo, statusFlags, userData)
wave = (0...frameCount).map{|i|@generator.call(@count+=1)}
@max = [wave.max, @max].max
volume = 0x8000.quo @max
output.write_array_of_int16 wave.map!{|v| v * volume}
:paContinue
end
end
include FFI::PortAudio
API.Pa_Initialize
output = API::PaStreamParameters.new
output[:device] = API.Pa_GetDefaultOutputDevice
output[:channelCount] = 1
output[:sampleFormat] = API::Int16
output[:suggestedLatency] = 0.2
output[:hostApiSpecificStreamInfo] = nil
generator = ARGV.shift
s = OLMStream.new
s.generator = safe { eval "lambda {|t| #{generator} }" }
s.open(nil, output, 8000)
s.start
at_exit do
s.close
API.Pa_Terminate
end
loop { sleep 1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment