2016-12-25 11 views
1

InputStream에서 바이트를 가져 오지만이를 Wav File에 수정하고 저장해야합니다.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(); 

수신 바이트 :

이 sdLine 후 주석 여기
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; 
    } 
} 

바이트의 코드를 수정, 이제 두 가지에 의해 증폭 가정에 대한 ...

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를 (의 InputStream 스트림, 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; 
}