/// <summary> /// Loads the viewer with a Score object generated from the specified MusicXml file. /// </summary> public void loadFile(string fileName) { this.fileName = fileName; if (File.Exists(fileName)) { // Parser instance converts between Score object and MusicXML var parser = new MusicXmlParser(); Score score = parser.Parse(XDocument.Load(fileName)); // Load the content of the specified file into Data data = null; Data = score; // Get the key signature keySig = (Key)score.FirstStaff.Elements.First(k => k.GetType() == typeof(Key)); if (KeySig == null) { KeySig = new Key(0); } // Get the time signature timeSig = (TimeSignature)score.FirstStaff.Elements.First(k => k.GetType() == typeof(TimeSignature)); if (timeSig == null) { timeSig = TimeSignature.CommonTime; } // Set up the back end data model staves = null; staves = new List <LStaff>(); foreach (var staff in score.Staves) { LStaff ls = null; List <LMeasure> measures = new List <LMeasure>(); foreach (var measure in staff.Measures) { LMeasure m = new LMeasure(measure.Elements, ls, timeSig.WholeNoteCapacity); measures.Add(m); } ls = new LStaff(staff, measures); this.staves.Add(ls); } player = new MidiTaskScorePlayer(data); updateView(); } else { throw new FileNotFoundException(fileName + " not found."); //This and any parser exceptions will be caught by the calling function } }
/// <summary> /// Adds the note to the staff in the correct location /// </summary> /// <param name="nr">The note to add</param> private void addNoteOrRestToStaff(NoteOrRest nr) { // New for LStaff var elem = Viewer.SelectedElement; LStaff staff = getLStaff(elem); NoteOrRest noteToPlay; if (isValidTarget(elem)) { LMeasure measure = staff.getMeasure(elem); staff.AddAfter(elem, nr); if (measure.Contains(nr)) { Viewer.SelectedElement = nr; } else if (measure.Node.Next != null && measure.Node.Next.Value.Count > 0) { Viewer.SelectedElement = measure.Node.Next.Value.First.Value; } noteToPlay = (NoteOrRest)Viewer.SelectedElement; } else { staff.Add(nr); noteToPlay = (NoteOrRest)staff.Last.Value.Last(e => e.GetType().IsSubclassOf(typeof(NoteOrRest))); } // Play the note if (noteToPlay.GetType() == typeof(Note)) { model.PlayNote((Note)noteToPlay); } // Update the view model to make the Play command available model.updateView(); }