gpt4 book ai didi

c++ - 24位音频处理

转载 作者:行者123 更新时间:2023-11-28 08:18:46 26 4
gpt4 key购买 nike

我编写了一些代码,根据位深度从音频缓冲区读取数据,对其进行处理并将其写回到同一个缓冲区。我必须处理 16 位和 24 位深度。 16 位代码工作正常,但 24 位代码却不行。

这是我对 24 位数据的每个样本所做的:

int data = 0;

//read data from buffer
RtlCopyMemory (&data, ((char*)Source) + head, 3);

//scale data for integer

data = data << 8;

//data processing

data = data * m_CurOutVolLevel;

//scaling down the data
data = (data >> 8);

RtlCopyMemory (((char*)Source) + head, &data, 3);

但输出不正确。有人可以指出这里出了什么问题吗??

谢谢

感谢您的建议。我实现了你们的建议,但仍然存在一些缺陷:

unsigned __int64 newData;
unsigned char* s = ((unsigned char*)Source) + head;
unsigned int data = ((unsigned int)s[0] << 8) | ((unsigned int)s[1] << 16) |((unsigned int)s[2] << 24);

// data processing
newData = (unsigned __int64)(data * m_pMiniport->m_CurOutVolLevel);
//divide this by 2 to the power 16
newData = (newData >> 16);

// store back
s[0] = (unsigned char)((newData >> 32) & 0xff);
s[1] = (unsigned char)((newData >> 40) & 0xff);
s[2] = (unsigned char)((newData >> 48) & 0xff);

有人看到上面代码有问题吗?

谢谢,安妮

最佳答案

好吧,您使用 int,如果它在 32 位机器上,这通常是 32 位。如果数据是 24 位,移动它意味着您甚至可以使用最高位。这导致两件事:

  • 第一个可能的移位重载,因为你的 int 是有符号的,如果位“24”(数据中的最高位)为 1,结果中的位 32 将为 1,它将变为负数数。

  • 第二种可能的乘法溢出,假设上述情况没有发生,您现在很可能会超出 32 位数字的边界。假设第 23 位已设置,因此您的数字保持正数,如果 m_curOutVolLevel 甚至低至 2,则这等于一个 exta 位移位并且您再次溢出。更有可能的是你甚至会丢失位,因为数字不能再以 32 位存储。

编辑:可能的解决方案,如果这是问题所在:使用 stdint.h

中的 uint64_t

编辑2:另请注意,您的代码可能会给endianness 带来问题。因为您只是将 int 编码为字节流(除非 RtlCopyMemory 处理这个,我对此表示怀疑)。

关于c++ - 24位音频处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6706881/

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