tags:

views:

134

answers:

2

In a java program, what is the best way to read an audio file (wav file) to an array of numbers (float[], short[],...), and to write a wav file from an array of numbers?

A: 

Wave files are supported by the javax.sound.sample package

Since isn't a trivial API you should read an articel / tutorial which introduces the API like

Java Sound, An Introduction

stacker
A: 

Some more detail on what you'd like to achieve would be helpful. If raw WAV data is okay for you, simply use a FileInputStream and probably a Scanner to turn it into numbers. But let me try to give you some meaningful sample code to get you started:

There is a class called com.sun.media.sound.WaveFileWriter for this purpose.

InputStream in = ...;
OutputStream out = ...;

AudioInputStream in = AudioSystem.getAudioInputStream(in);

WaveFileWriter writer = new WaveFileWriter();
writer.write(in, AudioFileFormat.Type.WAVE, outStream);

You could implement your own AudioInputStream that does whatever voodoo to turn your number arrays into audio data.

writer.write(new VoodooAudioInputStream(numbers), AudioFileFormat.Type.WAVE, outStream);

As @stacker mentioned, you should get yourself familiar with the API of course.

sfussenegger
My main problem was that voodoo itself. I wanted to see if there was ready code/class that did it.I think I succeeded now, using AudioSystem and AudioInputStream. The trick was to reverse the order of bytes in each sound sample before I convert it to short, since WAV encodes the numeric values in little-Endian manner.Thank you, Yonatan.
yonatan