Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Raw data to wav

843802Jul 25 2010 — edited Jul 26 2010
I have the following code:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);

int[] values = t.getIntArray();

for (int i = 0; i < values.length; i++)
    dos.writeInt(values);

AudioFormat wavFormat = new AudioFormat(8000, 16, 1, true, true);
byte[] abAudioData = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(abAudioData);
AudioInputStream stream = new AudioInputStream(bais, wavFormat, abAudioData.length
/ wavFormat.getFrameSize());
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File("mywav.wav"));
The problem with this is "clipping" some of the samples created are clipped.

In fact I have limited understanding of sound and WAV, I've been reading about sound in general but I still strugle with basic things.


The whole issue here is the data I am working with, it is raw integers.  So my questions are:

How do you convert such data into WAV format?  Am i on the right path by what you see above?
How do I deal with clipping?

I tried:
dos.writeInt(values[i]/100);
and
dos.writeInt(values[i]/1000);
Nothing changed except for the amplitude.

How do compute the correct amplification needed if any?
Can I just take the raw data and copy it in to the audioinputstream, or do I have to work the data before.

I know I have too many questions but I am hoping I get some input to guide me further.  Thank you in advance                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 23 2010
Added on Jul 25 2010
13 comments
1,524 views