14

I want to read wav files in Java and I am going to classify them with K-means.

How can I read wav files in Java and assign them into an array or something like that(you can suggest ideas for it) to classify them?

EDIT: I want to use APIs for reading wav files and for K-means.

kamaci
  • 72,915
  • 69
  • 228
  • 366
  • 1
    I think this [thread](http://stackoverflow.com/questions/3297749/java-reading-manipulating-and-writing-wav-files) will help you get started. – Umesh K Mar 06 '11 at 11:24

4 Answers4

21

The official Java Sound Programmer Guide walks through reading and writing audio files.

This article by A Greensted: Reading and Writing Wav Files in java should be helpful. The WavFile class is very useful and it can be tweaked to return the entire data array instead of buffered fragments.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
shams
  • 3,460
  • 24
  • 24
3

You could read the sound files using javax sound library and FileInputStream (found a nice example here) and treat the wave files as a vector of bits (0,1) or bytes.. using multiple sequence alignment (Wiki) create a distance matrix between every stream of bits/bytes, and from there, the clustering should be straight forward.

The Problem is, that this method is very sensitive to noise, etc, but it is worth a shot...

Protostome
  • 5,569
  • 5
  • 30
  • 45
3

Equivalent to matlab's wavread function:

http://web.archive.org/web/20120531113946/http://www.builogic.com/java/javasound-read-write.html

sastanin
  • 40,473
  • 13
  • 103
  • 130
aishas11
  • 31
  • 3
1

Not sure if this will help someone. Java JDK already provides AudioSystem class.

I used this as part of my tests to check generated WAV properties,

AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(new File(response.get()));
assertEquals(1, audioFileFormat.getFormat().getChannels());
assertEquals(8000.0, audioFileFormat.getFormat().getSampleRate(), 0.0f);
assertEquals(8, audioFileFormat.getFormat().getSampleSizeInBits());
Alexpandiyan Chokkan
  • 1,025
  • 1
  • 10
  • 30