Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/media/sound/AudioFloatInputStream.java
38924 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.media.sound;2526import java.io.ByteArrayInputStream;27import java.io.File;28import java.io.IOException;29import java.io.InputStream;30import java.net.URL;3132import javax.sound.sampled.AudioFormat;33import javax.sound.sampled.AudioInputStream;34import javax.sound.sampled.AudioSystem;35import javax.sound.sampled.UnsupportedAudioFileException;3637/**38* This class is used to create AudioFloatInputStream from AudioInputStream and39* byte buffers.40*41* @author Karl Helgason42*/43public abstract class AudioFloatInputStream {4445private static class BytaArrayAudioFloatInputStream46extends AudioFloatInputStream {4748private int pos = 0;49private int markpos = 0;50private final AudioFloatConverter converter;51private final AudioFormat format;52private final byte[] buffer;53private final int buffer_offset;54private final int buffer_len;55private final int framesize_pc;5657BytaArrayAudioFloatInputStream(AudioFloatConverter converter,58byte[] buffer, int offset, int len) {59this.converter = converter;60this.format = converter.getFormat();61this.buffer = buffer;62this.buffer_offset = offset;63framesize_pc = format.getFrameSize() / format.getChannels();64this.buffer_len = len / framesize_pc;6566}6768public AudioFormat getFormat() {69return format;70}7172public long getFrameLength() {73return buffer_len;// / format.getFrameSize();74}7576public int read(float[] b, int off, int len) throws IOException {77if (b == null)78throw new NullPointerException();79if (off < 0 || len < 0 || len > b.length - off)80throw new IndexOutOfBoundsException();81if (pos >= buffer_len)82return -1;83if (len == 0)84return 0;85if (pos + len > buffer_len)86len = buffer_len - pos;87converter.toFloatArray(buffer, buffer_offset + pos * framesize_pc,88b, off, len);89pos += len;90return len;91}9293public long skip(long len) throws IOException {94if (pos >= buffer_len)95return -1;96if (len <= 0)97return 0;98if (pos + len > buffer_len)99len = buffer_len - pos;100pos += len;101return len;102}103104public int available() throws IOException {105return buffer_len - pos;106}107108public void close() throws IOException {109}110111public void mark(int readlimit) {112markpos = pos;113}114115public boolean markSupported() {116return true;117}118119public void reset() throws IOException {120pos = markpos;121}122}123124private static class DirectAudioFloatInputStream125extends AudioFloatInputStream {126127private final AudioInputStream stream;128private AudioFloatConverter converter;129private final int framesize_pc; // framesize / channels130private byte[] buffer;131132DirectAudioFloatInputStream(AudioInputStream stream) {133converter = AudioFloatConverter.getConverter(stream.getFormat());134if (converter == null) {135AudioFormat format = stream.getFormat();136AudioFormat newformat;137138AudioFormat[] formats = AudioSystem.getTargetFormats(139AudioFormat.Encoding.PCM_SIGNED, format);140if (formats.length != 0) {141newformat = formats[0];142} else {143float samplerate = format.getSampleRate();144int samplesizeinbits = format.getSampleSizeInBits();145int framesize = format.getFrameSize();146float framerate = format.getFrameRate();147samplesizeinbits = 16;148framesize = format.getChannels() * (samplesizeinbits / 8);149framerate = samplerate;150151newformat = new AudioFormat(152AudioFormat.Encoding.PCM_SIGNED, samplerate,153samplesizeinbits, format.getChannels(), framesize,154framerate, false);155}156157stream = AudioSystem.getAudioInputStream(newformat, stream);158converter = AudioFloatConverter.getConverter(stream.getFormat());159}160framesize_pc = stream.getFormat().getFrameSize()161/ stream.getFormat().getChannels();162this.stream = stream;163}164165public AudioFormat getFormat() {166return stream.getFormat();167}168169public long getFrameLength() {170return stream.getFrameLength();171}172173public int read(float[] b, int off, int len) throws IOException {174int b_len = len * framesize_pc;175if (buffer == null || buffer.length < b_len)176buffer = new byte[b_len];177int ret = stream.read(buffer, 0, b_len);178if (ret == -1)179return -1;180converter.toFloatArray(buffer, b, off, ret / framesize_pc);181return ret / framesize_pc;182}183184public long skip(long len) throws IOException {185long b_len = len * framesize_pc;186long ret = stream.skip(b_len);187if (ret == -1)188return -1;189return ret / framesize_pc;190}191192public int available() throws IOException {193return stream.available() / framesize_pc;194}195196public void close() throws IOException {197stream.close();198}199200public void mark(int readlimit) {201stream.mark(readlimit * framesize_pc);202}203204public boolean markSupported() {205return stream.markSupported();206}207208public void reset() throws IOException {209stream.reset();210}211}212213public static AudioFloatInputStream getInputStream(URL url)214throws UnsupportedAudioFileException, IOException {215return new DirectAudioFloatInputStream(AudioSystem216.getAudioInputStream(url));217}218219public static AudioFloatInputStream getInputStream(File file)220throws UnsupportedAudioFileException, IOException {221return new DirectAudioFloatInputStream(AudioSystem222.getAudioInputStream(file));223}224225public static AudioFloatInputStream getInputStream(InputStream stream)226throws UnsupportedAudioFileException, IOException {227return new DirectAudioFloatInputStream(AudioSystem228.getAudioInputStream(stream));229}230231public static AudioFloatInputStream getInputStream(232AudioInputStream stream) {233return new DirectAudioFloatInputStream(stream);234}235236public static AudioFloatInputStream getInputStream(AudioFormat format,237byte[] buffer, int offset, int len) {238AudioFloatConverter converter = AudioFloatConverter239.getConverter(format);240if (converter != null)241return new BytaArrayAudioFloatInputStream(converter, buffer,242offset, len);243244InputStream stream = new ByteArrayInputStream(buffer, offset, len);245long aLen = format.getFrameSize() == AudioSystem.NOT_SPECIFIED246? AudioSystem.NOT_SPECIFIED : len / format.getFrameSize();247AudioInputStream astream = new AudioInputStream(stream, format, aLen);248return getInputStream(astream);249}250251public abstract AudioFormat getFormat();252253public abstract long getFrameLength();254255public abstract int read(float[] b, int off, int len) throws IOException;256257public final int read(float[] b) throws IOException {258return read(b, 0, b.length);259}260261public final float read() throws IOException {262float[] b = new float[1];263int ret = read(b, 0, 1);264if (ret == -1 || ret == 0)265return 0;266return b[0];267}268269public abstract long skip(long len) throws IOException;270271public abstract int available() throws IOException;272273public abstract void close() throws IOException;274275public abstract void mark(int readlimit);276277public abstract boolean markSupported();278279public abstract void reset() throws IOException;280}281282283