public TimeSig(TimeSig timeSig) { PosMes = timeSig.PosMes; Nume = timeSig.Nume; Denomi = timeSig.Denomi; }
enum TimeSig { pm, none } // am results to the same time as none static DateTime ParseDt(string sourceString) { try { Dictionary <string, string> dict = new Dictionary <string, string>(); dict.Add(@"^(\d{4})-(\d{2})-(\d{2})", "ymd"); dict.Add(@"^(\d{4})/(\d{1,2})/(\d{1,2})", "ymd"); dict.Add(@"^(\d{4})\.\s*(\d{1,2})\.\s*(\d{1,2})", "ymd"); dict.Add(@"^(\d{1,2})/(\d{1,2})/(\d+)", "dmy"); dict.Add(@"^(\d{1,2})/(\d{1,2})\s+(\d+)", "dmy"); dict.Add(@"^(\d{2})\.\s*(\d+)\.\s*(\d+)", "dmy"); dict.Add(@"^(\d+)-(\d+)-(\d+)", "dmy"); //This code is an attempt to process the different DateTime formats of different cultures. //static readonly string odisDateTimeFormat = "yyyy-MM-ddTHH'h'mm'm'ss's'"; //static readonly string deDateTimeFormat = "dd.MM.yyyy HH:mm:ss"; //static readonly string usDateTimeFormat = "MM'/'dd'/'yyyy HH:mm:ss"; int year = 0; int month = 0; int day = 0; int hour = 0; int minute = 0; int second = 0; Regex regex; Match match = null; string usPattern = @"^(\d{1,2})/(\d{1,2})/(\d{4})\s[^\s]+\s[A|P]M"; // US format must be distinguished from GB format regex = new Regex(usPattern); bool isUSFormat = regex.Match(sourceString).Success; foreach (string pattern in dict.Keys) { regex = new Regex(pattern); match = regex.Match(sourceString); if (match.Success) { switch (dict[pattern]) { case "ymd": year = int.Parse(match.Groups[1].Value); month = int.Parse(match.Groups[2].Value); day = int.Parse(match.Groups[3].Value); break; case "mdy": month = int.Parse(match.Groups[1].Value); day = int.Parse(match.Groups[2].Value); year = int.Parse(match.Groups[3].Value); break; case "dmy": day = int.Parse(match.Groups[1].Value); month = int.Parse(match.Groups[2].Value); year = int.Parse(match.Groups[3].Value); if (year < 100) { year += 2000; } break; } break; } } // Swap month and day values, if we have an US format if (isUSFormat) { int temp = month; month = day; day = temp; } string timeString = sourceString; if (match != null) { timeString = sourceString.Substring(match.Length + 1); } TimeSig timeSig = TimeSig.none; Dictionary <string, TimeSig> timeSigDict = new Dictionary <string, TimeSig>(); timeSigDict.Add("PM", TimeSig.pm); timeSigDict.Add("p.m.", TimeSig.pm); timeSigDict.Add("MD", TimeSig.pm); timeSigDict.Add("CH", TimeSig.pm); timeSigDict.Add("Ale", TimeSig.pm); timeSigDict.Add("Yamma", TimeSig.pm); timeSigDict.Add("م", TimeSig.pm); timeSigDict.Add("ب.ظ", TimeSig.pm); timeSigDict.Add("Efifie", TimeSig.pm); timeSigDict.Add("nm", TimeSig.pm); timeSigDict.Add("μμ", TimeSig.pm); timeSigDict.Add("غ.و", TimeSig.pm); timeSigDict.Add("ප.ව.", TimeSig.pm); timeSigDict.Add("ܒ.ܛ", TimeSig.pm); foreach (KeyValuePair <string, TimeSig> kvp in timeSigDict) { if (timeString.EndsWith(kvp.Key)) { timeSig = kvp.Value; break; } } if (timeSig == TimeSig.none) { if (timeString.IndexOf("下午") > -1) { timeSig = TimeSig.pm; } else if (timeString.IndexOf("আবেলি") > -1) { timeSig = TimeSig.pm; } else if (timeString.IndexOf("오후") > -1) { timeSig = TimeSig.pm; } else if (timeString.IndexOf("बेलुकी") > -1) { timeSig = TimeSig.pm; } else if (timeString.IndexOf("ਸ਼ਾਮ") > -1) { timeSig = TimeSig.pm; } else if (timeString.IndexOf(" PM ") > -1) { timeSig = TimeSig.pm; } } dict.Clear(); dict.Add(@"(\d{2}):(\d{2}):(\d{2})", "hms"); dict.Add(@"(\d{2}):(\d{2})", "hm"); dict.Add(@"(\d{2}).(\d{2}).(\d{2})", "hms"); dict.Add(@"(\d{2})h(\d{2})m(\d{2})s", "hms"); foreach (string pattern in dict.Keys) { regex = new Regex(pattern); match = regex.Match(timeString); if (match.Success) { switch (dict[pattern]) { case "hms": hour = int.Parse(match.Groups[1].Value); minute = int.Parse(match.Groups[2].Value); second = int.Parse(match.Groups[3].Value); break; case "hm": hour = int.Parse(match.Groups[1].Value); minute = int.Parse(match.Groups[2].Value); second = 0; break; } break; } } if (timeSig == TimeSig.pm) { hour += 12; } return(new DateTime(year, month, day, hour, minute, second)); } catch (Exception ex) { Exception ex2 = new Exception("Error while parsing a DateTime string. String = '" + sourceString + "' Message: " + ex.Message); throw (ex2); } }
public bool ImportCcs(List <string> filenames) { if (filenames.Count <= 0) { //MessageBox.Show("Cannot Import more than one ccs.", "Import"); return(false); } XDocument ccs = XDocument.Load(filenames[0]); //var nameSpace = (ccs.FirstNode as XElement)?.GetDefaultNamespace() ?? ""; //Luckily, CCS don't use the silly namespace PreMeasure = 1; //Set up tracklist var sequence = ccs.Descendants("Sequence").FirstOrDefault(); var scene = sequence.FirstChild("Scene"); var units = scene.FirstChild("Units"); var groups = scene.FirstChild("Groups"); ProjectName = Path.GetFileNameWithoutExtension(filenames[0]); var ccsValid = false; TrackList = new List <Track>(); int trackNum = 0; var tempoFinished = false; foreach (var unit in units.Descendants("Unit")) { if (unit.Attribute("Category")?.Value == "SingerSong") { if (!tempoFinished) { TempoList = new List <Tempo>(); foreach (var sound in unit.FirstChild("Tempo").Childs("Sound")) { Tempo newTempo = new Tempo { PosTick = int.Parse(sound.Attribute("Clock").Value) / 2, BpmTimes100 = (int)double.Parse(sound.Attribute("Tempo").Value) * 100 }; TempoList.Add(newTempo); } TimeSigList = new List <TimeSig>(); int time = 0; int mes = 0; int beats = 4; int beatType = 4; foreach (var timeNode in unit.FirstChild("Beat").Childs("Time")) { var clock = int.Parse(timeNode.Attribute("Clock").Value); TimeSig newtimeSig = new TimeSig { PosMes = mes + (clock - time) / (beats * 960 * 4 / beatType) }; mes = newtimeSig.PosMes; time = clock; newtimeSig.Nume = int.Parse(timeNode.Attribute("Beats").Value); beats = newtimeSig.Nume; newtimeSig.Denomi = int.Parse(timeNode.Attribute("BeatType").Value); beatType = newtimeSig.Denomi; TimeSigList.Add(newtimeSig); } } Track newTrack = new Track { TrackNum = trackNum, TrackName = (from g in groups.Descendants() where g.Attribute("Id").Value == unit.Attribute("Group").Value select g.Attribute("Name").Value).FirstOrDefault(), NoteList = new List <Note>(), SingerId = unit.Attribute("CastId")?.Value }; int noteNum = 0; foreach (var note in unit.FirstChild("Score").Childs("Note")) { var clock = int.Parse(note.Attribute("Clock").Value); Note newNote = new Note { NoteNum = noteNum, NoteTimeOn = clock / 2, NoteTimeOff = (clock + int.Parse(note.Attribute("Duration").Value)) / 2, NoteKey = int.Parse(note.Attribute("PitchStep").Value) + (int.Parse(note.Attribute("PitchOctave").Value) + 1) * 12, NoteLyric = note.Attribute("Lyric").Value }; noteNum++; newTrack.NoteList.Add(newNote); } if (newTrack.NoteList.Count > 0) { TrackList.Add(newTrack); trackNum++; ccsValid = true; } } } if (!ccsValid) { //MessageBox.Show("The Ccs is invalid or empty.", "Import"); return(false); } Lyric = new Lyric(this, true); return(true); }
public bool ImportUst(List <string> filenames) { int trackcount = 0; TrackList = new List <Track>(); foreach (string filename in filenames) { var ustValid = false; var ustReader = new StreamReader(filename, Encoding.GetEncoding("Shift-JIS")); int noteNum = 0; int time = 0; PreMeasure = 1; //建立音轨列表 Track newTrack = new Track(); newTrack.TrackNum = 0; newTrack.TrackName = Path.GetFileName(filename).Replace(".ust", ""); if (trackcount == 0) { TimeSig firsttimeSig = new TimeSig(); TimeSigList = new List <TimeSig>(); firsttimeSig.Denomi = 4; firsttimeSig.Nume = 4; firsttimeSig.PosMes = 0; TimeSigList.Add(firsttimeSig); TempoList = new List <Tempo>(); } //建立音符列表,读取音符信息 for (string readBuf = "Starting"; readBuf != "[#TRACKEND]" && readBuf != null; readBuf = ustReader.ReadLine()) { if (trackcount == 0) { if (readBuf.Contains("ProjectName=")) { ProjectName = readBuf.Remove(0, 12); } if (readBuf.Contains("Tempo=")) { Tempo firstTempo = new Tempo { PosTick = 0 }; if (double.TryParse(readBuf.Remove(0, 6), out var bpm)) { firstTempo.BpmTimes100 = (int)(bpm * 100); } TempoList.Add(firstTempo); } } if (readBuf.Contains("[#0000]")) { ustValid = false; newTrack.NoteList = new List <Note>(); Note newNote = new Note { NoteNum = noteNum, NoteIDforUTAU = readBuf.Substring(2, 4) }; bool noteIsValid = false; bool tempoTempFlag = false; for (readBuf = ustReader.ReadLine(); readBuf != "[#TRACKEND]" && readBuf != null; readBuf = ustReader.ReadLine()) { if (readBuf.Contains("[#")) { if (noteIsValid) { newTrack.NoteList.Add(newNote); noteNum++; } newNote = new Note { NoteNum = noteNum, NoteIDforUTAU = readBuf.Substring(2, 4) }; noteIsValid = false; } if (readBuf.Contains("Length=")) { newNote.NoteTimeOn = time; time += Convert.ToInt32(readBuf.Substring(7, readBuf.Length - 7)); newNote.NoteTimeOff = time; if (tempoTempFlag) { TempoList[TempoList.Count - 1].PosTick = newNote.NoteTimeOn; } } if (readBuf.Contains("Lyric=")) { if (readBuf.Substring(6, readBuf.Length - 6) != "R" && readBuf.Substring(6, readBuf.Length - 6) != "r" && newNote.GetNoteLength() != 0) { noteIsValid = true; ustValid = true; newNote.NoteLyric = readBuf.Substring(6, readBuf.Length - 6); } } if (trackcount == 0) { if (readBuf.Contains("Tempo=")) { Tempo newTempo = new Tempo(); try { newTempo.PosTick = newNote.NoteTimeOn; } catch { tempoTempFlag = true; } newTempo.BpmTimes100 = (int)(100 * double.Parse(readBuf.Substring(6, readBuf.Length - 6))); TempoList.Add(newTempo); } } if (readBuf.Contains("NoteNum=")) { newNote.NoteKey = Convert.ToInt32(readBuf.Substring(8, readBuf.Length - 8)); } } } } if (!ustValid) { //MessageBox.Show("The Ust is invalid or empty.", "Import"); return(false); } foreach (Note note in newTrack.NoteList) { note.NoteTimeOn += 1920; note.NoteTimeOff += 1920; } if (trackcount == 0) { foreach (Tempo tempo in TempoList) { if (TempoList.IndexOf(tempo) != 0) { tempo.PosTick += 1920; } } } TrackList.Add(newTrack); trackcount++; } Lyric = new Lyric(this, true); return(true); }
public bool ImportVsq4(List <string> filenames) { if (filenames.Count <= 0) { //MessageBox.Show("Cannot Import more than one vsqx.", "Import"); return(false); } XDocument vsq = XDocument.Load(filenames[0]); var nameSpace = (vsq.FirstNode as XElement)?.GetDefaultNamespace() ?? Vsq4NameSpace; //Set up tracklist ProjectName = Path.GetFileNameWithoutExtension(filenames[0]); TrackList = new List <Track>(); TimeSigList = new List <TimeSig>(); TempoList = new List <Tempo>(); int TrackNum = 0; //Master Track XElement masterTrack = vsq.Descendants(nameSpace + "masterTrack").FirstOrDefault(); if (masterTrack == null) { return(false); } PreMeasure = Convert.ToInt32(masterTrack.FirstChild("preMeasure").Value); var timeSigNode = masterTrack.FirstChild("timeSig"); TimeSig newtimeSig = new TimeSig { PosMes = Convert.ToInt32(timeSigNode.FirstChild("m").Value), Nume = Convert.ToInt32(timeSigNode.FirstChild("nu").Value), Denomi = Convert.ToInt32(timeSigNode.FirstChild("de").Value) }; TimeSigList.Add(newtimeSig); var tempoNode = masterTrack.FirstChild("tempo"); Tempo newtempo = new Tempo { PosTick = Convert.ToInt32(tempoNode.FirstChild("t").Value), BpmTimes100 = Convert.ToInt32(tempoNode.FirstChild("v").Value) }; TempoList.Add(newtempo); //Vocal Tracks foreach (var vstrack in vsq.Descendants(nameSpace + "vsTrack")) { int noteNum = 0; Track newTrack = new Track { TrackNum = int.Parse(vstrack.FirstChild("tNo").Value), NoteList = new List <Note>(), TrackName = vstrack.FirstChild("name").Value }; //Set up notelist for every track foreach (var musical in vstrack.Childs("vsPart")) { int partStartTime = Convert.ToInt32(musical.FirstChild("t").Value); foreach (var noteNode in musical.Childs("note")) { var posTick = Convert.ToInt32(noteNode.FirstChild("t").Value) + partStartTime; Note newNote = new Note { NoteNum = noteNum, NoteTimeOn = posTick, NoteTimeOff = posTick + Convert.ToInt32(noteNode.FirstChild("dur").Value), NoteKey = Convert.ToInt32(noteNode.FirstChild("n").Value), NoteLyric = noteNode.FirstChild("y").Value }; noteNum++; newTrack.NoteList.Add(newNote); } foreach (var cc in musical.Childs("cc")) { Pit newPit = new Pit() { Pos = int.Parse(cc.FirstChild("t").Value) + partStartTime, }; //TODO: 参数转换 } } if (newTrack.NoteList.Count > 0) { TrackList.Add(newTrack); } } if (TrackList.Count > 0) { Lyric = new Lyric(this, true); return(true); } //MessageBox.Show("The Vsqx is invalid or empty.", "Import"); return(false); }
public void SetTimeSig(TimeSig newTimeSig) { timeSig = newTimeSig; }