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

📄 new.cpp

📁 蓝牙耳机音频测试的源码,FFT库是网上下的,该源码业内人士很有用处.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// New.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "stdio.h"
#include "resource.h"
#include "VolumeOutWave.h"

#include <fstream.h>
#include <time.h>
#include <process.h>

#define IDE_FREQ 3
#define IDE_FREQDURA 4
#define IDE_WAVEVOLUME 5
#define IDE_TESTTIME 6
#define IDB_START 0
#define IDB_STOP 1
#define IDB_EXIT 2
#define IDL_FREQLIST 7
#define IDL_DEVLIST 12
#define IDS_FREQ 8
#define IDS_FREQDURA 9
#define IDS_WAVEVOLUME 10
#define IDS_TESTTIME 11

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
HWND hGlobalWnd;
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];						// The title bar text

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

#define WAVEHORIZON 97
#define FREQHORIZON 380

#define MAX_INQUEU 8
#define BUFSIZE    1024
#define FREQSCALE 43.066
#define BACKGROUND RGB(236,233,216)
#define PASSCOLOR RGB(0,128,0)
#define FAILCOLOR RGB(255,0,0)

int FREQCHECK,FREQCHECKTIME,WAVEVOLUME,TESTTIME;

typedef struct{
			int Freq;
			int Wave;
			} FreqWave;

static FreqWave PreFreqWave;
static FreqWave CurrentFreqWave;
FreqWave PreFreqWaveArray[BUFSIZE/2];
FreqWave CurrentFreqWaveArray[BUFSIZE/2];

static HWND hwndMain;		//Main window handle
static HDC whdc;
//static HMODULE hMod;
static HPEN hpenB,hpenG,hpenW,hpenG2;
static HFONT font;
static HWAVEIN hwi;
static WAVEHDR *pwhi,whis[MAX_INQUEU];
static char waveBufferRecord[MAX_INQUEU][BUFSIZE];
static DWORD currWavHdr=0,nextWavHdr;
static BOOL b_playing;

WNDPROC GlobalProc[8] ;
int     idFocus ;

unsigned HardWareAudioThreadID,BlueToothAudioThreadID;
HANDLE hAudioThread;

ofstream MaxWaveLog;
ifstream MaxWaveLogInput;
int iMax;
time_t lngTimeBegin;
time_t lngTestBegin;

HWND hwndControl[7];

long lngTxWaveSum;
int iTxSum;
long lngRxWaveSum;
int iRxSum;

int iTxMax,iTxMin,iRxMax,iRxMin;

BOOL bolTestState;
BOOL bolAudioStart;
BOOL bolTxChecked;
BOOL bolRxChecked;
BOOL bolFreqResponseFileOpen;
BOOL bolFreqResponseRecording;
BOOL bolFreqResponseRecorded;

RECT rectDisplayFreq;
RECT rectFreqCheck;
RECT rectFreqResponseCheck;
RECT rectTest;

void RrectIni();
void SetSystemWaveVolume();
void DisplayTxPass(HDC hdc);
void InvalidDisplayTxPass(HDC hdc);
void DisplayRxPass(HDC hdc);
void InvalidDisplayRxPass(HDC hdc);
void DisplayTestFail(HDC hdc);
void InvalidTestFail(HDC hdc);
void ParaInit(int &FreqChecked,int &FreqDura,int &WaveVolume,int &TestTime);
void ParaRecord();
void SetWindowPara();
void GetWindowPara();
void FillDevices();


LRESULT CALLBACK ControlProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);


typedef void (__stdcall *PFNFFT)(unsigned int p_nSamples, bool p_bInverseTransform, 
    double *p_lpRealIn, double *p_lpImagIn,
    double *p_lpRealOut, double *p_lpImagOut);

#define DllImport extern "C" __declspec( dllimport )
DllImport UINT __stdcall StartAudio(int AudioDuration,UINT DevID,int freq,char amp,float phase);

//声明我的函数
DllImport void __stdcall fft_double(
	unsigned int p_nSamples, bool p_bInverseTransform, 
    double *p_lpRealIn, double *p_lpImagIn,
    double *p_lpRealOut, double *p_lpImagOut);


//unsigned __stdcall HardWareVideoThread ( void* pArguments)
void HardWareVideoThread ( void* pArguments)
{	
	ifstream InitFile;
	char LineHead[22];
	int temp,Para;
	
	InitFile.open("Amplify.dat");
	while(InitFile>>LineHead>>temp)
	{
		if (strcmp("Receive:",LineHead)==0)
			Para=temp;
	}
	InitFile.close();

	// DllExport UINT __stdcall StartAudio(int AudioDuration,UINT DevID,int freq,char amp,float phase);
	StartAudio(3,0,1000,Para,0);
	//return 0;
}

