Parade Series - CoreAudio Loopback

Scenario

鉴于业务场景需要, 经过技术路径探索, 发现 comtypes 兼容性过于混乱,故而考虑整合一个 CoreAudio 的轮子dll来解决实际问题!



std::StringStream ⇒ std::ios::binary ⇒ std::ofstream

Loopback.dll

#ifndef _DLL_CORE_AUDIO_LOOPBACK_LOCK
#define _DLL_CORE_AUDIO_LOOPBACK_LOCK

#pragma data_seg("Shared")
HANDLE hMutex = NULL;
UINT   liveIndex = 0;
CComPtr<IMMDeviceEnumerator> pEnumerator;
#pragma data_seg()
#pragma comment(linker, "/section:Shared,rws")

class TimespanGuard {
public:
	TimespanGuard()
	{
		this->_bof_time = GetTickCount();
		this->_token = "Timespan";
	}

	TimespanGuard(std::string token)
	{
		this->_bof_time = GetTickCount();
		_token = token;
	}

	~TimespanGuard()
	{
		this->_eof_time = GetTickCount();
		std::cout << "" << this->_token << " > " << (this->_eof_time - this->_bof_time) << "ms elapsed" << std::endl;
	}

private:
	DWORD _bof_time, _eof_time;
	std::string _token;
};

CORE_AUDIO_LOOPBACK_API BOOL WINAPI init(UINT devNum)
{
	TimespanGuard timespanGuard("init");

	HRESULT hr{};

	CoInitializeGuard coInitializeGuard;
	if (FAILED(coInitializeGuard.result())) {
		return (FALSE);    // BOOL FALSE 0x0000
	}

	hr = pEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator));
	if (FAILED(hr)) {
		return (FALSE);    // BOOL FALSE 0x0000
	}

	Sleep(10);
	return (TRUE);         // BOOL TRUE  0x0001
}
CORE_AUDIO_LOOPBACK_API BOOL WINAPI getChunk(UINT devNum, UINT (*dtChunk)[2], UINT nDimension)
{
	TimespanGuard timespanGuard("getChunk");

	HRESULT hr{};

	CoInitializeGuard coInitializeGuard;
	if (FAILED(coInitializeGuard.result())) {
		return (FALSE);    // BOOL FALSE 0x0000
	}

	hr = pEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator));
	if (FAILED(hr)) {
		return (FALSE);    // BOOL FALSE 0x0000
	}

	std::cout << "getChunk > " << (nDimension) << "x" << (2) << std::endl;
	for (int i = 0; i < nDimension; i++) {
		printf("\t#%.4d ", i);
		for (int j = 0; j < 2; j++) {
			printf("%8d ", dtChunk[i][j]);
			dtChunk[i][j] = dtChunk[i][j] + 100;
		}
		printf("\n");
	}

	Sleep(10);
	return (TRUE);         // BOOL TRUE  0x0001
}

CORE_AUDIO_LOOPBACK_API BOOL WINAPI release(UINT devNum)
{
	TimespanGuard timespanGuard("release");

	CoUninitialize();

	return (TRUE);         // BOOL TRUE  0x0001
}

XAudio.py

import ctypes

if True:
	loopback = ctypes.CDLL("loopback.dll")
    if True:
        loopback.getAudio.argtypes = [c_int, c_char_p, c_int]
        loopback.getAudio.restype = c_int

        devNum = c_int(0)
        bytesSize = 256
        bytesName = (' ' * bytesSize).encode('utf-8')

        result = loopback.getAudio(devNum, bytesName, bytesSize)
        print('DLL invoked > ', result, (bytesName.decode('gb2312')))

GetBitmapBits 后位图颠倒

c 复制代码
	dumpCache(dataBuff, result);
	GetBitmapBits(hOutputBitmap, nBytesSize, rawData); // 获取位图的位
	for (int i = 0; i < nBytesSize; i++) {
		int row, column = 0;
		row = (sHeight - (i / bm.bmWidthBytes)) - 1;
		column = (i % bm.bmWidthBytes);
		dataBuff[result + row * bm.bmWidthBytes + column] = (rawData[i] & 0x00FF);
	}
	result += nBytesSize;
dumpCache(dataBuff, result);

dumpCache

c 复制代码
BOOL dumpCache(BYTE* dataBuff, UINT buffSize)
{
	for (int i = 0; i < buffSize; i++) {
		printf("%.2X ", dataBuff[i]);
		if (i % 16 == 15)
			printf("\n");
	}
	printf("\n");
	return (TRUE);
}

getBitmapHeader

c 复制代码
UINT getBitmapHeader(HDC hDC, HBITMAP hBitmap, BYTE* dataBuff, UINT buffSize)
{
	UINT result = 0;
	int iBits;
	//当前显示分辨率下每个像素所占字节数
	WORD wBitCount;
	//位图中每个像素所占字节数
   //定义调色板大小, 位图中像素字节大小 , 位图文件大小 , 写入文件字节数
	DWORD dwPaletteSize = 0, dwBmBitsSize, dwDIBSize, dwWritten;
	BITMAP Bitmap;
	//位图属性结构
	BITMAPFILEHEADER bmfHdr;
	//位图文件头结构
	BITMAPINFOHEADER bi;
	//位图信息头结构
	LPBITMAPINFOHEADER lpbi;
	//指向位图信息头结构
	HANDLE fh, hDib, hPal;
	HPALETTE hOldPal = NULL;
	//定义文件,分配内存句柄,调色板句柄

	//计算位图文件每个像素所占字节数
	iBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
	if (iBits <= 1)
		wBitCount = 1;
	else if (iBits <= 4)
		wBitCount = 4;
	else if (iBits <= 8)
		wBitCount = 8;
	else if (iBits <= 24)
		wBitCount = 24;
	else
		wBitCount = 32;
	//计算调色板大小
	if (wBitCount <= 8)
		dwPaletteSize = (1 << wBitCount) * sizeof(RGBQUAD);

	//设置位图信息头结构
	GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
	bi.biSize = sizeof(BITMAPINFOHEADER);
	bi.biWidth = Bitmap.bmWidth;
	bi.biHeight = Bitmap.bmHeight;
	bi.biPlanes = 1;
	bi.biBitCount = wBitCount;
	bi.biCompression = BI_RGB;
	bi.biSizeImage = 0;
	bi.biXPelsPerMeter = 0;
	bi.biYPelsPerMeter = 0;
	bi.biClrUsed = 0;
	bi.biClrImportant = 0;

	dwBmBitsSize = ((Bitmap.bmWidth * wBitCount + 31) / 32) * 4 * Bitmap.bmHeight;
	//为位图内容分配内存
	hDib = GlobalAlloc(GHND, dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER));
	lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
	*lpbi = bi;
	// 处理调色板  
	hPal = GetStockObject(DEFAULT_PALETTE);
	if (hPal)
	{
		hDC = ::GetDC(NULL);
		hOldPal = SelectPalette(hDC, (HPALETTE)hPal, FALSE);
		RealizePalette(hDC);
	}
	// 获取该调色板下新的像素值
	GetDIBits(hDC, hBitmap, 0, (UINT)Bitmap.bmHeight, (LPSTR)lpbi + sizeof(BITMAPINFOHEADER) + dwPaletteSize, (BITMAPINFO*)lpbi, DIB_RGB_COLORS);
	//恢复调色板  
	if (hOldPal)
	{
		SelectPalette(hDC, hOldPal, TRUE);
		RealizePalette(hDC);
		::ReleaseDC(NULL, hDC);
	}

	// 设置位图文件头
	bmfHdr.bfType = 0x4D42; // "BM"
	dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize;
	bmfHdr.bfSize = dwDIBSize;
	bmfHdr.bfReserved1 = 0;
	bmfHdr.bfReserved2 = 0;
	bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize;

	// 写入位图文件头
	memcpy(dataBuff, (BYTE*)&bmfHdr, sizeof(BITMAPFILEHEADER));
	result += sizeof(BITMAPFILEHEADER);

	memcpy((dataBuff + result), (BYTE*)lpbi, ((DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize));
	result += sizeof(BITMAPINFOHEADER);
	result += dwPaletteSize;

	//清除  
	GlobalUnlock(hDib);
	GlobalFree(hDib);

	return (result);
}
相关推荐
努力的家伙是不讨厌的7 分钟前
解析json导出csv或者直接入库
开发语言·python·json
云空29 分钟前
《Python 与 SQLite:强大的数据库组合》
数据库·python·sqlite
凤枭香1 小时前
Python OpenCV 傅里叶变换
开发语言·图像处理·python·opencv
测试杂货铺1 小时前
外包干了2年,快要废了。。
自动化测试·软件测试·python·功能测试·测试工具·面试·职场和发展
艾派森1 小时前
大数据分析案例-基于随机森林算法的智能手机价格预测模型
人工智能·python·随机森林·机器学习·数据挖掘
小码的头发丝、2 小时前
Django中ListView 和 DetailView类的区别
数据库·python·django
Chef_Chen3 小时前
从0开始机器学习--Day17--神经网络反向传播作业
python·神经网络·机器学习
千澜空3 小时前
celery在django项目中实现并发任务和定时任务
python·django·celery·定时任务·异步任务
斯凯利.瑞恩3 小时前
Python决策树、随机森林、朴素贝叶斯、KNN(K-最近邻居)分类分析银行拉新活动挖掘潜在贷款客户附数据代码
python·决策树·随机森林
yannan201903133 小时前
【算法】(Python)动态规划
python·算法·动态规划