NAudio - 改变缓冲麦克风音频的音调并发送到虚拟音频电缆

NAudio - Change pitch of buffered microphone audio and send to Virtual Audio Cable

我决定尝试使用 NAudio 和虚拟音频电缆创建一个声卡,以便与 Discord(或类似软件)一起使用。我能够 'inject' 将音频从麦克风传输到音频线,这样我就可以通过选择虚拟音频线作为 Discord 中的输入设备来播放声音文件和麦克风音频到 Discord。

为了好玩,我想看看我是否可以修改麦克风音频以使其成为 'squeaky' 或 'deep'。所以我开始研究修改音频的音调。我发现 NAudio 有一个 SmbPitchShiftingSampleProvider 然后发现 this question 这有助于处理缓冲音频,但我不知道该怎么做。到目前为止,这是我得到的:

    //Inject Mic Audio
    WaveIn injectMicIn = null;
    WaveOut injectMicOut = null;
    private BufferedWaveProvider bufferedWaveProvider; //Buffer for mic audio
    public int micDeviceID; //Device ID of selected microphone
    public int virtualAudioCableID; //Device ID of selected virtual audio cable
    ISampleProvider sampleP; //#### TO DO: Remove this if I don't use it.
    NAudio.Wave.SampleProviders.SmbPitchShiftingSampleProvider pitchProvider; //#### TO DO: Remove this if I don't use it

    private void InjectMicrophone()
    {
        //Mic Input
        if (injectMicIn == null)
        {
            injectMicIn = new WaveIn();
            injectMicIn.RecordingStopped += new EventHandler<StoppedEventArgs>(OnRecordingStopped);
            injectMicIn.DataAvailable += InjectMicOnDataAvailable;
            injectMicIn.WaveFormat = new WaveFormat(44100, 1);
        }

        injectMicIn.DeviceNumber = SharedVars.micInjectInputDeviceID; //Set the users selected input device

        //Mic Output
        if (injectMicOut == null)
        {
            injectMicOut = new WaveOut();
            injectMicOut.PlaybackStopped += new EventHandler<StoppedEventArgs>(OnPlaybackStopped);

        }

        bufferedWaveProvider = new BufferedWaveProvider(injectMicIn.WaveFormat); //Prepare the buffer for the microphone audio

        sampleP = bufferedWaveProvider.ToSampleProvider(); //#### TO DO: Remove this if I don't use it for pitch shifting

        injectMicOut.DeviceNumber = SharedVars.micInjectOutputDeviceID; //Set the users selected output device
        //injectMicOut.Init(bufferedWaveProvider);

        SharedVars.currentlyInjectingMic = true;
        injectMicIn.StartRecording(); //Record the mic and
        //injectMicOut.Play(); //out play it on the selected output device

    }

    bool init = false;
    private void InjectMicOnDataAvailable(object sender, WaveInEventArgs e)
    {
        bufferedWaveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded); //Add the mic audio to the buffer

        //#### TO DO: REMOVE THIS TEST CODE
        var bytesPerFrame = (injectMicIn.WaveFormat.BitsPerSample / 8) * injectMicIn.WaveFormat.Channels;
        var bufferedFrames = e.BytesRecorded / bytesPerFrame;

        var frames = new float[bufferedFrames];
        sampleP.Read(frames, 0, bufferedFrames);

        pitchProvider = new NAudio.Wave.SampleProviders.SmbPitchShiftingSampleProvider(sampleP);
        pitchProvider.PitchFactor = 2.0f;

        if (!init)
        {
            injectMicOut.Init(pitchProvider);
            init = true;
        }

        injectMicOut.Play();
        //#### TO DO: REMOVE THIS TEST CODE
    }

如有任何帮助,我们将不胜感激。

编辑:最终代码

    //Inject Mic Audio
    WaveIn injectMicIn = null;
    WaveOut injectMicOut = null;
    private BufferedWaveProvider bufferedWaveProvider; //Buffer for mic audio
    public int micDeviceID; //Device ID of selected microphone
    public int virtualAudioCableID; //Device ID of selected virtual audio cable
    SmbPitchShiftingSampleProvider pitchProvider; //Used to adjust the pitch of the mic audio if required

    private void InjectMicrophone()
    {
        //Mic Input
        if (injectMicIn == null)
        {
            injectMicIn = new WaveIn();
            injectMicIn.RecordingStopped += new EventHandler<StoppedEventArgs>(OnRecordingStopped);
            injectMicIn.DataAvailable += InjectMicOnDataAvailable;
            injectMicIn.WaveFormat = new WaveFormat(44100, 1);
        }

        injectMicIn.DeviceNumber = SharedVars.micInjectInputDeviceID; //Set the users selected input device

        //Mic Output
        if (injectMicOut == null)
        {
            injectMicOut = new WaveOut();
            injectMicOut.PlaybackStopped += new EventHandler<StoppedEventArgs>(OnMicPlaybackStopped);    
        }

        injectMicOut.DeviceNumber = SharedVars.micInjectOutputDeviceID; //Set the users selected output device

        bufferedWaveProvider = new BufferedWaveProvider(injectMicIn.WaveFormat); //Prepare the buffer for the microphone audio

        pitchProvider = new SmbPitchShiftingSampleProvider(bufferedWaveProvider.ToSampleProvider()); //Create a pitch shifting sample provider to adjust the pitch of the mic audio if required
        pitchProvider.PitchFactor = 1.0f; //#### TO DO: Retrieve value from a UI control

        injectMicOut.Init(pitchProvider);

        SharedVars.currentlyInjectingMic = true;
        injectMicIn.StartRecording(); //Record the mic and
        injectMicOut.Play(); //out play it on the selected output device

    }

    private void InjectMicOnDataAvailable(object sender, WaveInEventArgs e)
    {
        bufferedWaveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded); //Add the mic audio to the buffer
    }

你很接近

在录音设备上,直接把录好的音频放到BufferedWaveProvider即可。

在播放设备上,传入从 BufferedWaveProvider 读取的 SmbPitchShiftingProvider(使用 ToSampleProvider 将其转换为 ISampleProvider