diff audio/java/LineSource.java @ 0:672052bd81f8

Initial partial import.
author samer
date Wed, 19 Dec 2012 22:38:28 +0000
parents
children 63cefb01cbab
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/audio/java/LineSource.java	Wed Dec 19 22:38:28 2012 +0000
@@ -0,0 +1,66 @@
+/*
+ *	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 samer.audio.alt;
+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); 
+	}
+}
+