Created
November 20, 2011 16:05
-
-
Save nagachika/1380403 to your computer and use it in GitHub Desktop.
record via Soundflower with ruby-coreaudio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "coreaudio" | |
# Usage: record_soundflower.rb output.m4a | |
def silent?(wav) | |
wav.max == 0 and wav.min == 0 | |
end | |
# detect Soundflower (2ch) audio device | |
sf = CoreAudio.devices.find{|dev| dev.name == "Soundflower (2ch)" } | |
if sf.nil? | |
$stderr.puts "cannot found device 'Soundflower (2ch)'" | |
exit 1 | |
end | |
file, = ARGV | |
if file.nil? | |
$stderr.puts "please specify output audio file" | |
exit 1 | |
end | |
begin | |
af = CoreAudio::AudioFile.new(file, :write, :format => :m4a) | |
buf = sf.input_buffer(4096) | |
buf.start | |
# skip silence | |
loop do | |
wav = buf.read(4096) | |
unless silent?(wav) | |
puts "Start recording." | |
af.write(wav) | |
break | |
end | |
end | |
max_silence = 44100 * 2 / 4096 # about 2sec | |
cnt = 0 | |
loop do | |
wav = buf.read(4096) | |
if silent?(wav) | |
cnt += 1 | |
if cnt >= max_silence | |
# quit if 2sec of silence continues. | |
break | |
end | |
else | |
cnt = 0 | |
end | |
af.write(wav) | |
end | |
buf.stop | |
puts "dropped frames: #{buf.dropped_frame} frames" | |
ensure | |
af.close if af | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment