3
votes

I am modifying an application that plays audio data to write the data to a file instead. As it is currently implemented, a byte array is filled dynamically, and the contents of this buffer are written to a SourceDataLine each time it is filled. I basically want to write that buffer out to a file in a specified format.

I have read through this official tutorial and came across this code snipped for writing audio data to a file:

File fileOut = new File(someNewPathName);
AudioFileFormat.Type fileType = fileFormat.getType();
if (AudioSystem.isFileTypeSupported(fileType, 
    audioInputStream)) {
  AudioSystem.write(audioInputStream, fileType, fileOut);
}

I see from the API documentation that I can construct an AudioInputStream using a TargetDataLine, however in my case I have a SourceDataLine. I don't know how to get the data from my byte array into the TargetDataLine since it implements the read() method instead of write(). Other uses of the AudioInputStream in that and other documentation treat it as a way of reading from a file, so I'm a little confused by its use with AudioSystem.write().

So, how can I get the data from a SourceDataLine, or from the buffer directly, into a TargetDataLine or AudioInputStream so that it can be written out to a file?

1
what is the solution you used ? Were you able to write in a file what's being played/listened onto the speakers ?coding_idiot
@coding_idiot the audio was just a sequence of tones, so I was able to reproduce them from a tone generator more easily than copying them programmaticallyTravis Christian

1 Answers

2
votes
  1. Use the byte[] to establish a ByteArrayInputStream
  2. Provide the BAIS to AudioSystem.getAudioInputStream(InputStream)
  3. Use the AIS in AudioSystem.write(..)