//unsigned __stdcall BlueToothVideoThread ( void* pArguments)
void BlueToothVideoThread ( void* pArguments)
{
	ifstream InitFile;
	char LineHead[22];
	int temp,Para;
	
	InitFile.open("Amplify.dat");
	while(InitFile>>LineHead>>temp)
	{
		if (strcmp("Transmit:",LineHead)==0)
			Para=temp;
	}
	InitFile.close();

	// DllExport UINT __stdcall StartAudio(int AudioDuration,UINT DevID,int freq,char amp,float phase);
	
	StartAudio(3,2,1000,Para,0);
	//StartAudio(3,0,1000,Para,0); //for debug
	//return 0;
}

//////////////////////////////////////////////////////////////////////

inline double GetFrequencyIntensity(double re, double im)
{
    return sqrt((re*re)+(im*im));
}

#define mag_sqrd(re,im) (re*re+im*im)

#define Decibels(re,im) ( (re == 0 && im == 0) ? (-100) : (20.0 * log10(double(mag_sqrd(re,im)))) )

#define Amplitude(re,im,len) (GetFrequencyIntensity(re,im)/(len))
#define AmplitudeScaled(re,im,len,scale) ((int)Amplitude(re,im,len)%scale)

//////////////////////////////////////////////////////////////////////


PFNFFT pfnFft;

void CALLBACK MasterWaveChanged( DWORD dwCurrentVolume, DWORD dwUserValue)
{
	//AfxMessageBox("Volume Changed!");
}

void GetMaxFreqWave(FreqWave InputFreqWave[BUFSIZE/2])
{
	int i,iMaxWave;
	iMax=0;
	iMaxWave=0;
	for (i=0;i<BUFSIZE/2;i++)
	{
		if (InputFreqWave[i].Wave>iMaxWave)
		{
			iMaxWave=InputFreqWave[i].Wave;
			iMax=i;
		}
	}

	if (iMax==1)
	{
		iMaxWave=(InputFreqWave[iMax].Wave+InputFreqWave[iMax+1].Wave)/2;
		CurrentFreqWave.Freq=iMax;
		CurrentFreqWave.Wave=iMaxWave;
		return;
	}
	
	if (iMax==BUFSIZE/2-1)
	{
		iMaxWave=(InputFreqWave[iMax-1].Wave+InputFreqWave[iMax].Wave)/2;
		CurrentFreqWave.Freq=iMax;
		CurrentFreqWave.Wave=iMaxWave;
		return;
	}

	if ( iMax>0 && iMax<BUFSIZE/2-1 )
	{
		iMaxWave=(InputFreqWave[iMax-1].Wave+InputFreqWave[iMax].Wave+InputFreqWave[iMax+1].Wave)/3;
		CurrentFreqWave.Freq=iMax;
		CurrentFreqWave.Wave=iMaxWave;
		return;
	}
}


static void waveInit(HWND hWnd,UINT uDevice,WORD FormatTag,WORD Channel,DWORD Samples,WORD Bits)
{
	WAVEFORMATEX wfx;
	long k;
	memset(&wfx,0,sizeof(WAVEFORMATEX));
	wfx.wFormatTag=WAVE_FORMAT_PCM;
	wfx.nChannels=Channel;
	wfx.wBitsPerSample=Bits;	
	wfx.nSamplesPerSec=Samples;
	wfx.nBlockAlign=Bits*Channel/8;
	wfx.nAvgBytesPerSec=Samples*Bits*Channel/8;
	wfx.cbSize=0;
	waveInOpen(&hwi,uDevice,&wfx,(DWORD)hWnd,0,CALLBACK_WINDOW|WAVE_MAPPED);
	for(k=0;k<MAX_INQUEU;k++)
	{
		pwhi=&whis[k];
		pwhi->dwFlags=0;
		pwhi->dwLoops=0;
		pwhi->dwBytesRecorded=0;
		pwhi->dwBufferLength=BUFSIZE;
		pwhi->lpData=waveBufferRecord[k];
	}
	for(k=0;k<MAX_INQUEU-1;k++)
	{
		pwhi=&whis[k];
		waveInPrepareHeader(hwi,pwhi,sizeof(WAVEHDR));
		waveInAddBuffer(hwi,pwhi,sizeof(WAVEHDR));
	}
	currWavHdr=0;
	pwhi=&whis[currWavHdr];
	b_playing=TRUE;
	waveInStart(hwi);
}

