我有一個原始音頻流的二進制二進制文件。我想創建一個帶有屬性PCM簽名的16位單聲道小尾數的採樣率爲22050hz的wav文件。如何創建有PCM簽名的16位wav文件
目前正在使用下面的代碼:
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);
}
}
誰能幫助我這裏有什麼問題?
究竟錯在何處?編譯錯誤?運行時錯誤?意外的輸出? –