📄 btplay.cxx
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
#include <windows.h>
#include <bt_api.h>
#include <dsound.h>
#include "wavefile.h"
HANDLE g_hCommPort = 0;
#define MRCHECK(r,str,label)\
if ((r != MMSYSERR_NOERROR)) { wprintf(L#str L" failed. mr=%08x\r\n", r); mr = r; goto label;}
#undef wsprintf
#define wsprintf(x) ; // noop
MMRESULT
PlayFile(LPCTSTR pszFilename)
{ MMRESULT mr;
DWORD dwBufferSize;
PBYTE pBufferBits = NULL;
PWAVEFORMATEX pwfx = NULL;
DWORD dwSlop;
DWORD dwWait;
DWORD dwDuration;
HANDLE hevDone = CreateEvent(NULL, FALSE, FALSE, NULL);
if (hevDone == NULL) {
return MMSYSERR_NOMEM;
}
mr = ReadWaveFile(pszFilename,&pwfx,&dwBufferSize,&pBufferBits);
MRCHECK(mr, ReadWaveFile, ERROR_READ);
dwDuration = dwBufferSize * 1000 / pwfx->nSamplesPerSec;
wprintf(L"\"%s\" %c%02d %5dHz %6d bytes %5d ms\r\n"
, pszFilename
, pwfx->nChannels == 2 ? L'S' : L'M'
, pwfx->wBitsPerSample
, pwfx->nSamplesPerSec
, dwBufferSize
, dwDuration
);
HWAVEOUT hwo;
mr = waveOutOpen(&hwo, 1, pwfx, (DWORD) hevDone, NULL, CALLBACK_EVENT);
MRCHECK(mr, waveOutOpen, ERROR_OPEN);
WAVEHDR hdr;
memset(&hdr, 0, sizeof(hdr));
hdr.dwBufferLength = dwBufferSize;
hdr.lpData = (char *) pBufferBits;
mr = waveOutPrepareHeader(hwo, &hdr, sizeof(hdr));
MRCHECK(mr, waveOutPrepareHeader, ERROR_PLAY);
mr = waveOutWrite(hwo, &hdr, sizeof(hdr));
MRCHECK(mr, waveOutWrite, ERROR_PLAY);
dwSlop = dwDuration + 5;
dwWait = WaitForSingleObject(hevDone, dwDuration + dwSlop);
if (dwWait != WAIT_OBJECT_0) {
// not much to here, other than issue a warning
wprintf(L"Timeout waiting for playback to complete\r\n");
}
mr = waveOutUnprepareHeader(hwo, &hdr, sizeof(hdr));
MRCHECK(mr, waveOutUnprepareHeader, ERROR_PLAY);
ERROR_PLAY:
mr = waveOutClose(hwo);
MRCHECK(mr, waveOutClose, ERROR_OPEN);
ERROR_OPEN:
delete [] pBufferBits;
delete [] pwfx;
ERROR_READ:
CloseHandle(hevDone);
return mr;
}
/*
DWORD KeepAclAliveThreadProc(LPVOID lpParameter)
{
BT_ADDR bt = {0};
DWORD dwSizeOut = 0;
DeviceIoControl (g_hCommPort, IOCTL_BLUETOOTH_GET_PEER_DEVICE, NULL, 0, &bt, sizeof(bt), &dwSizeOut, NULL);
DWORD dwActual;
DWORD dwTotal = 0;
BYTE buffer[100];
BOOL bSuccess;
BYTE ch = 0;
while (1)
{
Sleep(50);
buffer[0] = ch++;
bSuccess = WriteFile(g_hCommPort, (LPVOID)buffer, 1, &dwActual, NULL);
if (!bSuccess) {
wprintf(L" ** WriteFile failure %d (dwTotal = %d)\n", GetLastError(), dwTotal);
return 0;
}
dwTotal += dwActual;
}
return 0;
}
*/
int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR lpCmdLine, int nShowCmd)
{
WAVEFORMATEX wfx;
DWORD dwBefore, dwAfter;
if (lpCmdLine == NULL || *lpCmdLine == 0) {
wprintf(L"Usage: btplay filename.wav\n");
return 1;
}
// set up the wave format structure
wfx.cbSize = 0;
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.wBitsPerSample = 8;
wfx.nSamplesPerSec = 8000;
wfx.nChannels = 1;
wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
dwBefore = GetTickCount();
// we're all set: go do the real work
PlayFile(lpCmdLine);
dwAfter = GetTickCount();
wprintf(L"Ran for %d ms\n", dwAfter - dwBefore);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -