CSCore:如何在音频捕获期间应用效果

时间:2014-05-12 12:46:26

标签: audio audio-recording effects cscore

首先:我发现了这个问题:Is it possible to capture audio output and apply effects to it?。但它没有回答我的问题。

我的问题:几个月前我已经问过如何使用cscore录制音频输出:C# recording audio from soundcard。一切正常,但现在我想扩展我的应用程序。我想提供实时对录制的音频应用效果的功能。我已经找到了这个文档:http://cscore.codeplex.com/wikipage?title=Build%20a%20source%20chain&referringTitle=Documentation但它只是展示了如何将效果应用于播放。 我正在寻找关于如何做到这一点的提示或文档。我很确定,我错过了一些东西,但我真的不知道如何将捕捉转换为回放之类的东西?

1 个答案:

答案 0 :(得分:0)

你正确的方式。建立源链是一种很好的方法。您可以使用ISoundIn - 类(SoundInSource)将SoundInSource对象转换为音频源。我修改了上一个问题的代码:

    using (WasapiCapture capture = new WasapiLoopbackCapture())
    {
        //if nessesary, you can choose a device here
        //to do so, simply set the device property of the capture to any MMDevice
        //to choose a device, take a look at the sample here: http://cscore.codeplex.com/

        //initialize the selected device for recording
        capture.Initialize();

        //create a wavewriter to write the data to
        using (WaveWriter w = new WaveWriter("dump.wav", capture.WaveFormat))
        {
            //convert the ISoundIn object into an audio source
            //make sure, that the ISoundIn object is already initialized
            var captureSource = new SoundInSource(capture){ FillWithZeros = false }; 

            //build any source chain
            var echoEffect = new DmoEchoEffect(captureSource);

            int read = 0;
            var buffer = new byte[echoEffect.WaveFormat.BytesPerSecond]; //buffer to read from the source chain

            captureSource.DataAvailable += (s, e) =>
            {
                while ((read = echoEffect.Read(buffer, 0, buffer.Length)) > 0) //read all available data from the source chain
                {
                    w.Write(buffer, 0, read); //write the read data to the wave file
                }
            };

            //start recording
            capture.Start();

            Console.ReadKey();

            //stop recording
            capture.Stop();
        }
    }