gpt4 book ai didi

c++ - 我如何使用c/c++实时录制和播放我的声音

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:49:31 26 4
gpt4 key购买 nike

我正在使用麦克风录制我的声音。为此,我使用了 waveIn() 和 waveOut() 函数。当我使用 waveInStart() 函数开始录音时,它首先会在指定时间内完全录制我的声音并将其存储在 () 缓冲区中,然后从该缓冲区播放我的声音。在录制和播放期间,它什么都不做。我想边录音边播放。为此,我想在录制期间访问缓冲区。这怎么可能 ?或者任何其他建议都会有所帮助。

Here is the code :


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

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

short int waveIn[8000 * 3];

void PlayRecord();
void writedataTofile(LPSTR lpData,DWORD dwBufferLength);

void StartRecord()
{
const int NUMPTS = 8000 * 3; // 3 seconds
int sampleRate = 8000;
// 'short int' is a 16-bit type; I request 16-bit samples below
// for 8-bit capture, you'd use 'unsigned char' or 'BYTE' 8-bit types

HWAVEIN hWaveIn;
MMRESULT result;

WAVEFORMATEX pFormat;
pFormat.wFormatTag=WAVE_FORMAT_PCM; // simple, uncompressed format
pFormat.nChannels=1; // 1=mono, 2=stereo
pFormat.nSamplesPerSec=sampleRate; // 8.0 kHz, 11.025 kHz, 22.05 kHz, and 44.1 kHz
pFormat.nAvgBytesPerSec=sampleRate*2; // = nSamplesPerSec × nBlockAlign
pFormat.nBlockAlign=2; // = (nChannels × wBitsPerSample) / 8
pFormat.wBitsPerSample=16; // 16 for high quality, 8 for telephone-grade
pFormat.cbSize=0;

// Specify recording parameters

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

WAVEHDR WaveInHdr;
// Set up and prepare header for input
WaveInHdr.lpData = (LPSTR)waveIn;
WaveInHdr.dwBufferLength = NUMPTS*2;
WaveInHdr.dwBytesRecorded=0;
WaveInHdr.dwUser = 0L;
WaveInHdr.dwFlags = 0L;
WaveInHdr.dwLoops = 0L;
waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

// Insert a wave input buffer
result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));


// Commence sampling input
result = waveInStart(hWaveIn);


cout << "recording..." << endl;

Sleep(3 * 1000);
// Wait until finished recording

waveInClose(hWaveIn);
PlayRecord();
}

void PlayRecord()
{
const int NUMPTS = 8000 * 3; // 3 seconds
int sampleRate = 8000;
// 'short int' is a 16-bit type; I request 16-bit samples below
// for 8-bit capture, you'd use 'unsigned char' or 'BYTE' 8-bit types

HWAVEIN hWaveIn;

WAVEFORMATEX pFormat;
pFormat.wFormatTag=WAVE_FORMAT_PCM; // simple, uncompressed format
pFormat.nChannels=1; // 1=mono, 2=stereo
pFormat.nSamplesPerSec=sampleRate; // 44100
pFormat.nAvgBytesPerSec=sampleRate*2; // = nSamplesPerSec * n.Channels * wBitsPerSample/8
pFormat.nBlockAlign=2; // = n.Channels * wBitsPerSample/8
pFormat.wBitsPerSample=16; // 16 for high quality, 8 for telephone-grade
pFormat.cbSize=0;

// Specify recording parameters

waveInOpen(&hWaveIn, WAVE_MAPPER,&pFormat, 0L, 0L, WAVE_FORMAT_DIRECT);

WAVEHDR WaveInHdr;
// Set up and prepare header for input
WaveInHdr.lpData = (LPSTR)waveIn;
WaveInHdr.dwBufferLength = NUMPTS*2;
WaveInHdr.dwBytesRecorded=0;
WaveInHdr.dwUser = 0L;
WaveInHdr.dwFlags = 0L;
WaveInHdr.dwLoops = 0L;
waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

HWAVEOUT hWaveOut;
cout << "playing..." << endl;
waveOutOpen(&hWaveOut, WAVE_MAPPER, &pFormat, 0, 0, WAVE_FORMAT_DIRECT);
waveOutWrite(hWaveOut, &WaveInHdr, sizeof(WaveInHdr)); // Playing the data
Sleep(3 * 1000); //Sleep for as long as there was recorded


waveInClose(hWaveIn);
waveOutClose(hWaveOut);
}
int main()
{
StartRecord();
return 0;
}

最佳答案

从技术上讲,如果分配缓冲区,您可以将输入和输出分配到同一个缓冲区,并运行一个线程进行播放和一个线程进行记录。但是,我希望您需要的数量远不止于此。

问题是缓冲区内容会通过某种机制从内存加载到硬件中,这会“预读”数据以小块播放,同时“缓冲”记录端。驱动程序和硬件都会有一些这种“缓存”机制。这意味着回放会在数据从录音存储到内存之前读取数据,这当然无法正常工作。

大多数音频处理系统的工作方式是稍微延迟输出,所以你输入一点,处理它,然后输出它。这当然会导致小的延迟,这可能会很烦人。

关于c++ - 我如何使用c/c++实时录制和播放我的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31646050/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com