view audio/java/LineSource.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
/*
 *	LineSource.java
 *
 *	Copyright (c) 2012, 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.*;
import java.io.*;


/**
	An AudioSource that reads from the sound card in real time.
	Uses a standard JavaSound TargetDataLine to get data.
	However, an alternative DataLine can be supplied instead.
*/

public class LineSource extends AudioSource
{
	private TargetDataLine	line;
	private int bufsize;

	/** Create LineSource reading from given TargetDataLine */
	public LineSource(AudioFormat f, int bufsize) throws Exception {
		this((TargetDataLine)AudioSystem.getLine(lineInfo(f,bufsize)),f,bufsize);
	}

	public LineSource(TargetDataLine l,AudioFormat f,int bs) throws Exception {
		super(f); line=l; 
		print("Opening audio data line: "+f);
		if (bs==0) line.open(f); else line.open(f,bs);
		bufsize=line.getBufferSize();
	}

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

	public void start() { line.flush(); line.start(); }
	public void stop() { line.stop(); }
	public int  read(byte [] buf, int offs, int len) throws Exception {
		return line.read(buf,offs,len);
	}

	public DataLine getLine() { return line; }
	public void check() { print("LineSource buffer room available: "+(bufsize-line.available())); }
	public String toString() { return "LineSource("+line.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( TargetDataLine.class, fmt); 
		else
			return new DataLine.Info( TargetDataLine.class, fmt, bufsize); 
	}
}