/// <summary> /// This example expects an MXL file with a score-partwise root element. /// </summary> /// <param name="args"></param> static void Main(string[] args) { string fileName; scorepartwise score = new scorepartwise(); // Get the file name from the user; // Check to ensure it exists do { Write(">: "); fileName = ReadLine(); }while (!Exists(fileName)); // Generate GUID for temporary XML file var guidFile = Guid.NewGuid().ToString() + ".xml"; // Unzip the MXL file // Get the XML file within; sxculding meta files using (var zip = ZipFile.OpenRead(fileName)) { if (zip.Entries.FirstOrDefault(e => e.Name.ToLower().Contains("xml") && !e.FullName.ToLower().Contains("meta")) is ZipArchiveEntry entry) { entry.ExtractToFile(guidFile); } else { WriteLine("Could not find entry."); } } // Read temporary XMl file // Deserialize to scorepartwise // Set DtdProcessing to Parse because that does something using (var reader = XmlReader.Create(guidFile, new XmlReaderSettings() { DtdProcessing = DtdProcessing.Parse })) { var serializer = new XmlSerializer(typeof(scorepartwise)); if (serializer.CanDeserialize(reader) && serializer.Deserialize(reader) is scorepartwise result) { score = result; } else { WriteLine("Could not deserialize XML file."); } } // Delete temporary XML file Delete(guidFile); // See that we have read the MXL file WriteLine(score.version); ReadLine(); }
/// <summary> /// Do export to MusicXML file. /// </summary> /// <param name="sequence">A sequence to be exported.</param> /// <param name="file_path">A file path.</param> public void write(VsqFile sequence, string file_path) { var score = new scorepartwise(); score.version = "2.0"; score.identification = new identification(); score.identification.encoding = new encoding(); score.identification.encoding.software.Add(this.GetType().FullName); score.partlist = new partlist(); score.partlist.scorepart = new scorepart(); score.partlist.scorepart.id = "P1"; score.partlist.scorepart.partname = new partname(); score.partlist.scorepart.partname.Value = sequence.Track[1].getName(); var partlist = new List <scorepart>(); for (int i = 2; i < sequence.Track.Count; ++i) { var track = sequence.Track[i]; var scorepart = new scorepart(); scorepart.id = "P" + i; scorepart.partname = new partname(); scorepart.partname.Value = track.getName(); partlist.Add(scorepart); } score.partlist.Items = partlist.ToArray(); var quantized_tempo_table = quantizeTempoTable(sequence.TempoTable); score.part = sequence.Track.Skip(1).Select((track) => { var result = createScorePart(track, sequence.TimesigTable, quantized_tempo_table); quantized_tempo_table.Clear(); return(result); }).ToArray(); for (int i = 0; i < score.part.Length; i++) { score.part[i].id = "P" + (i + 1); } var serializer = new System.Xml.Serialization.XmlSerializer(typeof(scorepartwise)); using (var stream = new FileStream(file_path, FileMode.Create, FileAccess.Write)) { var writer = new System.Xml.XmlTextWriter(stream, System.Text.Encoding.UTF8); writer.Formatting = System.Xml.Formatting.Indented; writer.WriteStartDocument(); writer.WriteDocType("score-partwise", "-//Recordare//DTD MusicXML 2.0 Partwise//EN", "http://www.musicxml.org/dtds/partwise.dtd", null); var ns = new System.Xml.Serialization.XmlSerializerNamespaces(); ns.Add(string.Empty, string.Empty); serializer.Serialize(writer, score, ns); } }
async void OpenFileAsync() { progressLayout.Visibility = ViewStates.Visible; var deserializer = new MusicXMLDeserializer(); score = await deserializer.DeserializeObjectAsync <scorepartwise>(path); ActionBar.Show(); ActionBar.Title = score.work.worktitle; this.progressLayout.Visibility = ViewStates.Gone; DrawNotation(); }
private Score BuildScore(scorepartwise rawScore) { var builtScore = new Score() { Info = BuildScoreInfo(rawScore), Parts = rawScore.part.Select(x => new Part() { Id = x.id, Staves = BuildPartStaves(rawScore, x.measure.ToArray()) }).ToArray() }; BuildScoreRemoveEmptyStaves(builtScore); return(builtScore); }
void SetPages(scorepartwise scorepartwise) { //foreach (var p in scorepartwise.part.Select((value,i) => new {i, value})) //{ // for (var i = 0; i < p.value.measure.Length; i++) // { // if (p.i == 0) // { // } // } //} foreach (var part in scorepartwise.part) { string partId = part.id; var measureList = new List <scorepartwisePartMeasure>(); int pageNumber = 0; foreach (var measure in part.measure) { if (measure.Items.ToList().Find(item => item.GetType() == typeof(print)) != null) { if (measureList.Count != 0) { if (Pages.Find(p => p.pageNumber == pageNumber) == null) { Pages.Add(new Page(pageNumber)); } Pages.Find(p => p.pageNumber == pageNumber).AddMeasures(partId, measureList); pageNumber++; } measureList.Clear(); } measureList.Add(measure); } if (measureList.Count != 0) { if (Pages.Find(p => p.pageNumber == pageNumber) == null) { Pages.Add(new Page(pageNumber)); } Pages.Find(p => p.pageNumber == pageNumber).AddMeasures(partId, measureList); } } }
static void Main(string[] args) { IDictionary <string, string[]> Arguments = ParseCommands(args); string inputFile = string.Empty; string outputFile = string.Empty; if (Arguments.ContainsKey("input")) { inputFile = Arguments["input"][0]; } if (Arguments.ContainsKey("output")) { outputFile = Arguments["output"][0]; } if (string.IsNullOrEmpty(inputFile)) { Console.WriteLine("Invalid arguments."); Console.WriteLine(); Console.WriteLine("Usage:"); Console.WriteLine("\tMusicXML2SonicPi.exe -input <filename> [-output <filename>]"); return; } if (string.IsNullOrEmpty(outputFile)) { outputFile = string.Concat(inputFile, ".rb"); } XmlSerializer serializer = new XmlSerializer(typeof(scorepartwise)); TextReader reader = new StreamReader(inputFile); scorepartwise score = (scorepartwise)serializer.Deserialize(reader); TextWriter writer = new StreamWriter(outputFile); reader.Close(); ProcessScore(score, ref writer); writer.Close(); }
private Staff[] BuildPartStaves(scorepartwise rawScore, scorepartwisePartMeasure[] measures) { var elementStaves = new List <List <List <IElement> > >(2) { new List <List <IElement> >(), new List <List <IElement> >() }; var beatDurationDirectives = new List <BeatDurationDirective>(); var repeatDirectives = new List <RepeatDirective>(); foreach (var measure in measures) { BuildPartStaffMeasure(measure, elementStaves, beatDurationDirectives, repeatDirectives); } return(elementStaves.Select((x, i) => new Staff() { Number = i + 1, Directives = new List <IDirective>().Concat(beatDurationDirectives) .Concat(repeatDirectives).ToArray(), Elements = x.Select(y => y.ToArray()).ToArray() }).Where(x => x.Elements.Length > 0).ToArray()); }
private static void ProcessScore(scorepartwise score, ref TextWriter writer) { var tempo = (score.part.Select(p => p.measure).Select(p => p.FirstOrDefault().Items.Where(i => i.GetType() == typeof(direction))).First().DefaultIfEmpty(new direction().sound = new sound() { tempo = 60 }).First() as direction).sound.tempo; Score score_ = new Score("SonicPi Music Score", (int)tempo, "piano"); score.part.ToList().ForEach(delegate(scorepartwisePart part) { Part part_ = new Part(part.id, "Score part", true); score_.Parts.Add(part_); part.measure.ToList().ForEach(delegate(scorepartwisePartMeasure measure) { Measure measure_ = new Measure(measure.number, "Score measure"); part_.BlockPart.Measures.Add(measure_); measure.Items.ToList().ForEach(delegate(object measureItem) { switch (measureItem.GetType().Name) { case "note": int voice = 1; int.TryParse((measureItem as note).voice, out voice); Note note_ = new Note(0, voice, false); measure_.Notes.Add(note_); if ((measureItem as note).accidental != null) { note_.Accidental = (NoteAccidental)Enum.Parse(typeof(NoteAccidental), ((measureItem as note).accidental).Value.ToString(), true); } ((measureItem as note).ItemsElementName as ItemsChoiceType1[]).ToList().ForEach(delegate(ItemsChoiceType1 item) { int i = ((measureItem as note).ItemsElementName as ItemsChoiceType1[]).ToList().IndexOf(item); switch (item) { case ItemsChoiceType1.chord: note_.IsChord = true; break; case ItemsChoiceType1.cue: break; case ItemsChoiceType1.duration: note_.Length = int.Parse((measureItem as note).Items[i].ToString()); break; case ItemsChoiceType1.grace: break; case ItemsChoiceType1.pitch: note_.NotePitch = new Pitch( (NoteStep)Enum.Parse(typeof(NoteStep), ((measureItem as note).Items[i] as pitch).step.ToString(), true), ((measureItem as note).Items[i] as pitch).octave); break; case ItemsChoiceType1.rest: note_.NotePitch = new Pitch(NoteStep.Rest); break; case ItemsChoiceType1.tie: if (((measureItem as note).Items[i] as tie).type == startstop.stop) { note_.Length = note_.Length * -1; } break; case ItemsChoiceType1.unpitched: break; } }); break; case "direction": break; case "attributes": break; } }); }); }); string code = score_.ToString(); writer.Write(code); }
private ScoreInfo BuildScoreInfo(scorepartwise score) { var info = new ScoreInfo(); if (score.work != null) { if (score.work.worktitle != null) { info.WorkTitle = score.work.worktitle; } if (score.work.worknumber != null) { info.WorkNumber = score.work.worknumber; } } if (score.movementnumber != null) { info.MovementNumber = score.movementnumber; } if (score.movementtitle != null) { info.MovementTitle = score.movementtitle; } if (score.identification != null) { if (score.identification.creator?.Length > 0) { info.Creators = score.identification.creator.Select(x => new ScoreCreator() { Type = x.type ?? "", Name = x.Value ?? "" }).ToArray(); } if (score.identification.rights?.Length > 0) { info.Rights = score.identification.rights.Select(x => new ScoreRights() { Type = x.type ?? "", Content = x.Value ?? "" }).ToArray(); } if (score.identification.miscellaneous?.Length > 0) { info.Misc = score.identification.miscellaneous.Select(x => new ScoreMisc() { Key = x.name ?? "", Value = x.Value ?? "" }).ToArray(); } if (score.identification.source != null) { info.Source = score.identification.source; } if (score.identification.encoding?.Items.Length > 0) { var encodingSoftware = score.identification.encoding.Items.Where((x, i) => score.identification.encoding.ItemsElementName[i] == ItemsChoiceType.software).Select(x => x.ToString()).ToArray(); var encodingDates = score.identification.encoding.Items.Where((x, i) => score.identification.encoding.ItemsElementName[i] == ItemsChoiceType.encodingdate).OfType <DateTime>().ToArray(); if (encodingSoftware.Length > 0) { info.EncodingSoftware = encodingSoftware; } if (encodingDates.Length > 0) { info.EncodingDates = encodingDates; } } } if (score.credit?.Length > 0) { info.Credits = score.credit.SelectMany(credit => credit.Items.OfType <formattedtextid>().Select(creditWords => creditWords.Value ?? "")).ToArray(); } return(info); }
public ScoreModified(scorepartwise scorepartwise) { Pages = new List <Page>(); SetPages(scorepartwise); }