从扬声器录制并通过按键结束录制过程,而不是等待指定的时间
Record from speakers and end a recording process by pressing a key instead of waiting specified amount of time
根据问题How to record wav sound from mic using Windows API?的答案,我进入了答案中提供link的页面,找到了一个从麦克风录制音频的示例代码。
我想知道如何将代码更改为:
1 - 从扬声器录制。
2-如果可能,按一个键停止记录,而不是等待作为函数参数给出的时间量。
#include <Windows.h>
#pragma comment (lib, "Winmm.lib")
DWORD recordWAVEFile(DWORD dwMilliSeconds)
{
UINT wDeviceID;
DWORD dwReturn;
MCI_OPEN_PARMS mciOpenParms;
MCI_RECORD_PARMS mciRecordParms;
MCI_SAVE_PARMS mciSaveParms;
MCI_PLAY_PARMS mciPlayParms;
mciOpenParms.lpstrDeviceType = "waveaudio";
mciOpenParms.lpstrElementName = "";
if (dwReturn = mciSendCommand(0, MCI_OPEN,
MCI_OPEN_ELEMENT | MCI_OPEN_TYPE,
(DWORD)(LPVOID)&mciOpenParms))
{
return (dwReturn);
}
wDeviceID = mciOpenParms.wDeviceID;
mciRecordParms.dwTo = dwMilliSeconds;
if (dwReturn = mciSendCommand(wDeviceID, MCI_RECORD,
MCI_TO | MCI_WAIT, (DWORD)(LPVOID)&mciRecordParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (dwReturn);
}
mciPlayParms.dwFrom = 0L;
if (dwReturn = mciSendCommand(wDeviceID, MCI_PLAY,
MCI_FROM | MCI_WAIT, (DWORD)(LPVOID)&mciPlayParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (dwReturn);
}
if (MessageBox(NULL, "Do you want to save this recording?",
"", MB_YESNO) == IDNO)
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (0L);
}
mciSaveParms.lpfilename = "tempfile.wav";
if (dwReturn = mciSendCommand(wDeviceID, MCI_SAVE,
MCI_SAVE_FILE | MCI_WAIT, (DWORD)(LPVOID)&mciSaveParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (dwReturn);
}
return (0L);
}
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{
char szAppName[] = "App";
char szWinName[] = "App";
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(
NULL,
TEXT("Failed to register window class."),
szAppName,
MB_ICONERROR);
return 0;
}
HWND hwnd;
hwnd = CreateWindow(
szAppName,
szWinName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if (!hwnd)
{
return 0;
}
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_KEYDOWN:
switch (wParam)
{
case VK_RETURN:
recordWAVEFile(4000);
break;
default:
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
对于 MCI,您不能选择默认以外的来源。您必须使用 waveIn* 函数(waveInOpen() 接受 select 输入源的参数)。
另一个选项是使用(可怕的)Mixer API 将默认输入设置为可用的东西,因此 MCI 使用它。
但是,并非所有声卡都支持从扬声器("what you hear" 输入)录音。其实现在大部分的主板自带的廉价显卡都没有这个选项。
另一个问题,要停止录制,waveIn 函数是异步的,因此您可以在按下某个键时使用 waveInStop()、waveInReset() 和 waveInClose() 简单地停止录制。
一般Windows的声音业务都不是用MCI做的,MCI是一个非常简单的快速播放和录音的接口。
根据问题How to record wav sound from mic using Windows API?的答案,我进入了答案中提供link的页面,找到了一个从麦克风录制音频的示例代码。
我想知道如何将代码更改为:
1 - 从扬声器录制。
2-如果可能,按一个键停止记录,而不是等待作为函数参数给出的时间量。
#include <Windows.h>
#pragma comment (lib, "Winmm.lib")
DWORD recordWAVEFile(DWORD dwMilliSeconds)
{
UINT wDeviceID;
DWORD dwReturn;
MCI_OPEN_PARMS mciOpenParms;
MCI_RECORD_PARMS mciRecordParms;
MCI_SAVE_PARMS mciSaveParms;
MCI_PLAY_PARMS mciPlayParms;
mciOpenParms.lpstrDeviceType = "waveaudio";
mciOpenParms.lpstrElementName = "";
if (dwReturn = mciSendCommand(0, MCI_OPEN,
MCI_OPEN_ELEMENT | MCI_OPEN_TYPE,
(DWORD)(LPVOID)&mciOpenParms))
{
return (dwReturn);
}
wDeviceID = mciOpenParms.wDeviceID;
mciRecordParms.dwTo = dwMilliSeconds;
if (dwReturn = mciSendCommand(wDeviceID, MCI_RECORD,
MCI_TO | MCI_WAIT, (DWORD)(LPVOID)&mciRecordParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (dwReturn);
}
mciPlayParms.dwFrom = 0L;
if (dwReturn = mciSendCommand(wDeviceID, MCI_PLAY,
MCI_FROM | MCI_WAIT, (DWORD)(LPVOID)&mciPlayParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (dwReturn);
}
if (MessageBox(NULL, "Do you want to save this recording?",
"", MB_YESNO) == IDNO)
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (0L);
}
mciSaveParms.lpfilename = "tempfile.wav";
if (dwReturn = mciSendCommand(wDeviceID, MCI_SAVE,
MCI_SAVE_FILE | MCI_WAIT, (DWORD)(LPVOID)&mciSaveParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (dwReturn);
}
return (0L);
}
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{
char szAppName[] = "App";
char szWinName[] = "App";
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(
NULL,
TEXT("Failed to register window class."),
szAppName,
MB_ICONERROR);
return 0;
}
HWND hwnd;
hwnd = CreateWindow(
szAppName,
szWinName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if (!hwnd)
{
return 0;
}
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_KEYDOWN:
switch (wParam)
{
case VK_RETURN:
recordWAVEFile(4000);
break;
default:
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
对于 MCI,您不能选择默认以外的来源。您必须使用 waveIn* 函数(waveInOpen() 接受 select 输入源的参数)。
另一个选项是使用(可怕的)Mixer API 将默认输入设置为可用的东西,因此 MCI 使用它。
但是,并非所有声卡都支持从扬声器("what you hear" 输入)录音。其实现在大部分的主板自带的廉价显卡都没有这个选项。
另一个问题,要停止录制,waveIn 函数是异步的,因此您可以在按下某个键时使用 waveInStop()、waveInReset() 和 waveInClose() 简单地停止录制。
一般Windows的声音业务都不是用MCI做的,MCI是一个非常简单的快速播放和录音的接口。