diff options
author | B. Watson <yalhcru@gmail.com> | 2015-04-08 03:18:53 -0400 |
---|---|---|
committer | B. Watson <yalhcru@gmail.com> | 2015-04-08 03:18:53 -0400 |
commit | 122f3c401f23f84799802c7b9667bda222646487 (patch) | |
tree | bc77cc44c516eac71b2d6490574fd32a5b5efd65 /freqnote.pl | |
download | misc-scripts-122f3c401f23f84799802c7b9667bda222646487.tar.gz |
initial commit
Diffstat (limited to 'freqnote.pl')
-rwxr-xr-x | freqnote.pl | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/freqnote.pl b/freqnote.pl new file mode 100755 index 0000000..eaf68ab --- /dev/null +++ b/freqnote.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl -w + +# convert a frequency to a note name. +# formula came from http://en.wikipedia.org/wiki/Note +# p = 69 + 12 * log2(f/440) +# ...where f is in Hz and p is the MIDI note number + +@notenames = ( + 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B' + ); + +sub log2 { + return log($_[0])/log(2); +} + +sub midi2note { + my $pitch = int(shift); + my $class = $notenames[$pitch % 12]; + my $octave = int($pitch / 12) - 1; + return $class . $octave; +} + +for(@ARGV) { + my $p = 69 + 12 * log2($_ / 440); + my $pitch = int($p); + my $cents = int(($p - $pitch) * 1000)/10; + if($cents > 50) { + $pitch++; + $cents = $cents - 100; + } + if($cents == 0) { + $cents = ""; + } elsif($cents > 0) { + $cents = "+$cents" . "c"; + } else { + $cents .= "c"; + } + print $_ . "Hz = MIDI $pitch = " . midi2note($pitch) . " $cents\n"; +} |