📄 pitchdemo.cpp
字号:
GetScrollInfo (hWnd, SB_HORZ, &si);
switch (LOWORD (wParam)) {
case SB_LINELEFT:
si.nPos -= TIMEDIV;
break;
case SB_LINERIGHT:
si.nPos += TIMEDIV;
break;
case SB_PAGELEFT:
si.nPos -= si.nPage;
break;
case SB_PAGERIGHT:
si.nPos += si.nPage;
break;
case SB_THUMBTRACK:
si.nPos = si.nTrackPos;
break;
}
pos = SetScrollInfo(hWnd,SB_HORZ,&si,true);
RECT rect;
GetClientRect(hWnd,&rect);
InvalidateRect(hWnd,&rect,true);
UpdateWindow(hWnd);
DrawXY(hWnd);
DrawPitch(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
DestroyWindow(hDlg);
hwndAbout = NULL;
return TRUE;
}
break;
}
return FALSE;
}
HWND hwndRecordButton;
HWND hwndStopButton;
// Mesage handler for record box.
LRESULT CALLBACK RecordDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
HMENU hMenu;
RECT rcClient;
int size;
switch (message)
{
case WM_INITDIALOG:
// ShowScrollBar(MainHWND,SB_HORZ,false);
GetClientRect(hDlg, &rcClient);
size = (rcClient.right-60)/2;
hwndRecordButton = CreateWindow("BUTTON","Record", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
20,20,size,50,hDlg,NULL,(HINSTANCE) GetWindowLong(hDlg, GWL_HINSTANCE), NULL);
hwndStopButton = CreateWindow("BUTTON","Stop", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
size+40,20,size,50,hDlg,NULL,(HINSTANCE) GetWindowLong(hDlg, GWL_HINSTANCE), NULL);
EnableWindow(hwndStopButton,false);
hMenu = GetMenu( MainHWND );
EnableMenuItem( hMenu, ID_FILE_OPEN, MF_GRAYED);
EnableMenuItem( hMenu, ID_RECORD, MF_GRAYED);
Count=0;
RECT rect;
GetClientRect(MainHWND,&rect);
InvalidateRect(MainHWND,&rect,true);
UpdateWindow(MainHWND);
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == BN_CLICKED){
if(IsWindowEnabled(hwndRecordButton)){
EnableWindow(hwndRecordButton,false);
EnableWindow(hwndStopButton,true);
Count=0;
RECT rect;
GetClientRect(MainHWND,&rect);
InvalidateRect(MainHWND,&rect,true);
UpdateWindow(MainHWND);
// ShowScrollBar(MainHWND,SB_HORZ,false);
RecordSound(MainHWND);
return TRUE;
}else
if(IsWindowEnabled(hwndStopButton)){
EnableWindow(hwndRecordButton,true);
EnableWindow(hwndStopButton,false);
StopRecordSound(MainHWND);
return TRUE;
}
}
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
if(IsWindowEnabled(hwndStopButton))
StopRecordSound(MainHWND);
hMenu = GetMenu( MainHWND );
EnableMenuItem( hMenu, ID_FILE_OPEN, MF_ENABLED);
EnableMenuItem( hMenu, ID_RECORD, MF_ENABLED);
DestroyWindow(hwndRecordButton);
DestroyWindow(hwndStopButton);
EndDialog(hDlg, LOWORD(wParam));
hwndRec = NULL;
return TRUE;
}
break;
}
return FALSE;
}
void ProcessWaveFile(HWND hwnd){
OPENFILENAME file;
char FileName[256],filetitle[256];
FileName[0]='\0';
memset(&file,0,sizeof(OPENFILENAME));
file.Flags = OFN_FILEMUSTEXIST;
file.lStructSize = sizeof(OPENFILENAME);
file.hwndOwner = hwnd;
file.lpstrFilter = "Wave file\0 *.wav\0";
file.nFilterIndex = 1;
file.lpstrFile = FileName;
file.lpstrDefExt = "*.wav";
file.nMaxFile = 256;
file.lpstrFileTitle = filetitle;
file.nMaxFileTitle = 256;
file.lpstrInitialDir = ".\\";
Count=0;
RECT rect;
GetClientRect(hwnd,&rect);
InvalidateRect(hwnd,&rect,true);
UpdateWindow(hwnd);
GetOpenFileName(&file);
int LengthBuffer;
if(strlen(FileName)!=0){
short *pBuffer = ReadWave(FileName,&LengthBuffer);
if(pBuffer==NULL)MessageBox(hwnd,"Unsupported WAV format.\n16KHz wave file is required",NULL,MB_OK);
else {
int TrackNumber = (LengthBuffer)/SAMPLE_SHIFT;
if(pPitch.Frame)ippsFree(pPitch.Frame);
if(pPitch.Freq)ippsFree(pPitch.Freq);
pPitch.Frame = ippsMalloc_32s(TrackNumber);
pPitch.Freq = ippsMalloc_32f(TrackNumber);
Count = PitchDetector (pBuffer, LengthBuffer, &pPitch);
DrawPitch(hwnd);
ippsFree(pBuffer);
}
}
}
bool RecordSound(HWND hwnd){
sound = new Sound(16000);
MMRESULT wErr = sound->OpenSoundDevice();
if(wErr!=MMSYSERR_NOERROR){
TCHAR tstr[256];
waveInGetErrorText(wErr, tstr, sizeof(tstr));
MessageBox(hwnd,TEXT("Error in audio input stream buffer"),tstr,MB_OK);
return false;
}
if (sound->RecordSound()!=MMSYSERR_NOERROR){
MessageBox (hwnd,_T("Error recording sound"), _T("Error"), MB_OK | MB_ICONERROR);
}
return true;
}
bool StopRecordSound(HWND hwnd){
if (sound->StopRecordSound()!=MMSYSERR_NOERROR){
MessageBox (hwnd,_T("Error recording sound"), _T("Error"), MB_OK | MB_ICONERROR);
}
int LengthBuffer=0;
short *pBuffer = sound->GetDataBuffer(&LengthBuffer);
if(pBuffer==NULL)MessageBox(hwnd,"Error recording sound",NULL,MB_OK);
else {
int TrackNumber = (LengthBuffer)/SAMPLE_SHIFT;
if(pPitch.Frame)ippsFree(pPitch.Frame);
if(pPitch.Freq)ippsFree(pPitch.Freq);
pPitch.Frame = ippsMalloc_32s(TrackNumber);
pPitch.Freq = ippsMalloc_32f(TrackNumber);
Count = 0;
Count += PitchDetector (pBuffer, LengthBuffer, &pPitch);
DrawPitch(hwnd);
ippsFree(pBuffer);
}
delete(sound);
sound =false;
return true;
}
void DrawXY(HWND hWnd){
RECT rect;
GetClientRect(hWnd,&rect);
HDC pDC = GetDC(hWnd);
POINT point;
xcoor0 = 100;
ycoor0 = rect.bottom-100;
MoveToEx(pDC,xcoor0,0,&point);
LineTo(pDC,xcoor0,rect.bottom);
MoveToEx(pDC,0,ycoor0,&point);
LineTo(pDC,rect.right,ycoor0);
ReleaseDC(hWnd,pDC);
}
void DrawPitch(HWND hWnd){
int j,pr;
int x,y;
int begpos=0;
RECT rect;
if(Count){
float max,min;
ippsMin_32f(pPitch.Freq,Count,&min);
ippsMax_32f(pPitch.Freq,Count,&max);
HDC pDC = GetDC(hWnd);
POINT point;
char str[32];
GetClientRect(hWnd,&rect);
sprintf(str,"%f",min);
TextOut(pDC, 0, ycoor0, str, strlen(str));
int xsize = rect.right-xcoor0-100;
si.cbSize=sizeof(SCROLLINFO);
if(pPitch.Frame[Count-1]-pPitch.Frame[0]>xsize){
EnableScrollBar(hWnd,SB_HORZ,ESB_ENABLE_BOTH);
si.fMask=SIF_RANGE | SIF_PAGE;
GetScrollInfo(hWnd,SB_HORZ,&si);
if((int)si.nPage != xsize){
si.fMask=SIF_PAGE;
si.nPage = xsize;
SetScrollInfo(hWnd,SB_HORZ,&si,true);
}
if(((pPitch.Frame[Count-1]/TIMEDIV-pPitch.Frame[0]/TIMEDIV))!=(si.nMax-si.nMin)){
si.cbSize=sizeof(SCROLLINFO);
si.fMask=SIF_ALL;
si.nMin=pPitch.Frame[0]/TIMEDIV;
si.nMax=pPitch.Frame[Count-1]/TIMEDIV;
si.nPage=xsize;
si.nPos=0;
si.nTrackPos=0;
SetScrollInfo(hWnd,SB_HORZ,&si,true);
}
}
else
EnableScrollBar(hWnd,SB_HORZ,ESB_DISABLE_BOTH);
si.fMask=SIF_POS;
GetScrollInfo(hWnd,SB_HORZ,&si);
begpos = si.nPos*TIMEDIV;
GetClientRect(hWnd,&rect);
ycoor0 = rect.bottom-100;
int ysize = ycoor0-100;
float freqmult=1.0f;
if(fabs(max-min)>0.000001)
freqmult=1.0f / ( (max-min)/ysize);
y = ycoor0-(int)((max-min)*freqmult);
MoveToEx(pDC,xcoor0-10,y,&point);
LineTo(pDC,xcoor0+10,y);
sprintf(str,"%f",max);
TextOut(pDC, 0, y, str, strlen(str));
y = ycoor0-(int)((pPitch.Freq[0]-min)*freqmult);
MoveToEx(pDC,xcoor0,y,&point);
bool l=true;
pr=0;
for(j=1;j<Count;j++){
if( (int)pPitch.Frame[j] - (int)pPitch.Frame[pr]==TIMEDIV){
pr = j;
x = (int)pPitch.Frame[j]+xcoor0-begpos;
if(x < rect.right && x > xcoor0){
float avr = (float)(( pPitch.Freq[j-3]+pPitch.Freq[j-2]+pPitch.Freq[j-1]+pPitch.Freq[j])/4.0);
y = ycoor0-(int)((avr-min)*freqmult);
if(l)
LineTo(pDC,x,y);
else{
MoveToEx(pDC,x,y,&point);
l=true;
}
}
}else if((int)pPitch.Frame[j] - (int)pPitch.Frame[pr]>TIMEDIV){
l=false;
pr = j;
}
}
ReleaseDC(hWnd,pDC);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -