kOS.Sound.NoteValue.LetterToHertz C# (CSharp) Method

LetterToHertz() public static method

A utility function to return the frequency in Hertz given a note expressed as in "letter" form as a string.
public static LetterToHertz ( string letterString ) : float
letterString string The format of the string must be as follows:
/// Starts with a Letter from A to G for the note.
/// Next, optionaly a "#" or "b" for sharp or flat.
/// Next, mandatorily, a digit of 0-8 for the octave.
/// Examples: "C4" = middle C. "C#4" = middle c sharp. "Cb4" = middle c flat. C5 = high C. C3 = low C.
/// octaves follow the weird musical convention of starting at C instead of at A. (i.e. C4 is one higher than B3, not B4)
return float
        public static float LetterToHertz(string letterString)
        {
            int len = letterString.Length;
            if (len < 2 || len > 3)
                return 0f;

            int octave = (int)(letterString[len - 1] - '0');
            string octaveLessNote = letterString.Substring(0,len-1).ToLower();
            double referenceHz;
            if (octave4Lookup.TryGetValue(octaveLessNote, out referenceHz))
            {
                int octaveDiff = octave - 4;
                return (float)(referenceHz * Math.Pow(2.0, (double)octaveDiff));
            }
            return 0f; // bogus when input was garbage
        }