KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
22 import javax.sound.sampled.*;
23
24 import org.lucane.applications.audioconf.AudioConf;
25 import org.xiph.speex.SpeexDecoder;
26
27 /**
28  * Audio player that plays speex streams
29  */

30 public class AudioPlayer implements Runnable JavaDoc
31 {
32     //-- sound system
33
private SourceDataLine dataLine;
34     private AudioFormat targetFormat;
35     
36     private AudioConfig audioConfig;
37     private InputStream source;
38
39     private AudioConf plugin;
40
41     /**
42      * Constructor.
43      *
44      * @param config the stream configuration
45      * @param source any input stream containing speex data
46      */

47     public AudioPlayer(AudioConf plugin, AudioConfig config, InputStream source)
48     {
49         this.plugin = plugin;
50         this.audioConfig = config;
51         this.source = source;
52         this.targetFormat = config.createAudioFormat(AudioFormat.Encoding.PCM_SIGNED);
53         initDataLine();
54     }
55
56     /**
57      * Initialize the sound system
58      */

59     private void initDataLine()
60     {
61         DataLine.Info info = new DataLine.Info(SourceDataLine.class, targetFormat, AudioSystem.NOT_SPECIFIED);
62         try {
63             dataLine = (SourceDataLine) AudioSystem.getLine(info);
64             dataLine.open(targetFormat, dataLine.getBufferSize());
65         } catch(LineUnavailableException e) {
66             //TODO handle this error
67
e.printStackTrace();
68         }
69     }
70
71     /**
72      * Stop the player
73      */

74     public void stop()
75     {
76         dataLine.flush();
77         dataLine.stop();
78         dataLine.close();
79     }
80
81     /**
82      * Run the player (used in a thread)
83      */

84     public void run()
85     {
86         dataLine.start();
87
88         int read = 1;
89
90         byte[] speex = new byte[audioConfig.getSpeexBufferSize()];
91         byte[] pcm = new byte[audioConfig.getPcmBufferSize()];
92         SpeexDecoder decoder = audioConfig.createDecoder();
93         
94         while (read != -1 && dataLine.isOpen())
95         {
96             try {
97                 read = source.read(speex, 0, speex.length);
98                 decoder.processData(speex, 0, read);
99                 
100                 int length = decoder.getProcessedDataByteSize();
101                 decoder.getProcessedData(pcm, 0);
102             
103                 if(length >= 0)
104                     dataLine.write(pcm, 0, length);
105                                 
106             } catch (Exception JavaDoc e) {
107                 plugin.reportPlayerError(e);
108                 break;
109             }
110             
111         }
112     }
113 }
114
Popular Tags