【发布时间】:2015-04-02 15:05:01
【问题描述】:
我写了一个 C# 函数来保存音频数据,它没有任何问题。这是用于将数据写入流的原始函数:
public override void store(double data)
// stores a sample in the stream
{
double sample_l;
short sl;
sample_l = data * 32767.0f;
sl = (short)sample_l;
stream.WriteByte((byte)(sl & 0xff));
stream.WriteByte((byte)(sl >> 8));
stream.WriteByte((byte)(sl & 0xff));
stream.WriteByte((byte)(sl >> 8));
}
我将其转换为一些 C++ 代码并用它来将数据输出到 wav 文件:
double data;
short smp;
char b1, b2;
int i;
std::ofstream sfile(fname);
...
for (i = 0; i < tot_smps; i++)
{
smp = (short)(rend() * 32767.0);
b1 = smp & 0xff;
b2 = smp >> 8;
sfile.write((char*)&b1, sizeof(char));
sfile.write((char*)&b2, sizeof(char));
sfile.write((char*)&b1, sizeof(char));
sfile.write((char*)&b2, sizeof(char));
}
rend 总是介于 -1 和 1 之间。当我收听/查看 C++ 程序中的 wav 文件时,会有额外的嗡嗡声。 C++ 代码中的数据转换与原始 C# 代码相比似乎有些不同,导致两个不同的程序输出的数据/声音不同。
【问题讨论】:
-
我假设
rend返回一个浮点值?