将 2 个 sint16s 样本表示为单个 uint32 样本
我的问题与这个问题相关,但方向相反。
将 UInt32(音频帧)拆分为两个 SInt16(左和右)对吗)?
在上面的链接中,操作员想要将 32 位帧拆分为 2 个 16 位样本(左和右)。
我有 2 个 16 位样本,我想将它们表示为单个 32 位交错帧。
到目前为止我所尝试的看起来像这样。
UInt32* ar = malloc(totalFramesInFile * sizeof (ar));
for (int b=0; b < totalFramesInFile; b++)
{
UInt32 l = soundStructArray[audioFile].audioDataLeft[b];
UInt32 r = soundStructArray[audioFile].audioDataRight[b];
UInt32 t = (UInt32) l + r;
ar[b] = t;
}
soundStructArray[audioFile].audioData = ar;
这合法且正确吗?由于我的经验不足,我不确定。我的音频输出听起来有点奇怪,我正在尝试通过排除过程确定出了什么问题。
如果有人能够确认我正在做的是将 2 个 16 位样本表示为 32 位帧的正确方法,或者提出正确的方法,那将会很有帮助。
我有一种感觉,我所做的事情是错误的。我认为这是因为 32 位帧的前 16 位应该在左边,第二个 16 位应该在右边。整个事情不应该是两者的总和……我想。
My question is related to this question but it is in reverse.
Split UInt32 (audio frame) into two SInt16s (left and right)?
In the link above the op wanted to split a 32 bit frame into 2 16 bit samples (left and right).
I have 2 16 bit samples and I would like to express them as a single 32 bit interleaved frame.
What I have attempted so far looks like this.
UInt32* ar = malloc(totalFramesInFile * sizeof (ar));
for (int b=0; b < totalFramesInFile; b++)
{
UInt32 l = soundStructArray[audioFile].audioDataLeft[b];
UInt32 r = soundStructArray[audioFile].audioDataRight[b];
UInt32 t = (UInt32) l + r;
ar[b] = t;
}
soundStructArray[audioFile].audioData = ar;
Is this legitimate and correct? I'm not sure due to my inexperience. My audio output is sounding a little peculiar and I'm trying to by process of elimination determine whats going wrong.
It would be helpful if somebody could either confirm that what I'm doing is the correct way to express 2 16 bit samples as a 32 bit frame or suggest a correct way.
I have a feeling that what I have done is wrong. I think this because the first 16 bits of the 32bit frame should be the left and the second 16 bits should be the right. The whole thing shouldnt be the sum of the two...... I think.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将:: 更改
为:
这会将
l
放入t
的最高有效 16 位,将r
放入最低有效 16 位。详细说明
如果您有两个 16 位样本,
l
和r
,它们的二进制形式如下所示:让我们首先将它们扩展到 32 位:
现在让我们将
l
左移 16 位 (l << 16
)现在让我们对它们进行 OR (
|
) 运算:瞧! 现在,您已将
l
和r
组合成一个 32 位值,以后可以轻松地再次从中提取它们。You need to change::
to:
This puts
l
in the most significant 16 bits oft
andr
in the least significant 16 bits.Detailed explanation
If you have two 16 bit samples,
l
andr
, which look like this in binary:let's first extend them to to 32 bits:
Now let's shift
l
left by 16 bits (l << 16
)Now let's OR (
|
) them together:Voilà ! Now you have
l
andr
combined into a 32 bit value from which they can easily be extracted again later.我会将其作为对 Paul R 答案的补充,但发表评论有点太长了。
可能更“明显”的替代解决方案是显式设置 32 位值中的顶部或底部 16 位值。您可以使用联合来完成此操作,即
用法:
但是,这种方法需要您更多地了解底层平台,因为它依赖于数据在内存中的实际存储方式。不过,根据目标平台和编译器,它可能会更有效。
I'll take this up as a complement to Paul R's answer, but it was a bit too long to put in a comment.
An alternative solution that may be more "obvious" is explicitly setting the top or bottom 16-bit values in the 32-bit value. You can do this with a union, i.e.
Usage:
However, this approach requires you to know a bit more about the underlying platform, since it relies on how data is actually stored in memory. Depending on the target platform and compiler, though, it could be more efficient.