This question explains how to convert pcm to wav.
I copied the Discord audio into a List<byte[]>
by overriding the Method AudioReceiveHandler#handleCombinedAudio
:
private List<byte[]> rescievedBytes=new ArrayList<>();
@Override
public void handleCombinedAudio(CombinedAudio combinedAudio) {
try {
rescievedBytes.add(combinedAudio.getAudioData(VOLUME));
}catch (OutOfMemoryError e) {
//close connection
}
}
After that, I copied the List
into a byte[]
and created the wav
file.
try {
int size=0;
for (byte[] bs : rescievedBytes) {
size+=bs.length;
}
byte[] decodedData=new byte[size];
int i=0;
for (byte[] bs : rescievedBytes) {
for (int j = 0; j < bs.length; j++) {
decodedData[i++]=bs[j];
}
}
getWavFile(getNextFile(), decodedData);
} catch (IOException|OutOfMemoryError e) {
e.printStackTrace();
}
private void getWavFile(File outFile, byte[] decodedData) throws IOException {
AudioFormat format = new AudioFormat(8000, 16, 1, true, false);
AudioSystem.write(new AudioInputStream(new ByteArrayInputStream(
decodedData), format, decodedData.length), AudioFileFormat.Type.WAVE, outFile);
}
In case that the rescieved audio gets too big (OutOfMemoryError
) the conversion is aborted.