Sound Recording

0
158

All Windows Phone 7 physical devices include a microphone. You can access this using the methods of the XNA Framework. This is more complex than using some other features of the phone because you must initiate an asynchronous dispatcher to raise the microphone events.

To record sounds from the microphone, you must do the following:

  • Add a reference to the assembly Microsoft.Xna.Framework to your project.
  • Create an XNA asynchronous dispatcher “pump” class and initialize it in the constructor or startup event in your App.xaml. cs file.
  • Get a reference to a microphone.
  • Specify a function or lambda expression that will receive the sound data.
  • Start the microphone.

Note: An XNA asynchronous event dispatcher is an application service that allows Silverlight to detect events raised by XNA components. Usually, these events are propagated by the XNA game loop mechanism, but this is not present in a Silverlight application. The dispatcher service collects these events and raises them through the Silverlight event mechanism.

In your application, declare variables to hold the microphone instance and a stream that will receive the sound data bytes. This example uses the default microphone and an isolated storage file stream. To use this code, you must reference the namespaces Microsoft.Xna. Framework.Audio and System.IO.IsolatedStorage.

C#
Microphone mic = Microphone.Default;
IsolatedStorageFileStream myFileStream;

Specify the callback for the BufferReady event and the size of the recording buffer. The maximum size is one second, though lower values tend to give better performance.

C#
mic.BufferReady += Mic_BufferReady;
mic.BufferDuration = TimeSpan.FromMilliseconds(500);

Open the file stream that will receive the recorded data (in this case, an isolated storage file), and then start the microphone to begin recording.

C#
// Create a virtual store and file stream.
// Check for an existing duplicate name.
var myStore = IsolatedStorageFile.GetUserStoreForApplication();
String fileName = “MyAudio.dat”;
if (myStore.FileExists(fileName))
{
myStore.DeleteFile(fileName);
}
myFileStream = myStore.CreateFile(fileName);
mic.Start();

The callback for the BufferReady event must capture the data from the microphone and store it in your target location.

C#
void Mic_BufferReady(object sender, EventArgs e)
{
Microphone mic = sender as Microphone;
byte[] content = new byte[mic.GetSampleSizeInBytes
(mic.BufferDuration)];
mic.GetData(content);
myFileStream.Write(content, 0, content.Length);
}

To stop recording, call the Stop method of the Microphone class and close the stream you are writing to.

C#
mic.Stop();
myFileStream.Close();

You can check the status of the microphone at any time by querying the State property of the Microphone class. This returns a value from the MicrophoneState enumeration. Possible values are Started and Stopped.

For more information, see “Microphone Class” on MSDN (http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.audio.microphone(XNAGameStudio.40).aspx).

Note: The files created from the byte stream returned by the Microphone class are not valid sound files. If you want to convert a saved byte stream as a .wav or .wmv file, you must use an auxiliary class or library to add the appropriate headers to the file and save it is the appropriate format. For a description of the .wav file format, see “Wave File Format” on The Sonic Spot website (http://www.sonicspot.com/guide/wavefiles.html).

The following series of posts by Microsoft academic developer evangelist Dan Waters describe the techniques of audio programming and provide code you can reuse:

“Intro to Audio Programming, Part 1: How Audio Data is Represented” at http://blogs.msdn.com/b/dawate/archive/2009/06/22/intro-to-audio-programming-part-1-howaudio-data-is-represented.aspx

“Intro to Audio Programming, Part 2: Demystifying the WAV Format” at http://blogs.msdn.com/b/dawate/archive/2009/06/23/intro-to-audio-programming-part-2-demystifying-the-wav-format.aspx

“Intro to Audio Programming, Part 3: Synthesizing Simple Wave Audio using C#” at http://blogs.msdn.com/b/dawate/archive/2009/06/24/intro-to-audio-programming-part-3-synthesizing-simple-wave-audio-using-c.aspx

“Intro to Audio Programming Part 4: Algorithms for Different Sound Waves in C#” at http://blogs.msdn.com/b/dawate/archive/2009/06/25/intro-to-audio-programming-part-4-algorithms-
for-different-sound-waves-in-c.aspx