0N/A/*
1472N/A * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
1472N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1472N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/Apackage com.sun.media.sound;
0N/A
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.File;
0N/Aimport java.io.IOException;
0N/Aimport java.io.InputStream;
0N/Aimport java.net.URL;
0N/A
0N/Aimport javax.sound.sampled.AudioFormat;
0N/Aimport javax.sound.sampled.AudioInputStream;
0N/Aimport javax.sound.sampled.AudioSystem;
0N/Aimport javax.sound.sampled.UnsupportedAudioFileException;
0N/A
0N/A/**
0N/A * This class is used to create AudioFloatInputStream from AudioInputStream and
0N/A * byte buffers.
0N/A *
0N/A * @author Karl Helgason
0N/A */
0N/Apublic abstract class AudioFloatInputStream {
0N/A
0N/A private static class BytaArrayAudioFloatInputStream
0N/A extends AudioFloatInputStream {
0N/A
0N/A private int pos = 0;
0N/A private int markpos = 0;
0N/A private final AudioFloatConverter converter;
0N/A private final AudioFormat format;
0N/A private final byte[] buffer;
0N/A private final int buffer_offset;
0N/A private final int buffer_len;
0N/A private final int framesize_pc;
0N/A
0N/A BytaArrayAudioFloatInputStream(AudioFloatConverter converter,
0N/A byte[] buffer, int offset, int len) {
0N/A this.converter = converter;
0N/A this.format = converter.getFormat();
0N/A this.buffer = buffer;
0N/A this.buffer_offset = offset;
0N/A framesize_pc = format.getFrameSize() / format.getChannels();
0N/A this.buffer_len = len / framesize_pc;
0N/A
0N/A }
0N/A
0N/A public AudioFormat getFormat() {
0N/A return format;
0N/A }
0N/A
0N/A public long getFrameLength() {
0N/A return buffer_len;// / format.getFrameSize();
0N/A }
0N/A
0N/A public int read(float[] b, int off, int len) throws IOException {
0N/A if (b == null)
0N/A throw new NullPointerException();
0N/A if (off < 0 || len < 0 || len > b.length - off)
0N/A throw new IndexOutOfBoundsException();
0N/A if (pos >= buffer_len)
0N/A return -1;
0N/A if (len == 0)
0N/A return 0;
0N/A if (pos + len > buffer_len)
0N/A len = buffer_len - pos;
0N/A converter.toFloatArray(buffer, buffer_offset + pos * framesize_pc,
0N/A b, off, len);
0N/A pos += len;
0N/A return len;
0N/A }
0N/A
0N/A public long skip(long len) throws IOException {
0N/A if (pos >= buffer_len)
0N/A return -1;
0N/A if (len <= 0)
0N/A return 0;
0N/A if (pos + len > buffer_len)
0N/A len = buffer_len - pos;
0N/A pos += len;
0N/A return len;
0N/A }
0N/A
0N/A public int available() throws IOException {
0N/A return buffer_len - pos;
0N/A }
0N/A
public void close() throws IOException {
}
public void mark(int readlimit) {
markpos = pos;
}
public boolean markSupported() {
return true;
}
public void reset() throws IOException {
pos = markpos;
}
}
private static class DirectAudioFloatInputStream
extends AudioFloatInputStream {
private final AudioInputStream stream;
private AudioFloatConverter converter;
private final int framesize_pc; // framesize / channels
private byte[] buffer;
DirectAudioFloatInputStream(AudioInputStream stream) {
converter = AudioFloatConverter.getConverter(stream.getFormat());
if (converter == null) {
AudioFormat format = stream.getFormat();
AudioFormat newformat;
AudioFormat[] formats = AudioSystem.getTargetFormats(
AudioFormat.Encoding.PCM_SIGNED, format);
if (formats.length != 0) {
newformat = formats[0];
} else {
float samplerate = format.getSampleRate();
int samplesizeinbits = format.getSampleSizeInBits();
int framesize = format.getFrameSize();
float framerate = format.getFrameRate();
samplesizeinbits = 16;
framesize = format.getChannels() * (samplesizeinbits / 8);
framerate = samplerate;
newformat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, samplerate,
samplesizeinbits, format.getChannels(), framesize,
framerate, false);
}
stream = AudioSystem.getAudioInputStream(newformat, stream);
converter = AudioFloatConverter.getConverter(stream.getFormat());
}
framesize_pc = stream.getFormat().getFrameSize()
/ stream.getFormat().getChannels();
this.stream = stream;
}
public AudioFormat getFormat() {
return stream.getFormat();
}
public long getFrameLength() {
return stream.getFrameLength();
}
public int read(float[] b, int off, int len) throws IOException {
int b_len = len * framesize_pc;
if (buffer == null || buffer.length < b_len)
buffer = new byte[b_len];
int ret = stream.read(buffer, 0, b_len);
if (ret == -1)
return -1;
converter.toFloatArray(buffer, b, off, ret / framesize_pc);
return ret / framesize_pc;
}
public long skip(long len) throws IOException {
long b_len = len * framesize_pc;
long ret = stream.skip(b_len);
if (ret == -1)
return -1;
return ret / framesize_pc;
}
public int available() throws IOException {
return stream.available() / framesize_pc;
}
public void close() throws IOException {
stream.close();
}
public void mark(int readlimit) {
stream.mark(readlimit * framesize_pc);
}
public boolean markSupported() {
return stream.markSupported();
}
public void reset() throws IOException {
stream.reset();
}
}
public static AudioFloatInputStream getInputStream(URL url)
throws UnsupportedAudioFileException, IOException {
return new DirectAudioFloatInputStream(AudioSystem
.getAudioInputStream(url));
}
public static AudioFloatInputStream getInputStream(File file)
throws UnsupportedAudioFileException, IOException {
return new DirectAudioFloatInputStream(AudioSystem
.getAudioInputStream(file));
}
public static AudioFloatInputStream getInputStream(InputStream stream)
throws UnsupportedAudioFileException, IOException {
return new DirectAudioFloatInputStream(AudioSystem
.getAudioInputStream(stream));
}
public static AudioFloatInputStream getInputStream(
AudioInputStream stream) {
return new DirectAudioFloatInputStream(stream);
}
public static AudioFloatInputStream getInputStream(AudioFormat format,
byte[] buffer, int offset, int len) {
AudioFloatConverter converter = AudioFloatConverter
.getConverter(format);
if (converter != null)
return new BytaArrayAudioFloatInputStream(converter, buffer,
offset, len);
InputStream stream = new ByteArrayInputStream(buffer, offset, len);
long aLen = format.getFrameSize() == AudioSystem.NOT_SPECIFIED
? AudioSystem.NOT_SPECIFIED : len / format.getFrameSize();
AudioInputStream astream = new AudioInputStream(stream, format, aLen);
return getInputStream(astream);
}
public abstract AudioFormat getFormat();
public abstract long getFrameLength();
public abstract int read(float[] b, int off, int len) throws IOException;
public final int read(float[] b) throws IOException {
return read(b, 0, b.length);
}
public final float read() throws IOException {
float[] b = new float[1];
int ret = read(b, 0, 1);
if (ret == -1 || ret == 0)
return 0;
return b[0];
}
public abstract long skip(long len) throws IOException;
public abstract int available() throws IOException;
public abstract void close() throws IOException;
public abstract void mark(int readlimit);
public abstract boolean markSupported();
public abstract void reset() throws IOException;
}