2014-11-05 40 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); 
    } 
}