This code is producing WAV files that don't work in many apps.
When I check in a RIFFVIEWER app it complains about invalid RIFF length. BWFMetaEdit claims the file is truncated. Some tolerant apps like Audacity will play them.
Am I doing something wrong here or is java audio buggy?
// The essence data is PCM formatted, so convert it to a WAVE file
File extractPCM(WAVEPCMDescriptor descriptor, EssenceData data, String name) {
try {
Stream stream = data.getEssenceStream();
URI uri = stream.getStreamURI();
int hashCode = uri.hashCode();
File file = new File(mediaDir,
name + "_" +
String.format("%08X", hashCode)
+ ".wav"
);
if (file.exists()) {
return file;
}
mediaDir.mkdir(); // Ensure exists
log("Copying essence data stream");
stream.setPosition(0);
ByteBuffer buff = stream.read((int)stream.getLength());
stream.close();
buff.flip();
AudioFormat format = new AudioFormat(
Encoding.PCM_SIGNED,
(float)descriptor.getSampleRate().doubleValue(),
(int)descriptor.getQuantizationBits(),
(int)descriptor.getChannelCount(),
(int)descriptor.getBlockAlign(),
(float)descriptor.getAverageBytesPerSecond(),
false
);
AudioInputStream input = new AudioInputStream(
new ByteArrayInputStream(buff.array()), format, buff.capacity());
AudioSystem.write(input,
AudioFileFormat.Type.WAVE, file);
log("Extracted file " + file);
return file;
} catch (EndOfDataException | IllegalArgumentException | IOException e1) {
log(e1);
return null;
}
}
It looks like I was supplying the length as bytes instead of sample frames.
When I divide the buffer size by the frame size, non tolerant apps stop complaining.
AudioInputStream input = new AudioInputStream(
new ByteArrayInputStream(buff.array()), format, buff.capacity() / format.getFrameSize());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.