static int GetFrequency(NoteName noteName, int octave = BaseOctave, NoteTuning tuning = NoteTuning.LA_440) { string noteKey = GetNoteKey(noteName, octave, tuning); int frequency; if (!frequencies.TryGetValue(noteKey, out frequency)) { int baseFrequency; string baseNoteKey = GetNoteKey(NoteName.LA, BaseOctave, tuning); if (!frequencies.TryGetValue(baseNoteKey, out baseFrequency)) { baseFrequency = (int)tuning; frequencies[baseNoteKey] = baseFrequency; } int semiTone = GetSemiTone(noteName, octave); int baseSemitone = GetSemiTone(NoteName.LA, BaseOctave); int deltaSemiTones = semiTone - baseSemitone; frequency = (int)Math.Round(baseFrequency * Math.Pow(SemiToneFactor, deltaSemiTones)); if (frequency < 37) { throw new Exception("Console.Beep won't work .. so don't make a note below RE_octave1_LA_446"); } frequencies[noteKey] = frequency; } return(frequency); }
static string GetNoteKey(NoteName noteName, int octave = BaseOctave, NoteTuning tuning = NoteTuning.LA_440) { var sb = new StringBuilder(); sb.Append(noteName.ToString()); sb.Append(octave.ToString()); sb.Append(tuning.ToString()); return(sb.ToString()); }
public Note(NoteName noteName, double duration = 1, int octave = BaseOctave, NoteTuning tuning = NoteTuning.LA_440) { if (octave < 1 || (octave == 1 && noteName < NoteName.RE)) { throw new Exception("Can't create a note lower than RE1"); } Name = Enum.GetName(typeof(NoteName), noteName).ToLower().Replace("_sharp", "#"); Octave = octave; Tuning = tuning; Frequency = GetFrequency(noteName, octave, tuning); Duration = duration; NotePlayer = PlayWithTextAndSound; }