001: /*
002: * Copyright (c) 2000 Silvere Martin-Michiellot All Rights Reserved.
003: *
004: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
005: * royalty free, license to use, modify and redistribute this
006: * software in source and binary code form,
007: * provided that i) this copyright notice and license appear on all copies of
008: * the software; and ii) Licensee does not utilize the software in a manner
009: * which is disparaging to Silvere Martin-Michiellot.
010: *
011: * This software is provided "AS IS," without a warranty of any kind. ALL
012: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
013: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
014: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
015: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
016: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
017: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
018: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
019: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
020: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
021: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
022: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
023: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
024: *
025: * This software is not designed or intended for use in on-line control of
026: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
027: * the design, construction, operation or maintenance of any nuclear
028: * facility. Licensee represents and warrants that it will not use or
029: * redistribute the Software for such purposes.
030: *
031: */
032:
033: package com.db.media.audio;
034:
035: import java.io.*;
036: import javax.sound.sampled.*;
037:
038: public class DirectRecordingStream extends Thread {
039:
040: private TargetDataLine line;
041: private AudioFileFormat.Type targetType;
042: private AudioInputStream audioInputStream;
043: private Object outputObject;
044: private boolean recording;
045:
046: public DirectRecordingStream(AudioFormat audioFormat,
047: AudioFileFormat.Type targetType, OutputStream outputStream)
048: throws LineUnavailableException {
049:
050: this (audioFormat, targetType, (Object) outputStream);
051:
052: }
053:
054: public DirectRecordingStream(AudioFormat audioFormat,
055: AudioFileFormat.Type targetType, File file)
056: throws LineUnavailableException {
057:
058: this (audioFormat, targetType, (Object) file);
059:
060: }
061:
062: private DirectRecordingStream(AudioFormat audioFormat,
063: AudioFileFormat.Type targetType, Object destination)
064: throws LineUnavailableException {
065:
066: TargetDataLine line = getTargetDataLine(audioFormat);
067: init(line, targetType, destination);
068:
069: }
070:
071: public DirectRecordingStream(TargetDataLine line,
072: AudioFileFormat.Type targetType, OutputStream outputStream) {
073:
074: this (line, targetType, (Object) outputStream);
075:
076: }
077:
078: public DirectRecordingStream(TargetDataLine line,
079: AudioFileFormat.Type targetType, File file) {
080:
081: this (line, targetType, (Object) file);
082:
083: }
084:
085: private DirectRecordingStream(TargetDataLine line,
086: AudioFileFormat.Type targetType, Object destination) {
087:
088: init(line, targetType, destination);
089:
090: }
091:
092: private static TargetDataLine getTargetDataLine(
093: AudioFormat audioFormat) throws LineUnavailableException {
094:
095: DataLine.Info info = new DataLine.Info(TargetDataLine.class,
096: audioFormat);
097: TargetDataLine aLine = null;
098: aLine = (TargetDataLine) AudioSystem.getLine(info);
099: aLine.open(audioFormat);
100:
101: return aLine;
102:
103: }
104:
105: private void init(TargetDataLine line,
106: AudioFileFormat.Type targetType, Object destination) {
107:
108: this .line = line;
109: this .audioInputStream = new AudioInputStream(line);
110: this .targetType = targetType;
111: this .outputObject = destination;
112:
113: }
114:
115: public void start() {
116:
117: line.start();
118: super .start();
119: recording = true;
120:
121: }
122:
123: public boolean isRecording() {
124:
125: return recording;
126:
127: }
128:
129: public void stopRecording() {
130:
131: line.stop();
132: line.close();
133: recording = false;
134:
135: }
136:
137: public void run() {
138:
139: if (outputObject instanceof File) {
140: try {
141: AudioSystem.write(audioInputStream, targetType,
142: (File) outputObject);
143: } catch (IOException e) {
144: e.printStackTrace();
145: }
146: } else if (outputObject instanceof OutputStream) {
147: try {
148: AudioSystem.write(audioInputStream, targetType,
149: (OutputStream) outputObject);
150: } catch (IOException e) {
151: e.printStackTrace();
152: }
153: }
154:
155: }
156:
157: }
|