⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vib.cpp

📁 声音处理程序,通过FFT可以看声音的频谱.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Vib.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "Vib.h"

//#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CVibApp

BEGIN_MESSAGE_MAP(CVibApp, CWinApp)
	//{{AFX_MSG_MAP(CVibApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVibApp construction

CVibApp::CVibApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}


/////////////////////////////////////////////////////////////////////////////
// The one and only CVibApp object

CVibApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CVibApp initialization

BOOL CVibApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	//SetRegistryKey(_T("Local AppWizard-Generated Applications"));
	LoadStdProfileSettings();
	CString str;
	::GetModuleFileName(NULL,str.GetBufferSetLength(255),255);
	str.ReleaseBuffer();
	str=str.Left(str.ReverseFind('.')+1)+"INI";
	free((void*)m_pszProfileName);
	m_pszProfileName=_tcsdup(str);
	
	m_nFFTCnt=0;
	m_nFFTIn=0;
	m_pFFT=NULL;

	//初始化参数
	DefautConfig();
	LoadConfig();
	// To create the main window, this code creates a new frame window
	// object and then sets it as the application's main window object.

	CMainFrame* pFrame = new CMainFrame;
	m_pMainWnd = pFrame;

	// create and load the frame with its resources

	pFrame->LoadFrame(IDR_MAINFRAME,
		WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
		NULL);

	// The one and only window has been initialized, so show and update it.
	pFrame->ShowWindow(SW_SHOW);
	pFrame->UpdateWindow();

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CVibApp message handlers





/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
		// No message handlers
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CVibApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CVibApp message handlers


int CVibApp::ExitInstance() 
{
	// TODO: Add your specialized code here and/or call the base class
	SaveConfig();
	if(m_pFFT)delete[]m_pFFT;
	return CWinApp::ExitInstance();
}

double* CVibApp::FFT(double aInData[],int nInCount,double aOutData[],int nOutCount)
{
	int i,j,k,b,p,nBits;
	double dr,di,dValue;
	if(m_nFFTCnt<nInCount)
	{
		if(m_pFFT)delete[] m_pFFT;
		m_nFFTCnt=nInCount;
		m_pFFT=new double[m_nFFTCnt*4];
	}
	double* aDataI=m_pFFT+0*nInCount;
	double* aDataR=m_pFFT+1*nInCount;
	double* aSin=m_pFFT+2*nInCount;
	double* aCos=m_pFFT+3*nInCount;
	if(m_nFFTIn!=nInCount)
	{
		m_nFFTIn=nInCount;
		dValue=MATH_PI*2/nInCount;
		for(i=0;i<nInCount;i++)
		{
			aSin[i]=sin(i*dValue);
			aCos[i]=cos(i*dValue);
		}
	}

	for(i=0;i<32;i++)
	{
		if(nInCount & (1<<i))
		{
			nBits=i;
			break;
		}
	}
	for(i=0;i<nInCount;i++)
	{
		k=(i&1)?1:0;
		for(j=1;j<nBits;j++)k=(k<<1)+((i&(1<<j))?1:0);
		aDataR[k]=aInData[i];
		aDataI[i]=0;
	}

	for(i=1;i<=nBits;i++)
	{
		b=1<<(i-1); // b= 2^(i-1)
		for(j=0;j<b;j++)
		{
			p=(1<<(nBits-i))*j; /* p=pow(2,nBit-i)*j; */
			for(k=j;k<nInCount;k+=2*b)
			{
				dr=aDataR[k];
				di=aDataI[k]; 
				dValue=aDataR[k+b];
				aDataR[k]+=aDataI[k+b]*aSin[p]+aDataR[k+b]*aCos[p];
				aDataI[k]+=aDataI[k+b]*aCos[p]-aDataR[k+b]*aSin[p];
				aDataR[k+b]=dr-aDataR[k+b]*aCos[p]-aDataI[k+b]*aSin[p];
				aDataI[k+b]=di+dValue*aSin[p]-aDataI[k+b]*aCos[p];
			}
		}
	}

	dValue=4.0/(nInCount*nInCount);
	for(i=0;i<nOutCount;i++)
	{
		aOutData[i]=(aDataR[i]*aDataR[i]+aDataI[i]*aDataI[i])*dValue;
	}
	aOutData[0]/=4;

	return m_pFFT;
}

