UMIC(开发人员)
+
-
自定义HID写PCM数据到UMIC中
其原理就是通过Windows提供的SETUPAPI接口查到系统中已有的所有自定义HID设备(当然包括我们的设备),通过判断其VID PID REV来是否为我们的设备。
如果是我们的设备,就打开该设备,下发PCM数据。
CHID类的封装详见:https://www.usbzh.com/article/detail-1306.html
#include <iostream>
#include "CHid.h"
#include <string>
BOOL GetBufferFromFile(TCHAR* pFileName, std::string& strFileStream)
{
HANDLE hFile = INVALID_HANDLE_VALUE;
BOOL isok = FALSE;
do
{
hFile = CreateFile(pFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
break;
}
DWORD dwFileSize = GetFileSize(hFile, NULL);
if (dwFileSize == 0)
{
break;
}
strFileStream.resize(dwFileSize);
DWORD dwBytesRead = 0;
isok = ReadFile(hFile, (PVOID)&strFileStream.at(0), dwFileSize, &dwBytesRead, NULL);
if (!isok)
{
break;
}
} while (0);
if (hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
}
return isok;
}
int _tmain(int argn,TCHAR* argv[])
{
const TCHAR* FileName = TEXT("桥边姑娘.wav");
if (argn > 1)
{
FileName = argv[1];
}
std::string strStream;
if (!GetBufferFromFile((TCHAR*)FileName, strStream))
{
_tprintf(TEXT("open PCM file err [%s]\n"), FileName);
return;
}
CHid hid;
TCHAR DeviceName[MAX_PATH];
if (!hid.OpenHid(0, 0x6001, 0x0100))
{
hid.CloseHid();
printf("OpenHid err\n");
return 0;
}
char* pData = (char*)&strStream.at(0);
int total = strStream.size();
ULONG rtn = 0;
SetThreadPriority(NULL, THREAD_PRIORITY_HIGHEST);
char buff[4096];
LONG NUM = (44100 * 2 * 2) / 100; //2CH 2BYTE
for (int i = 0; ; i++)
{
DWORD t = GetTickCount();
int nPos = i * NUM % total;
int len = total - nPos;
if (len >= NUM)
{
len = NUM;
}
else
{
i = 0;
}
memset(buff, 0, sizeof(buff));
buff[0] = 0xA0;
memcpy(&buff[1], &NUM, sizeof(NUM));
hid.SetReport(buff, sizeof(buff));
Sleep(9);
}
}