Results 1 to 5 of 5
  1. #1
    blind melon is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default 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 12:24 PM.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    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. #3
    blind melon is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default

    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. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    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. #5
    blind melon is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default

    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.

Similar Threads

  1. find the unique values of a double array
    By tyang in forum New To Java
    Replies: 3
    Last Post: 09-11-2011, 02:47 PM
  2. Converting double to char array?
    By Blasz in forum New To Java
    Replies: 9
    Last Post: 08-29-2010, 07:32 PM
  3. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 10:40 AM
  4. Selecting the greatest "double" in a string array
    By gangsterooseven in forum New To Java
    Replies: 6
    Last Post: 11-07-2009, 11:37 PM
  5. manipulation in double dimensional array
    By akulkarni1234 in forum New To Java
    Replies: 1
    Last Post: 07-19-2009, 07:37 PM

Posting Permissions

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