< prev index next >
src/java.desktop/share/classes/com/sun/media/sound/AudioFloatFormatConverter.java
Print this page
@@ -30,13 +30,13 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import javax.sound.sampled.AudioFormat;
+import javax.sound.sampled.AudioFormat.Encoding;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
-import javax.sound.sampled.AudioFormat.Encoding;
import javax.sound.sampled.spi.FormatConversionProvider;
/**
* This class is used to convert between 8,16,24,32 bit signed/unsigned
* big/litle endian fixed/floating stereo/mono/multi-channel audio streams and
@@ -61,18 +61,20 @@
this.stream = stream;
converter = AudioFloatConverter.getConverter(targetFormat);
fsize = ((targetFormat.getSampleSizeInBits() + 7) / 8);
}
+ @Override
public int read() throws IOException {
byte[] b = new byte[1];
int ret = read(b);
if (ret < 0)
return ret;
return b[0] & 0xFF;
}
+ @Override
public int read(byte[] b, int off, int len) throws IOException {
int flen = len / fsize;
if (readfloatbuffer == null || readfloatbuffer.length < flen)
readfloatbuffer = new float[flen];
@@ -81,33 +83,39 @@
return ret;
converter.toByteArray(readfloatbuffer, 0, ret, b, off);
return ret * fsize;
}
+ @Override
public int available() throws IOException {
int ret = stream.available();
if (ret < 0)
return ret;
return ret * fsize;
}
+ @Override
public void close() throws IOException {
stream.close();
}
+ @Override
public synchronized void mark(int readlimit) {
stream.mark(readlimit * fsize);
}
+ @Override
public boolean markSupported() {
return stream.markSupported();
}
+ @Override
public synchronized void reset() throws IOException {
stream.reset();
}
+ @Override
public long skip(long n) throws IOException {
long ret = stream.skip(n / fsize);
if (ret < 0)
return ret;
return ret * fsize;
@@ -139,34 +147,41 @@
targetChannels, (format.getFrameSize() / sourceChannels)
* targetChannels, format.getFrameRate(), format
.isBigEndian());
}
+ @Override
public int available() throws IOException {
return (ais.available() / sourceChannels) * targetChannels;
}
+ @Override
public void close() throws IOException {
ais.close();
}
+ @Override
public AudioFormat getFormat() {
return targetFormat;
}
+ @Override
public long getFrameLength() {
return ais.getFrameLength();
}
+ @Override
public void mark(int readlimit) {
ais.mark((readlimit / targetChannels) * sourceChannels);
}
+ @Override
public boolean markSupported() {
return ais.markSupported();
}
+ @Override
public int read(float[] b, int off, int len) throws IOException {
int len2 = (len / targetChannels) * sourceChannels;
if (conversion_buffer == null || conversion_buffer.length < len2)
conversion_buffer = new float[len2];
int ret = ais.read(conversion_buffer, 0, len2);
@@ -210,14 +225,16 @@
}
}
return (ret / sourceChannels) * targetChannels;
}
+ @Override
public void reset() throws IOException {
ais.reset();
}
+ @Override
public long skip(long len) throws IOException {
long ret = ais.skip((len / targetChannels) * sourceChannels);
if (ret < 0)
return ret;
return (ret / sourceChannels) * targetChannels;
@@ -303,26 +320,31 @@
ibuffer2 = new float[nrofchannels * buffer_len];
ibuffer_index = buffer_len + pad;
ibuffer_len = buffer_len;
}
+ @Override
public int available() throws IOException {
return 0;
}
+ @Override
public void close() throws IOException {
ais.close();
}
+ @Override
public AudioFormat getFormat() {
return targetFormat;
}
+ @Override
public long getFrameLength() {
return AudioSystem.NOT_SPECIFIED; // ais.getFrameLength();
}
+ @Override
public void mark(int readlimit) {
ais.mark((int) (readlimit * pitch[0]));
mark_ibuffer_index = ibuffer_index;
mark_ibuffer_len = ibuffer_len;
if (mark_ibuffer == null) {
@@ -335,10 +357,11 @@
to[i] = from[i];
}
}
}
+ @Override
public boolean markSupported() {
return ais.markSupported();
}
private void readNextBuffer() throws IOException {
@@ -379,10 +402,11 @@
}
}
}
+ @Override
public int read(float[] b, int off, int len) throws IOException {
if (cbuffer == null || cbuffer[0].length < len / nrofchannels) {
cbuffer = new float[nrofchannels][len / nrofchannels];
}
@@ -429,10 +453,11 @@
}
}
return len - remain * nrofchannels;
}
+ @Override
public void reset() throws IOException {
ais.reset();
if (mark_ibuffer == null)
return;
ibuffer_index = mark_ibuffer_index;
@@ -445,10 +470,11 @@
}
}
}
+ @Override
public long skip(long len) throws IOException {
if (len < 0)
return 0;
if (skipbuffer == null)
skipbuffer = new float[1024 * targetFormat.getFrameSize()];
@@ -472,10 +498,11 @@
private final Encoding[] formats = {Encoding.PCM_SIGNED,
Encoding.PCM_UNSIGNED,
Encoding.PCM_FLOAT};
+ @Override
public AudioInputStream getAudioInputStream(Encoding targetEncoding,
AudioInputStream sourceStream) {
if (!isConversionSupported(targetEncoding, sourceStream.getFormat())) {
throw new IllegalArgumentException(
"Unsupported conversion: " + sourceStream.getFormat()
@@ -494,10 +521,11 @@
AudioFormat targetFormat = new AudioFormat(encoding, samplerate, bits,
channels, channels * bits / 8, samplerate, bigendian);
return getAudioInputStream(targetFormat, sourceStream);
}
+ @Override
public AudioInputStream getAudioInputStream(AudioFormat targetFormat,
AudioInputStream sourceStream) {
if (!isConversionSupported(targetFormat, sourceStream.getFormat()))
throw new IllegalArgumentException("Unsupported conversion: "
+ sourceStream.getFormat().toString() + " to "
@@ -524,35 +552,39 @@
return new AudioInputStream(new AudioFloatFormatConverterInputStream(
targetFormat, sourceStream), targetFormat, sourceStream
.getFrameLength());
}
+ @Override
public Encoding[] getSourceEncodings() {
return new Encoding[] { Encoding.PCM_SIGNED, Encoding.PCM_UNSIGNED,
Encoding.PCM_FLOAT };
}
+ @Override
public Encoding[] getTargetEncodings() {
return new Encoding[] { Encoding.PCM_SIGNED, Encoding.PCM_UNSIGNED,
Encoding.PCM_FLOAT };
}
+ @Override
public Encoding[] getTargetEncodings(AudioFormat sourceFormat) {
if (AudioFloatConverter.getConverter(sourceFormat) == null)
return new Encoding[0];
return new Encoding[] { Encoding.PCM_SIGNED, Encoding.PCM_UNSIGNED,
Encoding.PCM_FLOAT };
}
+ @Override
public AudioFormat[] getTargetFormats(Encoding targetEncoding,
AudioFormat sourceFormat) {
Objects.requireNonNull(targetEncoding);
if (AudioFloatConverter.getConverter(sourceFormat) == null)
return new AudioFormat[0];
int channels = sourceFormat.getChannels();
- ArrayList<AudioFormat> formats = new ArrayList<AudioFormat>();
+ ArrayList<AudioFormat> formats = new ArrayList<>();
if (targetEncoding.equals(Encoding.PCM_SIGNED))
formats.add(new AudioFormat(Encoding.PCM_SIGNED,
AudioSystem.NOT_SPECIFIED, 8, channels, channels,
AudioSystem.NOT_SPECIFIED, false));
@@ -596,10 +628,11 @@
}
return formats.toArray(new AudioFormat[formats.size()]);
}
+ @Override
public boolean isConversionSupported(AudioFormat targetFormat,
AudioFormat sourceFormat) {
Objects.requireNonNull(targetFormat);
if (AudioFloatConverter.getConverter(sourceFormat) == null)
return false;
@@ -610,10 +643,11 @@
if (targetFormat.getChannels() <= 0)
return false;
return true;
}
+ @Override
public boolean isConversionSupported(Encoding targetEncoding,
AudioFormat sourceFormat) {
Objects.requireNonNull(targetEncoding);
if (AudioFloatConverter.getConverter(sourceFormat) == null)
return false;
< prev index next >