Reading wav file in Java

2019-01-09 07:17发布

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.

4条回答
2楼-- · 2019-01-09 07:31

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()
{

    samplesArray.addAll(Arrays.asList(folder.listFiles()));
    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.

查看更多
Anthone
3楼-- · 2019-01-09 07:35

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

查看更多
smile是对你的礼貌
4楼-- · 2019-01-09 07:46

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.

查看更多
登录 后发表回答