将16位标度PCM数据转换为浮点PCM数据C++



我无法在C++中转换16位缩放PCM数据和浮点PCM数据。我想我一定很接近,因为输出的音频有点像我所期望的,但它是失真的。

我这么做的原因是因为我在浏览器中运行ScummVM游戏。ScummVM代码在服务器上运行,我的自定义代码将音频和图像发布到网站上。我正在使用"Web Audio Api"在前端用JavaScript播放声音。

我正在尝试向JavaScript提供按通道分割的原始PCM数据。我在这里的逻辑是,如果不需要解码,延迟会更小。

我知道音频数据一定很好,因为我已经成功地将其转换为波形文件格式并播放

我正在尝试获得一个多维的浮动数组。第一个维度表示通道,第二个维度是该通道的采样数据。

我要求我们不要讨论这个项目的用处。这对我来说是一个学习项目,而不是试图构建一个广泛使用的产品。

这是我的算法,并附有描述我推理的注释。

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);
}

我从另一个论坛得到了这个答案。我的endianness是错误的,当PCM数据是小endianes时,我假设了大endianes。

请参阅:http://cplusplus.com/forum/general/269888/

最新更新