?? audioapp.cpp
字號:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include <windows.h>
#include <mmsystem.h>
#include "CWaveFile.h"
#define MAX_FILENAME_LENGTH 200
#define WM_PLAY_WAVEFILE (WM_USER + 1)
#define WM_RECORD_WAVEFILE (WM_USER + 2)
#define WM_RECORD_STOP (WM_USER + 3)
#define WM_PLAY_STOP (WM_USER + 4)
#define WM_RECORD_PAUSE (WM_USER + 5)
#define WM_PLAY_PAUSE (WM_USER + 6)
#define WM_RECORD_FINISHED (WM_USER + 7)
#define WM_PLAYBACK_FINISHED (WM_USER + 8)
#define PLAY_BUTTON_ID 100
#define PLAY_EDITBOX_ID 101
#define PLAY_STOP_BUTTON_ID 102
#define PLAY_PAUSE_BUTTON_ID 103
#define RECORD_BUTTON_ID 104
#define RECORD_EDITBOX_ID 105
#define RECORD_STOP_BUTTON_ID 106
#define RECORD_PAUSE_BUTTON_ID 107
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static CWaveFile WaveFile;
static CWaveFile RecordFile;
static WAVEHDR wh1, wh2;
static WAVEHDR whr1, whr2; // the r is for recording
LPWAVEHDR lpwhdr;
static LPWAVEFORMATEX pwfx;
static LPWAVEFORMATEX pwfxRec;
static WAVEFORMATEX wfxRec;
MMRESULT mmRtn;
DWORD nBytes = 0;
static HRESULT hResult;
BOOL bRet = true;
static HWAVEOUT hwo;
static HWAVEIN hwi;
DWORD dwDeviceID = 0; // WAVE DEVICE ID to use
int n = 0; // hold number of wavedevices
static bool bIsPlaybackPaused = false;
static bool bIsRecordingPaused = false;
static bool bStopPlaying = false;
static bool bStopRecording = false;
char* data1 = NULL;
char* data2 = NULL;
char* playData1 = NULL;
char* playData2 = NULL;
static int nBufSize;
static int nPlaybackBufSize;
DWORD dwBytesRecorded = 0;
static DWORD dwBytesWritten;
static DWORD dwNumCaptureBuffers;
static DWORD dwNumPlaybackBuffers;
// GUI components
static HWND hwndPlayButton;
static HWND hwndPlayEditBox;
static HWND hwndPlayStopButton;
static HWND hwndPlayPauseButton;
static HWND hwndRecordButton;
static HWND hwndRecordEditBox;
static HWND hwndRecordStopButton;
static HWND hwndRecordPauseButton;
LPCTSTR pszWAVFilename = NULL;
int height;
int width;
switch(message)
{
case WM_CREATE:
height = GetSystemMetrics(SM_CYSCREEN);
width = GetSystemMetrics(SM_CXSCREEN);
// set up the Play Wave Button
hwndPlayButton = CreateWindow(TEXT("BUTTON"), // class name
TEXT("Play Wave"), // window name/text
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // style
10, // x
10, // y
75, // width
25, // height
hwnd, // hwndParent
(HMENU)PLAY_BUTTON_ID, // child window identifier
((LPCREATESTRUCT)lParam)->hInstance, // hInstance
NULL);
// check to make sure CreateWindow Didn't Fail
// set up the WaveFile Name Edit Box
hwndPlayEditBox = CreateWindow(TEXT("EDIT"), // class name
TEXT("release\\22050_16b_S.wav"), // window name/text
WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER, // style
100, // x
10, // y
200, // width
25, //height
hwnd, // hwndParent
(HMENU)PLAY_EDITBOX_ID, // child window identifier
((LPCREATESTRUCT)lParam)->hInstance, // hInstance
NULL);
// check to make sure CreateWindow Didn't Fail
// set up the Stop Playback Button
hwndPlayStopButton = CreateWindow(TEXT("BUTTON"), // class name
TEXT("Stop"), // window name/text
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // style
240, // x
100, // y
60, // width
25, //height
hwnd, // hwndParent
(HMENU)PLAY_STOP_BUTTON_ID, // child window identifier
((LPCREATESTRUCT)lParam)->hInstance, // hInstance
NULL);
// check to make sure CreateWindow Didn't Fail
// set up the Pause Playback Button
hwndPlayPauseButton = CreateWindow(TEXT("BUTTON"), // class name
TEXT("Pause"), // window name/text
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // style
420, // x
10, // y
60, // width
25, //height
hwnd, // hwndParent
(HMENU)PLAY_PAUSE_BUTTON_ID, // child window identifier
((LPCREATESTRUCT)lParam)->hInstance, // hInstance
NULL);
// check to make sure CreateWindow Didn't Fail
// set up the Record Wave Button
hwndRecordButton = CreateWindow(TEXT("BUTTON"), // class name
TEXT("Record Wave"), // window name/text
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // style
10, // x
45, // y
100, // width
25, // height
hwnd, // hwndParent
(HMENU)RECORD_BUTTON_ID, // child window identifier
((LPCREATESTRUCT)lParam)->hInstance, // hInstance
NULL);
// check to make sure CreateWindow Didn't Fail
// set up the Record to File Name Edit box
hwndRecordEditBox = CreateWindow(TEXT("EDIT"), // class name
TEXT("release\\MyRecording.wav"), // window name/text
WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER, // style
120, // x
45, // y
200, // width
25, //height
hwnd, // hwndParent
(HMENU)RECORD_EDITBOX_ID, // child window identifier
((LPCREATESTRUCT)lParam)->hInstance, // hInstance
NULL);
// check to make sure CreateWindow Didn't Fail
// set up the stop recording button
hwndRecordStopButton = CreateWindow(TEXT("BUTTON"), // class name
TEXT("Stop"), // window name/text
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // style
240, // x
145, // y
60, // width
25, //height
hwnd, // hwndParent
(HMENU)RECORD_STOP_BUTTON_ID, // child window identifier
((LPCREATESTRUCT)lParam)->hInstance, // hInstance
NULL);
// check to make sure CreateWindow Didn't Fail
// set up the Pause Recording Button
hwndRecordPauseButton = CreateWindow(TEXT("BUTTON"), // class name
TEXT("Pause"), // window name/text
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // style
420, // x
45, // y
60, // width
25, //height
hwnd, // hwndParent
(HMENU)RECORD_PAUSE_BUTTON_ID, // child window identifier
((LPCREATESTRUCT)lParam)->hInstance, // hInstance
NULL);
// check to make sure CreateWindow Didn't Fail
return 0;
break;
case WM_COMMAND:
if(LOWORD(wParam) == PLAY_BUTTON_ID && HIWORD(wParam) == BN_CLICKED )
{
// Play Wave button was clicked
// read the file name from the edit box and send the info in the WM_PLAY_WAVEFILE message
int iTextLength = GetWindowTextLength(hwndPlayEditBox);
if(0 == iTextLength)
{
// control has no text
MessageBox(hwnd, TEXT("Edit Box has no text. No WAV file to play."), TEXT("NotesApp"), MB_OK | MB_ICONSTOP);
return 0;
}
else if(MAX_FILENAME_LENGTH < iTextLength)
{
// filename exceeds max length of filename
MessageBox(hwnd, TEXT("File Name Length exceeds maximum."), TEXT("NotesApp"), MB_OK | MB_ICONSTOP);
return 0;
}
TCHAR szText[MAX_FILENAME_LENGTH];
//---- zero a buffer before using it
memset( szText, '\0', sizeof(szText)/sizeof(szText[0]) );
int iStringLength = GetWindowText(hwndPlayEditBox, szText, sizeof(szText)/sizeof(szText[0]));
if(0 == iStringLength)
{
// control has no text
MessageBox(hwnd, TEXT("Edit Box returned no text. No WAV file to play"), TEXT("NotesApp"), MB_OK | MB_ICONSTOP);
return 0;
}
// Disable the button so you can't press play again until we are done playing the file
EnableWindow(hwndPlayButton, false);
// Send message to the window
SendMessage(hwnd, WM_PLAY_WAVEFILE, iStringLength, (LPARAM)szText);
}
else if(LOWORD(wParam) == RECORD_BUTTON_ID && HIWORD(wParam) == BN_CLICKED )
{
// Record Wave button was clicked
// read the file name from the edit box and send the info in the WM_RECORD_WAVEFILE message
int iTextLength = GetWindowTextLength(hwndRecordEditBox);
if(0 == iTextLength)
{
// control has no text
MessageBox(hwnd, TEXT("Edit Box has no text. No WAV file to play."), TEXT("NotesApp"), MB_OK | MB_ICONSTOP);
return 0;
}
else if(MAX_FILENAME_LENGTH < iTextLength)
{
// filename exceeds max length of filename
MessageBox(hwnd, TEXT("File Name Length exceeds maximum."), TEXT("NotesApp"), MB_OK | MB_ICONSTOP);
return 0;
}
TCHAR szText[MAX_FILENAME_LENGTH];
//---- zero a buffer before using it
memset( szText, '\0', sizeof(szText)/sizeof(szText[0]) );
int iStringLength = GetWindowText(hwndRecordEditBox, szText, sizeof(szText)/sizeof(szText[0]));
if(0 == iStringLength)
{
// control has no text
MessageBox(hwnd, TEXT("Edit Box returned no text. No WAV file to play"), TEXT("NotesApp"), MB_OK | MB_ICONSTOP);
return 0;
}
// Disable the button so you can't press record again until we are done playing the file
EnableWindow(hwndRecordButton, false);
// Send message to the window
SendMessage(hwnd, WM_RECORD_WAVEFILE, iStringLength, (LPARAM)szText);
}
else if(LOWORD(wParam) == RECORD_STOP_BUTTON_ID && HIWORD(wParam) == BN_CLICKED)
{
// Stop Recording Button was clicked.
SendMessage(hwnd, WM_RECORD_STOP, NULL, NULL);
}
else if(LOWORD(wParam) == PLAY_STOP_BUTTON_ID && HIWORD(wParam) == BN_CLICKED)
{
// Stop Playing Button was clicked.
SendMessage(hwnd, WM_PLAY_STOP, NULL, NULL);
}
else if(LOWORD(wParam) == RECORD_PAUSE_BUTTON_ID && HIWORD(wParam) == BN_CLICKED)
{
// Pause Recording Button was clicked.
SendMessage(hwnd, WM_RECORD_PAUSE, NULL, NULL);
}
else if(LOWORD(wParam) == PLAY_PAUSE_BUTTON_ID && HIWORD(wParam) == BN_CLICKED)
{
// Pause Playing Button was clicked.
SendMessage(hwnd, WM_PLAY_PAUSE, NULL, NULL);
}
return 0;
break;
case WM_RECORD_WAVEFILE:
// Record to wavefile
// Record to the given filename
pszWAVFilename = (LPTSTR)lParam;
if(NULL == pszWAVFilename)
{
// no/invalid file name given
MessageBox(hwnd, TEXT("WAVE File Name was NULL"), TEXT("NotesApp"), MB_OK | MB_ICONSTOP);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -