Do I need to care about big endian and little endian when I read data through AudioInputStream?
Asked Answered
H

1

10

I am reading a wav file through AudioInputStream into a byte array,

    AudioInputStream audiofile = AudioSystem.getAudioInputStream(f);
    byte[] audio=new byte[numberofframes*framesize];
    int bytes=audiofile.read(audio);

do I need to arrange the bytes of a sample considering that the data is arranged in little endian or does the AudioInputStream do it for me?

Hirokohiroshi answered 2/1, 2013 at 4:41 Comment(5)
are you reading from file?Firm
yes I am through the JFilechooser, File f = chooser.getSelectedFile();Hirokohiroshi
framesize What is the value of that? If 1, endian is not relevant. If 4, it is.Lipase
@AndrewThompson I think it could be any number as framesize=(bitdepth*numberofchannels)/8, in bytes,what do you think about 8 bit mltichannel wav file?Hirokohiroshi
"I think it could be any number as framesize" I am certain it would be 1, 2` or 4.Lipase
J
1

Big- versus little-endian matters if the data is encoded in more than a single byte, e.g., bit depths of 16 or more, regardless of the number of channels. Java does not automatically arrange the PCM bytes in a default order, it just accepts them.

The following is the clearest, best written single section of the java audio tutorials, imho, and covers issues pertaining to formats and their conversions:

http://docs.oracle.com/javase/tutorial/sound/converters.html

Johst answered 2/1, 2013 at 17:4 Comment(2)
SO you say the byte array will have Formatted Audio Data i.e. in Little Endian which I'll have to convert by my self to read the samples?Hirokohiroshi
Output lines can either be Big- or Little-Endian, yes? If you input the opposite type, then I'm 90% sure you will have to do a swap of the bytes before outputting. But there likely will be a converter for this, so you don't necessarily have to get your hands dirty at the byte level. Did you look at the link I sent? I'm not sure how appropriate the term "Formatted Audio Data" is for PCM data. If the encoding is 16-bit or more, the bytes have to be in one order or the other, regardless. I don't think the AudioInputStream "cares," it just brings in raw bytes of PCM.Johst

© 2022 - 2024 — McMap. All rights reserved.