static public List <UVoicePart> Load(string file, UProject project) { List <UVoicePart> resultParts = new List <UVoicePart>(); MidiFile midi = new MidiFile(file); for (int i = 0; i < midi.Tracks; i++) { Dictionary <int, UVoicePart> parts = new Dictionary <int, UVoicePart>(); foreach (var e in midi.Events.GetTrackEvents(i)) { if (e is NoteOnEvent) { var _e = e as NoteOnEvent; if (!parts.ContainsKey(_e.Channel)) { parts.Add(_e.Channel, new UVoicePart()); } var note = project.CreateNote( _e.NoteNumber, (int)_e.AbsoluteTime * project.Resolution / midi.DeltaTicksPerQuarterNote, _e.NoteLength * project.Resolution / midi.DeltaTicksPerQuarterNote); parts[e.Channel].Notes.Add(note); } } foreach (var pair in parts) { pair.Value.DurTick = pair.Value.GetMinDurTick(project); resultParts.Add(pair.Value); } } return(resultParts); }
private static void ParsePart(UProject project, UVoicePart part, List <List <UstLine> > blocks) { int tick = 0; foreach (var block in blocks) { string header = block[0].line; try { switch (header) { case "[#VERSION]": break; case "[#SETTING]": ParseSetting(project, block); break; case "[#TRACKEND]": break; default: if (int.TryParse(header.Substring(2, header.Length - 3), out int noteIndex)) { UNote note = project.CreateNote(); ParseNote(note, block, out var noteTempo); note.position = tick; tick += note.duration; if (note.lyric.ToLower() != "r") { part.notes.Add(note); } if (noteTempo != null && (project.bpm <= 0 || project.bpm > 1000)) { // Fix tempo=500k error. project.bpm = noteTempo.Value; } } else { throw new FileFormatException($"Unexpected header\n{block[0]}"); } break; } } catch (Exception e) when(!(e is FileFormatException)) { throw new FileFormatException($"Failed to parse block\n{block[0]}", e); } } SnapPitchPoints(part); }
private static void ParsePart(UProject project, UVoicePart part, List <List <UstLine> > blocks) { int tick = 0; foreach (var block in blocks) { string header = block[0].line; try { switch (header) { case "[#VERSION]": break; case "[#SETTING]": ParseSetting(project, block); break; case "[#TRACKEND]": break; default: if (int.TryParse(header.Substring(2, header.Length - 3), out int noteIndex)) { UNote note = project.CreateNote(); ParseNote(note, block); note.PosTick = tick; tick += note.DurTick; if (note.Lyric != "R") { part.Notes.Add(note); } } else { throw new FileFormatException($"Unexpected header\n{block[0]}"); } break; } } catch (Exception e) when(!(e is FileFormatException)) { throw new FileFormatException($"Failed to parse block\n{block[0]}", e); } } }
public void Run(UProject project, UVoicePart part, List <UNote> selectedNotes, DocManager docManager) { List <UNote> toAdd = new List <UNote>(); var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList(); foreach (var note in notes) { if (note.Next == null || note.Next.position > note.End + 120) { toAdd.Add(project.CreateNote(note.tone, note.End, 120)); } } if (toAdd.Count == 0) { return; } docManager.StartUndoGroup(); foreach (var note in toAdd) { note.lyric = "-"; docManager.ExecuteCmd(new AddNoteCommand(part, note)); } docManager.EndUndoGroup(); }
public static UProject Load(string file, Encoding encoding = null) { var currentNoteIndex = 0; var version = UstVersion.Early; var currentBlock = UstBlock.None; string[] lines; try { if (encoding == null) { lines = File.ReadAllLines(file, FileEncoding.DetectFileEncoding(file)); } else { lines = File.ReadAllLines(file, encoding); } } catch (Exception e) { DocManager.Inst.ExecuteCmd(new UserMessageNotification(e.GetType().ToString() + "\n" + e.Message)); return(null); } var project = new UProject() { Resolution = 480, FilePath = file, Saved = false }; project.RegisterExpression(new IntExpression(null, "velocity", "VEL") { Data = 100, Min = 0, Max = 200 }); project.RegisterExpression(new IntExpression(null, "volume", "VOL") { Data = 100, Min = 0, Max = 200 }); project.RegisterExpression(new IntExpression(null, "gender", "GEN") { Data = 0, Min = -100, Max = 100 }); project.RegisterExpression(new IntExpression(null, "lowpass", "LPF") { Data = 0, Min = 0, Max = 100 }); project.RegisterExpression(new IntExpression(null, "highpass", "HPF") { Data = 0, Min = 0, Max = 100 }); project.RegisterExpression(new IntExpression(null, "accent", "ACC") { Data = 100, Min = 0, Max = 200 }); project.RegisterExpression(new IntExpression(null, "decay", "DEC") { Data = 0, Min = 0, Max = 100 }); var _track = new UTrack(); project.Tracks.Add(_track); _track.TrackNo = 0; var part = new UVoicePart() { TrackNo = 0, PosTick = 0 }; project.Parts.Add(part); var currentLines = new List <string>(); var currentTick = 0; UNote currentNote = null; foreach (var line in lines) { if (line.Trim().StartsWith(@"[#") && line.Trim().EndsWith(@"]")) { if (line.Equals(versionTag)) { currentBlock = UstBlock.Version; } else if (line.Equals(settingTag)) { currentBlock = UstBlock.Setting; } else { if (line.Equals(endTag)) { currentBlock = UstBlock.Trackend; } else { try { currentNoteIndex = int.Parse(line.Replace("[#", string.Empty).Replace("]", string.Empty)); } catch { DocManager.Inst.ExecuteCmd(new UserMessageNotification("Unknown ust format")); return(null); } currentBlock = UstBlock.Note; } if (currentLines.Count != 0) { currentNote = NoteFromUst(project.CreateNote(), currentLines, version); currentNote.PosTick = currentTick; if (!currentNote.Lyric.Replace("R", string.Empty).Replace("r", string.Empty).Equals(string.Empty)) { part.Notes.Add(currentNote); } currentTick += currentNote.DurTick; currentLines.Clear(); } } } else { if (currentBlock == UstBlock.Version) { if (line.StartsWith("UST Version")) { var v = line.Trim().Replace("UST Version", string.Empty); switch (v) { case "1.0": version = UstVersion.V1_0; break; case "1.1": version = UstVersion.V1_1; break; case "1.2": version = UstVersion.V1_2; break; default: version = UstVersion.Unknown; break; } } } if (currentBlock == UstBlock.Setting) { if (line.StartsWith("Tempo=")) { project.BPM = double.Parse(line.Trim().Replace("Tempo=", string.Empty)); if (project.BPM == 0) { project.BPM = 120; } } if (line.StartsWith("ProjectName=")) { project.Name = line.Trim().Replace("ProjectName=", string.Empty); } if (line.StartsWith("VoiceDir=")) { var singerpath = line.Trim().Replace("VoiceDir=", string.Empty); var singer = DocManager.Inst.GetSinger(singerpath); if (singer == null) { singer = new USinger() { Name = "", Path = singerpath }; } project.Singers.Add(singer); project.Tracks[0].Singer = singer; } } else if (currentBlock == UstBlock.Note) { currentLines.Add(line); } else if (currentBlock == UstBlock.Trackend) { break; } } } if (currentBlock != UstBlock.Trackend) { DocManager.Inst.ExecuteCmd(new UserMessageNotification("Unexpected ust file end")); } part.DurTick = currentTick; return(project); }
static public UProject Load(string file) { XmlDocument vsqx = new XmlDocument(); try { vsqx.Load(file); } catch (Exception e) { System.Windows.MessageBox.Show(e.GetType().ToString() + "\n" + e.Message); return(null); } XmlNamespaceManager nsmanager = new XmlNamespaceManager(vsqx.NameTable); nsmanager.AddNamespace("v3", vsq3NameSpace); nsmanager.AddNamespace("v4", vsq4NameSpace); XmlNode root; string nsPrefix; // Detect vsqx version root = vsqx.SelectSingleNode("v3:vsq3", nsmanager); if (root != null) { nsPrefix = "v3:"; } else { root = vsqx.SelectSingleNode("v4:vsq4", nsmanager); if (root != null) { nsPrefix = "v4:"; } else { System.Windows.MessageBox.Show("Unrecognizable VSQx file format."); return(null); } } UProject uproject = new UProject(); uproject.RegisterExpression(new IntExpression(null, "velocity", "VEL") { Data = 64, Min = 0, Max = 127 }); uproject.RegisterExpression(new IntExpression(null, "volume", "VOL") { Data = 100, Min = 0, Max = 200 }); uproject.RegisterExpression(new IntExpression(null, "opening", "OPE") { Data = 127, Min = 0, Max = 127 }); uproject.RegisterExpression(new IntExpression(null, "accent", "ACC") { Data = 50, Min = 0, Max = 100 }); uproject.RegisterExpression(new IntExpression(null, "decay", "DEC") { Data = 50, Min = 0, Max = 100 }); string bpmPath = string.Format("{0}masterTrack/{0}tempo/{0}{1}", nsPrefix, nsPrefix == "v3:" ? "bpm" : "v"); string beatperbarPath = string.Format("{0}masterTrack/{0}timeSig/{0}{1}", nsPrefix, nsPrefix == "v3:" ? "nume" : "nu"); string beatunitPath = string.Format("{0}masterTrack/{0}timeSig/{0}{1}", nsPrefix, nsPrefix == "v3:" ? "denomi" : "de"); string premeasurePath = string.Format("{0}masterTrack/{0}preMeasure", nsPrefix); string resolutionPath = string.Format("{0}masterTrack/{0}resolution", nsPrefix); string projectnamePath = string.Format("{0}masterTrack/{0}seqName", nsPrefix); string projectcommentPath = string.Format("{0}masterTrack/{0}comment", nsPrefix); string trackPath = string.Format("{0}vsTrack", nsPrefix); string tracknamePath = string.Format("{0}{1}", nsPrefix, nsPrefix == "v3:" ? "trackName" : "name"); string trackcommentPath = string.Format("{0}comment", nsPrefix); string tracknoPath = string.Format("{0}{1}", nsPrefix, nsPrefix == "v3:" ? "vsTrackNo" : "tNo"); string partPath = string.Format("{0}{1}", nsPrefix, nsPrefix == "v3:" ? "musicalPart" : "vsPart"); string partnamePath = string.Format("{0}{1}", nsPrefix, nsPrefix == "v3:" ? "partName" : "name"); string partcommentPath = string.Format("{0}comment", nsPrefix); string notePath = string.Format("{0}note", nsPrefix); string postickPath = string.Format("{0}{1}", nsPrefix, nsPrefix == "v3:" ? "posTick" : "t"); string durtickPath = string.Format("{0}{1}", nsPrefix, nsPrefix == "v3:" ? "durTick" : "dur"); string notenumPath = string.Format("{0}{1}", nsPrefix, nsPrefix == "v3:" ? "noteNum" : "n"); string velocityPath = string.Format("{0}{1}", nsPrefix, nsPrefix == "v3:" ? "velocity" : "v"); string lyricPath = string.Format("{0}{1}", nsPrefix, nsPrefix == "v3:" ? "lyric" : "y"); string phonemePath = string.Format("{0}{1}", nsPrefix, nsPrefix == "v3:" ? "phnms" : "p"); string playtimePath = string.Format("{0}playTime", nsPrefix); string partstyleattrPath = string.Format("{0}{1}/{0}{2}", nsPrefix, nsPrefix == "v3:" ? "partStyle" : "pStyle", nsPrefix == "v3:" ? "attr" : "v"); string notestyleattrPath = string.Format("{0}{1}/{0}{2}", nsPrefix, nsPrefix == "v3:" ? "noteStyle" : "nStyle", nsPrefix == "v3:" ? "attr" : "v"); uproject.BPM = Convert.ToDouble(root.SelectSingleNode(bpmPath, nsmanager).InnerText) / 100; uproject.BeatPerBar = int.Parse(root.SelectSingleNode(beatperbarPath, nsmanager).InnerText); uproject.BeatUnit = int.Parse(root.SelectSingleNode(beatunitPath, nsmanager).InnerText); uproject.Resolution = int.Parse(root.SelectSingleNode(resolutionPath, nsmanager).InnerText); uproject.FilePath = file; uproject.Name = root.SelectSingleNode(projectnamePath, nsmanager).InnerText; uproject.Comment = root.SelectSingleNode(projectcommentPath, nsmanager).InnerText; int preMeasure = int.Parse(root.SelectSingleNode(premeasurePath, nsmanager).InnerText); int partPosTickShift = -preMeasure * uproject.Resolution * uproject.BeatPerBar * 4 / uproject.BeatUnit; USinger usinger = new USinger(); uproject.Singers.Add(usinger); foreach (XmlNode track in root.SelectNodes(trackPath, nsmanager)) // track { UTrack utrack = new UTrack() { Singer = usinger, TrackNo = uproject.Tracks.Count }; uproject.Tracks.Add(utrack); utrack.Name = track.SelectSingleNode(tracknamePath, nsmanager).InnerText; utrack.Comment = track.SelectSingleNode(trackcommentPath, nsmanager).InnerText; utrack.TrackNo = int.Parse(track.SelectSingleNode(tracknoPath, nsmanager).InnerText); foreach (XmlNode part in track.SelectNodes(partPath, nsmanager)) // musical part { UVoicePart upart = new UVoicePart(); uproject.Parts.Add(upart); upart.Name = part.SelectSingleNode(partnamePath, nsmanager).InnerText; upart.Comment = part.SelectSingleNode(partcommentPath, nsmanager).InnerText; upart.PosTick = int.Parse(part.SelectSingleNode(postickPath, nsmanager).InnerText) + partPosTickShift; upart.DurTick = int.Parse(part.SelectSingleNode(playtimePath, nsmanager).InnerText); upart.TrackNo = utrack.TrackNo; foreach (XmlNode note in part.SelectNodes(notePath, nsmanager)) { UNote unote = uproject.CreateNote(); unote.PosTick = int.Parse(note.SelectSingleNode(postickPath, nsmanager).InnerText); unote.DurTick = int.Parse(note.SelectSingleNode(durtickPath, nsmanager).InnerText); unote.NoteNum = int.Parse(note.SelectSingleNode(notenumPath, nsmanager).InnerText); unote.Lyric = note.SelectSingleNode(lyricPath, nsmanager).InnerText; unote.Phonemes[0].Phoneme = note.SelectSingleNode(phonemePath, nsmanager).InnerText; unote.Expressions["velocity"].Data = int.Parse(note.SelectSingleNode(velocityPath, nsmanager).InnerText); foreach (XmlNode notestyle in note.SelectNodes(notestyleattrPath, nsmanager)) { if (notestyle.Attributes["id"].Value == "opening") { unote.Expressions["opening"].Data = int.Parse(notestyle.InnerText); } else if (notestyle.Attributes["id"].Value == "accent") { unote.Expressions["accent"].Data = int.Parse(notestyle.InnerText); } else if (notestyle.Attributes["id"].Value == "decay") { unote.Expressions["decay"].Data = int.Parse(notestyle.InnerText); } } unote.PitchBend.Points[0].X = -uproject.TickToMillisecond(Math.Min(15, unote.DurTick / 3)); unote.PitchBend.Points[1].X = -unote.PitchBend.Points[0].X; upart.Notes.Add(unote); } } } return(uproject); }
public static UProject Load(string file) { XmlDocument vsqx = new XmlDocument(); vsqx.Load(file); XmlNamespaceManager nsmanager = new XmlNamespaceManager(vsqx.NameTable); nsmanager.AddNamespace("v3", vsq3NameSpace); nsmanager.AddNamespace("v4", vsq4NameSpace); XmlNode root; string nsPrefix; // Detect vsqx version if ((root = vsqx.SelectSingleNode("v3:vsq3", nsmanager)) != null) { nsPrefix = "v3:"; } else if ((root = vsqx.SelectSingleNode("v4:vsq4", nsmanager)) != null) { nsPrefix = "v4:"; } else { throw new FileFormatException("Unrecognizable VSQx file format."); } UProject uproject = new UProject(); Ustx.AddDefaultExpressions(uproject); uproject.RegisterExpression(new UExpressionDescriptor("opening", "ope", 0, 100, 100)); string bpmPath = $"{nsPrefix}masterTrack/{nsPrefix}tempo/{nsPrefix}{(nsPrefix == "v3:" ? "bpm" : "v")}"; string beatperbarPath = $"{nsPrefix}masterTrack/{nsPrefix}timeSig/{nsPrefix}{(nsPrefix == "v3:" ? "nume" : "nu")}"; string beatunitPath = $"{nsPrefix}masterTrack/{nsPrefix}timeSig/{nsPrefix}{(nsPrefix == "v3:" ? "denomi" : "de")}"; string premeasurePath = $"{nsPrefix}masterTrack/{nsPrefix}preMeasure"; string resolutionPath = $"{nsPrefix}masterTrack/{nsPrefix}resolution"; string projectnamePath = $"{nsPrefix}masterTrack/{nsPrefix}seqName"; string projectcommentPath = $"{nsPrefix}masterTrack/{nsPrefix}comment"; string trackPath = $"{nsPrefix}vsTrack"; string tracknamePath = $"{nsPrefix}{(nsPrefix == "v3:" ? "trackName" : "name")}"; string trackcommentPath = $"{nsPrefix}comment"; string tracknoPath = $"{nsPrefix}{(nsPrefix == "v3:" ? "vsTrackNo" : "tNo")}"; string partPath = $"{nsPrefix}{(nsPrefix == "v3:" ? "musicalPart" : "vsPart")}"; string partnamePath = $"{nsPrefix}{(nsPrefix == "v3:" ? "partName" : "name")}"; string partcommentPath = $"{nsPrefix}comment"; string notePath = $"{nsPrefix}note"; string postickPath = $"{nsPrefix}{(nsPrefix == "v3:" ? "posTick" : "t")}"; string durtickPath = $"{nsPrefix}{(nsPrefix == "v3:" ? "durTick" : "dur")}"; string notenumPath = $"{nsPrefix}{(nsPrefix == "v3:" ? "noteNum" : "n")}"; string velocityPath = $"{nsPrefix}{(nsPrefix == "v3:" ? "velocity" : "v")}"; string lyricPath = $"{nsPrefix}{(nsPrefix == "v3:" ? "lyric" : "y")}"; string phonemePath = $"{nsPrefix}{(nsPrefix == "v3:" ? "phnms" : "p")}"; string playtimePath = $"{nsPrefix}playTime"; string partstyleattrPath = $"{nsPrefix}{(nsPrefix == "v3:" ? "partStyle" : "pStyle")}/{nsPrefix}{(nsPrefix == "v3:" ? "attr" : "v")}"; string notestyleattrPath = $"{nsPrefix}{(nsPrefix == "v3:" ? "noteStyle" : "nStyle")}/{nsPrefix}{(nsPrefix == "v3:" ? "attr" : "v")}"; uproject.bpm = Convert.ToDouble(root.SelectSingleNode(bpmPath, nsmanager).InnerText) / 100; uproject.beatPerBar = int.Parse(root.SelectSingleNode(beatperbarPath, nsmanager).InnerText); uproject.beatUnit = int.Parse(root.SelectSingleNode(beatunitPath, nsmanager).InnerText); uproject.resolution = int.Parse(root.SelectSingleNode(resolutionPath, nsmanager).InnerText); uproject.FilePath = file; uproject.name = root.SelectSingleNode(projectnamePath, nsmanager).InnerText; uproject.comment = root.SelectSingleNode(projectcommentPath, nsmanager).InnerText; int preMeasure = int.Parse(root.SelectSingleNode(premeasurePath, nsmanager).InnerText); int partPosTickShift = -preMeasure * uproject.resolution * uproject.beatPerBar * 4 / uproject.beatUnit; USinger usinger = new USinger(""); foreach (XmlNode track in root.SelectNodes(trackPath, nsmanager)) // track { UTrack utrack = new UTrack() { Singer = usinger, TrackNo = uproject.tracks.Count }; uproject.tracks.Add(utrack); //utrack.Name = track.SelectSingleNode(tracknamePath, nsmanager).InnerText; //utrack.Comment = track.SelectSingleNode(trackcommentPath, nsmanager).InnerText; utrack.TrackNo = int.Parse(track.SelectSingleNode(tracknoPath, nsmanager).InnerText); foreach (XmlNode part in track.SelectNodes(partPath, nsmanager)) // musical part { UVoicePart upart = new UVoicePart(); uproject.parts.Add(upart); upart.name = part.SelectSingleNode(partnamePath, nsmanager).InnerText; upart.comment = part.SelectSingleNode(partcommentPath, nsmanager).InnerText; upart.position = int.Parse(part.SelectSingleNode(postickPath, nsmanager).InnerText) + partPosTickShift; upart.Duration = int.Parse(part.SelectSingleNode(playtimePath, nsmanager).InnerText); upart.trackNo = utrack.TrackNo; foreach (XmlNode note in part.SelectNodes(notePath, nsmanager)) { UNote unote = uproject.CreateNote(); unote.position = int.Parse(note.SelectSingleNode(postickPath, nsmanager).InnerText); unote.duration = int.Parse(note.SelectSingleNode(durtickPath, nsmanager).InnerText); unote.tone = int.Parse(note.SelectSingleNode(notenumPath, nsmanager).InnerText); unote.lyric = note.SelectSingleNode(lyricPath, nsmanager).InnerText; if (unote.lyric == "-") { unote.lyric = "..."; } unote.phonemeExpressions.Add(new UExpression("vel") { index = 0, value = int.Parse(note.SelectSingleNode(velocityPath, nsmanager).InnerText) * 100 / 64, }); foreach (XmlNode notestyle in note.SelectNodes(notestyleattrPath, nsmanager)) { if (notestyle.Attributes["id"].Value == "opening") { unote.phonemeExpressions.Add(new UExpression("ope") { index = 0, value = int.Parse(notestyle.InnerText) * 100 / 127, }); } else if (notestyle.Attributes["id"].Value == "accent") { unote.phonemeExpressions.Add(new UExpression("atk") { index = 0, value = int.Parse(notestyle.InnerText) * 2, }); } else if (notestyle.Attributes["id"].Value == "decay") { unote.phonemeExpressions.Add(new UExpression("dec") { index = 0, // V4 default is 50. Translate it to no effect in OU. V4 dec 100 roughly maps to OU 50. value = Math.Max(0, int.Parse(notestyle.InnerText) - 50), }); } } unote.pitch.data[0].X = -(float)uproject.TickToMillisecond(Math.Min(15, unote.duration / 3)); unote.pitch.data[1].X = -unote.pitch.data[0].X; upart.notes.Add(unote); } } } uproject.AfterLoad(); uproject.Validate(); return(uproject); }