马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
在网上下的一个音频频谱实时显示程序(有源代码),它采集声卡信号,显示其频谱图,为什么程序在别的机器上没有正常运行?
我在自己的电脑上编译运行该程序,可以正确运行,程序实时采集了声卡的信号并对其进行处理(包括fft变换等等),最后将其频谱图显示出 来。但是换了台电脑试就不行了,程序能运行,但是频谱图没有显示,改了程序代码中的一些参数(比如采样频率),但还是不行。把那个程 序的下载地址及其关键代码帖了出来,请大家帮忙看看,指点指点,谢谢!
程序下载地址:
http://www.pudn.com/downloads70/sourcecode/windows/other/detail251397.html
程序关键代码:
// 全局变量
CSoundIn SoundIn;
BOOL CSndApp::InitInstance()
{
//......
SoundIn.OpenMic();
return TRUE;
}
CSoundIn::CSoundIn()
{
oldtim=0;
newtim=0;
m_NbMaxSamples = 2048;
m_CalOffset = DEFAULT_CAL_OFFSET;
m_CalGain = DEFAULT_CAL_GAIN;
m_WaveInSampleRate = 44100;//11025;//
Tm1=Tm2=fr=fi=Pm=Am=Aver=NULL;
}
MMRESULT CSoundIn::OpenMic()
{
MMRESULT result;
fr=new double[m_NbMaxSamples];
fi=new double[m_NbMaxSamples];
Pm=new double[m_NbMaxSamples/2];
Am=new double[m_NbMaxSamples/2];
Tm1=new double[m_NbMaxSamples];
Tm2=new double[m_NbMaxSamples];
Aver=new double[m_NbMaxSamples/2];
Amave=new double[m_NbMaxSamples/2];
for(int i=0;i <m_NbMaxSamples/2;i++)
Amave=0;
result=waveInGetNumDevs();
if (result == 0)
{
AfxMessageBox("No Sound Device");
return result;
}
// test for Mic available
result=waveInGetDevCaps (0, &m_WaveInDevCaps, sizeof(WAVEINCAPS));
if ( result!= MMSYSERR_NOERROR)
{
AfxMessageBox(_T("Cannot determine sound card capabilities !"));
}
// The Sound Devive is OK now we can create an Event and start the Thread
m_WaveInEvent = CreateEvent(NULL,FALSE,FALSE,"WaveInThreadEvent");
m_TerminateThread = FALSE;
m_WaveInThread= AfxBeginThread(WaveInThreadProc,this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL);
m_WaveInThread- >m_bAutoDelete = TRUE;
m_WaveInThread- >ResumeThread();
// init format
WaveInitFormat(1/* mono*/,m_WaveInSampleRate /* khz */,16 /* bits */);
// Open Input
result = waveInOpen( &m_WaveIn,0, &m_WaveFormat,(DWORD)m_WaveInEvent ,NULL ,CALLBACK_EVENT);
if ( result!= MMSYSERR_NOERROR)
{
AfxMessageBox(_T("Cannot Open Sound Input Device!"));
return result;
}
// prepare header
/*
typedef struct { LPSTR lpData;
DWORD dwBufferLength;
DWORD dwBytesRecorded;
DWORD dwUser;
DWORD dwFlags;
DWORD dwLoops;
struct wavehdr_tag * lpNext;
DWORD reserved;
} WAVEHDR;
*/
m_SizeRecord = m_NbMaxSamples;
m_WaveHeader.lpData = (CHAR *)&InputBuffer[0];
m_WaveHeader.dwBufferLength = m_SizeRecord*2;
m_WaveHeader.dwFlags = 0;
result = waveInPrepareHeader( m_WaveIn, &m_WaveHeader,
sizeof(WAVEHDR) );
if ( (result!= MMSYSERR_NOERROR) ¦ ¦
( m_WaveHeader.dwFlags != WHDR_PREPARED) )
{
AfxMessageBox(_T("Cannot Prepare Header !"));
return result;
}
result = waveInAddBuffer( m_WaveIn, &m_WaveHeader, sizeof(WAVEHDR) );
if (result!= MMSYSERR_NOERROR)
{
AfxMessageBox(_T("Cannot Add Buffer !"));
return result;
}
// all is correct now we can start the process
result = waveInStart( m_WaveIn );
if (result!= MMSYSERR_NOERROR)
{
AfxMessageBox(_T("Cannot Start Wave In !"));
return result;
}
return result;
}
#define PT_S ((CSoundIn*)pParam)
UINT WaveInThreadProc(void * pParam)
{
UINT result;
UINT FirstPass = TRUE;
if ( FirstPass)
result = WaitForSingleObject(((CSoundIn*)pParam)- >m_WaveInEvent,INFINITE);
FirstPass = FALSE;
while (!((CSoundIn*)pParam)- >m_TerminateThread)
{
result = WaitForSingleObject(((CSoundIn*)pParam)- >m_WaveInEvent,INFINITE);
if ((result == WAIT_OBJECT_0)&&(!((CSoundIn*)pParam)- >m_TerminateThread ))
{
PT_S- >AddBuffer(); // Toggle as changed state here !Toggle point to the just received buffer
PT_S- >ComputeSamples(PT_S- >InputBuffer);
}
else
return 0; //
}
return 0;
}
void CSoundIn::ComputeSamples(SHORT *pt)
{
SYSTEMTIME systime;
GetSystemTime(&systime);
oldtim=systime.wMilliseconds;
int i;
int n=m_NbMaxSamples;
int fs=m_WaveInSampleRate;//11025;//500;//22050
for(i=0;i <n;i++)
{
if(i <n)
fr=pt*(1.0/exp(18*((n)/2.0-i)*((n)/2.0-i)/(n*n)));
else
fr=0;
fi=0;
}
FFT(fr,n,0);
Aver[0]=1.0;
for(i=0;i <n/2;i++)
{
Pm=fr*fr+fi*fi;//计算功率谱
Am=sqrt(Pm)/10000;
if(i <n/4)
Aver[0]+=Am;
Amave=(Amave+Am)/2;
}
Aver[0]/=(n/4);
GetSystemTime(&systime);
newtim=systime.wMilliseconds;
newtim-=oldtim;
}
void CSndView::OnTimer(UINT nIDEvent)
{
//......
// 关键的绘图代码
for (x =0 ; x < (rcClient.right); x++) // display Input
{
y = 3*rcClient.bottom/4 - (int)SoundIn.Amave[x];
dcMem.LineTo(x,y);
}
dcMem.MoveTo(rcClient.right,3*rcClient.bottom/4 - (int)SoundIn.Aver[0]);
dcMem.LineTo(rcClient.right-20,3*rcClient.bottom/4 - (int)SoundIn.Aver[0]);
//......
} |