samer@0: /* samer@0: * LineSource.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@0: package samer.audio; samer@0: import samer.core.*; samer@0: import samer.tools.*; samer@0: import java.io.*; samer@0: import javax.sound.sampled.*; samer@0: samer@0: /** samer@0: An AudioSource that reads from the sound card in real time. samer@0: Uses a standard JavaSound TargetDataLine to get data. samer@0: However, an alternative DataLine can be supplied instead. samer@0: Audio format is determined by current environment--see samer@0: VLine. samer@0:
Object is a Viewable, and is called "linein". Node is samer@0: created in current context, ie with a parent determined by samer@0: current Environment. samer@0: @see samer.core.AudioSource samer@0: */ samer@0: samer@0: public class LineSource extends VLine implements AudioSource samer@0: { samer@0: private TargetDataLine line; samer@0: private AudioFormat fmt; samer@0: private int bufsize=0; samer@0: samer@0: /** Create LineSource reading from given TargetDataLine */ samer@0: public LineSource(TargetDataLine l,AudioFormat f) throws Exception { samer@0: super("linein"); line=l; fmt=f; samer@0: } samer@0: samer@1: public int getChannels() { return fmt.getChannels(); } samer@1: public float getRate() { return fmt.getFrameRate(); } samer@1: public AudioFormat getFormat() { return fmt; } samer@0: public void setBufferSize(int b) { bufsize=b; } samer@0: samer@0: public DataLine getLine() { return line; } samer@0: public boolean isOpen() { return line.isOpen(); } samer@0: samer@0: /** Open using default format */ samer@0: public void openImpl() throws Exception { samer@0: if (bufsize==0) line.open(fmt); else line.open(fmt,bufsize); samer@0: } samer@0: samer@0: public void closeImpl() throws Exception { line.close(); } samer@0: samer@0: public Task reader(final double dbuf[], final int off, final int len) { samer@0: return new Reader(2*len) { samer@0: public void run() throws Exception { samer@0: super.run(); Util.shortToDouble(buf,dbuf,off,len); samer@0: } samer@0: }; samer@0: } samer@0: samer@0: public Task reader(final float dbuf[], final int off, final int len) { samer@0: return new Reader(2*len) { samer@0: public void run() throws Exception { samer@0: super.run(); Util.shortToFloat(buf,dbuf,off,len); samer@0: } samer@0: }; samer@0: } samer@0: samer@0: samer@0: protected class Reader implements Task { samer@0: byte buf[]; samer@0: int size; samer@0: samer@0: public Reader(int bufsize) { size=bufsize; buf=new byte[size]; } samer@0: samer@0: public void starting() { start(); } samer@0: public void stopping() { stop(); } samer@0: public void dispose() {} samer@0: public void run() throws Exception { samer@0: if (line.read(buf,0,size)<=0) throw new EOFException(); samer@0: changed(); samer@0: } samer@0: } samer@0: samer@0: public static DataLine.Info lineInfo(AudioFormat fmt) { samer@0: return new DataLine.Info( TargetDataLine.class, fmt); samer@0: } samer@0: samer@0: public static DataLine.Info lineInfo(AudioFormat fmt, int bufsize) { samer@0: return new DataLine.Info( TargetDataLine.class, fmt, bufsize); samer@0: } samer@0: } samer@0: