gpt4 book ai didi

c# - NAudio - 更改缓冲麦克风音频的音高并发送到虚拟音频电缆

转载 作者:行者123 更新时间:2023-12-02 22:53:40 30 4
gpt4 key购买 nike

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

为了好玩,我想我会看看我是否可以修改麦克风音频以使其“吱吱”或“深沉”。所以我开始研究修改音频的音高。我发现 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 .

在播放设备上,传入 SmbPitchShiftingProviderBufferedWaveProvider 读取(使用 ToSampleProvider 将其变成 ISampleProvider )

关于c# - NAudio - 更改缓冲麦克风音频的音高并发送到虚拟音频电缆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48064221/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com