Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- struct Key : IComparable
- {
- private readonly sbyte _key;
- public Key(Note note, Accidental accidental, Octave octave)
- {
- int noteNumber = ((int)note + 1) / 2;
- _key = (sbyte)(3 * noteNumber + (sbyte)accidental + 21 * (sbyte)octave);
- int keyNumber = ToTone();
- // Если представить что "С" 1-ой октавы - это ноль, тогда номер самой левой
- // клавиши - это -39
- if (keyNumber < -39)
- {
- _key = -69;
- }
- else if (keyNumber > 48)
- {
- _key = 84;
- }
- }
- public bool Equals(Key other)
- {
- return ToTone() == other.ToTone();
- }
- public override bool Equals(object obj)
- {
- return obj is Key key && Equals(key);
- }
- public override int GetHashCode()
- {
- return ToTone();
- }
- public override string ToString()
- {
- Note note = new[] { Note.C, Note.D, Note.E, Note.F, Note.G, Note.A, Note.H }[(_key + 85) % 21 / 3];
- switch ((Accidental)(((_key + 85) % 3) - 1))
- {
- case Accidental.Flat:
- return $"{note}b ({(Octave)((_key+ 85) / 21 - 4)})";
- case Accidental.Sharp:
- return $"{note}# ({(Octave)((_key + 85) / 21 - 4)})";
- default:
- return $"{note} ({(Octave)((_key + 85) / 21 - 4)})";
- }
- }
- public int CompareTo(object obj)
- {
- return obj is Key key ? ToTone() - key.ToTone() : -1;
- }
- private int ToTone()
- {
- Octave octave = (Octave)((_key + 85) / 21 - 4);
- Note note = new[] { Note.C, Note.D, Note.E, Note.F, Note.G, Note.A, Note.H }[(_key + 85) % 21 / 3];
- Accidental accidental = (Accidental)((_key + 85) % 3 - 1);
- return (int)octave * 12 + (int)note + (int)accidental;
- }
- }
Add Comment
Please, Sign In to add comment