Skip to content

Instantly share code, notes, and snippets.

@earlonrails
Created October 7, 2011 23:52
Show Gist options
  • Save earlonrails/1271638 to your computer and use it in GitHub Desktop.
Save earlonrails/1271638 to your computer and use it in GitHub Desktop.
Use icanhasaudio gem to decode mp3 and find the points of the sound wave to plot on a graph
#!/usr/local/bin/ruby
require 'stringio'
require 'rubygems'
require 'icanhasaudio'
reader = ::Audio::MPEG::Decoder.new
str_io = StringIO.new
input = File.open(ARGV[0], "rb")
plot_points = []
buf = reader.send(:skip_id3_header, input)
reader.send(:decode_headers_for, buf)
while !reader.mp3data.header_parsed?
reader.send(:decode_headers_for, input.read(100))
end
reader.mp3data.nsamp = Audio::MPEG::Decoder::MP3Data::MAX_U_32_NUM unless reader.mp3data.total_frames > 0
reader.send(:native_decode, input, str_io)
str_leng = str_io.size - 1
str_leng.times do |i|
str_io.seek(i)
plot_points << str_io.read(4).unpack('ss')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment