2016-12-25 26 views
1

我从InputStream获取字节,但我需要修改并将它们保存到Wav文件。AudioSystem Write,AudioInputStream从InputStream获得的修改字节,Java

这里我的代码:

套接字发送音频从麦克风获得。

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(); 

其另一端的Socket接收字节:

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; 
} 

当sdLine是注释掉然后我可以在这里传送所有声音。

的AudioInputStream(InputStream的流,AudioFormat的格式,长的长度)

AudioSystem.write(的AudioInputStream流,为AudioFileFormat.Type的fileType,文件出)

问题

此代码仅保存从MethodChangerBytes获取的最后字节。

问题

如何保存处理的Wav字节的所有字节,直到Socket连接被关闭?

谢谢

回答

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; 
}