本文介绍了尝试写函数的记录,并通过WinAPI的播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C创建两个功能:

I'm trying to create 2 functions in C :


  • 一说记录X秒麦克风

  • 一说得到一个记录,并将其发挥到用户

这是我写的:

#include <iostream>
#include <Windows.h>
using namespace std;

#pragma comment(lib, "winmm.lib")

WAVEHDR StartRecord(int seconds)
{
const int NUMPTS = 44100 * seconds;
int sampleRate = 44100;
short int *waveIn = new short int[NUMPTS];

HWAVEIN hWaveIn;
WAVEHDR WaveInHdr;
MMRESULT result;

WAVEFORMATEX pFormat;
pFormat.wFormatTag = WAVE_FORMAT_PCM;
pFormat.nChannels = 1;
pFormat.nSamplesPerSec = sampleRate;
pFormat.nAvgBytesPerSec = 2 * sampleRate;
pFormat.nBlockAlign = 2;
pFormat.wBitsPerSample = 16;
pFormat.cbSize = 0;

result = waveInOpen(&hWaveIn, WAVE_MAPPER, &pFormat, 0, 0, WAVE_FORMAT_DIRECT);

if(result)
{
    char fault[256];
    waveInGetErrorTextA(result, fault, 256);
    MessageBoxA(NULL, fault, "Failed to open waveform input device.", MB_OK | MB_ICONEXCLAMATION);
}

WaveInHdr.lpData = (LPSTR)waveIn;
WaveInHdr.dwBufferLength = 2 * NUMPTS;
WaveInHdr.dwBytesRecorded = 0;
WaveInHdr.dwUser = 0;
WaveInHdr.dwFlags = 0;
WaveInHdr.dwLoops = 0;
waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
if(result)
{
    MessageBoxA(NULL, "Failed to read block from device", NULL, MB_OK | MB_ICONEXCLAMATION);
}

result = waveInStart(hWaveIn);
if(result)
{
    MessageBoxA(NULL, "Failed to start recording", NULL, MB_OK | MB_ICONEXCLAMATION);
}

cout << "Recording..." << endl;
Sleep(seconds * 1000); //Sleep while recording

return WaveInHdr;

}

void PlayRecord(WAVEHDR WaveInHdr, int seconds)
{
WAVEFORMATEX pFormat;
pFormat.wFormatTag = WAVE_FORMAT_PCM;
pFormat.nChannels = 1;
pFormat.nSamplesPerSec = 44100;
pFormat.nAvgBytesPerSec = 2 * 44100;
pFormat.nBlockAlign = 2;
pFormat.wBitsPerSample = 16;
pFormat.cbSize = 0;

HWAVEOUT hWaveOut;

if(waveOutOpen(&hWaveOut, WAVE_MAPPER, &pFormat, 0, 0, WAVE_FORMAT_DIRECT))
{
    MessageBoxA(NULL, "Failed to replay", NULL, MB_OK | MB_ICONEXCLAMATION );
}

waveOutWrite(hWaveOut, &WaveInHdr, sizeof(WaveInHdr)); // Playing the data
Sleep(seconds * 1000); //Sleep for as long as there was recorded
 }

int main()
{
PlayRecord(StartRecord(3), 3);
return 0;
}

哪些错误与code?为什么犯规工作?我似乎听到什么......

Whats wrong with the code? Why doesnt it work ? I cant seem to hear anything...

此外,有没有记录的麦克风,而不是X秒,记录它,例如一个选项,直到没有从麦克风输入(例如 - 开始录制了,我一句话也没说对着麦克风,当的theres沉默了两秒钟停止录音什么的?)

Also, is there an option to record the microphone, instead of X seconds, to record it for example untill there is no input from the microphone (for example - start recording it, I'm saying a word into the microphone, and when theres a silence for a second or two it stops recording or something?)

谢谢!

推荐答案

回复:记录直到没有从麦克风输入。是的,prepare几个缓冲区和你WaveInStart之前,他们每个人打电话waveInAddBuffer。 WaveIn将连续和顺序填入缓冲区,并将其退回给你,因为他们充满。您将需要使用WaveIn提供的知道什么时候缓冲区已经充满通知之一。检查缓冲区中的数据来决定何时停止。如果你想继续下去,然后给每个填充的缓冲区回到WaveIn并能持续,只要你想填补他们。

Re: recording until there is no input from the microphone. Yes, prepare several buffers and call waveInAddBuffer with each of them before you WaveInStart. WaveIn will continuously and sequentially fill the buffers and return them to you as they are filled. You will need to use one of the notifications provided by WaveIn to know when a buffer has been filled. Examine the buffer data to decide when to stop. If you want to keep going then give each filled buffer back to WaveIn and it will keep filling them as long as you want.

这篇关于尝试写函数的记录,并通过WinAPI的播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 12:53