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

Initial partial import.
author samer
date Wed, 19 Dec 2012 22:38:28 +0000
parents
children 63cefb01cbab
comparison
equal deleted inserted replaced
-1:000000000000 0:672052bd81f8
1 /*
2 * LineSource.java
3 *
4 * Copyright (c) 2012, Samer Abdallah, King's College London.
5 * All rights reserved.
6 *
7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
8 * without even the implied warranty of MERCHANTABILITY or
9 * FITNESS FOR A PARTICULAR PURPOSE.
10 */
11
12 package samer.audio.alt;
13 import javax.sound.sampled.*;
14 import java.io.*;
15
16
17 /**
18 An AudioSource that reads from the sound card in real time.
19 Uses a standard JavaSound TargetDataLine to get data.
20 However, an alternative DataLine can be supplied instead.
21 */
22
23 public class LineSource extends AudioSource
24 {
25 private TargetDataLine line;
26 private int bufsize;
27
28 /** Create LineSource reading from given TargetDataLine */
29 public LineSource(AudioFormat f, int bufsize) throws Exception {
30 this((TargetDataLine)AudioSystem.getLine(lineInfo(f,bufsize)),f,bufsize);
31 }
32
33 public LineSource(TargetDataLine l,AudioFormat f,int bs) throws Exception {
34 super(f); line=l;
35 print("Opening audio data line: "+f);
36 if (bs==0) line.open(f); else line.open(f,bs);
37 bufsize=line.getBufferSize();
38 }
39
40 public void dispose() {
41 print("Closing audio data line");
42 try { line.close(); }
43 catch (Exception ex) {
44 print("line failed to close: "+ex);
45 }
46 }
47
48 public void start() { line.flush(); line.start(); }
49 public void stop() { line.stop(); }
50 public int read(byte [] buf, int offs, int len) throws Exception {
51 return line.read(buf,offs,len);
52 }
53
54 public DataLine getLine() { return line; }
55 public void check() { print("LineSource buffer room available: "+(bufsize-line.available())); }
56 public String toString() { return "LineSource("+line.getFormat()+")"; }
57
58 public static DataLine.Info lineInfo(AudioFormat fmt) { return lineInfo(fmt,0); }
59 public static DataLine.Info lineInfo(AudioFormat fmt, int bufsize) {
60 if (bufsize==0)
61 return new DataLine.Info( TargetDataLine.class, fmt);
62 else
63 return new DataLine.Info( TargetDataLine.class, fmt, bufsize);
64 }
65 }
66