1

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

这是我的代码:

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

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

这里是字节的代码修饰符,现在假设放大两个...

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 文件类型,文件输出)

问题

此代码仅保存从 MethodChangerBytes 获得的最后一个字节。

问题

如何在 Socket 连接关闭之前保存所有已处理的 Wav 字节?

谢谢

4

1 回答 1

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;
}
于 2016-12-25T22:27:24.337 回答