Skip to content

Instantly share code, notes, and snippets.

@avinasha
Created March 6, 2014 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avinasha/9386860 to your computer and use it in GitHub Desktop.
Save avinasha/9386860 to your computer and use it in GitHub Desktop.
hue_expts.rb
require 'hue'
require 'waveform'
audio = RubyAudio::Sound.open(source)
length = audio.info.length.to_i
frames = audio.info.frames
sample_period = 1
frames_per_sample = frames / sample_period
frame_samples = []
frames_read = 0
sample = RubyAudio::Buffer.new("float", frames_per_sample, audio.info.channels)
audio.seek 0
while(frames_read = audio.read(sample)) > 0
frame_samples << rms(sample, audio.info.channels)
end
attrs = frames_samples.map{|f| [(f[0]*65500).to_i,(f[1]*255).to_i]}
c = Hue::Client.new
l = c.lights.first
attrs.length.times do |i|
l.set_state({hue: attrs[i][0], bri: attrs[i][1], transitiontime: 10})
sleep(sample_period)
end
puts "Yay!"
def rms(frames, channels=1)
rms_frame = []
(0..channels-1).each do |channel|
rms_frame << channel_rms(frames, channel)
end
rms_frame
end
def channel_rms(frames, channel=0)
Math.sqrt(frames.inject(0.0){ |sum, frame| sum += (frame ? Array(frame)[channel] ** 2 : 0) } / frames.size)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment