gpt4 book ai didi

javascript - 在 16 位缩放 PCM 数据之间转换为浮点 PCM 数据 C++

转载 作者:行者123 更新时间:2023-12-03 02:34:50 29 4
gpt4 key购买 nike

我无法在 C++ 中的 16 位缩放 PCM 数据和浮点 PCM 数据之间进行转换。我认为我必须接近,因为输出音频有点类似于我的预期,但它是失真的。

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

我正在尝试向 JavaScript 提供按 channel 分段的原始 PCM 数据。我的逻辑是,如果不需要解码,延迟会更短。

我知道音频数据一定是好的,因为我已经成功地将这个转换成波形文件格式并播放了。

我正在尝试获得一个多维浮点数组。第一个维度代表 channel ,第二个维度是该 channel 的样本数据。

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

这是我的算法,评论描述了我的推理。

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 数据为小字节序时,我假设为大字节序。

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

关于javascript - 在 16 位缩放 PCM 数据之间转换为浮点 PCM 数据 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61409220/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com