# Thread: Array of 4 bytes to double.

1. Member
Join Date
Dec 2010
Posts
12
Rep Power
0

## Array of 4 bytes to double.

Hello all. So I'm looking for a way to convert an array that contains 4 bytes to a double. These bytes are actually 1 sample rate of an audio file, so it contains 2 channels, with each channel being 2 bytes. The bytes are also in Little Endian, so I've heard that you can just swap the bytes for each channel to convert to Big Endian. Example:

Array of bytes: [30, -20, 10, 50]
Left Channel (Little Endian): [30, -20]
Right Channel (Little Endian): [10, 50]

Left Channel (Big Endian): [-20, 30]
Right Channel (Big Endian): [50, 10]

So now that I've got the bytes in Big Endian (is this a valid way?), how can I convert these two channels into one double? I'm not sure if this information is needed, but the sample rate can be either 22050 (22.05 kHz) or 44100 (44.1 kHz), frame rate is 4 bytes / frame (with over all frame rate being the same as the sample rate, so if I want 1 second of audio, it's sample rate * 4 bytes), and it's all stereo (2 channel). If any more information is needed, let me know.
Last edited by blind melon; 12-06-2010 at 01:24 PM.

2. i'm not sure, is it correct to compute two channels into one value ? wouldn't that be like mixing the left and right channel together to one monaural channel output ?

but I imagine it could be done as a kind of average of two doubles,

Java Code:
```double leftValue = leftChannelBigEndian[0] << 8 | leftChannelBigEndian[1];
double rightValue = rightChannelBigEndian[0] << 8 | rightChannelBigEndian[1];

double mixedValue = leftValue + rightValue / 2;```

3. Member
Join Date
Dec 2010
Posts
12
Rep Power
0
Yeah, I think that's similar to what one of my buddies gave me:

short left = (short) ((0xff & main_data[1])<<8 | ((0xff & main_data[0])));
short right = (short) ((0xff & main_data[3])<<8 | ((0xff & main_data[2])));
main_d[i] = (left + right) / 2.0;

Where main_d[i] is an array of doubles. I'll try using what you have instead and seeing if it gives me any different results. Thanks!

Also, I came across this. Should I be using ints instead? I think with AudioStreamInput, when I call the read function (without giving it parameters), it returns the byte read as an int... so I guess I could use ints that way?

4. yea, i would think so, for the precision of bytes in the audio sample data, that the rounding errors would be small enough to not be perceptable on one sample. and ints would be probably faster, or in the case of other methods already returning them, easier to work with too.

5. Member
Join Date
Dec 2010
Posts
12
Rep Power
0
So if I used ints instead, would my conversion still be the same, just instead of the main_data byte array, I'd have the int, and I'd still have to do the swap?

And I'm not sure if you know the answer to this or not, but would it be better to bring the data in as mono instead of averaging the channels together? Because I guess I am essentially making the data mono, but probably not in the best fashion.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•