Extracting audio data from WAV file byte array

I am creating an audio player using the Java Sound API (using J2SDK 1.4.2). WAV files are stored in a database.  These WAV files are passed to me in the form of a byte array (headers included). Is there any easy way to extract the audio data without building a parser?

I've been creating an AudioInputStream as follows:

byte[] wavFileBytes = getByteArrayFromDataBase();
ByteArrayInputStream bais = new ByteArrayInputStream(wavFileBytes);
AudioInputStream ais = AudioSystem.getAudioInputStream(bais);

However, when I read the AudioInputStream ( ais.read(...) ) I get the WAV headers as well. Actually, I get back the original byte array (wavFileBytes). I was expecting the call to AudioSystem.getAudioInputStream() to be smart enough to know that headers were in the original byte array. I can actually play this data, but there is an annoying click at the start due to the headers.

I was reviewing how the Java Sound demo does it, but it reads a WAV file from the file system using:

File file = new File("<filepath>");AudioInputStream ais = AudioSystem.getAudioInputStream(file);

In this case, when the AudioInputStream is read only the audio bytes are received.

Does anyone know a way around this problem? Is there a way to treat the byte array as a file?
lizardpieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimmackCommented:
I know nothing about audio, so this is just based on what I've found in the APIs ;-)

Could you use the following:

Get the length (in sample frames) from ais
    ais.getFrameLength()

Get the audio type and using this, get the size of a frame for that type
    ais.getFormat().getFrameSize()

Mulitply the two together to calculate the size (in bytes) of the data in the audio

Remove (or skip) wavFileBytes.length - audioLength.

As I said, I've no idea if this will work or not, but it looks like it might.
0
lizardpieAuthor Commented:
The research I've done into wav files indicates that the data chunk doesn't have to be the last thing in the file.  There could be subsequent chunks.  Maybe this doesn't happen in practice, but you never know.  I know I could build a little parser that looks for the data chunk, which already contains the length of data, but apparently there are other considerations as well.  The fmt (format) could specify some form of compression used in the data.  I don't know all the ins and out of audio either.

However, I think I've solved my own problem.  I'm writing the byte array to a temporary file and then creating my AudioInputStream from it with AudioSystem.getAudioInputStream(File file). Now when I read the audioInputStream I just get the audio data and no headers.  I just tested it and the waveform now looks and sounds clean.
0
jimmackCommented:
File and data formats like this are never "easy" to deal with :-(

Thanks for the info about wavs.  I didn't know that :-)

Sorry I couldn't help more, but at least you've got your solution :-)

Jim.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

lizardpieAuthor Commented:
Jim,

Thanks for taking the time to respond.

Dave
0
jimmackCommented:
No problem.  It was quite interesting ;-)  It's always good to learn something new.
0
PashaModCommented:
Question closed and points refunded

PashaMod
CS Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.