Created
March 9, 2011 13:20
-
-
Save MrJaba/862169 to your computer and use it in GitHub Desktop.
A Processing 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
# Visualizer | |
class Visualizer < Processing::App | |
load_library "minim" #Java sound library included with processing. load_library is provided by ruby-processing | |
import "ddf.minim" | |
import "ddf.minim.analysis" | |
def setup | |
smooth | |
size(1280, 100) | |
background 10 | |
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 | |
@frequencies = [60,170,310,600,600,1000,3000,6000,12000,14000,16000] | |
@current_ffts = Array.new(@frequencies.size, 0.001) | |
@previous_ffts = Array.new(@frequencies.size, 0.001) | |
@max_ffts = Array.new(@frequencies.size, 0.001) | |
@scaled_ffts = Array.new(@frequencies.size, 0.001) | |
@fft_smoothing = 0.8 | |
end | |
def update_sound | |
@fft.forward(@input.left) | |
@previous_ffts = @current_ffts | |
@frequencies.each_with_index do |frequency, i| | |
new_fft = @fft.get_freq(frequency) | |
@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