Created
October 4, 2013 21:14
-
-
Save JonahMoses/6832892 to your computer and use it in GitHub Desktop.
Music Visualizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ruby-processing' | |
class Visualizer < Processing::App | |
load_library "minim" | |
import "ddf.minim" | |
import "ddf.minim.analysis" | |
def setup | |
smooth # smoother == prettier | |
size(1280,1000) # let's pick a more interesting size | |
background 10 # ...and a darker background color | |
setup_sound | |
end | |
def draw | |
update_sound | |
animate_sound | |
end | |
def setup_sound | |
@minim = Minim.new(self) | |
@input = @minim.get_line_in | |
@fft = FFT.new(@input.left.size, 44100) | |
@beat = BeatDetect.new | |
@freqs = [60, 170, 310, 600, 1000, 3000, 6000, 12000, 14000, 16000] | |
@current_ffts = Array.new(@freqs.size, 0.001) | |
@previous_ffts = Array.new(@freqs.size, 0.001) | |
@max_ffts = Array.new(@freqs.size, 0.001) | |
@scaled_ffts = Array.new(@freqs.size, 0.001) | |
@fft_smoothing = 0.8 | |
end | |
def update_sound | |
@fft.forward(@input.left) | |
@previous_ffts = @current_ffts | |
@freqs.each_with_index do |freq, i| | |
new_fft = @fft.get_freq(freq) | |
@max_ffts[i] = new_fft if new_fft > @max_ffts[i] | |
@current_ffts[i] = ((1 - @fft_smoothing) * new_fft) + (@fft_smoothing * @previous_ffts[i]) | |
@scaled_ffts[i] = (@current_ffts[i]/@max_ffts[i]) | |
end | |
@beat.detect(@input.left) | |
end | |
def animate_sound | |
@size = @scaled_ffts[1]*height | |
@size *= 4 if @beat.is_onset | |
@x1 = @scaled_ffts[0]*width + width/2 | |
@y1 = @scaled_ffts[1]*height + height/2 | |
@red1 = @scaled_ffts[2]*255 | |
@green1 = @scaled_ffts[3]*255 | |
@blue1 = @scaled_ffts[4]*255 | |
fill @red1, @green1, @blue1 | |
stroke @red1+20, @green1+20, @blue1+20 | |
ellipse(@x1, @y1, @size, @size) | |
end | |
end | |
Visualizer.new :title => "Visualizer" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment