private static void Main(string[] args) { const string input = @"../../../Test Data/cantus firmus.xml"; const string output = @"../../../Test Data/output.xml"; ScorePartwise.Serialize(output, ScorePartwise.Deserialize(input)); var score = ScorePartwise.Deserialize(input); var list = new List <int>(); foreach (var part in score.Parts) { Console.WriteLine("Part " + part.Id); list.AddRange(from measure in part.Measures from note in measure.Notes.Where(note => note.Staff == "1") select MidiMapping.MidiLookUp(note.Pitch)); } foreach (var i in list) { Console.Write(i + " "); } Console.WriteLine("\r\n\r\npress any key to continue..."); Console.ReadLine(); }
public void ConvertTo_part_deserialize_success() { var target = new Parser(); var xml = @"<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE score-partwise PUBLIC '-//Recordare//DTD MusicXML 2.0 Partwise//EN' 'http://www.musicxml.org/dtds/partwise.dtd'> <score-partwise> <part id='1'> <measure/> <measure/> </part> </score-partwise> "; var actual = target.Parse(xml); var expected = new ScorePartwise() { Parts = new ElementCollection <Part>() { new Part() { Id = "1", Measures = new ElementCollection <Measure>() { new Measure(), new Measure(), } }, }, }; Assert.AreEqual(expected, actual); }
//todo: properties with public getter and private setter for the Pages/Lines/Measures /// <summary> /// Constructor for the WPFRendering component /// </summary> /// <param name="fontSize">The size of the font to use</param> public WPFRendering(ScorePartwise originalScore, double fontSize) { FontSize = fontSize; ScoreSize = new Size(Application.Current.MainWindow.Width, Application.Current.MainWindow.Height); // keep a copy of the original score _originalScore = originalScore; }
private string ParseToXml(ScorePartwise spw) { StringWriter tw = new StringWriter(); XmlSerializer serializer = new XmlSerializer(typeof(MusicXmlSchema.ScorePartwise)); serializer.Serialize(tw, spw); var musicXml = tw.ToString(); musicXml = ReplaceHeader(musicXml); return(musicXml); }
//Reads the top level score static public ScoreProperties ReadScore(ScorePartwise scorePartwise) { //get the top level properties of the score ScoreProperties i = new ScoreProperties(); if (scorePartwise.getWork() != null) { i.WorkTitle = scorePartwise.getWork().getWorkTitle(); } if (scorePartwise.getMovementTitle() != null) { i.MovementTitle = scorePartwise.getMovementTitle(); } if (scorePartwise.getIdentification().getEncoding().getEncodingDateOrEncoderOrSoftware().get(0) != null) { javax.xml.bind.JAXBElement x = (javax.xml.bind.JAXBElement)scorePartwise.getIdentification().getEncoding().getEncodingDateOrEncoderOrSoftware().get(0); i.EncodingSoftware = x.getValue().ToString(); } if (scorePartwise.getCredit().size() > 0) { Credit credit = (Credit)scorePartwise.getCredit().get(0); if (credit.getCreditTypeOrLinkOrBookmark().get(0) is FormattedText) { FormattedText ft = (FormattedText)credit.getCreditTypeOrLinkOrBookmark().get(0); i.CreditWords = ft.getValue(); } } PartList partlist = scorePartwise.getPartList(); if (partlist.getPartGroupOrScorePart().get(0) is ScorePart) { ScorePart scorepart = (ScorePart)partlist.getPartGroupOrScorePart().get(0); i.ScorePartID = scorepart.getId(); i.PartDisplayName = scorepart.getPartName().getValue(); i.PartAbbreviation = scorepart.getPartAbbreviation() is PartName?scorepart.getPartAbbreviation().getValue() : ""; ScoreInstrument scoreinstrument = (ScoreInstrument)scorepart.getScoreInstrument().get(0); i.InstrumentID = scoreinstrument.getId(); i.InstrumentName = scoreinstrument.getInstrumentName(); if (scorepart.getMidiDeviceAndMidiInstrument().get(0) is MidiInstrument) { MidiInstrument midiinstrument = (MidiInstrument)scorepart.getMidiDeviceAndMidiInstrument().get(0); i.MidiChannel = midiinstrument.getMidiChannel(); i.MidiProgram = midiinstrument.getMidiProgram(); } } if (scorePartwise.getPart().size() > 0) { i.Part = (ScorePartwise.Part)scorePartwise.getPart().get(0); } return(i); }
public void ConvertTo_success() { var target = new Parser(); var xml = @"<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE score-partwise PUBLIC '-//Recordare//DTD MusicXML 2.0 Partwise//EN' 'http://www.musicxml.org/dtds/partwise.dtd'> <score-partwise version='1.2.3.4'> <work/> <movement-number/> <movement-title/> <identification/> <defaults/> <credit/> <credit/> <part-list/> <part/> <part/> </score-partwise> "; var actual = target.Parse(xml); var expected = new ScorePartwise() { Work = new Work(), Credits = new ElementCollection <Credit>() { new Credit(), new Credit() }, Defaults = new Defaults(), Identification = new Identification(), MovementNumber = "", MovementTitle = "", PartList = new PartList(), Parts = new ElementCollection <Part>() { new Part(), new Part(), }, Version = "1.2.3.4" }; Assert.AreEqual(expected, actual); }
public static ScorePartwise EditScorePartwise(ScorePartwise spw, ScoreProperties editedprop) { ScoreProperties scoreprop = ReadDS.ReadScore(spw); //if the edited properties are other than the default than assign them to the scoreprop scoreprop.WorkTitle = editedprop.WorkTitle == ""?scoreprop.WorkTitle:editedprop.WorkTitle; scoreprop.MovementTitle = editedprop.MovementTitle == "" ? scoreprop.MovementTitle : editedprop.MovementTitle; scoreprop.EncodingSoftware = editedprop.EncodingSoftware == "" ? scoreprop.EncodingSoftware : editedprop.EncodingSoftware; scoreprop.Creator = editedprop.Creator == "" ? scoreprop.Creator : editedprop.Creator; scoreprop.CreditWords = editedprop.CreditWords == "" ? scoreprop.CreditWords : editedprop.CreditWords; scoreprop.PartDisplayName = editedprop.PartDisplayName == "" ? scoreprop.PartDisplayName : editedprop.PartDisplayName; scoreprop.PartAbbreviation = editedprop.PartAbbreviation == "" ? scoreprop.PartAbbreviation : editedprop.PartAbbreviation; scoreprop.ScorePartID = editedprop.ScorePartID == "" ? scoreprop.ScorePartID : editedprop.ScorePartID; scoreprop.InstrumentID = editedprop.InstrumentID == "" ? scoreprop.InstrumentID : editedprop.InstrumentID; scoreprop.InstrumentName = editedprop.InstrumentName == "" ? scoreprop.InstrumentName : editedprop.InstrumentName; scoreprop.MidiChannel = editedprop.MidiChannel == new Integer(0) ? scoreprop.MidiChannel : editedprop.MidiChannel; scoreprop.MidiProgram = editedprop.MidiProgram == new Integer(0) ? scoreprop.MidiProgram : editedprop.MidiProgram; return(CreateDS.CreateScorePartwise(scoreprop)); }
public string KeyboardTilesToMusicXML(List <KeyboardTile> keyboardTiles, int frameCount) { // Downloaded youtube videos always have a fps of 23.98 int videoLength = frameCount / 24; int measureCount = videoLength / 2; ScorePartwisePartMeasure[] measures = new ScorePartwisePartMeasure[measureCount]; for (int i = 0; i < measureCount; i++) { measures[i] = new ScorePartwisePartMeasure(); measures[i].Number = i.ToString(); } Dictionary <Note, int> NoteFrameDict = new Dictionary <Note, int>(); foreach (var keyboardTile in keyboardTiles) { foreach (var press in keyboardTile.TilePresses) { var startFrame = press.Item1; var endFrame = press.Item2; Pitch keyboardPitch = new Pitch(); keyboardPitch.Octave = keyboardTile.Octave.ToString(); keyboardPitch.Step = NotesToMusicXMLStep[keyboardTile.Note]; keyboardPitch.Alter = NoteToAlter(keyboardTile.Note); double startSec = startFrame / 24; int measureIndex = (int)(startSec / 2); var measure = measures[measureIndex]; Note pressNote = new Note(); pressNote.Pitch = keyboardPitch; pressNote.Type = DurationToNodeType(press); measure.Note.Add(pressNote); NoteFrameDict.Add(pressNote, startFrame); } } for (int i = 0; i < measures.Length; i++) { var measure = measures[i]; Note[] notes = new Note[measure.Note.Count]; var notesOrdered = notes.OrderBy(x => NoteFrameDict[x]).Select(x => x); measure.Note.CopyTo(notes, 0); while (measure.Note.Count > 0) { measure.Note.RemoveAt(0); } foreach (var note in notesOrdered) { measure.Note.Add(note); } var lastFrame = 0; foreach (var note in measure.Note) { var startFrame = NoteFrameDict[note]; if (lastFrame == startFrame) { note.Chord = new Empty(); } lastFrame = startFrame; } } ScorePartwise spw = new ScorePartwise(); PartList partList = new PartList(); ScorePart scorePart = new ScorePart(); PartName partName = new PartName(); partName.Value = "NotesheetR"; scorePart.PartName = partName; scorePart.Id = "1"; partList.ScorePart = scorePart; ScorePartwisePart part = new ScorePartwisePart(); part.Id = "1"; foreach (var measure in measures) { part.Measure.Add(measure); } spw.Part.Add(part); spw.PartList = partList; return(ParseToXml(spw)); }
//***************************************** Create Object Factory *******************************************// //The Object Factory, Used to create the elements of the score as required //***************************************** ScorePartwise *******************************************// // Create the top level Partwise Score Element public static ScorePartwise CreateScorePartwise(ScoreProperties i) { ObjectFactory factory = new ObjectFactory(); //Create a scorepart ScorePartwise.Part part; Marshalling.getContext(); ///////////////////// Following are the main components required for the Score in Phase 1 //***************************************** ScorePartwise *******************************************// ScorePartwise scorePartwise = factory.createScorePartwise(); // Set the version of MusicXML scorePartwise.setVersion("3.0"); //***************************************** Set Work and Movement Titles *******************************************// // Create the work element and set its title Work work = factory.createWork(); work.setWorkTitle(i.WorkTitle); //Setting some properties of the root element. scorePartwise.setMovementTitle(i.MovementTitle); //Set the work title of the scorepartwise scorePartwise.setWork(work); //***************************************** Create and set Identification Element *******************************************// //Create identification and set its properties Identification identification = factory.createIdentification(); com.audiveris.proxymusic.Encoding encoding = factory.createEncoding(); //set the software used for encoding encoding.getEncodingDateOrEncoderOrSoftware().add(factory.createEncodingSoftware(i.EncodingSoftware)); // set the encoding identification.setEncoding(encoding); scorePartwise.setIdentification(identification); //***************************************** Set the Credit words *******************************************// Credit credit = factory.createCredit(); FormattedText creditwordtext = new FormattedText(); creditwordtext.setValue(i.CreditWords); credit.getCreditTypeOrLinkOrBookmark().add(creditwordtext); scorePartwise.getCredit().add(credit); //***************************************** Create a PartList *******************************************// PartList partlist = factory.createPartList(); // create a scorepart ScorePart scorepart = factory.createScorePart(); ////////////////////////Some Properties of the scorepart PartName partname = factory.createPartName(); partname.setValue(i.PartDisplayName); scorepart.setPartName(partname); //set part abbreviation partname.setValue(i.PartAbbreviation); scorepart.setPartAbbreviation(partname); //set the scorepart id scorepart.setId(i.ScorePartID); // set the scorepart instrument ScoreInstrument scoreinstrument = factory.createScoreInstrument(); scoreinstrument.setId(i.InstrumentID); scoreinstrument.setInstrumentName(i.InstrumentName); scorepart.getScoreInstrument().add(scoreinstrument); //set the midi device parameters MidiInstrument midiinstrument = factory.createMidiInstrument(); //set midi instrument Id midiinstrument.setId(scoreinstrument); //set midi channel java.lang.Integer midichannel = i.MidiChannel; //channel number midiinstrument.setMidiChannel(midichannel); //set midi program java.lang.Integer midiprogram = i.MidiProgram; //program number midiinstrument.setMidiProgram(midiprogram); scorepart.getMidiDeviceAndMidiInstrument().add(midiinstrument); // add the scorepart to the partlist partlist.getPartGroupOrScorePart().add(scorepart); //Ambiguity scorePartwise.setPartList(partlist); //***************************************** Create a Part *******************************************// if (i.Part == null) { part = factory.createScorePartwisePart(); } else { part = i.Part; } //set part id part.setId(scorepart); // Add the part to the score scorePartwise.getPart().add(part); return(scorePartwise); }
public WPFRendering(ScorePartwise originalScore, Size size, double fontSize) : this(originalScore, fontSize) { ScoreSize = size; }
/// <summary> /// Checks if the notes are according to the timesignature /// </summary> /// <param name="scorePartwise"></param> /// <returns></returns> public static ScorePartwise CheckAndReAlign(ScorePartwise scorePartwise) { double DivPerMeasure = 16; for (int i = 0; i < scorePartwise.getPart().size(); i++) { //for each part in the score ScorePartwise.Part part = (ScorePartwise.Part)scorePartwise.getPart().get(i); Queue <Note> ExtraNotes = new Queue <Note>(); for (int j = 0; j < part.getMeasure().size(); j++) { //for each measure in a part int DSum = 0; ScorePartwise.Part.Measure measure = (ScorePartwise.Part.Measure)part.getMeasure().get(j); //ScorePartwise.Part.Measure TempMeasure = CreateDS.CreateMeasure(j.ToString()); AttributeProperties attprop; //check if timesignature is updated this measure or not if (ReadDS.ReadAttributes(measure) != null) { attprop = ReadDS.ReadAttributes(measure); DivPerMeasure = (attprop.divisions != 0 & attprop.BeatsPerMeasure != "" & attprop.BeatType != "") ? int.Parse(attprop.BeatsPerMeasure) * 4 * attprop.divisions / int.Parse(attprop.BeatType) : DivPerMeasure; } //check if there are any notes from the previous measure to be added to this measure //and add them to the start of the current measure int max = ExtraNotes.Count; for (int k = 0; k < max; k++) { measure.getNoteOrBackupOrForward().add(k, ExtraNotes.Dequeue()); } //check if the notes do not fit in this measure then remove them from the current measure //System.Windows.MessageBox.Show("Initial Number of notes in the measure: " + EditDS.getNotesperMeasure(measure).ToString()); int numNotes = EditDS.getNotesperMeasure(measure); int thresholdIndex = -1; for (int k = 1; k <= numNotes; k++) { NoteProperties noteprop = ReadDS.ReadNote(k, measure); if (DSum + noteprop.duration > DivPerMeasure) { // if the note is too big to fit a measure if (noteprop.duration > DivPerMeasure) { return(null); } thresholdIndex = k; break; } else { DSum += noteprop.duration; } } if (thresholdIndex != -1) { //first queue the extra notes for (int l = thresholdIndex; l <= numNotes; l++) { ExtraNotes.Enqueue((Note)(measure.getNoteOrBackupOrForward().get(EditDS.getnoteindex(l, measure)))); } //then remove from the current measure for (int l = thresholdIndex; l <= numNotes; l++) { measure.getNoteOrBackupOrForward().remove(EditDS.getnoteindex(thresholdIndex, measure)); } List <int> RestDurations = new List <int>(); int DivLeft = (int)DivPerMeasure - DSum; //number of divisions left empty in this measure int power = 0; while (DivLeft != 0) { if ((DivLeft & 1) != 0) //check the leftmost bit { RestDurations.Add(1 << power); //rest durations in powers of two } ++power; DivLeft = DivLeft >> 1; } foreach (int dur in RestDurations) { NoteProperties restprop = new NoteProperties(); restprop.rest = true; restprop.duration = dur; measure.getNoteOrBackupOrForward().add(CreateDS.CreateNote(restprop)); } } //set the measure in the part part.getMeasure().set(j, measure); } //After all the measures in the part are finished and the queue still have notes while (ExtraNotes.Count != 0) { ScorePartwise.Part.Measure measure = CreateDS.CreateMeasure("2"); int DSum = 0; while (DSum < DivPerMeasure) { if (ExtraNotes.Count == 0) { break; } if ((DSum + ((BigDecimal)ExtraNotes.Peek().getDuration()).intValue()) > DivPerMeasure) { break; } else { DSum += ((BigDecimal)ExtraNotes.Peek().getDuration()).intValue(); measure.getNoteOrBackupOrForward().add(ExtraNotes.Dequeue()); } } part.getMeasure().add(measure); } } return(scorePartwise); }
public void ConvertTo_measure_deserialize_success() { var target = new Parser(); var xml = @"<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE score-partwise PUBLIC '-//Recordare//DTD MusicXML 2.0 Partwise//EN' 'http://www.musicxml.org/dtds/partwise.dtd'> <score-partwise> <part id='1'> <measure number='1' implicit='yes' non-controlling='no' width='123.45'> <attributes/> <backup/> <barline/> <bookmark/> <direction/> <figured-bass/> <forward/> <grouping/> <harmony/> <link/> <note/> <print/> <sound/> </measure> </part> </score-partwise> "; var actual = target.Parse(xml); var expected = new ScorePartwise() { Parts = new ElementCollection <Part>() { new Part() { Id = "1", Measures = new ElementCollection <Measure>() { new Measure() { Number = "1", Implicit = YesNo.Yes, ImplicitSpecified = true, NonControlling = YesNo.No, NonControllingSpecified = true, Width = 123.45, WidthSpecified = true, Attributes = new ElementCollection <Attributes>() { new Attributes() }, Backups = new ElementCollection <Backup>() { new Backup() }, Barlines = new ElementCollection <Barline>() { new Barline() }, Bookmarks = new ElementCollection <Bookmark>() { new Bookmark() }, Directions = new ElementCollection <Direction>() { new Direction() }, FiguredBasses = new ElementCollection <FiguredBass>() { new FiguredBass() }, Forwards = new ElementCollection <Forward>() { new Forward() }, Groupings = new ElementCollection <Grouping>() { new Grouping() }, Harmonies = new ElementCollection <Harmony>() { new Harmony() }, Links = new ElementCollection <Link>() { new Link() }, Notes = new ElementCollection <Note>() { new Note() }, Prints = new ElementCollection <Print>() { new Print() }, Sounds = new ElementCollection <Sound>() { new Sound() }, }, } }, }, }; Assert.AreEqual(expected, actual); }
public void ParseSimpleOneNoteStringTest() { String testString1 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + "<!DOCTYPE score-partwise PUBLIC\n" + "\"-//Recordare//DTD MusicXML 3.0 Partwise//EN\"\n" + "\"http://www.musicxml.org/dtds/partwise.dtd\">\n" + "<score-partwise version=\"3.0\">\n" + "<part-list><score-part id=\"P1\"><part-name>Music</part-name></score-part></part-list>" + "<part id=\"P1\"><measure number=\"1\"><attributes><divisions>1</divisions><key>" + "<fifths>0</fifths></key><time><beats>4</beats><beat-type>4</beat-type></time>" + "<clef><sign>G</sign><line>2</line></clef></attributes>" + "<note><pitch><step>C</step><octave>4</octave></pitch><duration>4</duration>" + "<type>whole</type><stem>up</stem></note></measure></part></score-partwise>"; // create the expected structure Note n = new Note() { type = new NoteType { Value = NoteTypeValue.whole }, stem = new Stem { Value = stemvalue.up }, Items = new object[] { new Pitch { step = Step.C, octave = "4" }, new decimal(4) }, ItemsElementName = new[] { ItemsChoiceType1.pitch, ItemsChoiceType1.duration } }; ScorePartwisePartMeasure m = new ScorePartwisePartMeasure() { number = "1", Items = new object[]{ new Attributes() { divisions = 1, key = new[] { new Key { Items = new object[] { 0 }, ItemsElementName = new[] { ItemsChoiceType8.fifths }, } }, time = new[] { new Time { Beats = "4", BeatType = "4"} }, clef = new[] { new Clef { sign = ClefSign.G, line = "2" } } }, n } }; // set expected ScorePartwise expected = new ScorePartwise { version = "3.0", part = new ScorePartwisePart[1] }; // measure part and information expected.part[0] = new ScorePartwisePart { measure = new ScorePartwisePartMeasure[1] }; expected.part[0].measure[0] = m; expected.part[0].id = "P1"; // part list expected.partList = new PartList() { scorepart = new ScorePart() { id = "P1", partName = new PartName { Value = "Music" } } }; ScorePartwise actual = ScorePartwise.Deserialize(testString1); CompareProperties(actual.GetType(), actual, expected); }
private void Window_Loaded(object sender, RoutedEventArgs e) { try { String testString1 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + "<!DOCTYPE score-partwise PUBLIC\n" + "\"-//Recordare//DTD MusicXML 3.0 Partwise//EN\"\n" + "\"http://www.musicxml.org/dtds/partwise.dtd\">\n" + "<score-partwise version=\"3.0\">\n" + "<part-list><score-part id=\"P1\"><part-name>Music</part-name></score-part></part-list>" + "<part id=\"P1\"><measure number=\"1\">" + "<note><pitch><step>C</step><octave>4</octave></pitch><duration>4</duration><type>whole</type><stem>up</stem></note>" + "<attributes><divisions>1</divisions><key>" + "<fifths>0</fifths></key><time><beats>4</beats><beat-type>4</beat-type></time>" + "<clef><sign>G</sign><line>2</line></clef></attributes>" + "</measure></part></score-partwise>"; bool onlyRenderOne = true; //todo: remove for regular debugging ScorePartwise sp = ScorePartwise.Deserialize(XMLStringFetcher.GetXMLFile("00-BasicPitches.xml")); Note v = sp.part[0].measure[0].Items[1] as Note; //ScorePartwise sp = ScorePartwise.Deserialize(XMLStringFetcher.GetXMLFile("BrahWiMeSample.xml")); //ScorePartwise sp = ScorePartwise.Deserialize(XMLStringFetcher.GetXMLFile("01a-Pitches-Pitches.xml")); //ScorePartwise sp = ScorePartwise.LoadFromFile(@"C:\Users\nathan\Documents\GitHub\NETScoreTranscription\NETScoreTranscription\NETScoreTranscriptionLibrary\MusicXMLSamples\BrahWiMeSample.xml"); //ScorePartwise sp = ScorePartwise.LoadFromFile(""); //ScorePartwise sp = ScorePartwise.Deserialize(testString1); WPFRendering wpfmrLarge = new WPFRendering(sp, new Size(400, 900), 100); FrameworkElement largeGrid = wpfmrLarge.RenderMeasure(sp.part[0].measure[0]); v.color = "#00FF00"; //todo: remove largeGrid = wpfmrLarge.RenderLine(); //todo: figure out how to refresh without having to redraw everything WPFRendering wpfmrBase; WPFRendering wpfmrSmall; WPFRendering wpfmrSmallest; FrameworkElement baseGrid = new FrameworkElement(); FrameworkElement smallGrid = new FrameworkElement(); FrameworkElement smallestGrid = new FrameworkElement(); if (!onlyRenderOne) { wpfmrBase = new WPFRendering(sp, Constants.MusicFonts.DEFAULT_SIZE); baseGrid = wpfmrBase.RenderLine(); wpfmrSmall = new WPFRendering(sp, 50); smallGrid = wpfmrSmall.RenderLine(); wpfmrSmallest = new WPFRendering(sp, 25); smallestGrid = wpfmrSmallest.RenderLine(); } // put content on screen and into a grid Grid contentGrid = new Grid(); contentGrid.RowDefinitions.Add(new RowDefinition()); contentGrid.RowDefinitions.Add(new RowDefinition()); contentGrid.RowDefinitions.Add(new RowDefinition()); contentGrid.RowDefinitions.Add(new RowDefinition()); Grid.SetRow(largeGrid, 0); if (!onlyRenderOne) { Grid.SetRow(baseGrid, 1); Grid.SetRow(smallGrid, 2); Grid.SetRow(smallestGrid, 3); } contentGrid.Children.Add(largeGrid); if (!onlyRenderOne) { contentGrid.Children.Add(baseGrid); contentGrid.Children.Add(smallGrid); contentGrid.Children.Add(smallestGrid); } //set window stuff this.Content = contentGrid; this.Height = 900; this.Left = 0; this.Top = 0; } catch (Exception ex) { Console.Out.WriteLine(ex.ToString()); } }