void Resample(RenderItem item) { byte[] data = null; try { uint hash = item.HashParameters(); data = cache.Get(hash); if (data == null) { CopySourceTemp(item); if (!resamplers.TryGetValue(item.ResamplerName, out var driver)) { driver = this.driver; } data = driver.DoResampler(DriverModels.CreateInputModel(item, 0), Log.Logger); if (data == null || data.Length == 0) { throw new Exception("Empty render result."); } cache.Put(hash, data); Log.Information($"Sound {hash:x} {item.Oto.Alias} {item.GetResamplerExeArgs()} resampled."); CopyBackMetaFiles(item); } } catch (Exception e) { Log.Error(e, $"Failed to render item {item.SourceFile} {item.Oto.Alias} {item.GetResamplerExeArgs()}."); } finally { item.Data = data ?? new byte[0]; item.OnComplete?.Invoke(item.Data); item.progress?.CompleteOne($"Resampling \"{item.phonemeName}\""); } }
RenderItem ResamplePhonemeAsync(object state) { RenderItem item = state as RenderItem; Log.Verbose($"Sound {item.HashParameters():x} resampling {item.GetResamplerExeArgs()}"); var output = driver.DoResampler(DriverModels.CreateInputModel(item, 0)); item.Sound = MemorySampleProvider.FromStream(output); output.Dispose(); item.progress.CompleteOne($"Resampling \"{item.phonemeName}\""); return(item); }
private List<RenderItem> RenderAsync(UVoicePart part, UProject project, IResamplerDriver engine, BackgroundWorker worker) { List<RenderItem> renderItems = new List<RenderItem>(); System.Diagnostics.Stopwatch watch = new Stopwatch(); watch.Start(); System.Diagnostics.Debug.WriteLine("Resampling start"); lock (part) { string cacheDir = PathManager.Inst.GetCachePath(project.FilePath); string[] cacheFiles = Directory.EnumerateFiles(cacheDir).ToArray(); int count = 0, i = 0; foreach (UNote note in part.Notes) foreach (UPhoneme phoneme in note.Phonemes) count++; foreach (UNote note in part.Notes) { foreach (UPhoneme phoneme in note.Phonemes) { RenderItem item = BuildRenderItem(phoneme, part, project); var sound = RenderCache.Inst.Get(item.HashParameters()); if (sound == null) { string cachefile = Path.Combine(cacheDir, string.Format("{0:x}.wav", item.HashParameters())); if (!cacheFiles.Contains(cachefile)) { System.Diagnostics.Debug.WriteLine("Sound {0:x} resampling {1}", item.HashParameters(), item.GetResamplerExeArgs()); DriverModels.EngineInput engineArgs = DriverModels.CreateInputModel(item, 0); System.IO.Stream output = engine.DoResampler(engineArgs); sound = new CachedSound(output); } else { System.Diagnostics.Debug.WriteLine("Sound {0:x} found on disk {1}", item.HashParameters(), item.GetResamplerExeArgs()); sound = new CachedSound(cachefile); } RenderCache.Inst.Put(item.HashParameters(), sound, engine.GetInfo().ToString()); } else System.Diagnostics.Debug.WriteLine("Sound {0} found in cache {1}", item.HashParameters(), item.GetResamplerExeArgs()); item.Sound = sound; renderItems.Add(item); worker.ReportProgress(100 * ++i / count, string.Format("Resampling \"{0}\" {1}/{2}", phoneme.Phoneme, i, count)); } } } watch.Stop(); System.Diagnostics.Debug.WriteLine("Resampling end"); System.Diagnostics.Debug.WriteLine("Total cache size {0:n0} bytes", RenderCache.Inst.TotalMemSize); System.Diagnostics.Debug.WriteLine("Total time {0} ms", watch.Elapsed.TotalMilliseconds); return renderItems; }
private List <RenderItem> RenderAsync(UVoicePart part, UProject project, IResamplerDriver engine, BackgroundWorker worker) { var renderItems = new List <RenderItem>(); var watch = new Stopwatch(); watch.Start(); Log.Information("Resampling start."); lock (part) { var cacheDir = PathManager.Inst.GetCachePath(project.FilePath); var cacheFiles = Directory.EnumerateFiles(cacheDir).ToArray(); int count = 0, i = 0; foreach (var note in part.Notes) { foreach (var phoneme in note.Phonemes) { count++; } } foreach (var note in part.Notes) { foreach (var phoneme in note.Phonemes) { if (string.IsNullOrEmpty(phoneme.Oto.File)) { Log.Warning($"Cannot find phoneme in note {note.Lyric}"); continue; } var item = new RenderItem(phoneme, part, project); //System.Diagnostics.Debug.WriteLine("Sound {0:x} resampling {1}", item.HashParameters(), item.GetResamplerExeArgs()); var engineArgs = DriverModels.CreateInputModel(item, 0); var output = engine.DoResampler(engineArgs); item.Sound = MemorySampleProvider.FromStream(output); output.Dispose(); renderItems.Add(item); worker.ReportProgress(100 * ++i / count, $"Resampling \"{phoneme.Phoneme}\" {i}/{count}"); } } } watch.Stop(); Log.Information($"Resampling end, total time {watch.Elapsed}"); return(renderItems); }
RenderItem ResamplePhonemeAsync(object state) { RenderItem item = state as RenderItem; uint hash = item.HashParameters(); byte[] data = cache.Get(hash); if (data == null) { data = driver.DoResampler(DriverModels.CreateInputModel(item, 0)); cache.Put(hash, data); Log.Information($"Sound {hash:x} {item.GetResamplerExeArgs()} resampled."); } else { Log.Information($"Sound {hash:x} {item.GetResamplerExeArgs()} cache retrieved."); } var stream = new MemoryStream(data); item.Sound = MemorySampleProvider.FromStream(stream); item.progress.CompleteOne($"Resampling \"{item.phonemeName}\""); return(item); }
private List <RenderItem> RenderAsync(UVoicePart part, UProject project, IResamplerDriver engine, BackgroundWorker worker) { List <RenderItem> renderItems = new List <RenderItem>(); System.Diagnostics.Stopwatch watch = new Stopwatch(); watch.Start(); System.Diagnostics.Debug.WriteLine("Resampling start"); lock (part) { string cacheDir = PathManager.Inst.GetCachePath(project.FilePath); string[] cacheFiles = Directory.EnumerateFiles(cacheDir).ToArray(); int count = 0, i = 0; foreach (UNote note in part.Notes) { foreach (UPhoneme phoneme in note.Phonemes) { count++; } } foreach (UNote note in part.Notes) { foreach (UPhoneme phoneme in note.Phonemes) { RenderItem item = BuildRenderItem(phoneme, part, project); var sound = RenderCache.Inst.Get(item.HashParameters()); if (sound == null) { string cachefile = Path.Combine(cacheDir, string.Format("{0:x}.wav", item.HashParameters())); if (!cacheFiles.Contains(cachefile)) { System.Diagnostics.Debug.WriteLine("Sound {0:x} resampling {1}", item.HashParameters(), item.GetResamplerExeArgs()); DriverModels.EngineInput engineArgs = DriverModels.CreateInputModel(item, 0); System.IO.Stream output = engine.DoResampler(engineArgs); sound = new CachedSound(output); } else { System.Diagnostics.Debug.WriteLine("Sound {0:x} found on disk {1}", item.HashParameters(), item.GetResamplerExeArgs()); sound = new CachedSound(cachefile); } RenderCache.Inst.Put(item.HashParameters(), sound, engine.GetInfo().ToString()); } else { System.Diagnostics.Debug.WriteLine("Sound {0} found in cache {1}", item.HashParameters(), item.GetResamplerExeArgs()); } item.Sound = sound; renderItems.Add(item); worker.ReportProgress(100 * ++i / count, string.Format("Resampling \"{0}\" {1}/{2}", phoneme.Phoneme, i, count)); } } } watch.Stop(); System.Diagnostics.Debug.WriteLine("Resampling end"); System.Diagnostics.Debug.WriteLine("Total cache size {0:n0} bytes", RenderCache.Inst.TotalMemSize); System.Diagnostics.Debug.WriteLine("Total time {0} ms", watch.Elapsed.TotalMilliseconds); return(renderItems); }