samer@1: /* samer@1: * FileSource.java samer@1: * samer@1: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@1: * All rights reserved. samer@1: * samer@1: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@1: * without even the implied warranty of MERCHANTABILITY or samer@1: * FITNESS FOR A PARTICULAR PURPOSE. samer@1: */ samer@1: samer@1: package samer.audio; samer@1: samer@1: import samer.core.*; samer@1: import samer.tools.*; samer@1: import javax.sound.sampled.*; samer@1: import java.io.*; samer@1: samer@1: /** samer@1: A lightweight AudioSource that reads from a single Stream. samer@1: Any format for which the appropriate JavaSound plug-in is installed on samer@1: your system, eg WAV, AU, MP3, OGG. The implementation of samer@1: */ samer@1: samer@1: public class StreamSource implements AudioSource samer@1: { samer@1: InputStream in=null; samer@1: AudioFormat format=null; samer@1: samer@1: /** samer@1: * Construct a FileSource initialised with current file and loop initialised samer@1: * from the current Environment. No exception is thrown if the current file samer@1: * cannot be opened, however, you must be sure to set it to a valid file samer@1: * (or to set a playlist) before trying to read any samples. samer@1: */ samer@1: public StreamSource(InputStream in) throws Exception { open(in); } samer@1: public StreamSource(InputStream in, AudioFormat f) throws Exception { format=f; open(in); } samer@1: public StreamSource(AudioFormat f) { format=f; } samer@1: samer@1: public void dispose() { close(); } samer@1: public void setTargetFormat(AudioFormat f) { format=f; } samer@1: samer@1: /** Returns number of bytes available in current file */ samer@1: public int available() throws Exception { return in.available(); } samer@1: samer@1: samer@1: /** Allows reading from any given InputStream, but does not affect samer@1: * current file or playlist. */ samer@1: public synchronized void open(InputStream s) throws Exception { samer@1: open(AudioSystem.getAudioInputStream(s)); samer@1: } samer@1: samer@1: public synchronized void open(AudioInputStream s) throws Exception samer@1: { samer@1: Shell.trace("stream format: "+s.getFormat()); samer@1: Shell.trace("file format: "+s.getFormat()); samer@1: samer@1: // in = new BufferedInputStream(s,16*1024); samer@1: // convert to target format if required samer@1: in=s; samer@1: if (format!=null) { samer@1: Shell.trace("Converting to "+format); samer@1: in=AudioSystem.getAudioInputStream(format,s); samer@1: } samer@1: } samer@1: samer@1: /** Closes current input stream */ samer@1: public synchronized void close() { samer@1: try { if (in!=null) in.close(); in=null; } samer@1: catch (IOException ex) {} samer@1: } samer@1: samer@1: /** Returns a Task which copies samples as doubles into the given samer@1: * buffer between the given positions. */ samer@1: public Task reader(final double [] dbuf, final int off, final int len) { samer@1: return new AnonymousTask() { samer@1: int blen=2*len; samer@1: byte [] bbuf = new byte[blen]; samer@1: samer@1: public void run() throws Exception { samer@1: int n = 0; samer@1: samer@1: while (n < blen) { samer@1: int count = in.read(bbuf, n, blen - n); samer@1: if (count > 0) n+=count; samer@1: else throw new EOFException(); samer@1: } samer@1: Util.shortToDouble(bbuf,dbuf,off,len); samer@1: } samer@1: }; samer@1: } samer@1: samer@1: /** Returns a Task which copies samples as floats into the given samer@1: * buffer between the given positions. */ samer@1: public Task reader(final float [] dbuf, final int off, final int len) { samer@1: return new AnonymousTask() { samer@1: int blen=2*len; samer@1: byte [] bbuf = new byte[blen]; samer@1: samer@1: public synchronized void run() throws Exception { samer@1: int n = 0; samer@1: samer@1: while (n < blen) { samer@1: int count = in.read(bbuf, n, blen - n); samer@1: if (count > 0) n+=count; samer@1: else throw new EOFException(); samer@1: } samer@1: Util.shortToFloat(bbuf,dbuf,off,len); samer@1: } samer@1: }; samer@1: } samer@1: } samer@1: