示例#1
0
        public bool ProcessOffline(String waveInputFilePath, String waveOutputFilePath, String pluginPath, String fxpFilePath = null, float volume = 1.0f)
        {
            var wavFileReader = new WaveFileReader(waveInputFilePath);

            // reuse if batch processing
            bool doUpdateVstPlugin = false;

            if (_pluginPath != null)
            {
                if (_pluginPath.Equals(pluginPath))
                {
                    // plugin has not changed
                }
                else
                {
                    // plugin has changed!
                    doUpdateVstPlugin = true;
                }
            }
            else
            {
                _pluginPath       = pluginPath;
                doUpdateVstPlugin = true;
            }

            if (doUpdateVstPlugin)
            {
                var hcs = new HostCommandStub();
                hcs.Directory = Path.GetDirectoryName(pluginPath);
                vst           = new VST();

                try
                {
                    vst.PluginContext = VstPluginContext.Create(pluginPath, hcs);

                    if (vst.PluginContext == null)
                    {
                        Console.Out.WriteLine("Could not open up the plugin specified by {0}!", pluginPath);
                        return(false);
                    }

                    // plugin does not support processing audio
                    if ((vst.PluginContext.PluginInfo.Flags & VstPluginFlags.CanReplacing) == 0)
                    {
                        Console.Out.WriteLine("This plugin does not process any audio.");
                        return(false);
                    }

                    // check if the plugin supports offline proccesing
                    if (vst.PluginContext.PluginCommandStub.CanDo(VstCanDoHelper.ToString(VstPluginCanDo.Offline)) == VstCanDoResult.No)
                    {
                        Console.Out.WriteLine("This plugin does not support offline processing.");
                        Console.Out.WriteLine("Try use realtime (-play) instead!");
                        return(false);
                    }

                    // add custom data to the context
                    vst.PluginContext.Set("PluginPath", pluginPath);
                    vst.PluginContext.Set("HostCmdStub", hcs);

                    // actually open the plugin itself
                    vst.PluginContext.PluginCommandStub.Open();

                    Console.Out.WriteLine("Enabling the audio output on the VST!");
                    vst.PluginContext.PluginCommandStub.MainsChanged(true);

                    // setup the VSTStream
                    vstStream = new VSTStream();
                    vstStream.ProcessCalled  += vst_ProcessCalled;
                    vstStream.PlayingStarted += vst_PlayingStarted;
                    vstStream.PlayingStopped += vst_PlayingStopped;
                    vstStream.pluginContext   = vst.PluginContext;

                    vstStream.SetWaveFormat(wavFileReader.WaveFormat.SampleRate, wavFileReader.WaveFormat.Channels);
                } catch (Exception ex) {
                    Console.Out.WriteLine("Could not load VST! ({0})", ex.Message);
                    return(false);
                }
            }

            if (File.Exists(fxpFilePath))
            {
                vst.LoadFXP(fxpFilePath);
            }
            else
            {
                Console.Out.WriteLine("Could not find preset file (fxp|fxb) ({0})", fxpFilePath);
            }

            // each float is 4 bytes
            var buffer = new byte[512 * 4];

            using (var ms = new MemoryStream())
            {
                vstStream.SetInputWave(waveInputFilePath, volume);
                vstStream.DoProcess = true;

                // wait a little while
                Thread.Sleep(1000);

                // keep on reading until it stops playing.
                while (!stoppedPlaying)
                {
                    int read = vstStream.Read(buffer, 0, buffer.Length);
                    if (read <= 0)
                    {
                        break;
                    }
                    ms.Write(buffer, 0, read);
                }

                // save
                using (WaveStream ws = new RawSourceWaveStream(ms, vstStream.WaveFormat))
                {
                    ws.Position = 0;
                    WaveFileWriter.CreateWaveFile(waveOutputFilePath, ws);
                }
            }

            // reset the input wave file
            vstStream.DoProcess = false;
            vstStream.DisposeInputWave();

            // reset if calling this method multiple times
            stoppedPlaying = false;
            return(true);
        }