Extracting audio data from WAV file byte array

Posted on 2003-10-30
Last Modified: 2008-02-01
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 ( ) 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?
Question by:lizardpie
  • 3
  • 2
LVL 15

Expert Comment

ID: 9654636
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

Get the audio type and using this, get the size of a frame for that type

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.

Author Comment

ID: 9654717
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.
LVL 15

Expert Comment

ID: 9656395
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 :-)

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


Author Comment

ID: 9658261

Thanks for taking the time to respond.

LVL 15

Expert Comment

ID: 9658266
No problem.  It was quite interesting ;-)  It's always good to learn something new.

Accepted Solution

PashaMod earned 0 total points
ID: 9812055
Question closed and points refunded

CS Moderator

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 51
table example 4 29
Way to decrease size of apk file 9 65
Running JavaFX on JDeveloper 12C 1 31
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now