1
我正在用java制作反相声音(反相是反射波,x坐标不变,但y坐标颠倒)如何以字节数组的形式打印声音?或int数组?
在反射声波之前,我必须得到字节数组(或int数组)与声音。
我从我的笔记本电脑的麦克风获取声音。
这里是我的CODE(我得到了原码,里面记录声音文件,在网上,我修改了它小)
public class NoiseController extends Thread{
private TargetDataLine line;
private AudioInputStream audioInputStream;
public NoiseController(TargetDataLine line) {
this.line = line;
this.audioInputStream = new AudioInputStream(line);
}
public void start() {
line.start();
super.start();
}
public void stopRecording() {
line.stop();
line.close();
}
public void run() {
try {
int packet;
while((packet = audioInputStream.read()) != -1)
System.out.println(packet);
}
catch(IOException ioe) {
ioe.getStackTrace();
}
}
public static void main(String[] args) {
AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
TargetDataLine targetDataLine = null;
try {
targetDataLine = (TargetDataLine)AudioSystem.getLine(info);
targetDataLine.open(audioFormat);
}
catch(LineUnavailableException lue) {
out("unable to get a recording line");
lue.printStackTrace();
System.exit(-1);
}
AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
NoiseController recorder = new NoiseController(targetDataLine);
System.out.println(targetDataLine);
System.out.println(targetType);
out("Press ENTER to start the recording.");
try {
System.in.read();
}
catch(IOException ioe) {
ioe.printStackTrace();
}
recorder.start();
out("Recording...");
out("Press ENTER to stop the recording.");
try {
System.in.read();
System.in.read();
}
catch(IOException ioe) {
ioe.getStackTrace();
}
recorder.stopRecording();
out("Recording stopped.");
}
private static void out(String msg) {
System.out.println(msg);
}
}
然而,控制台不显示任何信息同时记录...... 这表明只是
[email protected]_ WAVE Press ENTER to start the recording. Recording... Press ENTER to stop the recording. Recording stopped.
如果我编辑run()
像AudioSystem.write(stream, fileType, out);
,而不是
int packet;
while((packet = audioInputStream.read()) != -1)
System.out.println(packet);
程序保存wav文件。
我的程序出了什么问题?
也许你在你的'抓抑制堆栈跟踪(IOException异常IOE){',你只'IOE。 getStackTrace();'而不是'ioe.printStackTrace();'... – 2014-11-05 08:38:42