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