public TargetDataLine targetDataLine;
private static AudioFormat getAudioFormat()
{
return new AudioFormat(16000, 16, 2, true, false);
}
AudioFormat a = getAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, a);
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
targetDataLine.open(a);
targetDataLine.start();
AudioInputStream ais = new AudioInputStream(targetDataLine);
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File("record.wav"));
- How can i calculate the decibel of audio at the same time?
- How can i calculate the decibel of existing wav file?
Assumption One: You would like to process and save the data at the end of your recording.
ByteArrayOutputStream
using ByteArrayOutputStream.write()ByteArrayOutputStream
intobyte[]
array.ByteArrayInputStream
using yourbyte[]
arrayProduce your audio file using
AudioSystem.write()
Assumption Two: You would like to read, process, and save the data continuously.
You need an intermediate class in order to convert
ByteArrayOutputStream
toByteArrayInputStream
. In a background thread capture audio data, and in another thread process and save the data when your desired amount of data is available.Encoding: You can encode audio samples from your
byte[]
array. In your case, two consecutive bytes produce one sample. You will get the samples for both channels one after another. If you have any channel specific processing then you need to segregate the samples for each channel. The following code snippet may help you for encoding -Decibel: Db is a logarithmic ratio of a given level with a reference level. For example, if you would like to calculate RMS/Peak in dBFS, following code snippet may help you.