view src/samer/audio/StreamSource.java.old @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents 5df24c91468d
children
line wrap: on
line source
/*
 *	FileSource.java
 *
 *	Copyright (c) 2000, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY;
 *	without even the implied warranty of MERCHANTABILITY or
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.audio;

import  samer.core.*;
import  samer.tools.*;
import  javax.sound.sampled.*;
import  java.io.*;

/**
	A lightweight AudioSource that reads from a single Stream.
	Any format for which the appropriate JavaSound plug-in is installed on
	your system, eg WAV, AU, MP3, OGG. The implementation of 
*/

public class StreamSource implements AudioSource
{
	InputStream	in=null;
	AudioFormat	format=null;

	/**
	 * Construct a FileSource initialised with current file and loop initialised
	 * from the current Environment. No exception is thrown if the current file
	 * cannot be opened, however, you must be sure to set it to a valid file
	 * (or to set a playlist) before trying to read any samples.
	 */
	public StreamSource(InputStream in) throws Exception { open(in); }
	public StreamSource(InputStream in, AudioFormat f) throws Exception { format=f; open(in); }
	public StreamSource(AudioFormat f) { format=f; }

	public void dispose() { close(); }
	public void setTargetFormat(AudioFormat f) { format=f; }

	/** Returns number of bytes available in current file */
	public int available() throws Exception { return in.available(); }


	/** Allows reading from any given InputStream, but does not affect
	 *  current file or playlist. */
	public synchronized void open(InputStream s) throws Exception {
		open(AudioSystem.getAudioInputStream(s));
	}

	public synchronized void open(AudioInputStream s) throws Exception
	{
		Shell.trace("stream format: "+s.getFormat());
		Shell.trace("file format: "+s.getFormat());

		// in = new BufferedInputStream(s,16*1024);
		// convert to target format if required
		in=s;
		if (format!=null) {
			Shell.trace("Converting to "+format);
			in=AudioSystem.getAudioInputStream(format,s);
		}
	}

	/** Closes current input stream */
	public synchronized void close() {
		try { if (in!=null) in.close(); in=null; }
		catch (IOException ex) {}
	}

	/** Returns a Task which copies samples as doubles into the given
	 *  buffer between the given positions. */
	public Task reader(final double [] dbuf, final int off, final int len) {
		return new AnonymousTask() {
			int			blen=2*len;
			byte [] bbuf = new byte[blen];

			public void run() throws Exception {
				int n = 0;

				while (n < blen) {
					int count = in.read(bbuf, n, blen - n);
					if (count > 0) n+=count;
					else throw new EOFException();
				}
				Util.shortToDouble(bbuf,dbuf,off,len);
			}
		};
	}

	/** Returns a Task which copies samples as floats into the given
	 *  buffer between the given positions. */
	public Task reader(final float [] dbuf, final int off, final int len) {
		return new AnonymousTask() {
			int		blen=2*len;
			byte [] bbuf = new byte[blen];

			public synchronized void run() throws Exception {
				int n = 0;

				while (n < blen) {
					int count = in.read(bbuf, n, blen - n);
					if (count > 0) n+=count;
					else throw new EOFException();
				}
				Util.shortToFloat(bbuf,dbuf,off,len);
			}
		};
	}
}