Jump to content
  • Advertisement
Sign in to follow this  
Oduig

Delay when using Microphone in XNA

This topic is 2352 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been working on a game where you can use an electrical guitar as a controller. Much like Guitar Hero :)
The guitar is plugged into an amp, which has a line out. The line out is connected to the microphone port on the back side of my PC.

I am using the XNA Microphone class, the code is like this:

protected override void Initialize() {
// Initialize Sound
mic.BufferDuration = TimeSpan.FromMilliseconds(100);
sound = new DynamicSoundEffectInstance(mic.SampleRate, AudioChannels.Mono);

mic.BufferReady +=new EventHandler<EventArgs>(mic_BufferReady);


base.Initialize();
}

// Activates on sound input
void mic_BufferReady(object sender, EventArgs e) {
byte[] data = new byte[mic.GetSampleSizeInBytes(mic.BufferDuration)];
mic.GetData(data);
sound.SubmitBuffer(data);
}



In the update routine I call mic.Start() and sound.Play(). This works, but the sound is delayed by 100 milliseconds, which is the size of the buffer. For other uses such as voice comms, this delay is not a problem. When you're playing the guitar however, you will hear the notes you play before you hear the sound from the application, which is incredibly confusing. It seems weird that a gaming framework thinks a 100ms sound delay is a normal thing to deal with, which is why I am asking this question.

According to the documentation, 100ms is the minimal value of a Microphone.BufferDuration. Is there any way to work around this, or is there another XNA feature I could use? Or will I have to resort to using/writing a generic library for C#?


Thank you in advance,
Guido

Share this post


Link to post
Share on other sites
Advertisement
This problem is not directly related to the XNA library itself.
I've done lots of work with instruments including eletric guitars, and you usually get a latency like this if you use a "normal" sound card.
For example, using my builtin soundcard I always have a delay in order of 100ms, but with a expensive external USB card I am able to get a latency similar to 30ms (if I remember correctly)
You should read about ASIO for more information about this..

Share this post


Link to post
Share on other sites

This problem is not directly related to the XNA library itself.
I've done lots of work with instruments including eletric guitars, and you usually get a latency like this if you use a "normal" sound card.
For example, using my builtin soundcard I always have a delay in order of 100ms, but with a expensive external USB card I am able to get a latency similar to 30ms (if I remember correctly)
You should read about ASIO for more information about this..


I was completely wrong about the reason for delay. After some testing it turns out that a system beep (1ms to play) takes 130ms to be recognized by my Windows recording device. That's disappointing, but it answers my question. Thanks!



Edit: after a driver update on my motherboard, it's now only a delay of 60ms. Still not enough, but a lot better than before.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!