AudioSystem Write, AudioInputStream from modified bytes obtained from InputStream, Java

1.9k views Asked by At

I obtaining bytes from InputStream, but I need to modify and save them to Wav File.

Here my code:

Socket Sending Audio Obtained from Microphone.

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

In the Other Side Socket receiving bytes :

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

Here the code modifier of Bytes, for Now assume amplify by two...

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

When sdLine is uncommented then I can here all sound transmitted.

AudioInputStream(InputStream stream, AudioFormat format, long length)

AudioSystem.write(AudioInputStream stream, AudioFileFormat.Type fileType, File out)

The problem:

This code Only Save the Last Bytes obtained from MethodChangerBytes.

Question:

How Save all bytes processed Wav bytes until Socket connection is closed?

Thank you

1

There are 1 answers

0
gpasch On

Have a buffer:

ByteArrayOutputStream outputStream=new ByteArrayOutputStream();

write to this buffer then move the writing outside the loop; when all bytes are read save:

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