×
Namespaces

Variants
Actions
(Difference between revisions)

How to access and manage the Microphone raw data in WP

From Nokia Developer Wiki
Jump to: navigation, search
galazzo (Talk | contribs)
m (Galazzo -)
galazzo (Talk | contribs)
m (Galazzo -)
Line 1: Line 1:
 
[[Category:Draft]]
 
[[Category:Draft]]
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
+
|sourcecode= [[Media:MicrophoneVolumeTest.zip]]
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= Nokia Lumia 800
 
|devices= Nokia Lumia 800
|sdk= built and tested against WP SDK 7.1
+
|sdk= built and tested against Windows Phone SDK 7.1
 
|platform= Windows Phone
 
|platform= Windows Phone
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
Line 10: Line 10:
 
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|keywords= Microphone, Volume, Raw
+
|keywords= Xna,Microphone, Volume, Raw, Audio
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->

Revision as of 13:23, 6 April 2012

WP Metro Icon Multimedia.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleTested with
SDK: built and tested against Windows Phone SDK 7.1
Devices(s): Nokia Lumia 800
Compatibility
Platform(s): Windows Phone
Windows Phone 8
Windows Phone 7.5
Article
Keywords: Xna,Microphone, Volume, Raw, Audio
Created: galazzo (06 Apr 2012)
Last edited: galazzo (06 Apr 2012)

Contents

Introduction

This article will explain how to manage microphone raw data in Windows Phone, focusing on Volume management. Will be shown a real application Shooter Assistant, a work in progress project to assist the shooter during training for Olympic Trap speciality, also applicable for Skeet and Double Trap speciality.

Creating the project

  • Install [Windows Phone SDK http://msdn.microsoft.com/en-us/library/ff402530%28v=vs.92%29.aspx]
  • Create a new project by selecting the File | New Project menu command.
  • The New Project window will be displayed. Expand the Visual C# templates, and then select the Silverlight for Windows Phone templates.
  • Select the Windows Phone Application template. Fill in the project name as desired. {Note|Don't call it simply "Microphone"}
  • In the Solution Explorer, right-click References and choose Add Reference.
  • Choose Microsoft.Xna.Framework from the list of .NET components and click the OK button.
  • If you see a dialog that warns about adding a reference to a Silverlight assembly, click the Yes button.

Add the following using statements to the top of your MainPage.xaml.cs file:

using System.IO;
using System.Windows.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

In MainPage.xaml.cs, declare variables as global members of your MainPage class:

public partial class MainPage : PhoneApplicationPage
{
 
Microphone microphone = Microphone.Default;
byte[] buffer;
MemoryStream stream = new MemoryStream();
SoundEffect sound;
 
// Constructor
public MainPage()
{

Add the following code to the constructor of your MainPage class after the call to InitializeComponent

// Contructor
public MainPage()
{
InitializeComponent();
 
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = TimeSpan.FromMilliseconds(50);
dt.Tick += delegate { try { FrameworkDispatcher.Update(); } catch { } };
dt.Start();
 
microphone.BufferReady += new EventHandler<EventArgs>(microphone_BufferReady);
}

Now add the following method to manage Microphone raw data

void microphone_BufferReady(object sender, EventArgs e)
{
microphone.GetData(buffer);
// If you want to store the audio data in a stream
stream.Write(buffer, 0, buffer.Length);
}

Detecting Volume changes. The RMS Method.

Declare a global variable in your MainPage class to set threshold:

public partial class MainPage : PhoneApplicationPage
{
Microphone microphone = Microphone.Default;
byte[] buffer;
MemoryStream stream = new MemoryStream();
SoundEffect sound;
....
private int minimumThreshold = 500;
....
void microphone_BufferReady(object sender, EventArgs e)
{
// Retrieve audio data
microphone.GetData(buffer);
 
// RMS Method
double rms = 0;
ushort byte1 = 0;
ushort byte2 = 0;
short value = 0;
int volume = 0;
rms = (short)(byte1 | (byte2 << 8));
 
for (int i = 0; i < buffer.Length - 1; i += 2)
{
byte1 = buffer[i];
byte2 = buffer[i + 1];
 
value = (short)(byte1 | (byte2 << 8));
rms += Math.Pow(value, 2);
}
rms /= (double)(buffer.Length / 2);
volume = (int)Math.Floor(Math.Sqrt(rms));
 
if ((volume > minimumThreshold))
{
System.Diagnostics.Debug.WriteLine("Threshold exceeded");
System.Diagnostics.Debug.WriteLine("buffer.Length" + buffer.Length + " Volume:" + volume);
}
}

Start Recording

private void recordButton_Click(object sender, RoutedEventArgs e)
{
if (microphone.State == MicrophoneState.Stopped)
{
microphone.BufferDuration = TimeSpan.FromMilliseconds(1000);
buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
microphone.Start();
System.Diagnostics.Debug.WriteLine("Threshold setted to:" + minimumThreshold);
}
}

Stop Recording

private void stopButton_Click(object sender, RoutedEventArgs e)
{
if (microphone.State == MicrophoneState.Started)
{
microphone.Stop();
}
}
407 page views in the last 30 days.