我以前做的一部分吧,波形图其实是时间与PCM的数据振幅大小的对应关系,所以又叫时域图,频谱图是频率与能量(分贝)的对应关系,又叫频域图。两者可以通过FFT进行转换。 明白了这个后,就好做了,下面是我以前做的部分代码:供参考if(fileName == null){ throw new Exception("File name can't be null!"); }
File file = new File(fileName); if(!file.exists()){ throw new Exception(fileName + " is not exist!"); } AudioInputStream ais = null; AudioFormat baseFormat = null; DataLine.Info info = null; SourceDataLine line = null; try { ais = AudioSystem.getAudioInputStream(file); baseFormat = ais.getFormat();
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(baseFormat); line.start(); int intBytes = 0; vector.clear();
while (intBytes != -1) { intBytes = ais.read(buf, 0, BUFFER_SIZE);// 从音频流读取指定的最大数量的数据字节,并将其放入给定的字节数组中。 for(int c = 0; c < intBytes; c++){ //System.out.println(buf[c]); int value = buf[c] > 0 ? buf[c]:(buf[c] + 256); vector.add(value); } //播放代码 //if (intBytes >= 0) { // int outBytes = line.write(buf, 0, intBytes);// 通过此源数据行将音频数据写入混频器。 //} }
} catch (UnsupportedAudioFileException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (LineUnavailableException e) { // TODO Auto-generated catch block e.printStackTrace(); } float[] f = new float[vector.size()]; File ff = new File("f:\\pcm.txt"); FileWriter fw = new FileWriter(ff); for(int c = 0; c < vector.size(); c++){ f[c] = vector.get(c); fw.write("" + f[c] + "\r\n");
} fw.flush(); fw.close();
Complex[] comples = new Complex[1024]; for(int c = 0; c < f.length; c++){ comples[c] = new Complex(f[c], 0); } for(int c = 971; c < 1024; c++){ comples[c] = new Complex(0, 0); }
Complex[] values = FFT.fft(comples); double[] d = FFT.normalize(values);
File fff = new File("f:\\pcm2.txt"); FileWriter fw2 = new FileWriter(fff); for(int c= 0; c < d.length; c++){
波形好做,频谱需要FFT转换,比较复杂。
明白了这个后,就好做了,下面是我以前做的部分代码:供参考if(fileName == null){
throw new Exception("File name can't be null!");
}
File file = new File(fileName);
if(!file.exists()){
throw new Exception(fileName + " is not exist!");
}
AudioInputStream ais = null;
AudioFormat baseFormat = null;
DataLine.Info info = null;
SourceDataLine line = null;
try {
ais = AudioSystem.getAudioInputStream(file);
baseFormat = ais.getFormat();
info = new DataLine.Info(SourceDataLine.class,
baseFormat);
//提取信息
this.channels = baseFormat.getChannels();
this.frameRate = baseFormat.getFrameRate();
this.frameSize = baseFormat.getFrameSize();
this.sampleRate = baseFormat.getSampleRate();
this.sampleSizeInBits = baseFormat.getSampleSizeInBits();
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(baseFormat);
line.start();
int intBytes = 0;
vector.clear();
while (intBytes != -1) {
intBytes = ais.read(buf, 0, BUFFER_SIZE);// 从音频流读取指定的最大数量的数据字节,并将其放入给定的字节数组中。
for(int c = 0; c < intBytes; c++){
//System.out.println(buf[c]);
int value = buf[c] > 0 ? buf[c]:(buf[c] + 256);
vector.add(value);
}
//播放代码
//if (intBytes >= 0) {
// int outBytes = line.write(buf, 0, intBytes);// 通过此源数据行将音频数据写入混频器。
//}
}
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
float[] f = new float[vector.size()];
File ff = new File("f:\\pcm.txt");
FileWriter fw = new FileWriter(ff);
for(int c = 0; c < vector.size(); c++){
f[c] = vector.get(c);
fw.write("" + f[c] + "\r\n");
}
fw.flush();
fw.close();
Complex[] comples = new Complex[1024];
for(int c = 0; c < f.length; c++){
comples[c] = new Complex(f[c], 0);
}
for(int c = 971; c < 1024; c++){
comples[c] = new Complex(0, 0);
}
Complex[] values = FFT.fft(comples);
double[] d = FFT.normalize(values);
File fff = new File("f:\\pcm2.txt");
FileWriter fw2 = new FileWriter(fff);
for(int c= 0; c < d.length; c++){
fw2.write("" + d[c] + "\r\n");
}