|
So you are doing this for performance reasons. Than scripts are probably too slow for you too.
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Hi,
I have a project which i am unable to do myself.It is not a very long project but because of the time constraint,I would like it to be completed at most by the 7th of November.
The specifications of the project are as follows:
I actually am developing a hearing aid and now the problem is in real time implementation of it.
1. I have to capture speech coming in through "mic in" port of the computer.
2. The captured speech would pass through a module(consisting of filters and Fourier transforms)..which i have written below.
3. The output through my module has to be played back out through the "mic out" port of the computer.
Code is :
#include <stdio.h>
#include <math.h>
#include <string.h>
double wdata[13]; //required for the FFT
double datafft[8192]; //length required is twice the inverse FFT
double x[295000]; //input data vector,maximum,length
int expo = 12; //required for the FFT
int main ()
{
double pi,ex,temp,theta1,coefa,coefb,G;
double X2,W1,bf,phikb,phia,phib;
double phi[1260],XR[4096],XI[4096],X[4096],XM[4096];
double w[512]; // vector of hamming window values
double ret[4096]; // vector of the inverse FFT returned data
double x1[512],x2[512],f[512];
double G1;
int a,i,j,k,m,n,FL,NW,L,l,q1,q2,kb,ks,ke;
double sr = 25000.0; // sampling rate
int CF = 15; // compression factor,CF:1
int hfru = 10500; // high frequency range upper limit
int hfrl = 4400; // high frequency range lower limit
int N = 2048; // number of frequency samples,top half
int ND = 512; // length of input data
int M = 4096; // length of inverse FFT
FILE *fp;
fp = fopen("ELPHRG01.WAV","r+b");
i = 0;
*/
while((fscanf(fp,"%f/n",&x[i++])) != EOF);
fclose(fp);
FL = i;
pi = 4.0 * atan(1.0);
ex = exp(1.0);
NW = 512;
W1 = 0.5;
l = 0;
L = 1;
for (n=0; n < NW; n++) {
theta1 = (2 * pi * n) / (NW - 1);
w[n] = W1 - (W1 * cos(theta1));
}
for (n = 0; n < NW; n++) {
x2[n] = 0.0;
}
bf = sr / (2 * CF); // break frequency
phikb = (pi * bf) / (sr / 2); // angle of break frequency
kb = N / CF; // number of samples from DC to break
phia = (hfrl / (sr /2 )) * pi; // high frequency region starting angle
phib = (hfru / (sr /2 )) *pi; // high frequency region ending angle
coefa = 0.00799;
coefb = -0.3722072;
//1. Obtain a windowed Segment of the input data
for (a = 1; a < 2 * FL / NW + 1; a++) {
for (m = 0, n = 1; m < NW; m++, n++) {
f[m] = w[m] * x[n];
}
}
//2. Obtain Nonlinearly Compressed and Transpossed frequency samples
for (k = 0; k < 150; k++) {
temp = coefa * k;
phi[k] = pow(ex, temp) + coefb;
G = pow(ex, temp);
G1 = G * sqrt(G) + 6;
XR[k] = 0.0;
XI[k] = 0.0;
for (n = 0; n < ND; n++) {
XR[k] = XR[k] + f[n] * cos(phi[k] * n);
XI[k] = XI[k] + f[n] * sin(phi[k]*n);
}
XR[k] = G1 * XR[k];
XI[k] = G1 * XI[k];
}
//3. Pad zeros in the center of the Spectrum
for (k = 150; k < M - 150; k++) {
XR[k] = 0.0;
XI[k] = 0.0;
}
//4. Form the Complex conjugate part of the spectrum
for (k = M - 150 + 1; k < M + 1; k++) {
XR[k] = XR[M-k];
XI[k] = -XI[M-k];
}
//5. Convert from Polar to rectangular for the inverse FT
for (i = 0; i < M; i++) {
j = 2 * i;
k = j + 1;
datafft[j] = XR[i];
datafft[k] = XI[i];
}
//6. Call the FFT
f_init(); //I need to implement this FFT function in C++ rather than Foxpro
MAIN_();
//7. Obtain the inverse FFT from the FFT call
ret[0] = datafft[0] / M;
for (i = 2244; i < 2756; i++) {
j = 2 * i;
k = 2 *M - j;
ret[i+1] = datafft[k] / M;
}
for (k = 0; k < 512; k++) {
ret[k] = ret[k+2244];
}
// Multiply by the hamming
q1 = a / 2;
q2 = q1 * 2;
if (q2 == a) {
for (i = 0; i < 256; i++) {
X2 = ret[i] * w[i] + x1[i + 256];
printf("%f\n",X2);
}
for (i = 256; i < 512; i++) {
x2[i] = ret[i] * w[i];
}
} else {
for (i = 0; i < 256; i++) {
X2 = ret[i] * w[i] + x2[i + 256];
printf("%f\n",X2);
}
for (i = 256; i < 512; i++) {
x1[i] = ret[i] * w[i];
}
}
l = (NW * L / 2);
L = L+1;
return 0;
}
MAIN_()
{ //This is again using Fox Pro but i am not sure
fft2c_(datafft,&expo,wdata);
return 0;
}
So,in all i just need code development in C++ to capture speech and play it back and a slight modification in my module to make it compatible.
If somebody can please help me out,I am even willing to pay for the project.
Thanks,
Student
-- modified at 21:21 Monday 24th October, 2005
|
|
|
|
|
There is a ton of source code online for fourier transforms, it's a common problem, and one that is well documented.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Ya, but could you help me out in implementing the code which I have written in real time ?
|
|
|
|
|
Real time ? I doubt it. I'm sure I could do it, but not in the timeframe that I could give to helping someone on a forum, sorry. You'd do better to look for a solution than hope for someone to dedicate time to writing this for you specifically.
If you're calling functions in Foxpro somehow, you'll definately gain speed from moving to C++.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I pulled the following lines from my C program, which captures video and audio, and saves it to a file.
I have an array of video and audio buffers, each 33.3 mSec (NTSC) or 40 mSec (PAL), and I capture to these in turn, wrapping round to the first when I finish the last buffer. When the user stops capture, the buffers can be stored as an AVI file with sound.
I have NOT done any audio output stuff, so I cannot give sample code there!
Use the code given below as hints - I found all of this on the MSDN CDs that came with Visual Studio 6. I have left out most of the error detection code.
uiNumWaveInDevices = waveInGetNumDevs();<br />
<br />
waveInGetDevCaps(0, &WaveInCaps, sizeof(WaveInCaps));
<br />
if (!(WaveInCaps.dwFormats & WAVE_FORMAT_1M16))<br />
<br />
AudioFormat.wFormatTag = WAVE_FORMAT_PCM;<br />
AudioFormat.nChannels = 1;
AudioFormat.nSamplesPerSec = 11025;
AudioFormat.nBlockAlign = 2;
AudioFormat.wBitsPerSample = 16;
AudioFormat.nAvgBytesPerSec = 11025 * 2;
AudioFormat.cbSize = 0;
<br />
hAudioInDevice = NULL;<br />
<br />
uiReturn = waveInOpen(&hAudioInDevice, 0, &AudioFormat, 0, 0, CALLBACK_NULL);<br />
if (uiReturn != MMSYSERR_NOERROR)<br />
<br />
uiAudioBufferMaxSize = AudioFormat.nSamplesPerSec * AudioFormat.nBlockAlign * AudioFormat.nChannels / 25;<br />
<br />
AudioHeader[uiCounter].lpData = GlobalLock(GlobalAlloc(GMEM_FIXED, uiAudioBufferMaxSize));<br />
<br />
for(uiCounter = 1; uiCounter<uiMaxNumOfFrames; uiCounter++)<br />
pAudioDataBlock[uiCounter] = pAudioDataBlock[0] + uiCounter * uiAudioBufferMaxSize;<br />
<br />
uiAudioBufferActualSize = uiAudioBufferMaxSize;<br />
for(uiCounter = 0; uiCounter<DEF_NumberAudioDeviceBuffers; uiCounter++)<br />
{<br />
AudioHeader[uiCounter].dwBufferLength = uiAudioBufferActualSize;<br />
AudioHeader[uiCounter].dwBytesRecorded = 0;<br />
AudioHeader[uiCounter].dwUser = 0;<br />
AudioHeader[uiCounter].dwFlags = 0;<br />
AudioHeader[uiCounter].dwLoops = 0;<br />
uiResult = waveInPrepareHeader(hAudioInDevice, &(AudioHeader[uiCounter]), sizeof(WAVEHDR));<br />
}<br />
<br />
<br />
for (i = 1 to 5)<br />
waveInAddBuffer(hAudioInDevice, &(AudioHeader[uiNextIndexToQueue]), sizeof(WAVEHDR))<br />
<br />
if (bThisIsFirstBuffer)<br />
waveInStart(hAudioInDevice);<br />
<br />
<br />
while (!(AudioHeader[uiAudioNextIndexToProcess].dwFlags & WHDR_DONE))<br />
;<br />
<br />
<br />
memcpy(pAudioDataBlock[uiNextIndexToProcess], AudioHeader[uiAudioNextIndexToProcess].lpData, uiAudioBufferActualSize);<br />
<br />
waveInAddBuffer(hAudioInDevice, &(AudioHeader[uiAudioNextIndexToQueue]), sizeof(WAVEHDR))<br />
<br />
<br />
waveInStop(hAudioInDevice);<br />
<br />
for(uiCounter = 0; uiCounter<DEF_NumberAudioDeviceBuffers; uiCounter++)<br />
{<br />
while (!(AudioHeader[uiCounter ].dwFlags & WHDR_DONE))<br />
;<br />
waveInUnprepareHeader(hAudioInDevice, &(AudioHeader[uiCounter]), sizeof(WAVEHDR)))<br />
GlobalFree(GlobalHandle(AudioHeader[uiCounter].lpData));<br />
}<br />
<br />
GlobalFree(GlobalHandle(pAudioDataBlock[0]));<br />
<br />
GlobalFree(GlobalHandle(pAudioDataBlock));<br />
<br />
waveInClose(hAudioInDevice);
|
|
|
|
|
I have a thread in high priority, which runs at certain peorid. it collect data and send to another thread which is responsible for display.
My question is what is a good way to impliment this. I know I can't use SendMessage() from HighPriorityThread, coz it blocks. if use PostMessage() to pass pointer and length, then the buffer could be overwriten by HighPriorityThread() before LowPriorityThread read the buffer.
May be a buffer with criticalSection control may solve the problem. but Could you give me other inspiration? I think MicroSoft should forsee this kind application.
Thanks
|
|
|
|
|
xianwuluo wrote: if use PostMessage() to pass pointer and length, then the buffer could be overwriten by HighPriorityThread() before LowPriorityThread read the buffer.
Just about there. Use new to allocate a new buffer every time, then delete it in the low priority thread after it has been read.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04
"There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05
Within you lies the power for good - Use it!
|
|
|
|
|
This is a definitely a simple way, I should have think of it. Thank you.
|
|
|
|
|
I would use criticalSections and Events.
I've used a circular que before to accomplish this. Each slot consisted of a buffer, allocated to a fixed size at start up (no more allocating required, unless user requested change at runtime). The lower priority thread checks for an event that says there is data in que (that can be read from the current head position). While the prioity thread can still be writing to the tail positon in the que (two seperate critical sections).
Basicaly, I automaticaly try to avoid thrashing memory by constantly allocating and deallocating memory. It slows things down and increases the chance that the a memory exception will occur. So if you can set a side memory for the sole purpose of communicating between your threads, you can avoid unexpected errors.
INTP
Every thing is relative...
|
|
|
|
|
Yes, I had thought this approach. Seems it's better than new and delete. I hate use new, coz u have to remember to delete it. u have to concern about memory leak or something. thank u.
|
|
|
|
|
I am trying to create and set evnets using Win32 API functions CreateEvent, SetEvent and OpenEvent.
Everything works fine if both projects are in the same language and environment (Visual C++) but when I try to create the event in a C++ application and set the event in a different application (VS.NET C#), the event does not exist.
Here is the code for both (c++ and c#):
C++
-----------------------------------------------------------------------------
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
// Get the handler to the event for which we need to wait in
// this thread.
HANDLE hEvent = CreateEvent ( EVENT_ALL_ACCESS, false, alse, "theEvent" );
cout<
|
|
|
|
|
Events are used within an application.
Use Mutexes.
CreateMutex(...)<br />
OpenMutex(...)<br />
ReleaseMutex(...)
this is this.
|
|
|
|
|
|
I have been too much lately.
I was confusing this with the Critical-section stuff.
Thanks for the correction.
this is this.
|
|
|
|
|
suezzz00 wrote: I suspect that the event that is created is not an OS event , but rather local to the running application.
No they are not. See my reply to khan++.
Your SetEvent PInvoke signature is wrong. You can use http://pinvoke.net[^] to get P/Invoke signatures for Win32 API functions.
The following code works fine for me.
C++
HANDLE h = CreateEvent(NULL, FALSE, FALSE, "TestEvent");
WaitForSingleObject(h, 5000);
cout << "past event";
C#
[DllImport("kernel32.dll")]
static extern bool SetEvent(IntPtr hEvent);
[DllImport("Kernel32.dll", SetLastError=true)]
static extern IntPtr OpenEvent(uint dwDesiredAccess, bool bInheritHandle, string lpName);
IntPtr handle = OpenEvent(0x000F0000 | 0x00100000 | 0x3, false, "TestEvent");
SetEvent(handle);
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Hi,
In my win32 app, I want to draw a line as the user moves the mouse (just as you wud do in paint).
how do I do this. I tried SetPixel(), but it's like drawing dotted line rather than a full line.
I want to use LineTo(), but each time do I have to store the previous mouse point into a global variable as the starting point for lineto().
Is there any other easier way to do this.
Thanks.
|
|
|
|
|
Ann66 wrote: I want to use LineTo(), but each time do I have to store the previous mouse point into a global variable as the starting point for lineto().
Yes, you need to use lineto. No, the variable does not have to be global, why would it ? It is a member of the class.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
hi man
It is most common thing while learining Drawing by mouse
U can refer the book "Mastering VC++" BPB pubilcations.
There is a good detailed example in VC++ , will also need
good C++ knowledge to understand that .
Thank you
Vikas Amin
Embin Technology
Bombay
vikas.amin@embin.com
|
|
|
|
|
Hello guys,
Is there any API, which will return the full path of the folder from where my application is running ?
Like GetModuleFilename() API , - but it returns the fullpath , including the filename.
thanks
"Success is the ability to go from one failure to another with no loss of enthusiasm." - W.Churchill
-- modified at 15:24 Monday 24th October, 2005
|
|
|
|
|
Hi
You can extract Path easily, Like
GetModuleFileName(NULL,m_CurrentPath.GetBuffer(512),512);<br />
m_CurrentPath.ReleaseBuffer();<br />
m_CurrentPath.Delete(m_CurrentPath.ReverseFind('\\'),m_CurrentPath.GetLength()-m_CurrentPath.ReverseFind('\\'));
Iman Ghasrfakhri
|
|
|
|
|
Ghasrfakhri wrote: You can extract Path easily, Like
I know , but I am interested if there is a API function which will do this
"Success is the ability to go from one failure to another with no loss of enthusiasm." - W.Churchill
|
|
|
|
|
PathRemoveFileSpec Function
Removes the trailing file name and backslash from a path, if it has them.
BOOL PathRemoveFileSpec(LPTSTR pszPath);
|
|
|
|
|
as Blake Miller explained, PathRemoveFileSpec() exists for that but hey, look, it certainly does what one presented you before (with the reverse string search), and calling it will only slow your prgram down because of an added function call...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
toxcct wrote: PathRemoveFileSpec() exists...and calling it will only slow your prgram down...
Slower compared to what?
"Take only what you need and leave the land as you found it." - Native American Proverb
|
|
|
|
|