Solved

Extracting audio data from WAV file byte array

Posted on 2003-10-30
6
1,976 Views
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 ( 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?
0
Comment
Question by:lizardpie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:jimmack
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
    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
 

Author Comment

by:lizardpie
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.
0
 
LVL 15

Expert Comment

by:jimmack
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 :-)

Jim.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:lizardpie
ID: 9658261
Jim,

Thanks for taking the time to respond.

Dave
0
 
LVL 15

Expert Comment

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

Accepted Solution

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

PashaMod
CS Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.

729 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