我正在使用 [VS 2015 for desktop] 并且我正在使用 c++ 进行编码。我用 audacity 录制 wav 文件,然后用 PlaySound 播放它们,它们工作正常。
我确实打开了 fstreams 并逐字节输入新 wav 文件的标头和数据。(我承认,几乎所有这些代码都是从网上复制的,但我还是剖析了它以供自己学习。)从这段代码合成的 wav 文件可以在任何音频播放器(包括 Audacity)中播放,就像 Audacity 录音一样,但它们不适用于 PlaySound。标题设置是一样的,我已经检查过了。
namespace little_endian_io {
template <typename Word> std::ostream& write_word(std::ostream& outs, Word value, unsigned size = sizeof(Word))
{
for (; size; --size, value >>= 8)
outs.put(static_cast <char> (value & 0xFF));
return outs;
}
}
using namespace little_endian_io;
ofstream out(pathpat + "\\voces\\_audiophiles\\outputs.wav", std::ios::binary);
///HEADER
out << "RIFF----WAVEfmt ";
write_word(out, 16, 4); // no extension data
write_word(out, 1, 2); // PCM - integer samples
write_word(out, 2, 2); // stereo
write_word(out, 44100, 4); // Hz
write_word(out, 176400, 4); // (Sample Rate * BitsPerSample * Channels) / 8
write_word(out, 4, 2); // data block size (size of two integer samples, one for each channel, in bytes)
write_word(out, 16, 2); // number of bits per sample (use a multiple of 8)
///DATA
size_t data_chunk_pos = out.tellp();
out << "data----";
//~~CONTENTS
for (int i = 0; i < gemz.size(); i++) {
ifstream inn(gemz[i], std::ios::binary);
inn.seekg(44);
out << inn.rdbuf();
}
//~~CONTENTS
// (We'll need the final file size to fix the chunk sizes above)
size_t file_length = out.tellp();
// Fix the data chunk header to contain the data size
out.seekp(data_chunk_pos + 4);
write_word(out, file_length - data_chunk_pos + 8,4); //i added the 4 as the third arg, thanks to Remy
// Fix the file header to contain the proper RIFF chunk size, which is (file size - 8) bytes
out.seekp(0 + 4);
write_word(out, file_length - 8, 4);
out.close();
PlaySound(TEXT(pathpat + "\\voces\\_audiophiles\\output.wav".c_str()), NULL, SND_FILENAME);
PlaySound(pathpat + "\\voces\\_audiophiles\\output.wav".c_str(), NULL, SND_FILENAME);