Reading wav file in Java

May 03, 2021, at 7:30 PM

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.

Answer 1

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.

Answer 2

Equivalent to matlab's wavread function:

Answer 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...

Answer 4

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());
Answer 5

I did some research and found many ways to read wav files. I think the simplest way to read wav files from specific folder is:

private final String directoryOfSamples = "./res/semplai";
private ArrayList<File> samplesArray = new ArrayList<File>();
private final File folder = new File(directoryOfSamples);

public ReadSample()
    testSample(getSamplesArray().get(0)); // testing if sample can be played from arrayList

I hope somehow this would help for some people. Let me know if you don't understand something.

Is PowerMock using Java agents to instrument Java code?

Is PowerMock using Java agents to instrument Java code?

PowerMock is able to mock final classes because it modifies the bytecodeIs PowerMock using a Java Agent to do that or something else?

iterative through json data in flask application

iterative through json data in flask application

I've a sample flask application

how do i rectify the undefined output for this.Status?

how do i rectify the undefined output for this.Status?

My AJAX code for a reqres server doe not display the desired output or it does not update the default text when hit on submitenter image description here