MusicBoxConverter/MusicBoxConverter/MusicBox.cs

77 lines
1.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2017-12-03 11:36:47 +00:00
using System.Linq;
2017-12-02 19:47:40 +00:00
using Melanchall.DryWetMidi.Smf.Interaction;
namespace MusicBoxConverter
{
public class MusicBox
{
2017-12-02 19:47:40 +00:00
public string Name { get; private set; }
public List<string> ValidNotes { get; private set; }
2017-12-03 11:36:47 +00:00
public Note LowestNote {
get {
return NoteUtilities.StringToNote(ValidNotes.First());
}
}
public Note HighestNote {
get {
return NoteUtilities.StringToNote(ValidNotes.Last());
}
}
public int NoteCount {
get {
return ValidNotes.Count;
}
}
2017-12-02 19:47:40 +00:00
private MusicBox(string inName, List<string> inValidNotes)
{
2017-12-02 19:47:40 +00:00
Name = inName;
ValidNotes = inValidNotes;
}
2017-12-03 11:36:47 +00:00
2017-12-02 19:47:40 +00:00
/// <summary>
/// Calculates whether the specified note can be played by this music box.
/// </summary>
/// <param name="note">The note to check.</param>
/// <returns>Whether the note can be played by this music box.</returns>
public bool IsValidNote(Note note)
{
2017-12-02 19:47:40 +00:00
return ValidNotes.Contains(
(note.NoteName.ToString() + note.Octave).Replace("Sharp", "")
);
}
public int NoteToBoxNumber(Note note)
{
return ValidNotes.FindIndex((string playableNote) => $"{note.NoteName}{note.Octave}".Replace("Sharp", "#") == playableNote);
}
2017-12-02 19:47:40 +00:00
public override string ToString()
{
return Name;
//return string.Format("[MusicBox: Name={0}, ValidNotes={1}]", Name, ValidNotes);
}
/// <summary>
/// A 30 note music box.
/// @Starbeamrainbowlabs has one of these - it was the part of the
/// inspiration for the whole project!
/// </summary>
public static MusicBox Note30 = new MusicBox(
2017-12-02 19:47:40 +00:00
"30 Note Music Box",
new List<string>() {
"G3",
"C4", "D4", "E4", "F4", "G4", "A4", "A#4", "B4",
"C5", "C#5", "D5", "D#5", "E5", "F5", "F#5", "G5", "G#5", "A5", "A#5", "B5",
"C6", "C#6", "D6", "D#6", "E6", "F6", "F#6", "G6", "A6"
}
);
}
}