Skip to content

Instantly share code, notes, and snippets.

@jgrevich
Created July 9, 2013 04:55
Show Gist options
  • Save jgrevich/5954811 to your computer and use it in GitHub Desktop.
Save jgrevich/5954811 to your computer and use it in GitHub Desktop.
module PIFRecord
class Record
require "coreaudio"
include Celluloid
def initialize
dev = CoreAudio.default_input_device
@buf = dev.input_buffer(1024)
filename = Time.now.strftime("%Y.%m.%d.wav")
wav = CoreAudio::AudioFile.new(filename, :write, :format => :wav, :rate => dev.nominal_rate, :channels => dev.input_stream.channels)
samples = 0
th = Thread.start do
loop do |variable|
w = @buf.read(4096)
samples += w.size / dev.input_stream.channels
wav.write(w)
end
end
@buf.start
$stdout.print "RECORDING..."
$stdout.flush
end
def self.stop
@buf.stop
$stdout.puts "done."
th.kill.join
wav.close
puts "#{samples} samples read."
puts "#{@buf.dropped_frame} frame dropped."
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment