samer@0
|
1 /*
|
samer@0
|
2 * LineSource.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@0
|
12 package samer.audio;
|
samer@0
|
13 import samer.core.*;
|
samer@0
|
14 import samer.tools.*;
|
samer@0
|
15 import java.io.*;
|
samer@0
|
16 import javax.sound.sampled.*;
|
samer@0
|
17
|
samer@0
|
18 /**
|
samer@0
|
19 An AudioSource that reads from the sound card in real time.
|
samer@0
|
20 Uses a standard JavaSound TargetDataLine to get data.
|
samer@0
|
21 However, an alternative DataLine can be supplied instead.
|
samer@0
|
22 Audio format is determined by current environment--see
|
samer@0
|
23 VLine.
|
samer@0
|
24 <p> Object is a Viewable, and is called "linein". Node is
|
samer@0
|
25 created in current context, ie with a parent determined by
|
samer@0
|
26 current Environment.
|
samer@0
|
27 @see samer.core.AudioSource
|
samer@0
|
28 */
|
samer@0
|
29
|
samer@0
|
30 public class LineSource extends VLine implements AudioSource
|
samer@0
|
31 {
|
samer@0
|
32 private TargetDataLine line;
|
samer@0
|
33 private AudioFormat fmt;
|
samer@0
|
34 private int bufsize=0;
|
samer@0
|
35
|
samer@0
|
36 /** Create LineSource reading from given TargetDataLine */
|
samer@0
|
37 public LineSource(TargetDataLine l,AudioFormat f) throws Exception {
|
samer@0
|
38 super("linein"); line=l; fmt=f;
|
samer@0
|
39 }
|
samer@0
|
40
|
samer@0
|
41 public void setBufferSize(int b) { bufsize=b; }
|
samer@0
|
42
|
samer@0
|
43 public DataLine getLine() { return line; }
|
samer@0
|
44 public boolean isOpen() { return line.isOpen(); }
|
samer@0
|
45
|
samer@0
|
46 /** Open using default format */
|
samer@0
|
47 public void openImpl() throws Exception {
|
samer@0
|
48 if (bufsize==0) line.open(fmt); else line.open(fmt,bufsize);
|
samer@0
|
49 }
|
samer@0
|
50
|
samer@0
|
51 public void closeImpl() throws Exception { line.close(); }
|
samer@0
|
52
|
samer@0
|
53 public Task reader(final double dbuf[], final int off, final int len) {
|
samer@0
|
54 return new Reader(2*len) {
|
samer@0
|
55 public void run() throws Exception {
|
samer@0
|
56 super.run(); Util.shortToDouble(buf,dbuf,off,len);
|
samer@0
|
57 }
|
samer@0
|
58 };
|
samer@0
|
59 }
|
samer@0
|
60
|
samer@0
|
61 public Task reader(final float dbuf[], final int off, final int len) {
|
samer@0
|
62 return new Reader(2*len) {
|
samer@0
|
63 public void run() throws Exception {
|
samer@0
|
64 super.run(); Util.shortToFloat(buf,dbuf,off,len);
|
samer@0
|
65 }
|
samer@0
|
66 };
|
samer@0
|
67 }
|
samer@0
|
68
|
samer@0
|
69
|
samer@0
|
70 protected class Reader implements Task {
|
samer@0
|
71 byte buf[];
|
samer@0
|
72 int size;
|
samer@0
|
73
|
samer@0
|
74 public Reader(int bufsize) { size=bufsize; buf=new byte[size]; }
|
samer@0
|
75
|
samer@0
|
76 public void starting() { start(); }
|
samer@0
|
77 public void stopping() { stop(); }
|
samer@0
|
78 public void dispose() {}
|
samer@0
|
79 public void run() throws Exception {
|
samer@0
|
80 if (line.read(buf,0,size)<=0) throw new EOFException();
|
samer@0
|
81 changed();
|
samer@0
|
82 }
|
samer@0
|
83 }
|
samer@0
|
84
|
samer@0
|
85 public static DataLine.Info lineInfo(AudioFormat fmt) {
|
samer@0
|
86 return new DataLine.Info( TargetDataLine.class, fmt);
|
samer@0
|
87 }
|
samer@0
|
88
|
samer@0
|
89 public static DataLine.Info lineInfo(AudioFormat fmt, int bufsize) {
|
samer@0
|
90 return new DataLine.Info( TargetDataLine.class, fmt, bufsize);
|
samer@0
|
91 }
|
samer@0
|
92 }
|
samer@0
|
93
|