KickJava   Java API By Example, From Geeks To Geeks.

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


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.Serializable JavaDoc;
22
23 import javax.sound.sampled.AudioFormat JavaDoc;
24 import org.xiph.speex.*;
25
26 /**
27  * Configuration for audio stream
28  */

29 public class AudioConfig implements Serializable JavaDoc
30 {
31     //-- speex buffer size
32
private static final int[][] SPEEX_BUFFERS = {
33         {8, 12, 17, 23, 23, 30, 30, 40, 40, 48, 64},
34         {12, 17, 22, 27, 35, 45, 54, 62, 72, 88, 108},
35         {13, 21, 26, 32, 39, 49, 59, 67, 77, 93, 113}
36     };
37     
38     //-- speex modes - frame rates
39
public static final int NARROWBAND = 1;
40     public static final int WIDEBAND = 2;
41     public static final int ULTRA_WIDEBAND = 3;
42     
43     //-- attributes
44
private int mode;
45     private int quality;
46     private int channels;
47     
48     /**
49      * Constructor
50      *
51      * @param mode the audio mode (NARROWBAND, WIDEBAND or ULTRA_WIDEBAND)
52      * @param quality the quality (1 - 10)
53      */

54     public AudioConfig(int mode, int quality)
55     {
56         this.mode = mode;
57         this.quality = quality;
58         this.channels = 2;
59     }
60     
61     /**
62      * Return the frame rate
63      *
64      * @return the frame rate
65      */

66     public int getFrameRate()
67     {
68         if(this.mode == NARROWBAND)
69             return 8000;
70         if(this.mode == WIDEBAND)
71             return 16000;
72         if(this.mode == ULTRA_WIDEBAND)
73             return 32000;
74         
75         throw new IllegalStateException JavaDoc("unknown mode: " + this.mode);
76     }
77     
78     /**
79      * Return the number of channels
80      *
81      * @return the number of channels
82      */

83     public int getChannels()
84     {
85         return this.channels;
86     }
87         
88     public int getQuality()
89     {
90         return this.quality;
91     }
92
93     public int getSpeexMode()
94     {
95         return this.mode -1;
96     }
97         
98     public int getPcmBufferSize()
99     {
100         if(this.mode == NARROWBAND)
101             return 640;
102         if(this.mode == WIDEBAND)
103             return 1280;
104         if(this.mode == ULTRA_WIDEBAND)
105             return 2560;
106         
107         throw new IllegalStateException JavaDoc("unknown mode: " + this.mode);
108     }
109     
110     public int getSpeexBufferSize()
111     {
112         return SPEEX_BUFFERS[this.mode-1][this.quality];
113     }
114     
115     public String JavaDoc toString()
116     {
117         return "" +this.mode;
118     }
119     
120     //-- factories
121

122     /**
123      * Factory to create AudioFormat with this configuration
124      *
125      * @param type the audio encoding (PCM or SPEEX)
126      * @return the audio format
127      */

128     public AudioFormat JavaDoc createAudioFormat(AudioFormat.Encoding JavaDoc type)
129     {
130         return new AudioFormat JavaDoc(
131                 type,
132                 this.getFrameRate(),
133                 16,
134                 this.getChannels(),
135                 this.getChannels()*2,
136                 this.getFrameRate(),
137                 false);
138     }
139     
140     public SpeexEncoder createEncoder()
141     {
142         SpeexEncoder encoder = new SpeexEncoder();
143         encoder.init(getSpeexMode(), getQuality(), getFrameRate(), getChannels());
144         return encoder;
145     }
146     
147     public SpeexDecoder createDecoder()
148     {
149         SpeexDecoder decoder = new SpeexDecoder();
150         decoder.init(getSpeexMode(), getFrameRate(), getChannels(), false);
151         return decoder;
152     }
153 }
Popular Tags