2014-11-05 42 views
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文件。

我的程序出了什么问题?

+1

也许你在你的'抓抑制堆栈跟踪(IOException异常IOE){',你只'IOE。 getStackTrace();'而不是'ioe.printStackTrace();'... – 2014-11-05 08:38:42

回答

0

由于Uwe Allner说你不打印Exception。

从来就还设法纠正它,我想结果一定是这样的:

public class NoiseController extends Thread { 
    private final TargetDataLine  line; 
    private final AudioInputStream audioInputStream; 

    public NoiseController(final TargetDataLine line) { 
     this.line = line; 
     this.audioInputStream = new AudioInputStream(line); 
    } 

    @Override 
    public void start() { 
     line.start(); 
     super.start(); 
    } 

    public void stopRecording() { 
     line.stop(); 
     line.close(); 
     try { 
      audioInputStream.close(); 
     } catch (final IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void run() { 
     try { 
      final int bufferSize = 1024; 
      int read = 0; 
      final byte[] frame = new byte[bufferSize]; 
      while ((read = audioInputStream.read(frame)) != -1 && line.isOpen()) { 
       // only the first read bytes are valid 
       System.out.println(Arrays.toString(frame)); 
      } 
     } catch (final IOException ioe) { 
      ioe.printStackTrace(); 
     } 
    } 

    public static void main(final String[] args) { 
     final AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F, false); 
     final DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat); 
     TargetDataLine targetDataLine = null; 

     try { 
      targetDataLine = (TargetDataLine) AudioSystem.getLine(info); 
      targetDataLine.open(audioFormat); 
     } catch (final LineUnavailableException lue) { 
      out("unable to get a recording line"); 
      lue.printStackTrace(); 
      System.exit(-1); 
     } 

     final AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE; 
     final 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 (final IOException ioe) { 
      ioe.printStackTrace(); 
     } 

     recorder.start(); 
     out("Recording..."); 
     out("Press ENTER to stop the recording."); 

     try { 
      System.in.read(); 
      System.in.read(); 
     } catch (final IOException ioe) { 
      ioe.printStackTrace(); 
     } 

     recorder.stopRecording(); 
     out("Recording stopped."); 
    } 

    private static void out(final String msg) { 
     System.out.println(msg); 
    } 
}