MusicBoxConverter/MusicBoxConverter/MusicBox.cs

146 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Melanchall.DryWetMidi.Smf.Interaction;
namespace MusicBoxConverter
{
public class MusicBox
{
public string Name { get; private set; }
/// <summary>
/// The height of a strip for this music box, in millimetres.
/// </summary>
public float StripHeightMm = 58.19f;
public List<string> ValidNotes { get; private set; }
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;
}
}
private MusicBox(string inName, List<string> inValidNotes)
{
Name = inName;
ValidNotes = inValidNotes;
}
/// <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)
{
return ValidNotes.Contains(
$"{note.NoteName}{note.Octave}".Replace("Sharp", "#")
);
}
public int NoteToBoxNumber(Note note)
{
string noteCompare = $"{note.NoteName}{note.Octave}".Replace("Sharp", "#");
return ValidNotes.FindIndex((string playableNote) => noteCompare == playableNote);
}
public override string ToString()
{
return Name;
//return string.Format("[MusicBox: Name={0}, ValidNotes={1}]", Name, ValidNotes);
}
/*
* Real Life Music Box Printed
*
*
* Middle C4 G
* D4 A
* E4 B
* F4 C
* G4 D
* A5 E
*
* A#5 F
* B5 F#
* C G
* D A
*
*
*
*
* F#3
* 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
* G6
* A6
*/
public static MusicBox Note30Corrected = new MusicBox(
"30 Note Music Box (Corrected)",
new List<string>() {
"F#3", "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", "G6", "A6"
}
);
/// <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(
"30 Note Music Box",
new List<string>() {
// When we asked for a G4, it played an F4
// When we asked for an A4, it played a G4
// When we tried to translate C5 from the reference performance, it translated as a B4
"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"
}
);
}
}