8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
Reviewed-by: prr, amenkov
This commit is contained in:
parent
8372c1c09a
commit
717ad7019c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -26,11 +26,11 @@
|
|||||||
package com.sun.media.sound;
|
package com.sun.media.sound;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
import javax.sound.sampled.AudioFileFormat;
|
import javax.sound.sampled.AudioFileFormat;
|
||||||
|
import javax.sound.sampled.AudioFileFormat.Type;
|
||||||
import javax.sound.sampled.AudioFormat;
|
import javax.sound.sampled.AudioFormat;
|
||||||
import javax.sound.sampled.AudioSystem;
|
import javax.sound.sampled.AudioSystem;
|
||||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||||
@ -49,11 +49,6 @@ public final class AiffFileReader extends SunFileReader {
|
|||||||
throws UnsupportedAudioFileException, IOException {
|
throws UnsupportedAudioFileException, IOException {
|
||||||
DataInputStream dis = new DataInputStream(stream);
|
DataInputStream dis = new DataInputStream(stream);
|
||||||
|
|
||||||
// assumes a stream at the beginning of the file which has already
|
|
||||||
// passed the magic number test...
|
|
||||||
// leaves the input stream at the beginning of the audio data
|
|
||||||
int fileRead = 0;
|
|
||||||
int dataLength = 0;
|
|
||||||
AudioFormat format = null;
|
AudioFormat format = null;
|
||||||
|
|
||||||
// Read the magic number
|
// Read the magic number
|
||||||
@ -65,9 +60,9 @@ public final class AiffFileReader extends SunFileReader {
|
|||||||
throw new UnsupportedAudioFileException("not an AIFF file");
|
throw new UnsupportedAudioFileException("not an AIFF file");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int frameLength = 0;
|
||||||
int length = dis.readInt();
|
int length = dis.readInt();
|
||||||
int iffType = dis.readInt();
|
int iffType = dis.readInt();
|
||||||
fileRead += 12;
|
|
||||||
|
|
||||||
int totallength;
|
int totallength;
|
||||||
if(length <= 0 ) {
|
if(length <= 0 ) {
|
||||||
@ -91,7 +86,6 @@ public final class AiffFileReader extends SunFileReader {
|
|||||||
// Read the chunk name
|
// Read the chunk name
|
||||||
int chunkName = dis.readInt();
|
int chunkName = dis.readInt();
|
||||||
int chunkLen = dis.readInt();
|
int chunkLen = dis.readInt();
|
||||||
fileRead += 8;
|
|
||||||
|
|
||||||
int chunkRead = 0;
|
int chunkRead = 0;
|
||||||
|
|
||||||
@ -112,7 +106,13 @@ public final class AiffFileReader extends SunFileReader {
|
|||||||
if (channels <= 0) {
|
if (channels <= 0) {
|
||||||
throw new UnsupportedAudioFileException("Invalid number of channels");
|
throw new UnsupportedAudioFileException("Invalid number of channels");
|
||||||
}
|
}
|
||||||
dis.readInt(); // numSampleFrames
|
frameLength = dis.readInt(); // numSampleFrames
|
||||||
|
if (frameLength < 0) {
|
||||||
|
// AiffFileFormat uses int, unlike AIS which uses long
|
||||||
|
//TODO this (negative) value should be passed as long to AIS
|
||||||
|
frameLength = AudioSystem.NOT_SPECIFIED;
|
||||||
|
}
|
||||||
|
|
||||||
int sampleSizeInBits = dis.readUnsignedShort();
|
int sampleSizeInBits = dis.readUnsignedShort();
|
||||||
if (sampleSizeInBits < 1 || sampleSizeInBits > 32) {
|
if (sampleSizeInBits < 1 || sampleSizeInBits > 32) {
|
||||||
throw new UnsupportedAudioFileException("Invalid AIFF/COMM sampleSize");
|
throw new UnsupportedAudioFileException("Invalid AIFF/COMM sampleSize");
|
||||||
@ -149,38 +149,17 @@ public final class AiffFileReader extends SunFileReader {
|
|||||||
break;
|
break;
|
||||||
case AiffFileFormat.SSND_MAGIC:
|
case AiffFileFormat.SSND_MAGIC:
|
||||||
// Data chunk.
|
// Data chunk.
|
||||||
// we are getting *weird* numbers for chunkLen sometimes;
|
int dataOffset = dis.readInt(); // for now unused in javasound
|
||||||
// this really should be the size of the data chunk....
|
int blocksize = dis.readInt(); // for now unused in javasound
|
||||||
int dataOffset = dis.readInt();
|
|
||||||
int blocksize = dis.readInt();
|
|
||||||
chunkRead += 8;
|
chunkRead += 8;
|
||||||
|
|
||||||
// okay, now we are done reading the header. we need to set the size
|
|
||||||
// of the data segment. we know that sometimes the value we get for
|
|
||||||
// the chunksize is absurd. this is the best i can think of:if the
|
|
||||||
// value seems okay, use it. otherwise, we get our value of
|
|
||||||
// length by assuming that everything left is the data segment;
|
|
||||||
// its length should be our original length (for all AIFF data chunks)
|
|
||||||
// minus what we've read so far.
|
|
||||||
// $$kk: we should be able to get length for the data chunk right after
|
|
||||||
// we find "SSND." however, some aiff files give *weird* numbers. what
|
|
||||||
// is going on??
|
|
||||||
|
|
||||||
if (chunkLen < length) {
|
|
||||||
dataLength = chunkLen - chunkRead;
|
|
||||||
} else {
|
|
||||||
// $$kk: 11.03.98: this seems dangerous!
|
|
||||||
dataLength = length - (fileRead + chunkRead);
|
|
||||||
}
|
|
||||||
ssndFound = true;
|
ssndFound = true;
|
||||||
break;
|
break;
|
||||||
} // switch
|
} // switch
|
||||||
fileRead += chunkRead;
|
|
||||||
// skip the remainder of this chunk
|
// skip the remainder of this chunk
|
||||||
if (!ssndFound) {
|
if (!ssndFound) {
|
||||||
int toSkip = chunkLen - chunkRead;
|
int toSkip = chunkLen - chunkRead;
|
||||||
if (toSkip > 0) {
|
if (toSkip > 0) {
|
||||||
fileRead += dis.skipBytes(toSkip);
|
dis.skipBytes(toSkip);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // while
|
} // while
|
||||||
@ -188,36 +167,12 @@ public final class AiffFileReader extends SunFileReader {
|
|||||||
if (format == null) {
|
if (format == null) {
|
||||||
throw new UnsupportedAudioFileException("missing COMM chunk");
|
throw new UnsupportedAudioFileException("missing COMM chunk");
|
||||||
}
|
}
|
||||||
AudioFileFormat.Type type = aifc?AudioFileFormat.Type.AIFC:AudioFileFormat.Type.AIFF;
|
Type type = aifc ? Type.AIFC : Type.AIFF;
|
||||||
|
|
||||||
return new AiffFileFormat(type, totallength, format, dataLength / format.getFrameSize());
|
return new AiffFileFormat(type, totallength, format, frameLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
// HELPER METHODS
|
// HELPER METHODS
|
||||||
/** write_ieee_extended(DataOutputStream dos, double f) throws IOException {
|
|
||||||
* Extended precision IEEE floating-point conversion routine.
|
|
||||||
* @argument DataOutputStream
|
|
||||||
* @argument double
|
|
||||||
* @return void
|
|
||||||
* @exception IOException
|
|
||||||
*/
|
|
||||||
private void write_ieee_extended(DataOutputStream dos, double f) throws IOException {
|
|
||||||
|
|
||||||
int exponent = 16398;
|
|
||||||
double highMantissa = f;
|
|
||||||
|
|
||||||
// For now write the integer portion of f
|
|
||||||
// $$jb: 03.30.99: stay in synch with JMF on this!!!!
|
|
||||||
while (highMantissa < 44000) {
|
|
||||||
highMantissa *= 2;
|
|
||||||
exponent--;
|
|
||||||
}
|
|
||||||
dos.writeShort(exponent);
|
|
||||||
dos.writeInt( ((int) highMantissa) << 16);
|
|
||||||
dos.writeInt(0); // low Mantissa
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* read_ieee_extended
|
* read_ieee_extended
|
||||||
* Extended precision IEEE floating-point conversion routine.
|
* Extended precision IEEE floating-point conversion routine.
|
||||||
|
@ -59,7 +59,6 @@ public final class AiffFileWriter extends SunFileWriter {
|
|||||||
super(new AudioFileFormat.Type[]{AudioFileFormat.Type.AIFF});
|
super(new AudioFileFormat.Type[]{AudioFileFormat.Type.AIFF});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// METHODS TO IMPLEMENT AudioFileWriter
|
// METHODS TO IMPLEMENT AudioFileWriter
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -83,7 +82,6 @@ public final class AiffFileWriter extends SunFileWriter {
|
|||||||
return new AudioFileFormat.Type[0];
|
return new AudioFileFormat.Type[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
|
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
|
||||||
Objects.requireNonNull(stream);
|
Objects.requireNonNull(stream);
|
||||||
@ -102,11 +100,9 @@ public final class AiffFileWriter extends SunFileWriter {
|
|||||||
throw new IOException("stream length not specified");
|
throw new IOException("stream length not specified");
|
||||||
}
|
}
|
||||||
|
|
||||||
int bytesWritten = writeAiffFile(stream, aiffFileFormat, out);
|
return writeAiffFile(stream, aiffFileFormat, out);
|
||||||
return bytesWritten;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
|
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
|
||||||
Objects.requireNonNull(stream);
|
Objects.requireNonNull(stream);
|
||||||
@ -129,12 +125,15 @@ public final class AiffFileWriter extends SunFileWriter {
|
|||||||
|
|
||||||
// $$kk: 10.22.99: jan: please either implement this or throw an exception!
|
// $$kk: 10.22.99: jan: please either implement this or throw an exception!
|
||||||
// $$fb: 2001-07-13: done. Fixes Bug 4479981
|
// $$fb: 2001-07-13: done. Fixes Bug 4479981
|
||||||
int ssndBlockSize = (aiffFileFormat.getFormat().getChannels() * aiffFileFormat.getFormat().getSampleSizeInBits());
|
int channels = aiffFileFormat.getFormat().getChannels();
|
||||||
|
int sampleSize = aiffFileFormat.getFormat().getSampleSizeInBits();
|
||||||
|
int ssndBlockSize = channels * ((sampleSize + 7) / 8);
|
||||||
|
|
||||||
int aiffLength=bytesWritten;
|
int aiffLength=bytesWritten;
|
||||||
int ssndChunkSize=aiffLength-aiffFileFormat.getHeaderSize()+16;
|
int ssndChunkSize=aiffLength-aiffFileFormat.getHeaderSize()+16;
|
||||||
long dataSize=ssndChunkSize-16;
|
long dataSize=ssndChunkSize-16;
|
||||||
int numFrames=(int) (dataSize*8/ssndBlockSize);
|
//TODO possibly incorrect round
|
||||||
|
int numFrames = (int) (dataSize / ssndBlockSize);
|
||||||
|
|
||||||
RandomAccessFile raf=new RandomAccessFile(out, "rw");
|
RandomAccessFile raf=new RandomAccessFile(out, "rw");
|
||||||
// skip FORM magic
|
// skip FORM magic
|
||||||
@ -173,12 +172,7 @@ public final class AiffFileWriter extends SunFileWriter {
|
|||||||
AudioFormat streamFormat = stream.getFormat();
|
AudioFormat streamFormat = stream.getFormat();
|
||||||
AudioFormat.Encoding streamEncoding = streamFormat.getEncoding();
|
AudioFormat.Encoding streamEncoding = streamFormat.getEncoding();
|
||||||
|
|
||||||
|
|
||||||
float sampleRate;
|
|
||||||
int sampleSizeInBits;
|
int sampleSizeInBits;
|
||||||
int channels;
|
|
||||||
int frameSize;
|
|
||||||
float frameRate;
|
|
||||||
int fileSize;
|
int fileSize;
|
||||||
boolean convert8to16 = false;
|
boolean convert8to16 = false;
|
||||||
|
|
||||||
@ -235,7 +229,6 @@ public final class AiffFileWriter extends SunFileWriter {
|
|||||||
return fileFormat;
|
return fileFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private int writeAiffFile(InputStream in, AiffFileFormat aiffFileFormat, OutputStream out) throws IOException {
|
private int writeAiffFile(InputStream in, AiffFileFormat aiffFileFormat, OutputStream out) throws IOException {
|
||||||
|
|
||||||
int bytesRead = 0;
|
int bytesRead = 0;
|
||||||
@ -275,25 +268,20 @@ public final class AiffFileWriter extends SunFileWriter {
|
|||||||
AudioFormat.Encoding encoding = null;
|
AudioFormat.Encoding encoding = null;
|
||||||
|
|
||||||
//$$fb a little bit nicer handling of constants
|
//$$fb a little bit nicer handling of constants
|
||||||
|
|
||||||
//int headerSize = 54;
|
|
||||||
int headerSize = aiffFileFormat.getHeaderSize();
|
int headerSize = aiffFileFormat.getHeaderSize();
|
||||||
|
|
||||||
//int fverChunkSize = 0;
|
//int fverChunkSize = 0;
|
||||||
int fverChunkSize = aiffFileFormat.getFverChunkSize();
|
int fverChunkSize = aiffFileFormat.getFverChunkSize();
|
||||||
//int commChunkSize = 26;
|
|
||||||
int commChunkSize = aiffFileFormat.getCommChunkSize();
|
int commChunkSize = aiffFileFormat.getCommChunkSize();
|
||||||
int aiffLength = -1;
|
int aiffLength = -1;
|
||||||
int ssndChunkSize = -1;
|
int ssndChunkSize = -1;
|
||||||
//int ssndOffset = headerSize - 16;
|
|
||||||
int ssndOffset = aiffFileFormat.getSsndChunkOffset();
|
int ssndOffset = aiffFileFormat.getSsndChunkOffset();
|
||||||
short channels = (short) format.getChannels();
|
short channels = (short) format.getChannels();
|
||||||
short sampleSize = (short) format.getSampleSizeInBits();
|
short sampleSize = (short) format.getSampleSizeInBits();
|
||||||
int ssndBlockSize = (channels * sampleSize);
|
int ssndBlockSize = channels * ((sampleSize + 7) / 8);
|
||||||
int numFrames = aiffFileFormat.getFrameLength();
|
int numFrames = aiffFileFormat.getFrameLength();
|
||||||
long dataSize = -1;
|
long dataSize = -1;
|
||||||
if( numFrames != AudioSystem.NOT_SPECIFIED) {
|
if( numFrames != AudioSystem.NOT_SPECIFIED) {
|
||||||
dataSize = (long) numFrames * ssndBlockSize / 8;
|
dataSize = (long) numFrames * ssndBlockSize;
|
||||||
ssndChunkSize = (int)dataSize + 16;
|
ssndChunkSize = (int)dataSize + 16;
|
||||||
aiffLength = (int)dataSize+headerSize;
|
aiffLength = (int)dataSize+headerSize;
|
||||||
}
|
}
|
||||||
@ -403,9 +391,6 @@ public final class AiffFileWriter extends SunFileWriter {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// HELPER METHODS
|
// HELPER METHODS
|
||||||
|
|
||||||
private static final int DOUBLE_MANTISSA_LENGTH = 52;
|
private static final int DOUBLE_MANTISSA_LENGTH = 52;
|
||||||
@ -452,6 +437,4 @@ public final class AiffFileWriter extends SunFileWriter {
|
|||||||
dos.writeShort(extendedBits79To64);
|
dos.writeShort(extendedBits79To64);
|
||||||
dos.writeLong(extendedBits63To0);
|
dos.writeLong(extendedBits63To0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -255,16 +255,17 @@ public final class WaveExtensibleFileReader extends SunFileReader {
|
|||||||
public AudioInputStream getAudioInputStream(final InputStream stream)
|
public AudioInputStream getAudioInputStream(final InputStream stream)
|
||||||
throws UnsupportedAudioFileException, IOException {
|
throws UnsupportedAudioFileException, IOException {
|
||||||
|
|
||||||
AudioFileFormat format = getAudioFileFormat(stream);
|
final AudioFileFormat format = getAudioFileFormat(stream);
|
||||||
// we've got everything, the stream is supported and it is at the
|
// we've got everything, the stream is supported and it is at the
|
||||||
// beginning of the header, so find the data chunk again and return an
|
// beginning of the header, so find the data chunk again and return an
|
||||||
// AudioInputStream
|
// AudioInputStream
|
||||||
RIFFReader riffiterator = new RIFFReader(stream);
|
final RIFFReader riffiterator = new RIFFReader(stream);
|
||||||
while (riffiterator.hasNextChunk()) {
|
while (riffiterator.hasNextChunk()) {
|
||||||
RIFFReader chunk = riffiterator.nextChunk();
|
RIFFReader chunk = riffiterator.nextChunk();
|
||||||
if (chunk.getFormat().equals("data")) {
|
if (chunk.getFormat().equals("data")) {
|
||||||
return new AudioInputStream(chunk, format.getFormat(), chunk
|
final AudioFormat af = format.getFormat();
|
||||||
.getSize());
|
final long length = chunk.getSize() / af.getFrameSize();
|
||||||
|
return new AudioInputStream(chunk, af, length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new UnsupportedAudioFileException();
|
throw new UnsupportedAudioFileException();
|
||||||
|
@ -95,16 +95,17 @@ public final class WaveFloatFileReader extends SunFileReader {
|
|||||||
public AudioInputStream getAudioInputStream(final InputStream stream)
|
public AudioInputStream getAudioInputStream(final InputStream stream)
|
||||||
throws UnsupportedAudioFileException, IOException {
|
throws UnsupportedAudioFileException, IOException {
|
||||||
|
|
||||||
AudioFileFormat format = getAudioFileFormat(stream);
|
final AudioFileFormat format = getAudioFileFormat(stream);
|
||||||
// we've got everything, the stream is supported and it is at the
|
// we've got everything, the stream is supported and it is at the
|
||||||
// beginning of the header, so find the data chunk again and return an
|
// beginning of the header, so find the data chunk again and return an
|
||||||
// AudioInputStream
|
// AudioInputStream
|
||||||
RIFFReader riffiterator = new RIFFReader(stream);
|
final RIFFReader riffiterator = new RIFFReader(stream);
|
||||||
while (riffiterator.hasNextChunk()) {
|
while (riffiterator.hasNextChunk()) {
|
||||||
RIFFReader chunk = riffiterator.nextChunk();
|
RIFFReader chunk = riffiterator.nextChunk();
|
||||||
if (chunk.getFormat().equals("data")) {
|
if (chunk.getFormat().equals("data")) {
|
||||||
return new AudioInputStream(chunk, format.getFormat(),
|
final AudioFormat af = format.getFormat();
|
||||||
chunk.getSize());
|
final long length = chunk.getSize() / af.getFrameSize();
|
||||||
|
return new AudioInputStream(chunk, af, length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new UnsupportedAudioFileException();
|
throw new UnsupportedAudioFileException();
|
||||||
|
@ -0,0 +1,209 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.sound.sampled.AudioFileFormat;
|
||||||
|
import javax.sound.sampled.AudioFormat;
|
||||||
|
import javax.sound.sampled.AudioInputStream;
|
||||||
|
import javax.sound.sampled.AudioSystem;
|
||||||
|
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||||
|
import javax.sound.sampled.spi.AudioFileWriter;
|
||||||
|
import javax.sound.sampled.spi.FormatConversionProvider;
|
||||||
|
|
||||||
|
import static java.util.ServiceLoader.load;
|
||||||
|
import static javax.sound.sampled.AudioFileFormat.Type.AIFC;
|
||||||
|
import static javax.sound.sampled.AudioFileFormat.Type.AIFF;
|
||||||
|
import static javax.sound.sampled.AudioFileFormat.Type.AU;
|
||||||
|
import static javax.sound.sampled.AudioFileFormat.Type.SND;
|
||||||
|
import static javax.sound.sampled.AudioFileFormat.Type.WAVE;
|
||||||
|
import static javax.sound.sampled.AudioSystem.NOT_SPECIFIED;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @bug 8038139
|
||||||
|
*/
|
||||||
|
public final class FrameLengthAfterConversion {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We will try to use all formats, in this case all our providers will be
|
||||||
|
* covered by supported/unsupported formats.
|
||||||
|
*/
|
||||||
|
private static final List<AudioFormat> formats = new ArrayList<>(23000);
|
||||||
|
|
||||||
|
private static final AudioFormat.Encoding[] encodings = {
|
||||||
|
AudioFormat.Encoding.ALAW, AudioFormat.Encoding.ULAW,
|
||||||
|
AudioFormat.Encoding.PCM_SIGNED, AudioFormat.Encoding.PCM_UNSIGNED,
|
||||||
|
AudioFormat.Encoding.PCM_FLOAT, new AudioFormat.Encoding("Test")
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final int[] sampleBits = {
|
||||||
|
1, 4, 8, 11, 16, 20, 24, 32
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final int[] channels = {
|
||||||
|
1, 2, 3, 4, 5
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final AudioFileFormat.Type[] types = {
|
||||||
|
WAVE, AU, AIFF, AIFC, SND,
|
||||||
|
new AudioFileFormat.Type("TestName", "TestExt")
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final int FRAME_LENGTH = 10;
|
||||||
|
|
||||||
|
static {
|
||||||
|
for (final int sampleSize : sampleBits) {
|
||||||
|
for (final int channel : channels) {
|
||||||
|
for (final AudioFormat.Encoding enc : encodings) {
|
||||||
|
final int frameSize = ((sampleSize + 7) / 8) * channel;
|
||||||
|
formats.add(new AudioFormat(enc, 44100, sampleSize, channel,
|
||||||
|
frameSize, 44100, true));
|
||||||
|
formats.add(new AudioFormat(enc, 44100, sampleSize, channel,
|
||||||
|
frameSize, 44100, false));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(final String[] args) {
|
||||||
|
for (final FormatConversionProvider fcp : load(
|
||||||
|
FormatConversionProvider.class)) {
|
||||||
|
System.out.println("fcp = " + fcp);
|
||||||
|
for (final AudioFormat from : formats) {
|
||||||
|
for (final AudioFormat to : formats) {
|
||||||
|
testAfterConversion(fcp, to, getStream(from, true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (final AudioFileWriter afw : load(AudioFileWriter.class)) {
|
||||||
|
System.out.println("afw = " + afw);
|
||||||
|
for (final AudioFileFormat.Type type : types) {
|
||||||
|
for (final AudioFormat from : formats) {
|
||||||
|
testAfterSaveToStream(afw, type, getStream(from, true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (final AudioFileWriter afw : load(AudioFileWriter.class)) {
|
||||||
|
System.out.println("afw = " + afw);
|
||||||
|
for (final AudioFileFormat.Type type : types) {
|
||||||
|
for (final AudioFormat from : formats) {
|
||||||
|
testAfterSaveToFile(afw, type, getStream(from, true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (final AudioFileWriter afw : load(AudioFileWriter.class)) {
|
||||||
|
System.out.println("afw = " + afw);
|
||||||
|
for (final AudioFileFormat.Type type : types) {
|
||||||
|
for (final AudioFormat from : formats) {
|
||||||
|
testAfterSaveToFile(afw, type, getStream(from, false));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies the frame length after the stream was saved/read to/from
|
||||||
|
* stream.
|
||||||
|
*/
|
||||||
|
private static void testAfterSaveToStream(final AudioFileWriter afw,
|
||||||
|
final AudioFileFormat.Type type,
|
||||||
|
final AudioInputStream ais) {
|
||||||
|
try {
|
||||||
|
final ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
afw.write(ais, type, out);
|
||||||
|
final InputStream input = new ByteArrayInputStream(
|
||||||
|
out.toByteArray());
|
||||||
|
validate(AudioSystem.getAudioInputStream(input).getFrameLength());
|
||||||
|
} catch (IllegalArgumentException | UnsupportedAudioFileException
|
||||||
|
| IOException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies the frame length after the stream was saved/read to/from file.
|
||||||
|
*/
|
||||||
|
private static void testAfterSaveToFile(final AudioFileWriter afw,
|
||||||
|
final AudioFileFormat.Type type,
|
||||||
|
AudioInputStream ais) {
|
||||||
|
try {
|
||||||
|
final File temp = File.createTempFile("sound", ".tmp");
|
||||||
|
temp.deleteOnExit();
|
||||||
|
afw.write(ais, type, temp);
|
||||||
|
ais = AudioSystem.getAudioInputStream(temp);
|
||||||
|
final long frameLength = ais.getFrameLength();
|
||||||
|
ais.close();
|
||||||
|
temp.delete();
|
||||||
|
validate(frameLength);
|
||||||
|
} catch (IllegalArgumentException | UnsupportedAudioFileException
|
||||||
|
| IOException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies the frame length after the stream was converted to other
|
||||||
|
* stream.
|
||||||
|
*
|
||||||
|
* @see FormatConversionProvider#getAudioInputStream(AudioFormat,
|
||||||
|
* AudioInputStream)
|
||||||
|
*/
|
||||||
|
private static void testAfterConversion(final FormatConversionProvider fcp,
|
||||||
|
final AudioFormat to,
|
||||||
|
final AudioInputStream ais) {
|
||||||
|
if (fcp.isConversionSupported(to, ais.getFormat())) {
|
||||||
|
validate(fcp.getAudioInputStream(to, ais).getFrameLength());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throws an exception if the frameLength is specified and is not equal to
|
||||||
|
* the gold value.
|
||||||
|
*/
|
||||||
|
private static void validate(final long frameLength) {
|
||||||
|
if (frameLength != FRAME_LENGTH) {
|
||||||
|
System.err.println("Expected: " + FRAME_LENGTH);
|
||||||
|
System.err.println("Actual: " + frameLength);
|
||||||
|
throw new RuntimeException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static AudioInputStream getStream(final AudioFormat format,
|
||||||
|
final boolean frameLength) {
|
||||||
|
final int dataSize = FRAME_LENGTH * format.getFrameSize();
|
||||||
|
final InputStream in = new ByteArrayInputStream(new byte[dataSize]);
|
||||||
|
if (frameLength) {
|
||||||
|
return new AudioInputStream(in, format, FRAME_LENGTH);
|
||||||
|
} else {
|
||||||
|
return new AudioInputStream(in, format, NOT_SPECIFIED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user