gpt4 book ai didi

c++ - 将此代码更改为追加而不是覆盖 (.Wav)

转载 作者:行者123 更新时间:2023-11-28 06:51:56 26 4
gpt4 key购买 nike

如标题所述,我希望帮助更改此代码,而不是将缓冲区等写入文件,我希望它附加,所以它第一次创建了文件,然后如果文件存在它将附加它。

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

template <typename T>
void writeFormat(std::ofstream& stream) {
write<short>(stream, 1);
}

template <>
void writeFormat<float>(std::ofstream& stream) {
write<short>(stream, 3);
}

template <typename SampleType>
void writeWAVData(
char const* 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);
writeFormat<SampleType>(stream); // Format
write<short>(stream, channels); // Channels
write<int>(stream, sampleRate); // Sample Rate
write<int>(stream, sampleRate * channels * sizeof(SampleType)); // Byterate
write<short>(stream, channels * sizeof(SampleType)); // Frame size
write<short>(stream, 8 * sizeof(SampleType)); // Bits per sample
stream.write("data", 4);
stream.write((const char*)&bufSize, 4);
stream.write((const char*)buf, bufSize);
}

这是该过程的一行代码

writeWAVData("test.wav", mixBuffer.Array(), audioSampleSize, 48000, 1);

最佳答案

将打开文件的行更改为类似的内容

std::ofstream stream(outFile, std::ios::out | std::ios::binary | std::ios::app);

std::ios::app 标志代表append。引用文档 here了解更多详情。

关于c++ - 将此代码更改为追加而不是覆盖 (.Wav),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23807106/

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