Question

I've never worked with real-time audio features. I would like to know whether there are ruby libraries out there that would allow me to create something like a guitar tuner.

Was it helpful?

Solution

There are two orthogonal tasks: 1) read the audio, 2) process it. To get the audio you could check ruby-audio though, to be honest, I've never used it and its documentation seems scarce. Personally I'd resort to whatever your operating system provides; for example in GNU/Linux we have handy tools like bplay. The second issue is how to calculate the FFT of audio, this should be easy with FFTW3.

Here is a quick and dirty example that gets the peak point of the FFT from stdin (16 bits, mono):

require 'rubygems'
require 'fftw3'

module Tuner
  def self.peaks(input_channel, samplerate, window_size)
    Enumerator.new do |enum|
      loop do
        data = input_channel.read(window_size).unpack("s*")
        na = NArray.to_na(data)
        fft = FFTW3.fft(na).to_a[0, window_size/2]
        max_n = fft.map(&:abs).each_with_index.drop(1).max[1]
        enum.yield(max_n.to_f * samplerate / window_size)
      end
    end
  end
end

if __FILE__ == $0
  Tuner.peaks(STDIN, 8000, 1024).each { |f| puts f }
end

To be called, for example:

$ brec -s 8000 -b 16 | ruby tuner.rb
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top