samer@0: /* samer@0: * LineSink.java samer@0: * samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@46: package ishara.audio; samer@0: import javax.sound.sampled.*; samer@0: samer@0: /** samer@0: An AudioSink that sends samples to a Java Sound SourceDataLine. samer@0: Audio format can be determined in several ways (see below). samer@0:

Object is a Viewable, and is called "lineout". samer@0: Reads property "scale" from current environment, but scale samer@0: can be adjusted afterwards. samer@46: @see ishara.audio.AudioSink samer@0: */ samer@0: samer@0: public class LineSink extends AudioSink samer@0: { samer@0: private SourceDataLine line; samer@0: private int bufsize; samer@0: samer@0: public LineSink(AudioFormat f, int bufsize) throws Exception { samer@0: this((SourceDataLine)AudioSystem.getLine(lineInfo(f,bufsize)),f,bufsize); samer@0: } samer@0: samer@0: public LineSink(SourceDataLine l, AudioFormat f, int bufsize) throws Exception { samer@0: super(f); line=l; samer@0: if (bufsize==0) line.open(f); else line.open(f,bufsize); samer@0: this.bufsize=line.getBufferSize(); samer@0: } samer@0: samer@0: public void dispose() { samer@0: print("Closing audio input line."); samer@0: try { line.close(); } samer@0: catch (Exception ex) { print("line failed to close: "+ex); } samer@0: } samer@0: samer@0: public void start() { line.start(); } samer@0: public void stop() { line.stop(); } samer@0: public int bwrite(byte [] buf, int off, int n) throws Exception { return line.write(buf,off,n); } samer@0: samer@0: public DataLine getLine() { return line; } samer@0: public void check() { print("LineSink samples in buffer: "+(bufsize-line.available())); } samer@0: public String toString() { return "LineSink: "+getLine().getFormat(); } samer@0: samer@0: public static DataLine.Info lineInfo(AudioFormat fmt) { return lineInfo(fmt,0); } samer@0: public static DataLine.Info lineInfo(AudioFormat fmt, int bufsize) { samer@0: if (bufsize==0) samer@0: return new DataLine.Info( SourceDataLine.class, fmt); samer@0: else samer@0: return new DataLine.Info( SourceDataLine.class, fmt, bufsize); samer@0: } samer@0: }