From 09dd4eed319d6f7ea6c8d8df000fe843cfb69149 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sat, 2 Dec 2017 19:47:40 +0000
Subject: [PATCH] Finish up note validation

---
 MusicBoxConverter/MusicBox.cs               | 23 +++++++++++--
 MusicBoxConverter/MusicBoxScoreGenerator.cs | 37 +++++++++++++--------
 MusicBoxConverter/Program.cs                |  3 +-
 3 files changed, 45 insertions(+), 18 deletions(-)

diff --git a/MusicBoxConverter/MusicBox.cs b/MusicBoxConverter/MusicBox.cs
index 3d2cf8a..323d8f3 100644
--- a/MusicBoxConverter/MusicBox.cs
+++ b/MusicBoxConverter/MusicBox.cs
@@ -1,28 +1,45 @@
 ﻿using System;
 using System.Collections.Generic;
+using Melanchall.DryWetMidi.Smf.Interaction;
 
 namespace MusicBoxConverter
 {
 	public class MusicBox
 	{
+        public string Name { get; private set; }
 		public List<string> ValidNotes { get; private set; }
 
-		private MusicBox(List<string> inValidNotes)
+		private MusicBox(string inName, List<string> inValidNotes)
 		{
+            Name = inName;
 			ValidNotes = inValidNotes;
 		}
 
-		public bool IsValidNote(string noteName)
+        /// <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)
 		{
-			throw new NotImplementedException();
+            return ValidNotes.Contains(
+                (note.NoteName.ToString() + note.Octave).Replace("Sharp", "")
+            );
 		}
 
+        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(
+            "30 Note Music Box",
 			new List<string>() {
 				"G3",
 				"C4", "D4", "E4", "F4", "G4", "A4", "A#4", "B4",
diff --git a/MusicBoxConverter/MusicBoxScoreGenerator.cs b/MusicBoxConverter/MusicBoxScoreGenerator.cs
index d9619c2..64d5db9 100644
--- a/MusicBoxConverter/MusicBoxScoreGenerator.cs
+++ b/MusicBoxConverter/MusicBoxScoreGenerator.cs
@@ -9,27 +9,36 @@ using SBRL.Utilities;
 
 namespace MusicBoxConverter
 {
-	public class MusicBoxScoreGenerator
-	{
-		public Vector2 offset { get; set; } = new Vector2(10, 10);
-		public Vector2 scaleFactor { get; set; } = new Vector2(0.1f, 4f);
+    public class MusicBoxScoreGenerator
+    {
+        public Vector2 offset { get; set; } = new Vector2(10, 10);
+        public Vector2 scaleFactor { get; set; } = new Vector2(0.1f, 4f);
 
-		public int holeSize { get; set; } = 5;
+        public int holeSize { get; set; } = 5;
 
-		private int trackLength;
-		public int TrackLength {
-			get {
-				if(trackLength == 0)
-					trackLength = (int)AllNotes().Max((Note arg) => arg.Time);
-				return trackLength;
-			}
-		}
+        private int trackLength;
+        public int TrackLength
+        {
+            get
+            {
+                if (trackLength == 0)
+                    trackLength = (int)AllNotes().Max((Note arg) => arg.Time);
+                return trackLength;
+            }
+        }
 
+        public MusicBox MusicBox { get; private set; }
 		MidiFile midiFile;
 		
-		public MusicBoxScoreGenerator(string filename)
+		public MusicBoxScoreGenerator(string filename, MusicBox inMusicBox)
 		{
+            MusicBox = inMusicBox;
 			midiFile = MidiFile.Read("/home/sbrl/Music/Sheets/HappyBirthday.midi");
+
+            foreach(Note note in AllNotes()) {
+                if (!MusicBox.IsValidNote(note))
+                    throw new Exception($"Error: The note {note} at {note.Time} can't be played by the {MusicBox}.");
+            }
 		}
 
 		public void Output(string destinationFilename)
diff --git a/MusicBoxConverter/Program.cs b/MusicBoxConverter/Program.cs
index 85fdecf..8a8e04f 100644
--- a/MusicBoxConverter/Program.cs
+++ b/MusicBoxConverter/Program.cs
@@ -7,7 +7,8 @@ namespace MusicBoxConverter
 		public static void Main(string[] args)
 		{
 			MusicBoxScoreGenerator converter = new MusicBoxScoreGenerator(
-				"/home/sbrl/Music/Sheets/HappyBirthday.midi"
+                "/home/sbrl/Music/Sheets/HappyBirthday.midi",
+				MusicBox.Note30
 			);
 			converter.Output("/tmp/test.svg");
 		}
