开发者

How to perform a Bitwise Operator on a byte array in C#

开发者 https://www.devze.com 2023-01-14 03:33 出处:网络
I am using C# and Microsoft.Xna.Framework.Audio; I have managed to record some audio into a byte[] array and I am able to play it back.

I am using C# and Microsoft.Xna.Framework.Audio;

I have managed to record some audio into a byte[] array and I am able to play it back.

The audio comes in as 8 bit unsigned data, and I would like to convert it into 16 bit mono signed audio so I can read the frequency what not.

I have read a few places that for sound sampling you perform a Bitwise Operator Or and shift the bits 8 places.

I have performed the code as follows;开发者_C百科

soundArray[i] = (short)(buffer[i] | (buffer[i + 1] << 8));

What I end up with is a lot of negative data.

From my understanding it would mostly need to be in the positive and would represent a wave length of data.

Any suggestions or help greatly appreciated,

Cheers.

MonkeyGuy.


This combines two 8-bit unsigned integers into one 16-bit signed integer:

soundArray[i] = (short)(buffer[i] | (buffer[i + 1] << 8));

I think what you might want is to simply scale each 8-bit unsigned integer to a 16-bit signed integer:

soundArray[i] = (short)((buffer[i] - 128) << 8);

How to perform a Bitwise Operator on a byte array in C#


Have you tried converting the byte to short before shifting?

0

精彩评论

暂无评论...
验证码 换一张
取 消