Hey there,
I’ve been trying to capture microphone input (Microsoft.Xna.Framework.Audio.Microphone) but after capturing the data, I can’t get it to play properly. The sound is distorted, it sounds like it’s repetitive every few milliseconds and advancing very slowly.
Basically, what I’m trying is configuring the capturing buffer, the stream, and starting the Microphone:
microphone = Microphone.Default;
microphone.BufferDuration = TimeSpan.FromMilliseconds(100);
buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
stream.SetLength(0);
microphone.Start();
Then have an event executed whenever the buffer is ready, in order to read the buffer and write it to the stream:
microphone.GetData(buffer);
stream.Write(buffer, 0, buffer.Length);
And finally, when the microphone stopped, try to instantiate a SoundEffect with the stream data and play it:
sound = new SoundEffect(stream.ToArray(), microphone.SampleRate, AudioChannels.Mono);
isound = sound.CreateInstance();
isound.Pitch = 0f;
isound.Volume = 1f;
isound.Play();
The implementation is based on a (very old, XNA) article, but I’d say it’s quite straightforward:
https://www.codeproject.com/Tips/535284/How-to-record-sound-using-XNA
It could be that some of its approach is not valid anymore though, so if someone has experience with it and could put me on the right track, that’d be great. I searched the forum and elsewhere, but unfortunately, I couldn’t find a solution or explanation on the topic.
Thanks!