samer@0
|
1 /*
|
samer@0
|
2 * LineSink.java
|
samer@0
|
3 *
|
samer@0
|
4 * Copyright (c) 2000, Samer Abdallah, King's College London.
|
samer@0
|
5 * All rights reserved.
|
samer@0
|
6 *
|
samer@0
|
7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
|
samer@0
|
8 * without even the implied warranty of MERCHANTABILITY or
|
samer@0
|
9 * FITNESS FOR A PARTICULAR PURPOSE.
|
samer@0
|
10 */
|
samer@0
|
11
|
samer@46
|
12 package ishara.audio;
|
samer@0
|
13 import javax.sound.sampled.*;
|
samer@0
|
14
|
samer@0
|
15 /**
|
samer@0
|
16 An AudioSink that sends samples to a Java Sound SourceDataLine.
|
samer@0
|
17 Audio format can be determined in several ways (see below).
|
samer@0
|
18 <p> Object is a Viewable, and is called "lineout".
|
samer@0
|
19 Reads property "scale" from current environment, but scale
|
samer@0
|
20 can be adjusted afterwards.
|
samer@46
|
21 @see ishara.audio.AudioSink
|
samer@0
|
22 */
|
samer@0
|
23
|
samer@0
|
24 public class LineSink extends AudioSink
|
samer@0
|
25 {
|
samer@0
|
26 private SourceDataLine line;
|
samer@0
|
27 private int bufsize;
|
samer@0
|
28
|
samer@0
|
29 public LineSink(AudioFormat f, int bufsize) throws Exception {
|
samer@0
|
30 this((SourceDataLine)AudioSystem.getLine(lineInfo(f,bufsize)),f,bufsize);
|
samer@0
|
31 }
|
samer@0
|
32
|
samer@0
|
33 public LineSink(SourceDataLine l, AudioFormat f, int bufsize) throws Exception {
|
samer@0
|
34 super(f); line=l;
|
samer@0
|
35 if (bufsize==0) line.open(f); else line.open(f,bufsize);
|
samer@0
|
36 this.bufsize=line.getBufferSize();
|
samer@0
|
37 }
|
samer@0
|
38
|
samer@0
|
39 public void dispose() {
|
samer@0
|
40 print("Closing audio input line.");
|
samer@0
|
41 try { line.close(); }
|
samer@0
|
42 catch (Exception ex) { print("line failed to close: "+ex); }
|
samer@0
|
43 }
|
samer@0
|
44
|
samer@0
|
45 public void start() { line.start(); }
|
samer@0
|
46 public void stop() { line.stop(); }
|
samer@0
|
47 public int bwrite(byte [] buf, int off, int n) throws Exception { return line.write(buf,off,n); }
|
samer@0
|
48
|
samer@0
|
49 public DataLine getLine() { return line; }
|
samer@0
|
50 public void check() { print("LineSink samples in buffer: "+(bufsize-line.available())); }
|
samer@0
|
51 public String toString() { return "LineSink: "+getLine().getFormat(); }
|
samer@0
|
52
|
samer@0
|
53 public static DataLine.Info lineInfo(AudioFormat fmt) { return lineInfo(fmt,0); }
|
samer@0
|
54 public static DataLine.Info lineInfo(AudioFormat fmt, int bufsize) {
|
samer@0
|
55 if (bufsize==0)
|
samer@0
|
56 return new DataLine.Info( SourceDataLine.class, fmt);
|
samer@0
|
57 else
|
samer@0
|
58 return new DataLine.Info( SourceDataLine.class, fmt, bufsize);
|
samer@0
|
59 }
|
samer@0
|
60 }
|