×
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)
(Galazzo -)
hamishwillee (Talk | contribs)
m (Hamishwillee - Minor updates.)
Line 24: Line 24:
  
 
== Introduction ==
 
== 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.
+
This article shows how to manage '''microphone raw data''' in Windows Phone, focusing on volume management.  The accompanying example project is a (work in progress) real application: '''Shooter Assistant''' which assists shooters during training for specialities including: Olympic Trap, Skeet and Double Trap.
  
 
== Shooter Assistant ==
 
== Shooter Assistant ==
Olympic Trap shooting is one of the three major forms of competitive clay shooting,  generally shot with a 12 gauge with a double barreled shotgun.  
+
Olympic Trap shooting is one of the three major forms of competitive clay shooting,  generally shot with a 12 gauge double barreled shotgun.  
The clay target is launched automatically and instantly when the shooter's order starts, while he's  waiting with the gun to his shoulder and charged with two shots.
+
 
 +
The clay target is launched automatically and instantly when the shooter's order starts, while the shooter waits with the gun to shoulder and charged with two shots.
 
Each series consists of twenty-five plates. For each footboard are arranged three trap machines for a total, therefore, of 15 trap machines.  
 
Each series consists of twenty-five plates. For each footboard are arranged three trap machines for a total, therefore, of 15 trap machines.  
 
The direction of the clay targets varies from 0 to 45 ° to the right or left. Its height, ten feet from the pit must be between 1.50 metres. 3, 50 metres.
 
The direction of the clay targets varies from 0 to 45 ° to the right or left. Its height, ten feet from the pit must be between 1.50 metres. 3, 50 metres.
Line 40: Line 41:
  
 
Wearing earphones, given the start command,  a beep will be played after 5 tenths seconds that means you have to press trigger.
 
Wearing earphones, given the start command,  a beep will be played after 5 tenths seconds that means you have to press trigger.
Of course reaction time changes based on target direction.<br />
+
Of course reaction time changes based on target direction.
Collected results ( good and bad ), using '''genetic algorithms''', the software fit itself your reaction time.
+
  
<gallery>
+
Collected results ( good and bad ), using '''genetic algorithms''', the software fit itself your reaction time.
 +
<gallery widths=250px heights=300px>
 
File:ShooterAssistantHome.png|Home screen
 
File:ShooterAssistantHome.png|Home screen
 
File:ShooterAssistant.png|Record Screen
 
File:ShooterAssistant.png|Record Screen
Line 59: Line 60:
  
 
Add the following using statements to the top of your MainPage.xaml.cs file:
 
Add the following using statements to the top of your MainPage.xaml.cs file:
<code cpp>
+
<code csharp>
 
using System.IO;
 
using System.IO;
 
using System.Windows.Threading;
 
using System.Windows.Threading;
Line 67: Line 68:
  
 
In '''MainPage.xaml.cs''', declare variables as global members of your MainPage class:
 
In '''MainPage.xaml.cs''', declare variables as global members of your MainPage class:
<code cpp>
+
<code csharp>
 
public partial class MainPage : PhoneApplicationPage
 
public partial class MainPage : PhoneApplicationPage
 
{
 
{
Line 82: Line 83:
  
 
Add the following code to the constructor of your MainPage class after the call to '''InitializeComponent'''
 
Add the following code to the constructor of your MainPage class after the call to '''InitializeComponent'''
<code cpp>
+
<code csharp>
 
// Contructor
 
// Contructor
 
public MainPage()
 
public MainPage()
Line 98: Line 99:
  
 
Now add the following method to manage Microphone '''raw data'''
 
Now add the following method to manage Microphone '''raw data'''
<code cpp>
+
<code csharp>
 
void microphone_BufferReady(object sender, EventArgs e)
 
void microphone_BufferReady(object sender, EventArgs e)
 
{
 
{
Line 109: Line 110:
 
=== Detecting Volume changes. The RMS Method. ===
 
=== Detecting Volume changes. The RMS Method. ===
 
Declare a global variable in your MainPage class to set '''threshold''':
 
Declare a global variable in your MainPage class to set '''threshold''':
<code cpp>
+
<code csharp>
 
public partial class MainPage : PhoneApplicationPage
 
public partial class MainPage : PhoneApplicationPage
 
{
 
{
Line 121: Line 122:
 
</code>
 
</code>
  
<code cpp>
+
<code csharp>
 
void microphone_BufferReady(object sender, EventArgs e)
 
void microphone_BufferReady(object sender, EventArgs e)
 
{
 
{
Line 155: Line 156:
  
 
=== Start Recording ===
 
=== Start Recording ===
<code cpp>
+
<code csharp>
 
private void recordButton_Click(object sender, RoutedEventArgs e)
 
private void recordButton_Click(object sender, RoutedEventArgs e)
 
{
 
{
Line 169: Line 170:
  
 
=== Stop Recording ===
 
=== Stop Recording ===
<code cpp>
+
<code csharp>
 
private void stopButton_Click(object sender, RoutedEventArgs e)
 
private void stopButton_Click(object sender, RoutedEventArgs e)
 
{
 
{
Line 180: Line 181:
  
 
== Related Links ==
 
== Related Links ==
* [http://www.developer.nokia.com/Community/Wiki/Using_QMicrophone Using QMicrophone] - Symbian platform
+
* [[Using QMicrophone]] - Symbian platform

Revision as of 07:59, 12 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: hamishwillee (12 Apr 2012)

Contents

Introduction

This article shows how to manage microphone raw data in Windows Phone, focusing on volume management. The accompanying example project is a (work in progress) real application: Shooter Assistant which assists shooters during training for specialities including: Olympic Trap, Skeet and Double Trap.

Shooter Assistant

Olympic Trap shooting is one of the three major forms of competitive clay shooting, generally shot with a 12 gauge double barreled shotgun.

The clay target is launched automatically and instantly when the shooter's order starts, while the shooter waits with the gun to shoulder and charged with two shots. Each series consists of twenty-five plates. For each footboard are arranged three trap machines for a total, therefore, of 15 trap machines. The direction of the clay targets varies from 0 to 45 ° to the right or left. Its height, ten feet from the pit must be between 1.50 metres. 3, 50 metres. The clay target will touch the ground, after a parabolic curve, between 75 and 80 meters. The output speed of the pit usually fluctuates between 100 km/h and 120 km/h. The target is centered, usually, within a radius of 35 meters, then the reaction time of the shooter road from 5 to 6 tenths of a second.

Reaction time is the key of success. Results can heavily influenced being 1 tenths faster or slower.

Shooter Assistant helps the shooter training in that way:

Wearing earphones, given the start command, a beep will be played after 5 tenths seconds that means you have to press trigger. Of course reaction time changes based on target direction.

Collected results ( good and bad ), using genetic algorithms, the software fit itself your reaction time.

Creating the project

  • Install Windows Phone SDK
  • 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();
}
}

Related Links

406 page views in the last 30 days.