6

Possible Duplicate:
byte array to short array and back again in java

the encodeAudio() method in Xuggler has the following parameters:

  • int streamIndes
  • short[] samples
  • long timeStamp
  • TimeUnit unit

  • Using TargetDataLine from javax.sound.sampled I can read the data into a byte[] array
    byte[] tempBuffer = new byte[10000];
    fromMic.read(tempBuffer,0,tempBuffer.length);
    

    But the problem is that the samples argument needs short[]

    Community
    • 1
    • 1
    An SO User
    • 24,612
    • 35
    • 133
    • 221
    • 5
      [byte-array-to-short-array-and-back-again-in-java](http://stackoverflow.com/questions/5625573/byte-array-to-short-array-and-back-again-in-java) – Rohit Jain Dec 25 '12 at 18:58
    • I am not using BigEndian encoding. – An SO User Dec 25 '12 at 19:00
    • It's not nearly as simple as that. You have a byte array with data in a specific format, and you need to do some conversion. See: https://groups.google.com/forum/?fromgroups=#!topic/xuggler-users/cKLS5KmbEIM – Diego Basch Dec 25 '12 at 19:05

    1 Answers1

    10

    You are lucky enough that byte is "fully castable" to short, so:

    // Grab size of the byte array, create an array of shorts of the same size
    int size = byteArray.length;
    short[] shortArray = new short[size];
    
    for (int index = 0; index < size; index++)
        shortArray[index] = (short) byteArray[index];
    

    And then use shortArray.

    Note: as far as primitive type goes, Java always treats them in big endian order, so converting, say, byte ff will yield short 00ff.

    fge
    • 119,121
    • 33
    • 254
    • 329
    • my `AudioFormat` object has set BigEndian to false. whill this be a problem? – An SO User Dec 25 '12 at 19:01
    • 2
      Ouch... Yes that is, in which case the first comment is the solution for you. – fge Dec 25 '12 at 19:02
    • and will it cause an issue if I set BigEndian to `true` because I do not know what that does. I am merely setting it to true or false arbitrarily. :) – An SO User Dec 25 '12 at 19:03
    • 1
      Typo... the i++ should be index++. – phatfingers Dec 25 '12 at 19:07
    • OK, let's take number 255: as a `byte`, this is `ff`. If you were to represent it on two bytes instead of one, on a little endian setup this would be `ff 00`, whereas on a big endian setup this would be `00 ff`. Note that network order is big endian, and that as I mentioned, Java's primitive types are all treated as big endian, even if the underlying CPU architectyre does not think that way (famous example: all x86/x86-64 archs). I suggest you treat everything as big endian, the code above will probably work. Note that `ByteBuffer` and friends also use big endian by default. – fge Dec 25 '12 at 19:08
    • @phatfingers: your name tells it all, doesn't it ;) Fixed, thanks! – fge Dec 25 '12 at 19:09
    • @fge I am stuck and I need help. Would you like me to include the code in the question?? – An SO User Dec 25 '12 at 19:15
    • 1
      @LittleChild certainly so, since all of StackOverflow can find what is wrong with it, if anything. – fge Dec 25 '12 at 19:16
    • @fge sorry, but I am dumb – An SO User Dec 25 '12 at 19:21