Ejemplo n.º 1
0
 public System.IO.Stream DoResampler(DriverModels.EngineInput Args)
 {
     System.IO.MemoryStream ms = new System.IO.MemoryStream();
     if (!_isLegalPlugin)
     {
         return(ms);
     }
     try
     {
         IntPtr hModule = LoadLibrary(DllPath);
         if (hModule == IntPtr.Zero)
         {
             _isLegalPlugin = false;
         }
         else
         {
             IntPtr m = GetProcAddress(hModule, "DoResampler");
             if (m != IntPtr.Zero)
             {
                 DoResamplerDelegate       g      = (DoResamplerDelegate)Marshal.GetDelegateForFunctionPointer(m, typeof(DoResamplerDelegate));
                 DriverModels.EngineOutput Output = Intptr2EngineOutput(g(Args));
                 ms = new System.IO.MemoryStream(Output.wavData);
             }
             FreeLibrary(hModule);
         }
     }
     catch {; }
     return(ms);
 }
Ejemplo n.º 2
0
        public byte[] DoResampler(DriverModels.EngineInput Args)
        {
            byte[] data = new byte[0];
            if (!_isLegalPlugin)
            {
                return(data);
            }
            try {
                string tmpFile  = Path.GetTempFileName();
                string ArgParam = FormattableString.Invariant(
                    $"\"{Args.inputWaveFile}\" \"{tmpFile}\" {Args.NoteString} {Args.Velocity} \"{Args.StrFlags}\" {Args.Offset} {Args.RequiredLength} {Args.Consonant} {Args.Cutoff} {Args.Volume} {Args.Modulation} !{Args.Tempo} {Base64.Base64EncodeInt12(Args.pitchBend)}");

                var p = Process.Start(new ProcessStartInfo(ExePath, ArgParam)
                {
                    UseShellExecute = false, CreateNoWindow = true
                });
                p.WaitForExit();
                if (p != null)
                {
                    p.Close();
                    p.Dispose();
                    p = null;
                }

                if (File.Exists(tmpFile))
                {
                    data = File.ReadAllBytes(tmpFile);
                    try {
                        File.Delete(tmpFile);
                    } catch {; }
                }
            } catch (Exception) {; }
            return(data);
        }
Ejemplo n.º 3
0
        public byte[] DoResampler(DriverModels.EngineInput Args, ILogger logger)
        {
            const bool debugResampler = false;

            byte[] data = new byte[0];
            if (!_isLegalPlugin)
            {
                return(data);
            }
            var    threadId = Thread.CurrentThread.ManagedThreadId;
            string tmpFile  = Path.GetTempFileName();
            string ArgParam = FormattableString.Invariant(
                $"\"{Args.inputWaveFile}\" \"{tmpFile}\" {Args.NoteString} {Args.Velocity} \"{Args.StrFlags}\" {Args.Offset} {Args.RequiredLength} {Args.Consonant} {Args.Cutoff} {Args.Volume} {Args.Modulation} !{Args.Tempo} {Base64.Base64EncodeInt12(Args.pitchBend)}");

            logger.Information($" > [thread-{threadId}] {ExePath} {ArgParam}");
            using (var proc = new Process()) {
                proc.StartInfo = new ProcessStartInfo(ExePath, ArgParam)
                {
                    UseShellExecute        = false,
                    RedirectStandardOutput = debugResampler,
                    RedirectStandardError  = debugResampler,
                    CreateNoWindow         = true,
                };
#pragma warning disable CS0162 // Unreachable code detected
                if (debugResampler)
                {
                    proc.OutputDataReceived += (o, e) => logger.Information($" >>> [thread-{threadId}] {e.Data}");
                    proc.ErrorDataReceived  += (o, e) => logger.Error($" >>> [thread-{threadId}] {e.Data}");
                }
                proc.Start();
                if (debugResampler)
                {
                    proc.BeginOutputReadLine();
                    proc.BeginErrorReadLine();
                }
#pragma warning restore CS0162 // Unreachable code detected
                if (!proc.WaitForExit(10000))
                {
                    logger.Warning($"[thread-{threadId}] Timeout, killing...");
                    try {
                        proc.Kill();
                        logger.Warning($"[thread-{threadId}] Killed.");
                    } catch (Exception e) {
                        logger.Error(e, $"[thread-{threadId}] Failed to kill");
                    }
                }
            }
            if (File.Exists(tmpFile))
            {
                data = File.ReadAllBytes(tmpFile);
                File.Delete(tmpFile);
            }
            return(data);
        }
Ejemplo n.º 4
0
        public System.IO.Stream DoResampler(DriverModels.EngineInput Args)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            if (!_isLegalPlugin)
            {
                return(ms);
            }
            try
            {
                string tmpFile  = System.IO.Path.GetTempFileName();
                string ArgParam = string.Format(
                    "\"{0}\" \"{1}\" {2} {3} \"{4}\" {5} {6} {7} {8} {9} {10} !{11} {12}",
                    Args.inputWaveFile,
                    tmpFile,
                    Args.NoteString,
                    Args.Velocity,
                    Args.StrFlags,
                    Args.Offset,
                    Args.RequiredLength,
                    Args.Consonant,
                    Args.Cutoff,
                    Args.Volume,
                    Args.Modulation,
                    Args.Tempo,
                    Base64.Base64EncodeInt12(Args.pitchBend));

                var p = Process.Start(new ProcessStartInfo(ExePath, ArgParam)
                {
                    UseShellExecute = false, CreateNoWindow = true
                });
                p.WaitForExit();
                if (p != null)
                {
                    p.Close();
                    p.Dispose();
                    p = null;
                }
                if (System.IO.File.Exists(tmpFile))
                {
                    byte[] Dat = System.IO.File.ReadAllBytes(tmpFile);
                    ms = new MemoryStream(Dat);
                    try
                    {
                        System.IO.File.Delete(tmpFile);
                    }
                    catch {; }
                }
            }
            catch (Exception e) {; }
            return(ms);
        }
Ejemplo n.º 5
0
 public System.IO.Stream DoResampler(DriverModels.EngineInput Args)
 {
     System.IO.MemoryStream ms = new System.IO.MemoryStream();
     if (!_isLegalPlugin)
     {
         return(ms);
     }
     if (DoResamplerMethod != null)
     {
         object inputarg = CopyObjectToNewType(Args, DoResamplerMethod.GetParameters()[0].ParameterType);
         object ret      = DoResamplerMethod.Invoke(null, new object[1] {
             inputarg
         });
         DriverModels.EngineOutput Out = (DriverModels.EngineOutput)CopyObjectToNewType(ret, typeof(DriverModels.EngineOutput));
         ms = new System.IO.MemoryStream(Out.wavData);
     }
     return(ms);
 }
Ejemplo n.º 6
0
        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);
        }