Home www.visualprog.cz

 

 

NF Spectrum analyzer software  (FreqAnalysis.exe)

The audio Frequency Analyzer and Fast Fourier Transform in .NET (C#).

 11.3.2007  start of date 

Email Kontakt .

Pro více informací (more info) je zde formulář

(c) 2006 Pavel Pindora 

Click on of the PayPal button to donate money to this Project

 

 

.NET and Audio hardware handling

.NET and Audio hardware handling

I had implemented a audio functions,FFT  in .NET dll library SamplingSound.dll

 

This software using the sound card of your PC ( Pocket PC ) . The nf signal, connected to microphone inputs of  sound card ,  is converted into digital stream and this is stored to buffer for FFT  . 

 

 

 

 

For retrieves the number of input devices in the system we can used function NumDevices  

 

//[DllImport ("coredll.dll")] for Pocket PC

[DllImport("Winmm.dll")]

protected static extern int waveInGetNumDevs();

 

public static uint NumDevices()

{

return (uint)waveInGetNumDevs();

}

 

SetWAVEINCAPS( WAVE_MAPPER ) Get the capabilities of a specified audio input device.

 

 

[DllImport("Winmm.dll")]

protected static extern Wave.MMSYSERR waveInGetDevCaps(uint uDeviceID, byte[] pwic, uint cbwic);

 

 

 

public static object SetWAVEINCAPS(uint deviceId)

{

WAVEINCAPS capswavein = new WAVEINCAPS();

MMSYSERR result = waveInGetDevCaps(deviceId, capswavein, capswavein.Size);

if (MMSYSERR.NOERROR == result)

return capswavein;

else

return null;

}

 

Set sound card format

 

format = new WaveFormat(44100, 16, 1);

 

 

ffttransform = new FFT((int)FftPoints * ((format.wBitsPerSample * format.nChannels) / 8), SamplesPerSecond);

 

uint wTemp = WAVE_MAPPER;

wFr = (Int16)wTemp;

uint dwI = 0;

callback = new WaveCallback(WaveCallback);

hwnd = System.Diagnostics.Process.GetCurrentProcess().Handle;

 

and define callback

public delegate void WaveCallback(IntPtr DeviceAddress, int Msg, IntPtr InstanceDat, int wParam, int lParam);

 

 

#region WaveCallback

public void WaveCallback(IntPtr DeviceAddress, int Msg, IntPtr InstanceDat, int wParam, int lParam)

{

const int MM_WIM_OPEN = 0x3BE;

const int MM_WIM_CLOSE = 0x3BF;

const int MM_WIM_DATA = 0x3C0;

 

countBf++;

if (Msg == MM_WIM_DATA)

{

try

{

SamplingSound.WaveIn.WaveFile.BlockDone();

}

catch

{

}

}

if (Msg == MM_WIM_OPEN)

{

SetLabel4Text();

listBox1.Items.Add("WaveCallback: open WIM_OPEN");

}

}

#endregion WaveCallback

 

open Wave

#region waveInOpen

errMy = SamplingSound.WaveIn.waveInOpen(out sIn, wFr, format, callback, (IntPtr)0, Wave.CALLBACK_FUNCTION);

if (errMy == Wave.MMSYSERR.NOERROR)

listBox1.Items.Add("ok waveInOpen");

else

{

listBox1.Items.Add("error waveInOpen = " + errMy);

errMy = Wave.MMSYSERR.INVALHANDLE;

return;

}

#endregion waveInOpe

 

and start working thread. 

 

 

 

#region ThreadProc

private void ThreadProc()

{

MethodInvoker mi = new MethodInvoker(this.SetLabel2Text);

while (!Finished)

{

if (SamplingSound.WaveIn.WaveFile.m_recording == true)

{

countTh++;

Thread.Sleep(10);

}

else

{

if (Paused == true)

{

GetDataFromBuffer();

if (ffttransform.EndFFT == false && OutBuf.Count>0)

{// EndFFT nastavuje button Start a fn GetDataFromBuffer OutBuf

ffttransform.Transform(OutBuf);

OutBuf.Clear();

}

}

Thread.Sleep(10);

}

this.BeginInvoke(mi);

//if (countTh > 0) Finished = true;

}

}

#endregion ThreadProc

 

Start recording audio stream to buffer ( set size buffer to bytesPerBuf )

 

 

long bytesPerBuf = FftPoints * ((format.wBitsPerSample * format.nChannels) / 8);

 

//prepare buffer  for recording

errMy = SamplingSound.WaveIn.WaveFile.Preload((uint)wFr, hwnd, 1000, (int)bytesPerBuf, format);

 

errMy = SamplingSound.WaveIn.WaveFile.Start();

 

{// Calling

 

[DllImport("Winmm.dll")]

public static extern Wave.MMSYSERR waveInStart(IntPtr hwi);

 

 

 

 

 

NAVRCHOLU.cz