I am building a tool which is supposed to run on a server and analyze sound files. I want to do this in Ruby as all my other tools are written in Ruby as well. But I am hav
Here's the final solution to what I was trying to achieve, thanks a lot to Randall Cook's helpful advice. The code to extract sound wave and FFT of a wav file in Ruby:
require "ruby-audio"
require "fftw3"
fname = ARGV[0]
window_size = 1024
wave = Array.new
fft = Array.new(window_size/2,[])
begin
buf = RubyAudio::Buffer.float(window_size)
RubyAudio::Sound.open(fname) do |snd|
while snd.read(buf) != 0
wave.concat(buf.to_a)
na = NArray.to_na(buf.to_a)
fft_slice = FFTW3.fft(na).to_a[0, window_size/2]
j=0
fft_slice.each { |x| fft[j] << x; j+=1 }
end
end
rescue => err
log.error "error reading audio file: " + err
exit
end
# now I can work on analyzing the "fft" and "wave" arrays...