view audio/java/LineSink.java @ 61:eff6bddf82e3 tip

Finally implemented perceptual brightness thing.
author samer
date Sun, 11 Oct 2015 10:20:42 +0100
parents 63cefb01cbab
children
line wrap: on
line source
/*
 *	LineSink.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 ishara.audio;
import  javax.sound.sampled.*;

/**
	An AudioSink that sends samples to a Java Sound SourceDataLine.
	Audio format can be determined in several ways (see below).
	<p> Object is a Viewable, and is called "lineout".
	Reads property "scale" from current environment, but scale
	can be adjusted afterwards.
	@see ishara.audio.AudioSink
*/

public class LineSink extends AudioSink
{
	private SourceDataLine	line;
	private int 			bufsize;

	public LineSink(AudioFormat f, int bufsize) throws Exception {
		this((SourceDataLine)AudioSystem.getLine(lineInfo(f,bufsize)),f,bufsize);
	}

	public LineSink(SourceDataLine l, AudioFormat f, int bufsize) throws Exception {
		super(f); line=l; 
		if (bufsize==0) line.open(f); else line.open(f,bufsize);
		this.bufsize=line.getBufferSize();
	}

	public void dispose() { 
		print("Closing audio input line.");
		try { line.close(); } 
		catch (Exception ex) { print("line failed to close: "+ex); } 
	}

	public void start() { line.start(); }
	public void stop() { line.stop(); }
	public int  bwrite(byte [] buf, int off, int n) throws Exception { return line.write(buf,off,n); }

	public DataLine getLine() { return line; }
	public void check() { print("LineSink samples in buffer: "+(bufsize-line.available())); }
	public String toString() { return "LineSink: "+getLine().getFormat(); }

	public static DataLine.Info lineInfo(AudioFormat fmt) { return lineInfo(fmt,0); }
	public static DataLine.Info lineInfo(AudioFormat fmt, int bufsize) {
		if (bufsize==0)
			return new DataLine.Info( SourceDataLine.class, fmt); 
		else
			return new DataLine.Info( SourceDataLine.class, fmt, bufsize); 
	}
}