gpt4 book ai didi

c++ - 使用 waveInOpen 保存波形文件

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

我正在尝试使用 waveInOpen 和 co ( see here ) 将声音录制到 .wave 文件

这是我记录到缓冲区的代码,它似乎可以工作:

#include <Windows.h>
#include <MMSystem.h>
#include <iostream>

using namespace std;

int main(){

HWAVEIN microHandle;
WAVEHDR waveHeader;

const int NUMPTS = 22050 * 10; // 10 seconds
int sampleRate = 22050;
short int waveIn[NUMPTS]; // '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

MMRESULT result = 0;

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

result = waveInOpen(&microHandle, WAVE_MAPPER, &format, 0L, 0L, WAVE_FORMAT_DIRECT);

if (result)
{
cout << "Fail step 1" << endl;
cout << result << endl;
Sleep(10000);
return 0;
}

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

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

if (result)
{
cout << "Fail step 2" << endl;
cout << result << endl;
Sleep(10000);
return 0;
}

result = waveInStart(microHandle);

if (result)
{
cout << "Fail step 3" << endl;
cout << result << endl;
Sleep(10000);
return 0;
}

// Wait until finished recording
do {} while (waveInUnprepareHeader(microHandle, &waveHeader, sizeof(WAVEHDR))==WAVERR_STILLPLAYING);

waveInClose(microHandle);

return 0;
}

但是我没有找到任何关于如何将它保存在磁盘上的功能或解释

我应该保存 WAVEFORMATEX 结构吗?还是有其他东西要添加到 wave 文件头?

唯一的问题是我只能使用 Windows 库,我不必安装任何其他库

谢谢你:)

最佳答案

.WAV 文件不仅包含原始数据,还包含格式和额外数据,并且通常具有 RIFF structure .

MSDN 解释了写入文件,例如这里:Writing to a WAV File .

此外,当前的 Windows SDK \Samples\multimedia\directshow\dmo\dmodemo\dsutil.* 文件具有类似的 CWaveFile 类的实现。

关于c++ - 使用 waveInOpen 保存波形文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11725528/

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