java显示声音波形图示例
package _tmp;
import java.awt.borderlayout;
import java.awt.color;
import java.awt.graphics;
import java.awt.image;
import java.awt.toolkit;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.ioexception;
import java.util.arraydeque;
import java.util.deque;
import java.util.iterator;
import java.util.linkedlist;
import java.util.timer;
import java.util.timertask;
import javax.sound.sampled.audioformat;
import javax.sound.sampled.audioinputstream;
import javax.sound.sampled.audiosystem;
import javax.sound.sampled.sourcedataline;
import javax.swing.jframe;
import javax.swing.swingutilities;
public class soundtest {
public static class waveformgraph extends jframe {
private deque<short> deque = new linkedlist<short>();
private timer timer;
private image buffered;
private image showing;
public waveformgraph(int width, int height) {
setsize(width, height);
timer = new timer();
buffered = new bufferedimage(width, height, bufferedimage.type_4byte_abgr);
timer.schedule(new timertask() {
@override public void run() {
graphics g = buffered.getgraphics();
g.setcolor(color.white);
g.fillrect(0, 0, getwidth(), getheight());
g.setcolor(color.black);
g.translate(10, getheight()/2);
synchronized (deque) {
float heightrate = 1;
if(deque.size() > 1) {
iterator<short> iter = deque.iterator();
short p1 = iter.next();
short p2 = iter.next();
int x1 = 0, x2 = 0;
while(iter.hasnext()) {
g.drawline(x1, (int)(p1*heightrate), x2, (int)(p2*heightrate));
p1 = p2;
p2 = iter.next();
x1 = x2;
x2 += 1;
}
}
}
g.dispose();
swingutilities.invokelater(new runnable() {
@override public void run() {
showing = buffered;
repaint();
showing = null;
}
});
}
}, 100, 100);
}
@override
public void paint(graphics g) {
super.paint(g);
if(buffered!=null) {
g.drawimage(buffered, 0, 0, null);
}
}
public void put(short v) {
synchronized (deque) {
deque.add(v);
if(deque.size() > 500) {
deque.removefirst();
}
}
}
public void clear() {
deque.clear();
}
}
public static void main(string[] args) throws exception {
// record();
waveformgraph waveformgraph = new waveformgraph(500, 300);
waveformgraph.setdefaultcloseoperation(jframe.exit_on_close);
waveformgraph.setvisible(true);
audioinputstream ais = audiosystem.getaudioinputstream(new file("c:\\documents and settings\\wml\\my documents\\my music\\苏仨 - 失眠症.wav"));
printformat(ais.getformat());
sourcedataline player = audiosystem.getsourcedataline(ais.getformat());
player.open();
player.start();
byte[] buf = new byte[4];
int len;
while((len=ais.read(buf))!=-1) {
if(ais.getformat().getchannels() == 2) {
if(ais.getformat().getsamplerate() == 16) {
waveformgraph.put((short) ((buf[1] << 8) | buf[0]));//左声道
// waveformgraph.put((short) ((buf[3] << 8) | buf[2]));//右声道
} else {
waveformgraph.put(buf[1]);//左声道
waveformgraph.put(buf[3]);//左声道
// waveformgraph.put(buf[2]);//右声道
// waveformgraph.put(buf[4]);//右声道
}
} else {
if(ais.getformat().getsamplerate() == 16) {
waveformgraph.put((short) ((buf[1] << 8) | buf[0]));
waveformgraph.put((short) ((buf[3] << 8) | buf[2]));
} else {
waveformgraph.put(buf[1]);
waveformgraph.put(buf[2]);
waveformgraph.put(buf[3]);
waveformgraph.put(buf[4]);
}
}
player.write(buf, 0, len);
}
player.close();
ais.close();
}
public static void printformat(audioformat format) {
system.out.println(format.getencoding() + " => "
+ format.getsamplerate()+" hz, "
+ format.getsamplesizeinbits() + " bit, "
+ format.getchannels() + " channel, "
+ format.getframerate() + " frames/second, "
+ format.getframesize() + " bytes/frame");
}
// public static void record() throws lineunavailableexception,
// interruptedexception {
// audioformat audioformat = new audioformat(audioformat.encoding.pcm_signed, 48000f, 16, 1, 2, 48000f, false);
// info recorddevinfo = new dataline.info(targetdataline.class, audioformat);
//
// final targetdataline recordline = (targetdataline) audiosystem.getline(recorddevinfo);
// final sourcedataline playline = audiosystem.getsourcedataline(audioformat);
//
// recordline.open(audioformat, recordline.getbuffersize());
// playline.open(audioformat, recordline.getbuffersize());
//
// thread recorder = new thread() {
// public void run() {
// recordline.start();
// playline.start();
//
// floatcontrol fc = (floatcontrol) playline.getcontrol(floatcontrol.type.master_gain);
// double value = 2;
// float db = (float) (math.log(value == 0.0 ? 0.0001 : value) / math.log(10.0) * 20.0);
// fc.setvalue(db);
//
// try {
// audioinputstream in = new audioinputstream(recordline);
// byte[] buf = new byte[recordline.getbuffersize()];
// int len;
// while((len=in.read(buf)) != -1) {
// playline.write(buf, 0, len);
// }
// } catch (ioexception e) {
// e.printstacktrace();
// } finally {
// recordline.stop();
// playline.stop();
// }
// };
// };
// recorder.start();
// recorder.join();
// }
}
上一篇: mysql创建内存表的方法