NAudio - 改变缓冲麦克风音频的音高并发送到虚拟音频线

时间:2018-01-02 16:10:52

标签: c# audio audio-recording naudio

我决定使用NAudio和虚拟音频线创建一个与Discord(或类似软件)配合使用的声卡。我能够注射'从麦克风到音频线的音频,所以我可以通过选择虚拟音频线作为Discord中的输入设备,将声音文件和麦克风音频播放到Discord。

为了好玩,我想我会看到我是否可以修改麦克风音频以使其“吱吱作响”。或者' 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
    }

1 个答案:

答案 0 :(得分:1)

你非常接近

在录音设备上,只需将录制的音频直接放入BufferedWaveProvider

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