0

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;

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.

2 Answers 2

1

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);

alt text

Sign up to request clarification or add additional context in comments.

1 Comment

Still getting quite a few negative but a lot of positives now and they're quite high in the range up to the 32,000 mark. The Sample Bit Rate is apparently 16000hz.
0

Have you tried converting the byte to short before shifting?

2 Comments

You can't shift bytes. If you use << on a byte, it's automatically converted to an int/Int32.
Yes I do it right before the bitwise conversion on; soundArray[i] = (short)(buffer[i] | (buffer[i + 1] << 8));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.