0 out of 1 rated this helpful - Rate this topic

Microphone Class

XNA Game Studio 4.0
Provides properties, methods, and fields and events for capturing audio data with microphones.

Namespace: Microsoft.Xna.Framework.Audio
Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)

public sealed class Microphone
Xbox 360, Windows 7, Windows Vista, Windows XP, Windows Phone 7
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Windows Phone 7 Tools Beta current considerations..

In Windows Phone 7, if you are using the EventDriven capture from Silverlight you need to pump the XNA dispatcher in order to get the events to fire.  If you are just polling GetData on your own timer then you will not need to do this.

public App()
        {
            // Add this to the end of your App.xaml.cs
            this.ApplicationLifetimeObjects.Add(new XNAAsyncDispatcher(TimeSpan.FromMilliseconds(50)));
        }
    public class XNAAsyncDispatcher : IApplicationService
    {
        private DispatcherTimer frameworkDispatcherTimer;
        public XNAAsyncDispatcher(TimeSpan dispatchInterval)
        {
            this.frameworkDispatcherTimer = new DispatcherTimer();
            this.frameworkDispatcherTimer.Tick += new EventHandler
(frameworkDispatcherTimer_Tick);
            this.frameworkDispatcherTimer.Interval = dispatchInterval;
        }



        void IApplicationService.StartService(ApplicationServiceContext context) { 
this.frameworkDispatcherTimer.Start(); }   
        void IApplicationService.StopService() { 
this.frameworkDispatcherTimer.Stop(); }   
        void frameworkDispatcherTimer_Tick(object sender, EventArgs e) { 
FrameworkDispatcher.Update(); }

    }







The data is 16-bit Mono PCM data in the sample rate of the microphone (which you can figure out from Microphone.Default.SampleRate).  You can push this data directly into a .SubmitBuffer call on the DynamicSoundEffectInstance. The event driven code to record looks something like this.. If you have problems you can try to up the buffer duration to 300 milliseconds.

public EventDrivenCapture()
{
    mic = Microphone.Default;
    mic.BufferDuration = TimeSpan.FromMilliseconds(100);
    bufferDuration = mic.BufferDuration;
    Buffer = new byte[mic.GetSampleSizeInBytes(mic.BufferDuration)];
    mic.BufferReady += new EventHandler(OnBufferReady);
    DynamicPlayback = new DynamicSoundEffectInstance(mic.SampleRate,  
AudioChannels.Mono);
}
  
 public void OnBufferReady(object sender, EventArgs args)
{
    mic.GetData(buffer, out duration);
    DynamicPlayback.SubmitBuffer(Buffer);
}