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@1
|
41 public int getChannels() { return fmt.getChannels(); }
|
samer@1
|
42 public float getRate() { return fmt.getFrameRate(); }
|
samer@1
|
43 public AudioFormat getFormat() { return fmt; }
|
samer@0
|
44 public void setBufferSize(int b) { bufsize=b; }
|
samer@0
|
45
|
samer@0
|
46 public DataLine getLine() { return line; }
|
samer@0
|
47 public boolean isOpen() { return line.isOpen(); }
|
samer@0
|
48
|
samer@0
|
49 /** Open using default format */
|
samer@0
|
50 public void openImpl() throws Exception {
|
samer@0
|
51 if (bufsize==0) line.open(fmt); else line.open(fmt,bufsize);
|
samer@0
|
52 }
|
samer@0
|
53
|
samer@0
|
54 public void closeImpl() throws Exception { line.close(); }
|
samer@0
|
55
|
samer@0
|
56 public Task reader(final double dbuf[], final int off, final int len) {
|
samer@0
|
57 return new Reader(2*len) {
|
samer@0
|
58 public void run() throws Exception {
|
samer@0
|
59 super.run(); Util.shortToDouble(buf,dbuf,off,len);
|
samer@0
|
60 }
|
samer@0
|
61 };
|
samer@0
|
62 }
|
samer@0
|
63
|
samer@0
|
64 public Task reader(final float dbuf[], final int off, final int len) {
|
samer@0
|
65 return new Reader(2*len) {
|
samer@0
|
66 public void run() throws Exception {
|
samer@0
|
67 super.run(); Util.shortToFloat(buf,dbuf,off,len);
|
samer@0
|
68 }
|
samer@0
|
69 };
|
samer@0
|
70 }
|
samer@0
|
71
|
samer@0
|
72
|
samer@0
|
73 protected class Reader implements Task {
|
samer@0
|
74 byte buf[];
|
samer@0
|
75 int size;
|
samer@0
|
76
|
samer@0
|
77 public Reader(int bufsize) { size=bufsize; buf=new byte[size]; }
|
samer@0
|
78
|
samer@0
|
79 public void starting() { start(); }
|
samer@0
|
80 public void stopping() { stop(); }
|
samer@0
|
81 public void dispose() {}
|
samer@0
|
82 public void run() throws Exception {
|
samer@0
|
83 if (line.read(buf,0,size)<=0) throw new EOFException();
|
samer@0
|
84 changed();
|
samer@0
|
85 }
|
samer@0
|
86 }
|
samer@0
|
87
|
samer@0
|
88 public static DataLine.Info lineInfo(AudioFormat fmt) {
|
samer@0
|
89 return new DataLine.Info( TargetDataLine.class, fmt);
|
samer@0
|
90 }
|
samer@0
|
91
|
samer@0
|
92 public static DataLine.Info lineInfo(AudioFormat fmt, int bufsize) {
|
samer@0
|
93 return new DataLine.Info( TargetDataLine.class, fmt, bufsize);
|
samer@0
|
94 }
|
samer@0
|
95 }
|
samer@0
|
96
|