Playing with Ruby + Processing + Audio

Don’t really know, what i wrote… but this thing can react on audio beats ))) and you can specify channels to visualize(circle).

Controls:

“a”, “s” — sets minimum & maximum samples to display as circle
“d” — output current min & max
“x” — exit xD

And don’t forget to put sound.mp3 near .rb file

You need ruby-processing gem. Tested at 1.9.2

require "pp"

class Visualizer < Processing::App
  load_library "minim"
  import "ddf.minim"
  import "ddf.minim.analysis"

  def setup
    smooth
    color_mode(RGB, 255)
    size(1280, 600)
    background(0)

    @min = 0
    @max = 2

    setup_sound
  end

  def setup_sound
    @minim = Minim.new(self)
    @input = @minim.get_line_in
    @input = @minim.loadFile("sound.mp3", 1024)
    @input.play

    @fft = FFT.new(@input.bufferSize, @input.sampleRate)
  end

  def draw
    background(0)

    avg = 0

    @fft.forward(@input.left)
    @fft.specSize.times { |i|
      stroke(255)

      case i
      when @min..@max
        stroke(255, 0, 0)
        # remove if statement for pure detection
        avg += @fft.getBand(i) if @fft.getBand(i) > 100
      end

      line(i, 600, i, @fft.getBand(i));
    }

    avg = (avg / (@max - @min)) * 10

    noStroke
    fill(255, 255, 0)
    ellipse(800, 300, avg, avg)
  end

  def keyPressed
    case key
    when "a"
      @min = mouseX if mouseX < @max && mouseX >= 0
    when "s"
      @max = mouseX if mouseX > @min && mouseX <= @fft.specSize
    when "d"
      puts "#{@min} #{@max}"
    when "x"
      exit
    end
  end

  def stop
    @input.pause
    @minim.stop
    super
  end
end

Visualizer.new :title => "Visualizer"

Notes

Top of Page