/// <summary> /// vsqの指定したトラックを元に,トラックを1つだけ持つustを構築します /// </summary> /// <param name="vsq"></param> /// <param name="track_index"></param> /// <param name="id_map">UstEventのIndexフィールドと、元になったVsqEventのInternalIDを対応付けるマップ。キーがIndex、値がInternalIDを表す</param> public UstFile(VsqFile vsq, int track_index, SortedDictionary <int, int> id_map) { VsqFile work = (VsqFile)vsq.clone(); //work.removePart( 0, work.getPreMeasureClocks() ); VsqTrack vsq_track = work.Track[track_index]; // デフォルトのテンポ if (work.TempoTable.Count <= 0) { m_tempo = 120.0f; } else { m_tempo = (float)(60e6 / (double)work.TempoTable[0].Tempo); } m_tempo_table = new List <TempoTableEntry>(); m_tempo_table.Clear(); // ustには、テンポチェンジを音符の先頭にしか入れられない // あとで音符に反映させるためのテンプレートを作っておく TempoVector tempo = new TempoVector(); int last_clock = 0; int itempo = (int)(60e6 / m_tempo); foreach (var item in vsq_track.getNoteEventIterator()) { if (last_clock < item.Clock) { // 休符Rの分 tempo.Add(new TempoTableEntry(last_clock, itempo, work.getSecFromClock(last_clock))); } tempo.Add(new TempoTableEntry(item.Clock, itempo, work.getSecFromClock(item.Clock))); last_clock = item.Clock + item.ID.getLength(); } if (tempo.Count == 0) { tempo.Add(new TempoTableEntry(0, (int)(60e6 / m_tempo), 0.0)); } // tempoの中の各要素の時刻が、vsq.TempoTableから計算した時刻と合致するよう調節 #if DEBUG sout.println("UstFile#.ctor; before; list="); for (int i = 0; i < tempo.Count; i++) { TempoTableEntry item = tempo[i]; sout.println(" #" + i + "; c" + item.Clock + "; T" + item.Tempo + "; t" + (60e6 / item.Tempo) + "; sec" + item.Time); } #endif TempoTableEntry prev = tempo[0]; for (int i = 1; i < tempo.Count; i++) { TempoTableEntry item = tempo[i]; double sec = item.Time - prev.Time; int delta = item.Clock - prev.Clock; // deltaクロックでsecを表現するにはテンポをいくらにすればいいか? int draft = (int)(480.0 * sec * 1e6 / (double)delta); // 丸め誤差が入るので、Timeを更新 // ustに実際に記録されるテンポはいくらか? float act_tempo = (float)double.Parse(PortUtil.formatDecimal("0.00", 60e6 / draft)); int i_act_tempo = (int)(60e6 / act_tempo); prev.Tempo = i_act_tempo; item.Time = prev.Time + 1e-6 * delta * prev.Tempo / 480.0; prev = item; } #if DEBUG sout.println("UstFile#.ctor; after; list="); for (int i = 0; i < tempo.Count; i++) { TempoTableEntry item = tempo[i]; sout.println(" #" + i + "; c" + item.Clock + "; T" + item.Tempo + "; t" + (60e6 / item.Tempo) + "; sec" + item.Time); } sout.println("UstFile#.ctor; vsq.TempoTable="); for (int i = 0; i < work.TempoTable.Count; i++) { TempoTableEntry item = work.TempoTable[i]; sout.println(" #" + i + "; c" + item.Clock + "; T" + item.Tempo + "; t" + (60e6 / item.Tempo) + "; sec" + item.Time); } #endif // R用音符のテンプレート int PBTYPE = 5; UstEvent template = new UstEvent(); template.setLyric("R"); template.setNote(60); template.setPreUtterance(0); template.setVoiceOverlap(0); template.setIntensity(100); template.setModuration(0); // 再生秒時をとりあえず無視して,ゲートタイム基準で音符を追加 UstTrack track_add = new UstTrack(); last_clock = 0; int index = 0; foreach (var item in vsq_track.getNoteEventIterator()) { if (last_clock < item.Clock) { // ゲートタイム差あり,Rを追加 UstEvent itemust = (UstEvent)template.clone(); itemust.setLength(item.Clock - last_clock); itemust.Index = index; index++; id_map[itemust.Index] = -1; track_add.addEvent(itemust); } UstEvent item_add = (UstEvent)item.UstEvent.clone(); item_add.setLength(item.ID.getLength()); item_add.setLyric(item.ID.LyricHandle.L0.Phrase); item_add.setNote(item.ID.Note); item_add.Index = index; id_map[item_add.Index] = item.InternalID; if (item.UstEvent.getEnvelope() != null) { item_add.setEnvelope((UstEnvelope)item.UstEvent.getEnvelope().clone()); } index++; track_add.addEvent(item_add); last_clock = item.Clock + item.ID.getLength(); } // テンポを格納(イベント数はあっているはず) if (track_add.getEventCount() > 0) { int size = track_add.getEventCount(); int lasttempo = -1; // ありえない値にしておく for (int i = 0; i < size; i++) { TempoTableEntry item = tempo[i]; if (lasttempo != item.Tempo) { // テンポ値が変わっているもののみ追加 UstEvent ue = track_add.getEvent(i); ue.setTempo((float)(60e6 / item.Tempo)); lasttempo = item.Tempo; m_tempo_table.Add(item); } } } else { // tempoはどうせ破棄されるのでクローンしなくていい m_tempo_table.Add(tempo[0]); } // ピッチを反映 // まず絶対ピッチを取得 VsqBPList abs_pit = new VsqBPList("", 600000, 0, 1280000); VsqBPList cpit = vsq_track.getCurve("pit"); int clock = 0; int search_indx = 0; int pit_size = cpit.size(); foreach (var item in track_add.getNoteEventIterator()) { int c = clock; int len = item.getLength(); clock += len; if (item.getLyric() == "R") { continue; } // 音符の先頭のpitは必ず入れる abs_pit.add(c, (int)(item.getNote() * 10000 + vsq_track.getPitchAt(c) * 100)); // c~c+lenまで for (int i = search_indx; i < pit_size; i++) { int c2 = cpit.getKeyClock(i); if (c < c2 && c2 < clock) { abs_pit.add(c2, (int)(item.getNote() * 10000 + vsq_track.getPitchAt(c2) * 100)); search_indx = i; } else if (clock <= c2) { break; } } } // ピッチをピッチベンドに変換しながら反映 clock = 0; foreach (var item in track_add.getNoteEventIterator()) { double sec_at_clock = tempo.getSecFromClock(clock); double sec_pre = item.getPreUtterance() / 1000.0; double sec_stp = item.getStartPoint() / 1000.0; double sec_at_begin = sec_at_clock - sec_pre - sec_stp; int clock_begin = (int)tempo.getClockFromSec(sec_at_begin); // 音符先頭との距離がPBTYPEの倍数になるようにする clock_begin -= (clock < clock_begin) ? ((clock_begin - clock) % PBTYPE) : ((clock - clock_begin) % PBTYPE); // clock_beginがsec_at_beginより前方になるとNGなので修正する double sec_at_clock_begin = tempo.getSecFromClock(clock_begin); while (sec_at_clock_begin < sec_at_begin) { clock_begin += PBTYPE; sec_at_clock_begin = tempo.getSecFromClock(clock_begin); } int clock_end = clock + item.getLength(); List <float> pitch = new List <float>(); bool allzero = true; ByRef <int> ref_indx = new ByRef <int>(0); for (int cl = clock_begin; cl < clock_end; cl += PBTYPE) { int abs = abs_pit.getValue(cl, ref_indx); float pit = (float)(abs / 100.0) - item.getNote() * 100; if (pit != 0.0) { allzero = false; } pitch.Add(pit); } if (!allzero) { item.setPitches(PortUtil.convertFloatArray(pitch.ToArray())); item.setPBType(PBTYPE); } else { item.setPBType(-1); } clock += item.getLength(); } m_tracks.Add(track_add); }
public static bool Edit(VsqFile vsq) { using (RenderAsUtau dlg = new RenderAsUtau()) { if (dlg.ShowDialog() == DialogResult.OK) { Singer = dlg.txtSinger.Text; Resampler = dlg.txtResampler.Text; WavTool = dlg.txtWavtool.Text; string script = Path.Combine(Application.StartupPath, Path.Combine("script", "Render As UTAU.cs")); //Script.ScriptPath; string temp_dir = Path.Combine(Path.GetDirectoryName(script), Path.GetFileNameWithoutExtension(script)); #if DEBUG if (!Directory.Exists(temp_dir)) { Directory.CreateDirectory(temp_dir); } StreamWriter sw = new StreamWriter(Path.Combine(temp_dir, "log.txt")); #endif // 原音設定を読み込み Dictionary <string, OtoArgs> config = new Dictionary <string, OtoArgs>(); string singer_name = Path.GetFileName(Singer); string config_file = Path.Combine(Singer, "oto.ini"); #if DEBUG sw.WriteLine("Singer=" + Singer); sw.WriteLine("singer_name=" + singer_name); sw.WriteLine("config_file=" + config_file); #endif if (File.Exists(config_file)) { using (cp932reader sr = new cp932reader(config_file)) { string line; while (sr.Peek() >= 0) { try { line = sr.ReadLine(); String[] spl = line.Split('='); String file_name = spl[0]; // あ.wav String a2 = spl[1]; // ,0,36,64,0,0 String a1 = Path.GetFileNameWithoutExtension(file_name); spl = a2.Split(','); OtoArgs oa = new OtoArgs(); oa.Alias = spl[0]; oa.msOffset = int.Parse(spl[1]); oa.msConsonant = int.Parse(spl[2]); oa.msBlank = int.Parse(spl[3]); oa.msPreUtterance = int.Parse(spl[4]); oa.msOverwrap = int.Parse(spl[5]); config.Add(a1, oa); } catch { } } } } int track = AppManager.getSelected(); List <Phon> phons = new List <Phon>(); if (!Directory.Exists(temp_dir)) { Directory.CreateDirectory(temp_dir); } int count = -1; double sec_end = 0; double sec_end_old = 0; for (Iterator <VsqEvent> itr = vsq.Track.get(track).getNoteEventIterator(); itr.hasNext();) { VsqEvent item = itr.next(); count++; double sec_start = vsq.getSecFromClock(item.Clock); sec_end_old = sec_end; sec_end = vsq.getSecFromClock(item.Clock + item.ID.Length); float t_temp = (float)(item.ID.Length / (sec_end - sec_start) / 8.0); if ((count == 0 && sec_start > 0.0) || (sec_start > sec_end_old)) { double sec_start2 = sec_end_old; double sec_end2 = sec_start; float t_temp2 = (float)(item.Clock / (sec_end2 - sec_start2) / 8.0); phons.Add(new Phon("R", Path.Combine(Singer, "R.wav"), item.Clock, t_temp2, true)); count++; } string lyric = item.ID.LyricHandle.L0.Phrase; string note = NoteStringFromNoteNumber(item.ID.Note); #if DEBUG sw.WriteLine("note=" + note); #endif string millisec = ((int)((sec_end - sec_start) * 1000) + 50).ToString(); //4_あ_C#4_550.wav string filename = Path.Combine(temp_dir, count + "_" + item.ID.Note + "_" + millisec + ".wav"); #if DEBUG sw.WriteLine("filename=" + filename); sw.WriteLine(); #endif if (File.Exists(filename)) { PortUtil.deleteFile(filename); } phons.Add(new Phon(lyric, filename, item.ID.Length, t_temp, false)); OtoArgs oa = new OtoArgs(); if (config.ContainsKey(lyric)) { oa = config[lyric]; } int velocity = 100; int moduration = 100; string flags = "L"; int time_percent = 100; // C4 100 L 0 550 0 0 100 100 string arg = "\"" + Path.Combine(Singer, lyric + ".wav") + "\" \"" + filename + "\" \"" + note + "\" " + time_percent + " " + flags + " " + oa.msOffset + " " + millisec + " " + oa.msConsonant + " " + oa.msBlank + " " + velocity + " " + moduration; using (System.Diagnostics.Process p = new System.Diagnostics.Process()) { p.StartInfo.FileName = (InvokeWithWine ? "wine \"" : "\"") + Resampler + "\""; p.StartInfo.Arguments = arg; p.StartInfo.WorkingDirectory = temp_dir; p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.Start(); p.WaitForExit(); } } #if DEBUG sw.Close(); #endif string filebase = "temp.wav"; string file = Path.Combine(temp_dir, filebase); if (File.Exists(file)) { PortUtil.deleteFile(file); } string file_whd = Path.Combine(temp_dir, filebase + ".whd"); if (File.Exists(file_whd)) { PortUtil.deleteFile(file_whd); } string file_dat = Path.Combine(temp_dir, filebase + ".dat"); if (File.Exists(file_dat)) { PortUtil.deleteFile(file_dat); } // wavtoolを呼び出す for (int i = 0; i < phons.Count; i++) { OtoArgs oa = new OtoArgs(); if (config.ContainsKey(phons[i].Lyric)) { oa = config[phons[i].Lyric]; } // 次の音符の先行発声とオーバーラップを取得 OtoArgs oa_next = new OtoArgs(); if (i + 1 < phons.Count) { if (config.ContainsKey(phons[i + 1].Lyric)) { oa_next = config[phons[i + 1].Lyric]; } } int mten = oa.msPreUtterance + oa_next.msOverwrap - oa_next.msPreUtterance; string arg = filebase + " \"" + phons[i].FileName + "\" 0 " + phons[i].ClockLength + "@" + string.Format("{0:f2}", phons[i].Tempo) + mten.ToString("+#;-#;0"); if (phons[i].ModeR) { arg += " 0 0"; } else { arg += " 0 5 35 0 100 100 100 " + oa.msOverwrap; // エンベロープ } using (System.Diagnostics.Process p = new System.Diagnostics.Process()) { p.StartInfo.FileName = (InvokeWithWine ? "wine \"" : "\"") + WavTool + "\""; p.StartInfo.Arguments = arg; p.StartInfo.WorkingDirectory = temp_dir; p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.Start(); p.WaitForExit(); } } // 波形とヘッダを結合 using (FileStream fs = new FileStream(file, FileMode.Create)) { string[] files = new string[] { file_whd, file_dat }; int buflen = 512; byte[] buff = new byte[buflen]; for (int i = 0; i < files.Length; i++) { using (FileStream fs2 = new FileStream(files[i], FileMode.Open)) { int len = fs2.Read(buff, 0, buflen); while (len > 0) { fs.Write(buff, 0, len); len = fs2.Read(buff, 0, buflen); } } } } // 後片付け foreach (Phon ph in phons) { if (!ph.ModeR) { if (File.Exists(ph.FileName)) { PortUtil.deleteFile(ph.FileName); } } } if (File.Exists(file_whd)) { PortUtil.deleteFile(file_whd); } if (File.Exists(file_dat)) { PortUtil.deleteFile(file_dat); } if (saveFileDialog.ShowDialog() == DialogResult.OK) { if (File.Exists(saveFileDialog.FileName)) { PortUtil.deleteFile(saveFileDialog.FileName); } LastWave = saveFileDialog.FileName; PortUtil.moveFile(file, saveFileDialog.FileName); } else { PortUtil.deleteFile(file); } return(true); } else { return(false); } } }
public static bool Edit( VsqFile vsq ) { using ( RenderAsUtau dlg = new RenderAsUtau() ) { if ( dlg.ShowDialog() == DialogResult.OK ) { Singer = dlg.txtSinger.Text; Resampler = dlg.txtResampler.Text; WavTool = dlg.txtWavtool.Text; string script = Path.Combine( Application.StartupPath, Path.Combine( "script", "Render As UTAU.cs" ) );//Script.ScriptPath; string temp_dir = Path.Combine( Path.GetDirectoryName( script ), Path.GetFileNameWithoutExtension( script ) ); #if DEBUG if ( !Directory.Exists( temp_dir ) ) { Directory.CreateDirectory( temp_dir ); } StreamWriter sw = new StreamWriter( Path.Combine( temp_dir, "log.txt" ) ); #endif // 原音設定を読み込み Dictionary<string, OtoArgs> config = new Dictionary<string, OtoArgs>(); string singer_name = Path.GetFileName( Singer ); string config_file = Path.Combine( Singer, "oto.ini" ); #if DEBUG sw.WriteLine( "Singer=" + Singer ); sw.WriteLine( "singer_name=" + singer_name ); sw.WriteLine( "config_file=" + config_file ); #endif if ( File.Exists( config_file ) ) { using ( cp932reader sr = new cp932reader( config_file ) ) { string line; while ( sr.Peek() >= 0 ) { try { line = sr.ReadLine(); String[] spl = line.Split( '=' ); String file_name = spl[0]; // あ.wav String a2 = spl[1]; // ,0,36,64,0,0 String a1 = Path.GetFileNameWithoutExtension( file_name ); spl = a2.Split( ',' ); OtoArgs oa = new OtoArgs(); oa.Alias = spl[0]; oa.msOffset = int.Parse( spl[1] ); oa.msConsonant = int.Parse( spl[2] ); oa.msBlank = int.Parse( spl[3] ); oa.msPreUtterance = int.Parse( spl[4] ); oa.msOverwrap = int.Parse( spl[5] ); config.Add( a1, oa ); } catch { } } } } int track = AppManager.getSelected(); List<Phon> phons = new List<Phon>(); if ( !Directory.Exists( temp_dir ) ) { Directory.CreateDirectory( temp_dir ); } int count = -1; double sec_end = 0; double sec_end_old = 0; for ( Iterator<VsqEvent> itr = vsq.Track.get( track ).getNoteEventIterator(); itr.hasNext(); ) { VsqEvent item = itr.next(); count++; double sec_start = vsq.getSecFromClock( item.Clock ); sec_end_old = sec_end; sec_end = vsq.getSecFromClock( item.Clock + item.ID.Length ); float t_temp = (float)(item.ID.Length / (sec_end - sec_start) / 8.0); if ( (count == 0 && sec_start > 0.0) || (sec_start > sec_end_old) ) { double sec_start2 = sec_end_old; double sec_end2 = sec_start; float t_temp2 = (float)(item.Clock / (sec_end2 - sec_start2) / 8.0); phons.Add( new Phon( "R", Path.Combine( Singer, "R.wav" ), item.Clock, t_temp2, true ) ); count++; } string lyric = item.ID.LyricHandle.L0.Phrase; string note = NoteStringFromNoteNumber( item.ID.Note ); #if DEBUG sw.WriteLine( "note=" + note ); #endif string millisec = ((int)((sec_end - sec_start) * 1000) + 50).ToString(); //4_あ_C#4_550.wav string filename = Path.Combine( temp_dir, count + "_" + item.ID.Note + "_" + millisec + ".wav" ); #if DEBUG sw.WriteLine( "filename=" + filename ); sw.WriteLine(); #endif if ( File.Exists( filename ) ) { PortUtil.deleteFile( filename ); } phons.Add( new Phon( lyric, filename, item.ID.Length, t_temp, false ) ); OtoArgs oa = new OtoArgs(); if ( config.ContainsKey( lyric ) ) { oa = config[lyric]; } int velocity = 100; int moduration = 100; string flags = "L"; int time_percent = 100; // C4 100 L 0 550 0 0 100 100 string arg = "\"" + Path.Combine( Singer, lyric + ".wav" ) + "\" \"" + filename + "\" \"" + note + "\" " + time_percent + " " + flags + " " + oa.msOffset + " " + millisec + " " + oa.msConsonant + " " + oa.msBlank + " " + velocity + " " + moduration; using ( System.Diagnostics.Process p = new System.Diagnostics.Process() ) { p.StartInfo.FileName = (InvokeWithWine ? "wine \"" : "\"") + Resampler + "\""; p.StartInfo.Arguments = arg; p.StartInfo.WorkingDirectory = temp_dir; p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.Start(); p.WaitForExit(); } } #if DEBUG sw.Close(); #endif string filebase = "temp.wav"; string file = Path.Combine( temp_dir, filebase ); if ( File.Exists( file ) ) { PortUtil.deleteFile( file ); } string file_whd = Path.Combine( temp_dir, filebase + ".whd" ); if ( File.Exists( file_whd ) ) { PortUtil.deleteFile( file_whd ); } string file_dat = Path.Combine( temp_dir, filebase + ".dat" ); if ( File.Exists( file_dat ) ) { PortUtil.deleteFile( file_dat ); } // wavtoolを呼び出す for ( int i = 0; i < phons.Count; i++ ) { OtoArgs oa = new OtoArgs(); if ( config.ContainsKey( phons[i].Lyric ) ) { oa = config[phons[i].Lyric]; } // 次の音符の先行発声とオーバーラップを取得 OtoArgs oa_next = new OtoArgs(); if ( i + 1 < phons.Count ) { if ( config.ContainsKey( phons[i + 1].Lyric ) ) { oa_next = config[phons[i + 1].Lyric]; } } int mten = oa.msPreUtterance + oa_next.msOverwrap - oa_next.msPreUtterance; string arg = filebase + " \"" + phons[i].FileName + "\" 0 " + phons[i].ClockLength + "@" + string.Format( "{0:f2}", phons[i].Tempo ) + mten.ToString( "+#;-#;0" ); if ( phons[i].ModeR ) { arg += " 0 0"; } else { arg += " 0 5 35 0 100 100 100 " + oa.msOverwrap; // エンベロープ } using ( System.Diagnostics.Process p = new System.Diagnostics.Process() ) { p.StartInfo.FileName = (InvokeWithWine ? "wine \"" : "\"") + WavTool + "\""; p.StartInfo.Arguments = arg; p.StartInfo.WorkingDirectory = temp_dir; p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.Start(); p.WaitForExit(); } } // 波形とヘッダを結合 using ( FileStream fs = new FileStream( file, FileMode.Create ) ) { string[] files = new string[] { file_whd, file_dat }; int buflen = 512; byte[] buff = new byte[buflen]; for ( int i = 0; i < files.Length; i++ ) { using ( FileStream fs2 = new FileStream( files[i], FileMode.Open ) ) { int len = fs2.Read( buff, 0, buflen ); while ( len > 0 ) { fs.Write( buff, 0, len ); len = fs2.Read( buff, 0, buflen ); } } } } // 後片付け foreach ( Phon ph in phons ) { if ( !ph.ModeR ) { if ( File.Exists( ph.FileName ) ) { PortUtil.deleteFile( ph.FileName ); } } } if ( File.Exists( file_whd ) ) { PortUtil.deleteFile( file_whd ); } if ( File.Exists( file_dat ) ) { PortUtil.deleteFile( file_dat ); } if ( saveFileDialog.ShowDialog() == DialogResult.OK ) { if ( File.Exists( saveFileDialog.FileName ) ) { PortUtil.deleteFile( saveFileDialog.FileName ); } LastWave = saveFileDialog.FileName; PortUtil.moveFile( file, saveFileDialog.FileName ); } else { PortUtil.deleteFile( file ); } return true; } else { return false; } } }