📄 abdlg.cpp
字号:
// ABDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AB.h"
#include "ABDlg.h"
#include "Mmsystem.h"
#include<cmath> // double pow(double, double);
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//float m_nData[4800];
float g_nData[10000000];
int i;
int x,y;
// int y,y0,x,ymax=200,ymin=200;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
int N_Buffer=0;
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
int num; // 样本数
LONG* data; // 样本数据
LONG *Ldata, *Rdata; // 双声道数据
// 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)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CABDlg dialog
CABDlg::CABDlg(CWnd* pParent /*=NULL*/)
: CDialog(CABDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CABDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
bRecording=FALSE;
bPlaying=FALSE;
bReverse=FALSE;
bPaused=FALSE;
bEnding=FALSE;
bTerminating=FALSE;
dwDataLength=0;
dwRepetitions=1;
}
void CABDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CABDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CABDlg, CDialog)
//{{AFX_MSG_MAP(CABDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_REC_START, OnRecStart)
ON_BN_CLICKED(IDC_REC_STOP, OnRecStop)
ON_BN_CLICKED(IDC_PLAY_START, OnPlayStart)
ON_BN_CLICKED(IDC_PLAY_PAUSE, OnPlayPause)
ON_BN_CLICKED(IDC_PLAY_STOP, OnPlayStop)
ON_WM_DESTROY()
ON_WM_TIMER()
ON_MESSAGE(MM_WIM_OPEN,OnMM_WIM_OPEN)
ON_MESSAGE(MM_WIM_DATA,OnMM_WIM_DATA)
ON_MESSAGE(MM_WIM_CLOSE,OnMM_WIM_CLOSE)
ON_MESSAGE(MM_WOM_OPEN,OnMM_WOM_OPEN)
ON_MESSAGE(MM_WOM_DONE,OnMM_WOM_DONE)
ON_MESSAGE(MM_WOM_CLOSE,OnMM_WOM_CLOSE)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CABDlg message handlers
BOOL CABDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
pWaveHdr1=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
pWaveHdr2=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
pSaveBuffer = reinterpret_cast<PBYTE>(malloc(1));
return TRUE; // return TRUE unless you set the focus to a control
}
void CABDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CABDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CRect rect;
CPaintDC dc(this);
GetClientRect(rect);
dc.FillSolidRect(rect,RGB(0,128,255)); //设置背景颜色
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CABDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CABDlg::OnRecStart()
{
// TODO: Add your control notification handler code here
pBuffer1=(PBYTE)malloc(INP_BUFFER_SIZE);
pBuffer2=(PBYTE)malloc(INP_BUFFER_SIZE);
//*pBuffer1=malloc(INP_BUFFER_SIZE);
// *pBuffer2=malloc(INP_BUFFER_SIZE);
if (!pBuffer1 || !pBuffer2)
{
if (pBuffer1) free(pBuffer1);
if (pBuffer2) free(pBuffer2);
MessageBeep(MB_ICONEXCLAMATION);
AfxMessageBox("Memory erro!");
return ;
}
//open waveform audo for input
waveform.wFormatTag=WAVE_FORMAT_PCM;
waveform.nChannels=1;
waveform.nSamplesPerSec=11025;
waveform.nAvgBytesPerSec=11025;
waveform.nBlockAlign=1;
waveform.wBitsPerSample=8;
waveform.cbSize=0;
//WaveInStart启动波形输入设备,即可开始进行数据采集,在采集的过程中,一旦有缓冲区被采样数据填满,
//系统就回调WaveInOpen中指定的dwCallback函数
//
// PBYTE pBuffer1,pBuffer2; //保存输入数据的两个缓冲区
if (waveInOpen(&hWaveIn,WAVE_MAPPER,&waveform,(DWORD)this->m_hWnd,NULL,CALLBACK_WINDOW))
{
free(pBuffer1);
free(pBuffer2);
MessageBeep(MB_ICONEXCLAMATION);
AfxMessageBox("Audio can not be open!");
}
//在Windows环境,可以用GlobalAllocPtr来获取一段内存空间,但是由于Windows操作系统采用了虚拟存储管理机制,
//这块内存空间随时有可能会被置换到硬盘上,读写硬盘所耗费的时间会造成采样的不连续。
//因此,在将缓冲区送往波形输入设备之前,必须调用WaveInPrepareHeader函数以保证缓冲区不会被置换到硬盘上。
//当然在用GlobalFreeP tr来释放缓冲区之前,必须先要用WaveInUnprepareHeader函数来解除这种保护。
pWaveHdr1->lpData=(LPTSTR)pBuffer1;
pWaveHdr1->dwBufferLength=INP_BUFFER_SIZE;
pWaveHdr1->dwBytesRecorded=0;
pWaveHdr1->dwUser=0;
pWaveHdr1->dwFlags=0;
pWaveHdr1->dwLoops=1;
pWaveHdr1->lpNext=NULL;
pWaveHdr1->reserved=0;
waveInPrepareHeader(hWaveIn,pWaveHdr1,sizeof(WAVEHDR));//保证缓冲区不会被置换到硬盘上
pWaveHdr2->lpData=(LPTSTR)pBuffer2;
pWaveHdr2->dwBufferLength=INP_BUFFER_SIZE;
pWaveHdr2->dwBytesRecorded=0;
pWaveHdr2->dwUser=0;
pWaveHdr2->dwFlags=0;
pWaveHdr2->dwLoops=1;
pWaveHdr2->lpNext=NULL;
pWaveHdr2->reserved=0;
waveInPrepareHeader(hWaveIn,pWaveHdr2,sizeof(WAVEHDR)); //保证缓冲区不会被置换到硬盘上
//////////////////////////////////////////////////////////////////////////
pSaveBuffer = (PBYTE)realloc (pSaveBuffer, 1) ;
// //pSaveBuffer是外部定义的一个临时缓存
// pSaveBuffer = (int)realloc (pSaveBuffer, 1) ;
// Add the buffers
waveInAddBuffer (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ;//waveInAddBuffer :向声音输入设备发送缓冲区,双缓存轮换采集
waveInAddBuffer (hWaveIn, pWaveHdr2, sizeof (WAVEHDR)) ;//双缓存轮换采集
// Begin sampling
bRecording = TRUE ;
bEnding = FALSE ;
dwDataLength = 0 ;
waveInStart (hWaveIn) ; //waveInStart :开始声音输入设备工作
}
void CABDlg::OnRecStop()
{
// TODO: Add your control notification handler code here
TRACE("rec stop \n");
bEnding=TRUE;
//Sleep(1500);
waveInReset(hWaveIn);//waveInReset :停止声音输入设备工作
}
void CABDlg::draw()
{
//////////////////////////////////////////////////////
//画曲线
static const int xCon = 13;
static const int yCon = 150;
static const int wCon = 1000;
static const int hCon = 400;
int y0,ymax=200,ymin=200;
CClientDC dc(this);
CBitmap Bitmap;
CBitmap * pbmOld = NULL;
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
Bitmap.CreateCompatibleBitmap(&dc,wCon,hCon);
// CreateCompatibleBitmap函数功能:该函数创建与指定的设备环境相关的设备兼容的位图。
// 函数原型:HBITMAP CreateCompatibleBitmap(HDC hdc,int nWidth,int nHeight);
// 参数:
// hdc: 设备环境句柄。
// nWidth:指定位图的宽度,单位为像素。
// nHeight:指定位图的高度,单位为像素
pbmOld = dcMem.SelectObject(&Bitmap);
dcMem.PatBlt(0,0,wCon,hCon, WHITENESS);
// PatBlt函数功能:该函数使用当前选入指定设备环境中的刷子绘制给定的矩形区域。通过使用给出的光栅操作来对该刷子的颜色和表面颜色进行组合。
// 函数原型:BOOL PatBlt(HDC hdc, int nXLeft, int nYLeft, int nWidth, int nHeight, DWORD dwRop);
// 参数:
// hdc:设备环境句柄。
// nXLeft:指定要填充的矩形左上角的X轴坐标,坐标按逻辑单位表示。
// nYLeft:指定要填充的矩形左上角的Y轴坐标,坐标按逻辑单位表示。
// nWidth:指定矩形的宽度,按逻辑单位表示宽度。
// nHeight:指定矩形的高度,按逻辑单位表示高度。
// dwRop:指定光栅操作码。
y0=yCon+150;
dcMem.MoveTo(-20,y0);
// display incomming signal--key idea!
if(N_Buffer==0)
{
pdraw=pBuffer1;
N_Buffer=1;}
else{
pdraw=pBuffer2;
N_Buffer=0;
}
for(x =4 ; x < 2048; x++) // display Input
{
y=2*pdraw[x]; //pBuffer1[x];/* if (y > ymax) { ymax = y; } if (y < ymin) { ymin = y; }*/
dcMem.LineTo(x/4, (y0-y*0.5));
}
// y0=yCon+150;
y0=yCon+150;// dcMem.MoveTo(0,y0);
for(x =0 ; x < 2048; x++) // display Input
{
y=2*pdraw[x+2048];//pBuffer1[x+2048];
if (y > ymax)
{
ymax = y;
}
if (y < ymin)
{
ymin = y;
}
dcMem.LineTo(512+x/4, (y0-y*0.5));
}
CString str;
str.Format("%d",ymax);
dcMem.TextOut(600,200,str);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -