I decided to have a go at creating a sound board for use with Discord (or similar software) using NAudio and a Virtual Audio Cable. I was able to 'inject' the audio from the microphone to the audio cable so I could play sound files and mic audio to Discord by selecting the virtual audio cable as the input device in Discord.
For fun I thought I would see if I could modify the mic audio to make it 'squeaky' or 'deep'. So I started looking into modifying the pitch of the audio. I discovered that NAudio has an SmbPitchShiftingSampleProvider
and then found this question which helps to work with buffered audio, but I can't figure out how to do it. Here's what I've got so far:
//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
}
Any help would be greatly appreciated.
EDIT: Final 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
}