Skip to content

Instantly share code, notes, and snippets.

@zakuroishikuro
Last active April 21, 2016 20:09
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 zakuroishikuro/e9b6f23d0f3f37c40c21818521f621b3 to your computer and use it in GitHub Desktop.
Save zakuroishikuro/e9b6f23d0f3f37c40c21818521f621b3 to your computer and use it in GitHub Desktop.
ピアノの鍵盤の周波数を求める http://www.yamaha.co.jp/plus/piano/trivia/?ln=ja&id=103007
' VBA
Private Const COMMON_RATIO As Double = 2 ^ (1 / 12) '1.0594630943592953...
Function calc_piano_frequency(Optional key_index As Integer = 49, Optional pitch As Integer = 440) As Double
Dim ratio As Double
ratio = COMMON_RATIO ^ (key_index - 49)
calc_piano_frequency = pitch * ratio
End Function
Sub exec_sample()
Debug.Print calc_piano_frequency
'=> 440
Debug.Print calc_piano_frequency(1)
'=> 27.49999999999999
Debug.Print calc_piano_frequency(88)
'=> 4186.009044809581
End Sub
// JavaScript
const COMMON_RATIO = Math.pow(2, 1/12); //1.0594630943592953...
function calc_piano_frequency(key_index, pitch){
if (key_index === undefined) key_index = 49;
if (pitch === undefined) pitch = 440;
var ratio = Math.pow(COMMON_RATIO, key_index - 49);
return pitch * ratio;
}
console.log(calc_piano_frequency());
//=> 440
console.log(calc_piano_frequency(1));
//=> 27.49999999999999
console.log(calc_piano_frequency(88));
//=> 4186.009044809581
# Ruby
COMMON_RATIO = 2 ** (1/12r) #1.0594630943592953...
def calc_piano_frequency(key_index = 49, pitch = 440)
ratio = COMMON_RATIO ** (key_index - 49)
pitch * ratio
end
puts calc_piano_frequency
#=> 440.0
puts calc_piano_frequency 1
#=> 27.499999999999947
puts calc_piano_frequency 88
#=> 4186.009044809585
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment