Skip to content

Instantly share code, notes, and snippets.

@slembcke
Created June 12, 2012 14:58
Show Gist options
  • Save slembcke/2918023 to your computer and use it in GitHub Desktop.
Save slembcke/2918023 to your computer and use it in GitHub Desktop.
Overly simplistic pitch detection
SAMPLE_RATE = 44100
DURATION = 2
def tone(freq, phase, t)
Math.sin(t*freq*2.0*Math::PI + phase)
end
sum = 0.0;
(DURATION*SAMPLE_RATE).to_i.times{|sample|
t = sample/Float(SAMPLE_RATE)
sum += tone(440, 0.0, t)*tone(440, 0.0, t)
}
puts sum*(0.5*DURATION/SAMPLE_RATE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment