我无法在C ++中的16位缩放PCM数据和浮点PCM数据之间进行转换。我想我必须亲近,因为输出音频在某种程度上类似于我的期望,但是失真。
之所以这样做,是因为我正在浏览器中运行ScummVM游戏。 ScummVM代码在服务器上运行,我的自定义代码将音频和图像发布到网站上。我正在使用“ Web-Audio Api”在前端播放JavaScript声音。
我正在尝试将按通道分段的原始PCM数据提供给JavaScript。我的逻辑是,如果不需要解码,则延迟会更少。
我知道音频数据必须很好,因为我已成功将其转换为wave文件格式并播放了。
我正在尝试获取浮点数的多维数组。第一个维度表示通道,第二个维度是该通道的样本数据。
我要求我们不要讨论该项目的有用性。对我来说,这是一个学习项目,而不是尝试构建广泛使用的产品。
这是我的算法,带有描述我推理的注释。
C ++面
typedef unsigned char byte;
double** OSystem_Cli::mixCallback(uint len)
{
const int NO_CHANNELS = 2;
double** result = new double*[NO_CHANNELS];
int sampleSize = len;
byte* samples = new byte[len];
_mixerImpl->mixCallback(samples, len); //Get the 16-bit PCM audio from Scumm VM. eg. [91,11,91,11 . . .] and stores it in samples
for (int channel = 0; channel < NO_CHANNELS; channel++)
{
for (int byteNo = channel * 2, channelByteNo = 0; byteNo < sampleSize - 1; byteNo = byteNo + NO_CHANNELS * 2, channelByteNo++)
{
if (channelByteNo == 0)
{
result[channel] = new double[sampleSize / NO_CHANNELS / 2];
}
unsigned short unsignedCombination = (static_cast<unsigned short>(samples[byteNo]) << 8) + samples[byteNo + 1]; //Join two bytes together to get 1 sixteen bit number.
short signedCombination;
memcpy(&signedCombination, &unsignedCombination, sizeof(signedCombination));
double signedFloat = static_cast<double>(signedCombination);
signedFloat = signedFloat / (float)32768; //Divide it to get the floating point representation, as https://stackoverflow.com/questions/15087668/how-to-convert-pcm-samples-in-byte-array-as-floating-point-numbers-in-the-range states.
if (signedFloat > 1)
{
signedFloat = 1;
}
else if (signedFloat < -1)
{
signedFloat = -1;
}
result[channel][channelByteNo] = signedFloat;
}
}
delete[] samples;
return result;
}
JavaScript面(键入脚本):
pushOntoAudioQueue(pcmBytesByChannel: number[][]) {
const streamer: WebAudioStreamer = this;
const samplePartsPerChannel = pcmBytesByChannel[0].length;
const noChannels = pcmBytesByChannel.length;
if (this.audioStack.length > MaxQueueLength) {
this.audioStack = [];
}
const buffer = this.context.createBuffer(noChannels, samplePartsPerChannel, 16000); //Length is total number of bytes for all channels
for (let channel = 0; channel < noChannels; channel++) {
let float32ChannelData = new Float32Array(pcmBytesByChannel[channel].length);
float32ChannelData.set(pcmBytesByChannel[channel]);
buffer.copyToChannel(float32ChannelData, channel);
}
streamer.audioQueue.push(buffer);
}
我从另一个论坛得到了这个答案。我的字节序错了,而当PCM数据很少字节序时,我就假定了大的字节序。