KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > audioconf > audio > AudioRecorder


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.audioconf.audio;
20
21 import java.util.*;
22
23 import javax.sound.sampled.*;
24
25 import org.xiph.speex.SpeexEncoder;
26
27 /**
28  * Audio recorder that encodes directly in Speex
29  */

30 public class AudioRecorder implements Runnable JavaDoc
31 {
32     //-- attributes
33
private TargetDataLine dataLine;
34     private AudioFormat audioFormat;
35     private AudioConfig audioConfig;
36     
37     private ArrayList listeners;
38     
39     /**
40      * Constructor
41      *
42      * @param config the audio configuration to use to encode
43      */

44     public AudioRecorder(AudioConfig config)
45     {
46         this.listeners = new ArrayList();
47         
48         this.audioConfig = config;
49         this.audioFormat = config.createAudioFormat(AudioFormat.Encoding.PCM_SIGNED);
50         DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
51         try {
52             dataLine = (TargetDataLine) AudioSystem.getLine(info);
53         } catch (LineUnavailableException lue) {
54             lue.printStackTrace();
55         }
56     }
57     
58     /**
59      * Add a listener that will be notified of recording status
60      *
61      * @param listener the listener to add
62      */

63     public void addAudioListener(AudioRecorderListener listener)
64     {
65         this.listeners.add(listener);
66     }
67     
68     /**
69      * Remove a listener
70      *
71      * @param listener the listener to remove
72      */

73     public void removeAudioListener(AudioRecorderListener listener)
74     {
75         this.listeners.remove(listener);
76     }
77
78     /**
79      * Stop recording
80      */

81     public void stop()
82     {
83         dataLine.stop();
84         dataLine.close();
85         
86         //notify listeners
87
Iterator listeners = this.listeners.iterator();
88         while(listeners.hasNext())
89             ((AudioRecorderListener)listeners.next()).audioRecordingEnded();
90     }
91     
92     /**
93      * Run recording as a thread
94      */

95     public void run()
96     {
97         try {
98             dataLine.open();
99             dataLine.start();
100         } catch(LineUnavailableException lue) {
101             lue.printStackTrace();
102             return;
103         }
104         
105         //notify listeners
106
Iterator listeners = this.listeners.iterator();
107         while(listeners.hasNext())
108             ((AudioRecorderListener)listeners.next()).audioRecordingStarted(audioConfig);
109         
110         
111         //do the real recording
112
byte[] pcm = new byte[audioConfig.getPcmBufferSize()];
113         byte[] speex = new byte[audioConfig.getSpeexBufferSize()];
114         SpeexEncoder encoder = audioConfig.createEncoder();
115         
116         while(dataLine.isOpen())
117         {
118             int length = dataLine.read(pcm, 0, pcm.length);
119             encoder.processData(pcm, 0, pcm.length);
120             length = encoder.getProcessedDataByteSize();
121             encoder.getProcessedData(speex, 0);
122                         
123             if(length > 0)
124             {
125                 listeners = this.listeners.iterator();
126                 while(listeners.hasNext())
127                     ((AudioRecorderListener)listeners.next()).audioRecorded(speex, length);
128             }
129         }
130     }
131 }
Popular Tags