private static UNote NoteFromUst(UNote note, List <string> lines, UstVersion version) { string pbs = "", pbw = "", pby = "", pbm = ""; foreach (var line in lines) { if (line.StartsWith("Lyric=")) { note.Phonemes[0].Phoneme = note.Lyric = line.Trim().Replace("Lyric=", string.Empty); if (note.Phonemes[0].Phoneme.StartsWith("?")) { note.Phonemes[0].Phoneme = note.Phonemes[0].Phoneme.Substring(1); note.Phonemes[0].AutoRemapped = false; } } if (line.StartsWith("Length=")) { note.DurTick = int.Parse(line.Trim().Replace("Length=", string.Empty)); } if (line.StartsWith("NoteNum=")) { note.NoteNum = int.Parse(line.Trim().Replace("NoteNum=", string.Empty)); } if (line.StartsWith("Velocity=")) { note.Expressions["velocity"].Data = int.Parse(line.Trim().Replace("Velocity=", string.Empty)); } if (line.StartsWith("Intensity=")) { note.Expressions["volume"].Data = int.Parse(line.Trim().Replace("Intensity=", string.Empty)); } if (line.StartsWith("PreUtterance=")) { if (line.Trim() == "PreUtterance=") { note.Phonemes[0].AutoEnvelope = true; } else { note.Phonemes[0].AutoEnvelope = false; note.Phonemes[0].Preutter = double.Parse(line.Trim().Replace("PreUtterance=", "")); } } if (line.StartsWith("VoiceOverlap=")) { note.Phonemes[0].Overlap = double.Parse(line.Trim().Replace("VoiceOverlap=", string.Empty)); } if (line.StartsWith("Envelope=")) { var pts = line.Trim().Replace("Envelope=", string.Empty).Split(new[] { ',' }); if (pts.Count() > 5) { note.Expressions["decay"].Data = 100 - (int)double.Parse(pts[5]); } } if (line.StartsWith("VBR=")) { VibratoFromUst(note.Vibrato, line.Trim().Replace("VBR=", string.Empty)); } if (line.StartsWith("PBS=")) { pbs = line.Trim().Replace("PBS=", string.Empty); } if (line.StartsWith("PBW=")) { pbw = line.Trim().Replace("PBW=", string.Empty); } if (line.StartsWith("PBY=")) { pby = line.Trim().Replace("PBY=", string.Empty); } if (line.StartsWith("PBM=")) { pbm = line.Trim().Replace("PBM=", string.Empty); } } if (pbs != string.Empty) { var pts = note.PitchBend.Data as List <PitchPoint>; pts.Clear(); // PBS if (pbs.Contains(';')) { pts.Add(new PitchPoint(double.Parse(pbs.Split(new[] { ';' })[0]), double.Parse(pbs.Split(new[] { ';' })[1]))); note.PitchBend.SnapFirst = false; } else { pts.Add(new PitchPoint(double.Parse(pbs), 0)); note.PitchBend.SnapFirst = true; } var x = pts.First().X; if (pbw != string.Empty) { var w = pbw.Split(new[] { ',' }); string[] y = null; if (w.Count() > 1) { y = pby.Split(new[] { ',' }); } for (var i = 0; i < w.Count() - 1; i++) { x += string.IsNullOrEmpty(w[i]) ? 0 : float.Parse(w[i]); pts.Add(new PitchPoint(x, string.IsNullOrEmpty(y[i]) ? 0 : double.Parse(y[i]))); } pts.Add(new PitchPoint(x + double.Parse(w[w.Count() - 1]), 0)); } if (pbm != string.Empty) { var m = pbw.Split(new[] { ',' }); for (var i = 0; i < m.Count() - 1; i++) { pts[i].Shape = m[i] == "r" ? PitchPointShape.o : m[i] == "s" ? PitchPointShape.l : m[i] == "j" ? PitchPointShape.i : PitchPointShape.io; } } } return(note); }
static public UProject Load(string file, Encoding encoding = null) { int currentNoteIndex = 0; UstVersion version = UstVersion.Early; UstBlock currentBlock = UstBlock.None; string[] lines; try { if (encoding == null) { lines = File.ReadAllLines(file, EncodingUtil.DetectFileEncoding(file)); } else { lines = File.ReadAllLines(file, encoding); } } catch (Exception e) { DocManager.Inst.ExecuteCmd(new UserMessageNotification(e.GetType().ToString() + "\n" + e.Message)); return(null); } UProject 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; UVoicePart part = new UVoicePart() { TrackNo = 0, PosTick = 0 }; project.Parts.Add(part); List <string> currentLines = new List <string>(); int currentTick = 0; UNote currentNote = null; foreach (string 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("[#", "").Replace("]", "")); } 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", "").Replace("r", "").Equals("")) { part.Notes.Add(currentNote); } currentTick += currentNote.DurTick; currentLines.Clear(); } } } else { if (currentBlock == UstBlock.Version) { if (line.StartsWith("UST Version")) { string v = line.Trim().Replace("UST Version", ""); if (v == "1.0") { version = UstVersion.V1_0; } else if (v == "1.1") { version = UstVersion.V1_1; } else if (v == "1.2") { version = UstVersion.V1_2; } else { version = UstVersion.Unknown; } } } if (currentBlock == UstBlock.Setting) { if (line.StartsWith("Tempo=")) { project.BPM = double.Parse(line.Trim().Replace("Tempo=", "")); if (project.BPM == 0) { project.BPM = 120; } } if (line.StartsWith("ProjectName=")) { project.Name = line.Trim().Replace("ProjectName=", ""); } if (line.StartsWith("VoiceDir=")) { string singerpath = line.Trim().Replace("VoiceDir=", ""); var singer = UtauSoundbank.GetSinger(singerpath, EncodingUtil.DetectFileEncoding(file), DocManager.Inst.Singers); 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 UNote NoteFromUst(UNote note, List<string> lines, UstVersion version) { string pbs = "", pbw = "", pby = "", pbm = ""; foreach (string line in lines) { if (line.StartsWith("Lyric=")) { note.Phonemes[0].Phoneme = note.Lyric = line.Trim().Replace("Lyric=", ""); if (note.Phonemes[0].Phoneme.StartsWith("?")) { note.Phonemes[0].Phoneme = note.Phonemes[0].Phoneme.Substring(1); note.Phonemes[0].AutoRemapped = false; } } if (line.StartsWith("Length=")) note.DurTick = int.Parse(line.Trim().Replace("Length=", "")); if (line.StartsWith("NoteNum=")) note.NoteNum = int.Parse(line.Trim().Replace("NoteNum=", "")); if (line.StartsWith("Velocity=")) note.Expressions["velocity"].Data = int.Parse(line.Trim().Replace("Velocity=", "")); if (line.StartsWith("Intensity=")) note.Expressions["volume"].Data = int.Parse(line.Trim().Replace("Intensity=", "")); if (line.StartsWith("PreUtterance=")) { if (line.Trim() == "PreUtterance=") note.Phonemes[0].AutoEnvelope = true; else { note.Phonemes[0].AutoEnvelope = false; note.Phonemes[0].Preutter = double.Parse(line.Trim().Replace("PreUtterance=", "")); } } if (line.StartsWith("VoiceOverlap=")) note.Phonemes[0].Overlap = double.Parse(line.Trim().Replace("VoiceOverlap=", "")); if (line.StartsWith("Envelope=")) { var pts = line.Trim().Replace("Envelope=", "").Split(new[] { ',' }); if (pts.Count() > 5) note.Expressions["decay"].Data = 100 - (int)double.Parse(pts[5]); } if (line.StartsWith("VBR=")) VibratoFromUst(note.Vibrato, line.Trim().Replace("VBR=", "")); if (line.StartsWith("PBS=")) pbs = line.Trim().Replace("PBS=", ""); if (line.StartsWith("PBW=")) pbw = line.Trim().Replace("PBW=", ""); if (line.StartsWith("PBY=")) pby = line.Trim().Replace("PBY=", ""); if (line.StartsWith("PBM=")) pbm = line.Trim().Replace("PBM=", ""); } if (pbs != "") { var pts = note.PitchBend.Data as List<PitchPoint>; pts.Clear(); // PBS if (pbs.Contains(';')) { pts.Add(new PitchPoint(double.Parse(pbs.Split(new[] { ';' })[0]), double.Parse(pbs.Split(new[] { ';' })[1]))); note.PitchBend.SnapFirst = false; } else { pts.Add(new PitchPoint(double.Parse(pbs), 0)); note.PitchBend.SnapFirst = true; } double x = pts.First().X; if (pbw != "") { string[] w = pbw.Split(new[] { ',' }); string[] y = null; if (w.Count() > 1) y = pby.Split(new[] { ',' }); for (int i = 0; i < w.Count() - 1; i++) { x += w[i] == "" ? 0 : float.Parse(w[i]); pts.Add(new PitchPoint(x, y[i] == "" ? 0 : double.Parse(y[i]))); } pts.Add(new PitchPoint(x + double.Parse(w[w.Count() - 1]), 0)); } if (pbm != "") { string[] m = pbw.Split(new[] { ',' }); for (int i = 0; i < m.Count() - 1; i++) { pts[i].Shape = m[i] == "r" ? PitchPointShape.o : m[i] == "s" ? PitchPointShape.l : m[i] == "j" ? PitchPointShape.i : PitchPointShape.io; } } } return note; }
static UNote NoteFromUst(UNote note, List <string> lines, UstVersion version) { string pbs = "", pbw = "", pby = "", pbm = ""; foreach (string line in lines) { if (line.StartsWith("Lyric=")) { note.Phonemes[0].Phoneme = note.Lyric = line.Trim().Replace("Lyric=", string.Empty); if (note.Phonemes[0].Phoneme.StartsWith("?")) { note.Phonemes[0].Phoneme = note.Phonemes[0].Phoneme.Substring(1); note.Phonemes[0].AutoRemapped = false; } } if (line.StartsWith("Length=")) { note.DurTick = int.Parse(line.Trim().Replace("Length=", string.Empty)); } if (line.StartsWith("NoteNum=")) { note.NoteNum = int.Parse(line.Trim().Replace("NoteNum=", string.Empty)); } if (line.StartsWith("Velocity=")) { note.Expressions["velocity"].Data = int.Parse(line.Trim().Replace("Velocity=", string.Empty)); } if (line.StartsWith("Intensity=")) { note.Expressions["volume"].Data = int.Parse(line.Trim().Replace("Intensity=", string.Empty)); } if (line.StartsWith("PreUtterance=")) { if (line.Trim() == "PreUtterance=") { note.Phonemes[0].AutoEnvelope = true; } else { note.Phonemes[0].AutoEnvelope = false; note.Phonemes[0].Preutter = double.Parse(line.Trim().Replace("PreUtterance=", string.Empty)); } } if (line.StartsWith("VoiceOverlap=")) { if (line.Trim() == "VoiceOverlap=") { note.Phonemes[0].Overlap = 0; } else { note.Phonemes[0].Overlap = double.Parse(line.Trim().Replace("VoiceOverlap=", string.Empty)); } } if (line.StartsWith("Envelope=")) { //Envelope=0,37.3,35,0,100,100,0 var pts = line.Trim().Replace("Envelope=", string.Empty).Split(new[] { ',' }); note.Expressions["decay"].Data = (int)double.Parse(pts[5]); //note.Expressions["accent"].Data = (int)double.Parse(pts[0]); #region Envelope Vals //arraycnt = 0,1, 2,3, 4, 5, 6,7, 8, 9, 10 //env long = 0,5,20,0,100,100,100,%,15,10,100 //0 = {0,0} //1 = { 5,100} //3 = { 20,100} //4 = { 15,100} //2 = { 10,100} //if (pts.Contains("%")) { // if (pts.Count() == 11) { // note.Phonemes[0].Envelope.Points[0].X = double.Parse(pts[0]) - note.Phonemes[0].Preutter; // note.Phonemes[0].Envelope.Points[0].Y = double.Parse(pts[3]); // note.Phonemes[0].Envelope.Points[1].X = double.Parse(pts[1]); // note.Phonemes[0].Envelope.Points[1].Y = double.Parse(pts[4]); // note.Phonemes[0].Envelope.Points[2].X = double.Parse(pts[9]); // note.Phonemes[0].Envelope.Points[2].Y = double.Parse(pts[10]); // note.Phonemes[0].Envelope.Points[3].X = double.Parse(pts[2]); // note.Phonemes[0].Envelope.Points[3].Y = double.Parse(pts[5]); // note.Phonemes[0].Envelope.Points[4].X = double.Parse(pts[8]); // note.Phonemes[0].Envelope.Points[4].Y = double.Parse(pts[6]); // } else if (pts.Count() == 9) { // //note.Expressions["decay"].Data = (int)double.Parse(pts[2]); // //note.Expressions["accent"].Data = (int)double.Parse(pts[1]); // note.Phonemes[0].Envelope.Points[0].X = double.Parse(pts[0]) - note.Phonemes[0].Preutter; // note.Phonemes[0].Envelope.Points[0].Y = double.Parse(pts[3]); // note.Phonemes[0].Envelope.Points[1].X = double.Parse(pts[1]); // note.Phonemes[0].Envelope.Points[1].Y = double.Parse(pts[4]); // note.Phonemes[0].Envelope.Points[2].X = 0; // note.Phonemes[0].Envelope.Points[2].Y = 100; // note.Phonemes[0].Envelope.Points[3].X = double.Parse(pts[2]); // note.Phonemes[0].Envelope.Points[3].Y = double.Parse(pts[5]); // note.Phonemes[0].Envelope.Points[4].X = double.Parse(pts[8]); // note.Phonemes[0].Envelope.Points[4].Y = double.Parse(pts[6]); // } else { // note.Phonemes[0].Envelope.Points[0].X = double.Parse(pts[0]) - note.Phonemes[0].Preutter; // note.Phonemes[0].Envelope.Points[0].Y = double.Parse(pts[3]); // note.Phonemes[0].Envelope.Points[1].X = double.Parse(pts[1]); // note.Phonemes[0].Envelope.Points[1].Y = double.Parse(pts[4]); // note.Phonemes[0].Envelope.Points[2].X = 0; // note.Phonemes[0].Envelope.Points[2].Y = 100; // note.Phonemes[0].Envelope.Points[3].X = double.Parse(pts[2]); // note.Phonemes[0].Envelope.Points[3].Y = double.Parse(pts[5]); // note.Phonemes[0].Envelope.Points[4].X = 0; // note.Phonemes[0].Envelope.Points[4].Y = double.Parse(pts[6]); // } //} // //Order =p1,p2,p3,v1,v2,v3,v4 // //Envpoints=1x,2x,3x,1y,2y,3y,4y // else { // note.Phonemes[0].Envelope.Points[0].X = double.Parse(pts[0]) - note.Phonemes[0].Preutter; // note.Phonemes[0].Envelope.Points[0].Y = double.Parse(pts[3]); // note.Phonemes[0].Envelope.Points[1].X = double.Parse(pts[1]); // note.Phonemes[0].Envelope.Points[1].Y = double.Parse(pts[4]); // note.Phonemes[0].Envelope.Points[2].X = 0; // note.Phonemes[0].Envelope.Points[2].Y = 100; // note.Phonemes[0].Envelope.Points[3].X = double.Parse(pts[2]); // note.Phonemes[0].Envelope.Points[3].Y = double.Parse(pts[5]); // note.Phonemes[0].Envelope.Points[4].X = 0; // note.Phonemes[0].Envelope.Points[4].Y = double.Parse(pts[6]); //} #endregion } if (line.StartsWith("StartPoint=")) { note.Expressions["accent"].Data = (int)double.Parse(line.Trim().Replace("StartPoint=", string.Empty)); } if (line.StartsWith("VBR=")) { VibratoFromUst(note.Vibrato, line.Trim().Replace("VBR=", string.Empty)); } if (line.StartsWith("PBS=")) { pbs = line.Trim().Replace("PBS=", string.Empty); } if (line.StartsWith("PBW=")) { pbw = line.Trim().Replace("PBW=", string.Empty); } if (line.StartsWith("PBY=")) { pby = line.Trim().Replace("PBY=", string.Empty); } if (line.StartsWith("PBM=")) { pbm = line.Trim().Replace("PBM=", string.Empty); } } if (pbs != string.Empty) { var pts = note.PitchBend.Data as List <PitchPoint>; pts.Clear(); // PBS if (pbs.Contains(';')) { pts.Add(new PitchPoint(double.Parse(pbs.Split(new[] { ';' })[0]), double.Parse(pbs.Split(new[] { ';' })[1]))); note.PitchBend.SnapFirst = false; } else { pts.Add(new PitchPoint(double.Parse(pbs), 0)); note.PitchBend.SnapFirst = true; } double x = pts.First().X; if (pbw != string.Empty) { string[] w = pbw.Split(new[] { ',' }); string[] y = null; if (w.Count() > 1) { y = pby.Split(new[] { ',' }); } for (int i = 0; i < w.Count() - 1; i++) { x += string.IsNullOrEmpty(w[i]) ? 0 : float.Parse(w[i]); if (y.Count() > 1) { if (y.Count() < w.Count()) { if (i >= y.Count()) { pts.Add(new PitchPoint(x, string.IsNullOrEmpty(y[y.Count() - 1]) ? 0 : double.Parse(y[y.Count() - 1]))); } else { pts.Add(new PitchPoint(x, string.IsNullOrEmpty(y[i]) ? 0 : double.Parse(y[i]))); } } else { pts.Add(new PitchPoint(x, string.IsNullOrEmpty(y[i]) ? 0 : double.Parse(y[i]))); } } else { pts.Add(new PitchPoint(x, string.IsNullOrEmpty(y[0]) ? 0 : double.Parse(y[0]))); } } pts.Add(new PitchPoint(x + double.Parse(w[w.Count() - 1]), 0)); } if (pbm != string.Empty) { string[] m = pbw.Split(new[] { ',' }); for (int i = 0; i < m.Count() - 1; i++) { pts[i].Shape = m[i] == "r" ? PitchPointShape.o : m[i] == "s" ? PitchPointShape.l : m[i] == "j" ? PitchPointShape.i : PitchPointShape.io; } } } Log.Warning(note.ToString()); return(note); }