void CVibApp::ReadWave(PSHORT pData,int nSize)
{
	int i,j,k,p;
	double a,b,dSum;
	nSize=min(m_nFFTPoint,nSize);
	if(m_nFilterCnt)
	{
		for(i=0;i<nSize;i++)
		{
			dSum=0;
			for(p=m_nFilterCnt-1;p>=0;p--)
			{
				j=i-p;
				a=j<0?m_aData[m_nFilterCnt+j]:pData[j];
				a=(m_bModulate && a<0)?-a:a;
				dSum+=a*m_aFilter[p];
			}
			m_aFFTIn[i]=dSum;
		}
		CopyMemory(m_aData,pData,nSize*sizeof(short));
	}
	else
	{
		for(i=0;i<nSize;i++)
		{
			a=pData[i];
			m_aFFTIn[i]=(m_bModulate && a<0)?-a:a;
		}
	}

	p=m_nDispMaxFreq*m_msWinFFT/1000+1;
	p=min(p,m_nFFTPoint/2);
	FFT(m_aFFTIn,m_nFFTPoint,m_aFFTOut,p);

	a=m_dFreqHalfLive;
	b=1-a;
	if(m_nFreqValue==2)
	{
		dSum=MATH_PI*2000/m_msWinFFT;dSum*=dSum;dSum*=dSum;
		for(i=1;i<p;i++)m_aFFTOut[i+p]=a*m_aFFTOut[i+p]+b*m_aFFTOut[i]/(dSum*i*i*i*i);
	}
	else if(m_nFreqValue==1)
	{
		dSum=MATH_PI*2000/m_msWinFFT;dSum*=dSum;
		for(i=1;i<p;i++)m_aFFTOut[i+p]=a*m_aFFTOut[i+p]+b*m_aFFTOut[i]/(dSum*i*i);
	}
	else
	{
		for(i=0;i<p;i++)m_aFFTOut[i+p]=a*m_aFFTOut[i+p]+b*m_aFFTOut[i];
	}
	k=m_nFreqBlur*m_msWinFFT/1000;
	if(k>0)
	{
		a=0;
		dSum=0;
		for(i=0;i<p;i++)
		{
			m_aFFTOut[i]=0;
			dSum=m_aFFTOut[i+p];
			for(j=1;j<k;j++)
			{
				a=(i>=j?m_aFFTOut[i+p-j]:0)+(i+j<p?m_aFFTOut[i+p+j]:0);
				dSum+=m_aFilter[m_nFFTPoint-j]*a;
			}
			m_aFFTOut[i]=dSum;
		}
	}
	else
	{
		for(i=0;i<p;i++)m_aFFTOut[i]=m_aFFTOut[i+p];
	}

	if(m_bEnvelopSense)
	{
		a=0;b=0;dSum=0;
		for(i=0;i<p;i++)
		{
			m_aPeak[i]=max(m_aPeak[i],m_aFFTOut[i]);
			a+=m_aFFTOut[i]*m_aEnvelop[i];
			b+=m_aPeak[i]*m_aEnvelop[i];
			dSum+=m_aEnvelop[i];
		}
		a=dSum>0?a/dSum:0;
		m_nCurVibration=int(sqrt(a)+0.5);
		b=dSum>0?b/dSum:0;
		m_nPeakVibration=int(sqrt(b)+0.5);
	}
	else
	{
		j=0;a=0;b=0;
		for(i=0;i<p;i++)
		{
			m_aPeak[i]=max(m_aPeak[i],m_aFFTOut[i]);
			if(m_aEnvelop[i]>0)
			{
				dSum=m_aFFTOut[i]-m_aEnvelop[i];
				dSum=dSum>0?dSum/m_aEnvelop[i]:0;
				a+=min(1,dSum);
				j++;
				dSum=m_aPeak[i]-m_aEnvelop[i];
				dSum=dSum>0?dSum/m_aEnvelop[i]:0;
				b+=min(1,dSum);
			}
		}
		a=j>0?a/j:0;
		m_nCurVibration=int(ceil(a*100));
		b=j>0?b/j:0;
		m_nPeakVibration=int(ceil(b*100));
	}
}

void CVibApp::DrawWave(CDC *pDC, CRect &rectClient)
{
	int x,y,i,p,y0;
	int c,p0,f0;
	double dScaleX,dScaleY;
	RECT rect;

	static CDC mdc;
	static CBitmap bmp;
	static CFont font;
	static int cx=0,cy=0,nMaxFreq=0;
	static int nSubGrids,nMainSubs;
	static double dFreqTo;
	static CString str;
	static SIZE sizeText;
	static CPen penEnvelop;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -