Ruby Guitar Tuner

I have never worked with real-time audio features. I would like to know if there are ruby ​​libraries there that will allow me to create something like a guitar tuner.

+4
source share
1 answer

There are two orthogonal tasks: 1) read the audio, 2) process it. To get audio, you can check out ruby-audio , although to be honest, I never used it, and its documentation seems to be scarce. Personally, I resort to what your operating system provides; for example, on GNU / Linux we have convenient tools like bplay . The second problem is how to calculate the FFT audio, this should be easy with FFTW3 .

Here is a quick and dirty example that gets the peak FFT point 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 

Called, for example:

 $ brec -s 8000 -b 16 | ruby tuner.rb 
+4
source

Source: https://habr.com/ru/post/1333071/


All Articles