gpt4 book ai didi

c++ - 如何创建 PCM 签名的 16 位 wav 文件

转载 作者:行者123 更新时间:2023-11-28 00:34:12 24 4
gpt4 key购买 nike

<分区>

我有一个原始音频流的二进制文件。我想创建一个 wav 文件,其属性为 PCM 签名的 16 位单声道小端,采样率为 22050hz。

目前正在使用以下代码:

template <typename T>
void write(std::ofstream& stream, const T& t) {
stream.write((const char*)&t, sizeof(T));
}

template <typename SampleType>
void writeWAVData(const char* outFile, SampleType* buf, size_t bufSize,
int sampleRate, short channels)
{
std::ofstream stream(outFile, std::ios::binary);
stream.write("RIFF", 4);
write<int>(stream, 36 + bufSize);
stream.write("WAVE", 4);
stream.write("fmt ", 4);
write<int>(stream, 16);
write<short>(stream, 1); // Format (1 = PCM)
write<short>(stream, channels); // Channels //mono/sterio
write<int>(stream, sampleRate);

write<int>(stream, sampleRate * channels * sizeof(SampleType)); // Byterate
write<short>(stream, channels * sizeof(SampleType)); // Frame size
write<short>(stream, 16 * sizeof(SampleType)); // Bits per sample
stream.write("data", 4);
stream.write((const char*)&bufSize, 4);
stream.write((const char*)buf, bufSize);
}


int wmain(int argc,wchar_t **argv)
{
std::ifstream is("c:\\stream", std::ios_base::binary);
if (is) {
// get length of file:
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
char * buffer = new char [length];
is.read (buffer,length);
writeWAVData("c:\\audio.wav", buffer, length, 22050, 1);
}
}

谁能帮我看看这是怎么回事?

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