2016-12-25 13 views
1

InputStreamからバイトを取得していますが、それらを変更してWavファイルに保存する必要があります。AudioSystem Write、InputStream、Javaから取得した変更されたバイトのAudioInputStream

ここに私のコード:

マイクから得られた音声。アザー・サイドソケットに

AudioFormat adfmt = new AudioFormat(8000.0f, 8, 1, true , true); 
int bufferSize = (int) adfmt.getSampleRate()* adfmt.getFrameSize(); 
byte[] buffer = new byte[bufferSize]; 
Socket clientSocketO = new Socket(...); 
OutputStream output = clientSocketO.getOutputStream(); 

DataLine.Info dlInfo = new DataLine.Info(TargetDataLine.class, adfmt); 
TargetDataLine tdLine = (TargetDataLine) AudioSystem.getLine(dlInfo); 
tdLine.open(adfmt); 
tdLine.start(); // start capturing 

boolean bRunningO = true; 
while (bRunningO) { 
    int count = tdLine.read(buffer, 0, buffer.length); 
    if (count > 0) { 
     byte[] outgoingBytes = Arrays.copyOf(buffer, count); 
     output.write(outgoingBytes); 
    } 
} 
tdLine.flush(); 

バイト受信:ここではバイトのコード修飾子を

AudioFormat adfmt = new AudioFormat(8000.0f, 8, 1, true , true); 
int bufferSize = (int) adfmt.getSampleRate()* adfmt.getFrameSize(); 
byte[] buffer = new byte[bufferSize]; 
Socket clientSocketI = new Socket(...); 
InputStream input = clientSocketI.getInputStream(); 
String fileName = System.getProperty("file.separator") + "SomeFile.wav" 
File fileStreamedWav = new File((new File("")).getAbsolutePath() + fileName); 
AudioInputStream ais; 
ByteArrayInputStream bis; 
DataLine.Info dlInfo = new DataLine.Info(SourceDataLine.class, adfmt); 
//SourceDataLine sdLine = (SourceDataLine) AudioSystem.getLine(dlInfo); 
//sdLine.open(adfmt); 
//sdLine.start(); // start playback 

AudioFileFormat.Type afType = AudioFileFormat.Type.WAVE; 
boolean bRunningI = true; 
while (bRunningI) { 
    try { 
     int read = input.read(buffer); //Socket Reading bytes 
     byte[] incomingBytes; 
     if (read > 0) { 
      incomingBytes = Arrays.copyOf(buffer, read); 
      if (incomingBytes!= null) { 
       //sdLine.write(incomingBytes, 0, incomingBytes.length); 

       //Same Size bytes, but isn't necessary submit the put Code 
       byte[] changedBytes = MethodChangerBytes(incomingBytes); 
       bis = new ByteArrayInputStream(changedBytes); 
       ais = new AudioInputStream(bis, adfmt, 
        changedBytes.length/adfmt.getFrameSize()); 
       int W = AudioSystem.write(ais, afType, fileStreamedWav); 
       System.out.println("AudioSystem.write:" + W); 
      } 
     } 
    } catch (IOException e) { 
     bRunningI = false; 
    } 
} 

を、今のsdLineはその後、コメント解除された場合2 ...

byte[] MethodChangerBytes(byte[] incoming) { 
    byte[] outgoing = new byte[incoming.length]; 
    for (int i = 0; i < incoming.length; i ++) { 
     // Really is not important what happens here 
     double Sample = (double)(short)(((incoming[i] - 128) & 0xFF) << 8); 
     Sample *= 2.0; 
     outgoing[i] = (byte)(((int()Sample >> 8) + 128) & 0xFF); 
    } 
    return outgoing; 
} 

によって増幅すると仮定私はここですべての音を伝えることができます。

たAudioInputStream(入力ストリームのストリームのAudioFormatフォーマット、長い長さ)

AudioSystem.write(たAudioInputStreamストリーム、AudioFileFormat.TypeたfileTypeは、アウトファイル)

問題

このコードのみMethodChangerBytesから取得した最後のバイトを保存します。

質問

どのようにソケット接続が閉じられるまでのWavバイトを処理し、すべてのバイトを保存しますか?

答えて

1

がバッファ持っていただきありがとうございます:ループの外書き込みを移動し、このバッファに

ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); 

書き込み。すべてのバイトが読み込まれると保存:

boolean bRunningI = true; 
try { 
while (bRunningI) { 
    int read = input.read(buffer); //Socket Reading bytes 
    byte[] incomingBytes; 
    if (read > 0) { 
     incomingBytes = Arrays.copyOf(buffer, read); 
     if (incomingBytes!= null) { 
      //sdLine.write(incomingBytes, 0, incomingBytes.length); 

      //Same Size bytes, but isn't necessary submit the put Code 
      byte[] changedBytes = MethodChangerBytes(incomingBytes); 
      outputStream.write(changedBytes, 0, changedBytes.length); 
     } 
    } 
} 
byte[] allBytes=outputStream.toByteArray(); 
bis = new ByteArrayInputStream(allBytes); 
ais = new AudioInputStream(bis, adfmt, 
       changedBytes.length/adfmt.getFrameSize()); 
int W = AudioSystem.write(ais, afType, fileStreamedWav); 
System.out.println("AudioSystem.write:" + W); 
} catch (IOException e) { 
    bRunningI = false; 
} 
関連する問題