分裂UInt32的(音频帧)分成两个SInt16s(左和右)? [英] Split UInt32 (audio frame) into two SInt16s (left and right)?

查看:200
本文介绍了分裂UInt32的(音频帧)分成两个SInt16s(左和右)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总小白到任何下级比Java,潜入iPhone音频和realing所有的铸件/指针/原始内存访问。

Total noob to anything lower-level than Java, diving into iPhone audio, and realing from all of the casting/pointers/raw memory access.

我与一些例如code 至极读取从盘WAV文件,并返回立体声采样单UInt32的值。如果我理解正确的话,这只是恢复到创建两个16位采样所需的内存对于32位的简便方法。最终,这个数据被写入缓冲器,以及音频单元捡起来的道路。即使数据被写入UInt32的大小的块,它最终是PTED作为对的16位样本间$ P $

I'm working with some example code wich reads a WAV file from disc and returns stereo samples as single UInt32 values. If I understand correctly, this is just a convenient way to return the 32 bits of memory required to create two 16 bit samples. Eventually this data gets written to a buffer, and an audio unit picks it up down the road. Even though the data is written in UInt32-sized chunks, it eventually is interpreted as pairs of 16-bit samples.

我需要极快这些UInt32的帧到左,右样品帮助。我假设我会想每一个UInt32的转换成SInt16,因为音频采样是有符号值。在我看来,对于效率的缘故,我应该能够简单地指向同一个块在内存中,并避免任何复制。

What I need help with is splitting these UInt32 frames into left and right samples. I'm assuming I'll want to convert each UInt32 into an SInt16, since an audio sample is a signed value. It seems to me that for efficiency's sake, I ought to be able to simply point to the same blocks in memory, and avoid any copying.

所以,在伪code,这将是这样的:

So, in pseudo-code, it would be something like this:

UInt32 myStereoFrame = getFramefromFilePlayer;

SInt16* leftChannel = getFirst16Bits(myStereoFrame);
SInt16* rightChannel = getSecond16Bits(myStereoFrame);

谁能帮我把我的伪成真正的code?

Can anyone help me turn my pseudo into real code?

编辑:

很多感谢zneak对于这个答案,它的伟大工程:

Much thanks to zneak for this answer, which works great:

SInt16* left = (SInt16*)&myUInt32Chunk;
SInt16* right = left + 1;

现在,重新组装我的SInt16s到UInt32的(假设我不希望只是用我的原始变量,myStereoFrame),我需要做更多大同小异反向:排队我的两个SInt16s在连续的内存插槽再抹上内存到UInt32的。

Now, to reassemble my SInt16s into a UInt32 (assuming I don't want to just use my original variable, myStereoFrame), I'll need to do more or less the same in reverse: line up my two SInt16s in contiguous memory slots and then cast that memory to a UInt32.

所以我做一个数组:

SInt16 newFrameInMemory[2] = {*left, *right}

和然后该阵列的整个长度复制到UInt32的

And then copy the entire length of that array into a UInt32

UInt32 newFrame32 = newFrameInMemory

这不工作,虽然,但如果我不丑此位 - 投数组的指针,然后解引用指针,它的工作

That doesn't work though, but if I do this bit of ugliness -- cast the array to a pointer, then dereference the pointer, it does work.

UInt32 newFrame32 = *((UInt32*)&newFrameInMemory)

有没有prettier方式做到这一点?

Is there a prettier way to do this?

推荐答案

这是很容易的:

SInt16* left = (SInt16*)&myUInt32Chunk;
SInt16* right = left + 1;

除非iPhone是小端,在这种情况下,你必须反转右键离开

由于一个 INT 可以被看作是两个相邻的 S,你只需要得到的地址吧,然后做就好像它是,事实上,两个秒。

Since an int can be seen as two contiguous shorts, you just have to get the address to it, and then do exactly as if it was, indeed, two shorts.

这篇关于分裂UInt32的(音频帧)分成两个SInt16s(左和右)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