weisct 发表于 2007-9-17 22:51

采集声卡信号,显示其频谱图,为什么程序在别的机器上没有正常运行?

在网上下的一个音频频谱实时显示程序(有源代码),它采集声卡信号,显示其频谱图,为什么程序在别的机器上没有正常运行?   

我在自己的电脑上编译运行该程序,可以正确运行,程序实时采集了声卡的信号并对其进行处理(包括fft变换等等),最后将其频谱图显示出来。但是换了台电脑试就不行了,程序能运行,但是频谱图没有显示,改了程序代码中的一些参数(比如采样频率),但还是不行。把那个程序的下载地址及其关键代码帖了出来,请大家帮忙看看,指点指点,谢谢!   

程序下载地址:   
http://www.pudn.com/downloads70/sourcecode/windows/other/detail251397.html   

程序关键代码:   

//全局变量   
CSoundInSoundIn;   
BOOLCSndApp::InitInstance()   
{   
   //......   
   SoundIn.OpenMic();   

   returnTRUE;   
}   
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;   
}   
MMRESULTCSoundIn::OpenMic()   
{   
         MMRESULTresult;   
         fr=newdouble;   
         fi=newdouble;   
         Pm=newdouble;   
         Am=newdouble;   
         Tm1=newdouble;   
         Tm2=newdouble;   
         Aver=newdouble;   
         Amave=newdouble;   
         for(inti=0;i <m_NbMaxSamples/2;i++)   
                     Amave=0;   
         result=waveInGetNumDevs();   
         if(result==0)   
         {   
               AfxMessageBox("NoSoundDevice");   
         returnresult;   
         }   

         //testforMicavailable         
         result=waveInGetDevCaps(0,&m_WaveInDevCaps,sizeof(WAVEINCAPS));   
      
         if(result!=MMSYSERR_NOERROR)   
       {   
             AfxMessageBox(_T("Cannotdeterminesoundcardcapabilities!"));   
       }   

       //TheSoundDeviveisOKnowwecancreateanEvent    andstarttheThread   
       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();   

       //initformat   
       WaveInitFormat(1/*mono*/,m_WaveInSampleRate/*khz*/,16/*bits*/);   

       //OpenInput   
       result=waveInOpen(&m_WaveIn,0,&m_WaveFormat,(DWORD)m_WaveInEvent,NULL,CALLBACK_EVENT);   
       if(result!=MMSYSERR_NOERROR)   
       {   
               AfxMessageBox(_T("CannotOpenSoundInputDevice!"));   
         returnresult;   
       }   

       //prepareheader   
       /*   
       typedefstruct{          LPSTR    lpData;            
                                       DWORD    dwBufferLength;   
                                     DWORD    dwBytesRecorded;      
                                     DWORD    dwUser;   
                                     DWORD    dwFlags;   
                                     DWORD    dwLoops;   
                                     structwavehdr_tag*lpNext;      
                                     DWORD    reserved;   
         }WAVEHDR;   
       */   
       m_SizeRecord=m_NbMaxSamples;   
       m_WaveHeader.lpData=(CHAR*)&InputBuffer;   
       m_WaveHeader.dwBufferLength=m_SizeRecord*2;   
       m_WaveHeader.dwFlags=0;   

       result=waveInPrepareHeader(m_WaveIn,&m_WaveHeader,   
                                                                   sizeof(WAVEHDR));   
       if((result!=MMSYSERR_NOERROR)    &brvbar;&brvbar;   
               (m_WaveHeader.dwFlags!=WHDR_PREPARED))   
       {   
               AfxMessageBox(_T("CannotPrepareHeader!"));   
         returnresult;   
       }   

       result=waveInAddBuffer(m_WaveIn,&m_WaveHeader,sizeof(WAVEHDR));   
       if    (result!=MMSYSERR_NOERROR)   
       {   
               AfxMessageBox(_T("CannotAddBuffer!"));   
         returnresult;   
       }   

       //alliscorrectnowwecanstarttheprocess   
       result=waveInStart(m_WaveIn);   
       if    (result!=MMSYSERR_NOERROR)   
       {   
               AfxMessageBox(_T("CannotStartWaveIn!"));   
         returnresult;   
       }   
       returnresult;   
}   

#definePT_S((CSoundIn*)pParam)   

UINTWaveInThreadProc(void*pParam)   
{   
       UINTresult;   
       UINTFirstPass=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();            //Toggleaschangedstatehere!Togglepointtothejustreceivedbuffer   
                   PT_S- >ComputeSamples(PT_S- >InputBuffer);   
         }   
         else   
                   return0;    //   
       }   
       return0;   
}   

voidCSoundIn::ComputeSamples(SHORT*pt)   
{   
         SYSTEMTIMEsystime;   
         GetSystemTime(&systime);   
         oldtim=systime.wMilliseconds;   
               inti;   
         intn=m_NbMaxSamples;   
            
         intfs=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=1.0;   
         for(i=0;i <n/2;i++)   
         {   
                     Pm=fr*fr+fi*fi;//计算功率谱   
                     Am=sqrt(Pm)/10000;   
                     if(i <n/4)   
                                 Aver+=Am;   
                     Amave=(Amave+Am)/2;   
         }   
         Aver/=(n/4);   
         GetSystemTime(&systime);   
         newtim=systime.wMilliseconds;   
         newtim-=oldtim;   
}      

voidCSndView::OnTimer(UINTnIDEvent)   
{   
          //......
          //关键的绘图代码   
         for    (x=0;x   <(rcClient.right);x++)    //displayInput   
         {      
                  y=3*rcClient.bottom/4-(int)SoundIn.Amave;   
                  dcMem.LineTo(x,y);                     
         }   
         dcMem.MoveTo(rcClient.right,3*rcClient.bottom/4-(int)SoundIn.Aver);   
         dcMem.LineTo(rcClient.right-20,3*rcClient.bottom/4-(int)SoundIn.Aver);   

         //......   
}
页: [1]
查看完整版本: 采集声卡信号,显示其频谱图,为什么程序在别的机器上没有正常运行?