static void DrawWave(WAVEHDR *whdr,HDC hdc)
{
	static unsigned char prevBuf[BUFSIZE];
	static DWORD prevBytes=0;
	DWORD i,j;

	if (bolTestState==FALSE)
		return;

	/*hpenOld = (HPEN)::*/SelectObject(hdc, hpenB);
	
	MoveToEx(hdc,10,prevBuf[0],NULL);
	for(i=0;i<512;i++)
	{
		j=prevBuf[i];
	    LineTo(hdc,i+10,prevBuf[i]-30);
	}

	SelectObject(hdc, hpenW);

	MoveToEx(hdc,10,WAVEHORIZON,NULL);
	LineTo(hdc,522,WAVEHORIZON);           //horizon

	SelectObject(hdc, hpenG);

	MoveToEx(hdc,10,(unsigned char)(whdr->lpData[0])-30,NULL);

	for(i=0;i<512;i++)
	{
	    LineTo(hdc,i+10,(unsigned char)(whdr->lpData[i])-30);

	}

	memcpy(prevBuf,whdr->lpData,whdr->dwBytesRecorded);
	prevBytes=whdr->dwBytesRecorded;
}


static void DrawFreq(WAVEHDR *whdr,HDC hdc)
{
	static double prevFreqI[BUFSIZE];
	static double currFreqI[BUFSIZE];
	static double prevFreqR[BUFSIZE];
	static double currFreqR[BUFSIZE];
	double WaveR[BUFSIZE];
	double WaveI[BUFSIZE];

	time_t lngCurrentTime;

	char  CurFreq[8];
	char ListItem[42],ListItem1[12],ListItem2[10],ListItem3[10],ListItem4[10];
	DWORD i;


	SelectObject(hdc, hpenW);
	MoveToEx(hdc,10,380,NULL);
	LineTo(hdc,522,380);			//horizon

	for (i=0;i<22;i++)
	{
		MoveToEx(hdc,(int)(10+i*1000/FREQSCALE),380,NULL);
		LineTo(hdc,(int)(10+i*1000/FREQSCALE),200);
	}	

	if (bolTestState==FALSE)
		return;

	memset(WaveR,0,BUFSIZE*sizeof(double));
	memset(WaveI,0,BUFSIZE*sizeof(double));

	for(i=0;i<whdr->dwBytesRecorded;i++)
	{
	    WaveR[i]=(unsigned char)whdr->lpData[i]-128;
	}

	pfnFft(1024,0,WaveR,WaveI,currFreqR,currFreqI);

	/*hpenOld = (HPEN)::*/SelectObject(hdc, hpenB);
	MoveToEx(hdc,10,380,NULL);

	PreFreqWaveArray[i].Wave=0;
	CurrentFreqWaveArray[i].Wave=0;

	for(i=1;i<BUFSIZE/2;i++)
	{
		PreFreqWaveArray[i].Freq=i;
		PreFreqWaveArray[i].Wave=(int)sqrt((prevFreqR[i]*prevFreqR[i]+prevFreqI[i]*prevFreqI[i])/BUFSIZE);
		CurrentFreqWaveArray[i].Freq=i;
		CurrentFreqWaveArray[i].Wave=(int)sqrt((currFreqR[i]*currFreqR[i]+currFreqI[i]*currFreqI[i])/BUFSIZE);
	}
	
	for(i=0;i<BUFSIZE/2;i++)
	{
		LineTo(hdc,i+10,380-PreFreqWaveArray[i].Wave);
	}

	SelectObject(hdc, hpenG);
	MoveToEx(hdc,10,380,NULL);

	for(i=0;i<BUFSIZE/2;i++)
	{
		LineTo(hdc,i+10,380-CurrentFreqWaveArray[i].Wave);
	}	
		
	GetMaxFreqWave(CurrentFreqWaveArray);

	
	//////////////Display Freq////////////

	sprintf(CurFreq,"Frequency(Hz): %d     ",int(CurrentFreqWave.Freq*FREQSCALE));
	::SetTextColor(hdc,RGB(0,0,0));
	::SetBkColor(hdc,BACKGROUND);
	::DrawText(hdc,CurFreq,strlen(CurFreq),&rectDisplayFreq,DT_LEFT|DT_TOP|DT_INTERNAL);

	//////////////


	//========================================

	if (bolTxChecked==TRUE && bolRxChecked==TRUE)
	{
		bolTestState=FALSE;
		Sleep(500);
		EnableWindow(hwndControl[IDB_START],TRUE);
		return;
	}

	if (iMax==0  && bolFreqResponseFileOpen==FALSE && bolTxChecked==FALSE && bolRxChecked==FALSE)
	{
		MaxWaveLog.open("MaxWaveLog.dat");
		bolFreqResponseFileOpen=TRUE;

⌨️ 快捷键说明

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