📄 pitchdemo.cpp
字号:
/*
//
// INTEL CORPORATION PROPRIETARY INFORMATION
// This software is supplied under the terms of a license agreement or
// nondisclosure agreement with Intel Corporation and may not be copied
// or disclosed except in accordance with the terms of that agreement.
// Copyright(c) 1999-2006 Intel Corporation. All Rights Reserved.
//
// Intel(R) Integrated Performance Primitives Speech Processing Sample for Windows*
//
// By downloading and installing this sample, you hereby agree that the
// accompanying Materials are being provided to you under the terms and
// conditions of the End User License Agreement for the Intel(R) Integrated
// Performance Primitives product previously accepted by you. Please refer
// to the file ippEULA.rtf located in the root directory of your Intel(R) IPP
// product installation for more information.
//
*/
// pitchdemo.cpp : Defines the entry point for the application.
//
#include <windows.h>
#include <windowsx.h>
#include "Commdlg.h"
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include "resource.h"
#include "pitchdemo.h"
#include "math.h"
#define TIMEDIV 8
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
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);
LRESULT CALLBACK RecordDialog(HWND, UINT, WPARAM, LPARAM);
int xcoor0;
int ycoor0;
_PitchStruct pPitch;
int Count=0;
Sound *sound=NULL;
HWND pScrollBar=NULL;
SCROLLINFO si;
int verif;
char vfilename[256],voutfilename[256];
short * ReadWave(char *filename,int *InputLength){
FILE *in = fopen(filename,"rb");
if (in==NULL){
printf("Error open file %s",filename);
return NULL;
}
fseek(in,0L,SEEK_END);
int Size = ftell(in);
fseek(in,24L,SEEK_SET);
int freq=-1;
fread(&freq,sizeof(int),1,in);
if (freq!=16000)return NULL;
fseek(in,56L,SEEK_SET);
*InputLength = (Size-56)>>1;
short *pInBuffer = ippsMalloc_16s((*InputLength));
fread(pInBuffer,sizeof(short),(*InputLength),in);
fclose(in);
return pInBuffer;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
verif=0;vfilename[0]='\0';voutfilename[0]='\0';
if(strlen(lpCmdLine)>0){
char str[256],*tmp;
sscanf(lpCmdLine,"%s",str);
verif=1;
if(strcmp("-v",str)==0)
verif=2;
tmp = &lpCmdLine[strlen(str)];
sscanf(tmp,"%s",vfilename);
tmp = &tmp[strlen(vfilename)+1];
sscanf(tmp,"%s",voutfilename);
}
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_PITCHDEMO, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_PITCHDEMO);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_PITCHDEMO);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_PITCHDEMO;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
int xcentre = GetSystemMetrics(SM_CXSCREEN)>>1;
int ycentre = GetSystemMetrics(SM_CYSCREEN)>>1;
int xsize = xcentre-50;
int ysize = ycentre-50;
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_VSCROLL,
xcentre-xsize, ycentre-ysize, xsize<<1, ysize<<1, NULL, NULL, hInstance, NULL);
/* hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_BORDER | WS_HSCROLL,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
*/
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
pPitch.Frame = NULL;
pPitch.Freq = NULL;
DrawXY(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
HWND hwndRec = NULL;
HWND hwndAbout = NULL;
HWND MainHWND = NULL;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent,pos;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
HMENU hMenu;
char FileName[256];
FILE *out;
int LengthBuffer,i;
switch (message)
{
case WM_CREATE:
hMenu = GetMenu( hWnd );
if(verif==2){
EnableMenuItem( hMenu, ID_FILE_OPEN, MF_GRAYED);
EnableMenuItem( hMenu, ID_RECORD, MF_GRAYED);
if(strlen(vfilename)>0)
strcpy(FileName,vfilename);
else
strcpy(FileName,".//test.wav\0");
if(strlen(FileName)!=0){
short *pBuffer = ReadWave(FileName,&LengthBuffer);
if(pBuffer==NULL)MessageBox(hWnd,FileName,"Error read file",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);
out=NULL;
if(strlen(voutfilename)>0)
out=fopen(voutfilename,"w");
else
out=fopen(".//out.log","w");
for(i=0;i<Count;i++)
fprintf(out,"%d %f\n",pPitch.Frame[i],pPitch.Freq[i]);
if(out)fclose(out);
Count = 0;
if(pBuffer)ippsFree(pBuffer);
pBuffer=NULL;
}
PostMessage(hWnd,WM_DESTROY,0,0);
}
}else if(verif==1)
MessageBox (hWnd,_T("Must be:-v <inputfile outfile>"), _T("Error command line parameter"), MB_OK);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case ID_FILE_OPEN:
ProcessWaveFile( hWnd );
break;
case ID_RECORD:
if (!IsWindow(hwndRec)) {
MainHWND = hWnd;
hwndRec = CreateDialog(hInst, MAKEINTRESOURCE(IDD_DIALOGRECORD),
hWnd, (DLGPROC)RecordDialog);
ShowWindow(hwndRec, SW_SHOW);
}
break;
case IDM_ABOUT:
if (!IsWindow(hwndAbout)) {
hwndAbout = CreateDialog(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX),
hWnd, (DLGPROC)About);
}
ShowWindow(hwndAbout, SW_SHOW);
// }
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
DrawXY(hWnd);
DrawPitch(hWnd);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
if(pPitch.Frame)ippsFree(pPitch.Frame);
pPitch.Frame=NULL;
if(pPitch.Freq)ippsFree(pPitch.Freq);
pPitch.Freq=NULL;
PostQuitMessage(0);
break;
case WM_HSCROLL:
si.fMask = SIF_ALL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -