4 * JavaZOOM : jlgui@javazoom.net
5 * http://www.javazoom.net
7 *-----------------------------------------------------------------------
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Library General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *----------------------------------------------------------------------
25 import java
.io
.IOException
;
26 import java
.io
.InputStream
;
27 import java
.io
.FileInputStream
;
28 import java
.io
.BufferedInputStream
;
31 import java
.util
.Vector
;
33 import javax
.sound
.sampled
.AudioFileFormat
;
34 import javax
.sound
.sampled
.AudioSystem
;
35 import javax
.sound
.sampled
.UnsupportedAudioFileException
;
37 import org
.tritonus
.share
.sampled
.file
.TAudioFileFormat
;
40 * This class gives information (audio format and comments) about Ogg Vorbis file or URL.
42 public class OggVorbisInfo
implements TagInfo
44 protected int serial
= 0;
45 protected int channels
= 0;
46 protected int version
= 0;
47 protected int rate
= 0;
48 protected int minbitrate
= 0;
49 protected int maxbitrate
= 0;
50 protected int averagebitrate
= 0;
51 protected int nominalbitrate
= 0;
52 protected long totalms
= 0;
53 protected String vendor
= "";
54 protected String location
= null;
56 protected long size
= 0;
57 protected int track
= -1;
58 protected String year
= null;
59 protected String genre
= null;
60 protected String title
= null;
61 protected String artist
= null;
62 protected String album
= null;
63 protected Vector comments
= new Vector();
69 public OggVorbisInfo()
75 * Load and parse Ogg Vorbis info from File.
79 public void load(File input
) throws IOException
, UnsupportedAudioFileException
81 size
= input
.length();
82 location
= input
.getPath();
87 * Load and parse Ogg Vorbis info from URL.
90 * @throws UnsupportedAudioFileException
92 public void load(URL input
) throws IOException
, UnsupportedAudioFileException
94 location
= input
.toString();
99 * Load and parse Ogg Vorbis info from InputStream.
101 * @throws IOException
102 * @throws UnsupportedAudioFileException
104 public void load(InputStream input
) throws IOException
, UnsupportedAudioFileException
110 * Load info from input stream.
112 * @throws IOException
113 * @throws UnsupportedAudioFileException
115 protected void loadInfo(InputStream input
) throws IOException
, UnsupportedAudioFileException
117 AudioFileFormat aff
= AudioSystem
.getAudioFileFormat(input
);
122 * Load Ogg Vorbis info from file.
124 * @throws IOException
125 * @throws UnsupportedAudioFileException
127 protected void loadInfo(File file
) throws IOException
, UnsupportedAudioFileException
129 InputStream in
= new BufferedInputStream(new FileInputStream(file
));
135 * Load Ogg Vorbis info from URL.
137 * @throws IOException
138 * @throws UnsupportedAudioFileException
140 protected void loadInfo(URL input
) throws IOException
, UnsupportedAudioFileException
142 AudioFileFormat aff
= AudioSystem
.getAudioFileFormat(input
);
144 loadExtendedInfo(aff
);
148 * Load info from AudioFileFormat.
150 * @throws UnsupportedAudioFileException
152 protected void loadInfo(AudioFileFormat aff
) throws UnsupportedAudioFileException
154 String type
= aff
.getType().toString();
155 if (!type
.equalsIgnoreCase("ogg")) throw new UnsupportedAudioFileException("Not Ogg Vorbis audio format");
156 if (aff
instanceof TAudioFileFormat
)
158 Map props
= ((TAudioFileFormat
) aff
).properties();
159 if (props
.containsKey("ogg.channels")) channels
= ((Integer
)props
.get("ogg.channels")).intValue();
160 if (props
.containsKey("ogg.frequency.hz")) rate
= ((Integer
)props
.get("ogg.frequency.hz")).intValue();
161 if (props
.containsKey("ogg.bitrate.nominal.bps")) nominalbitrate
= ((Integer
)props
.get("ogg.bitrate.nominal.bps")).intValue();
162 averagebitrate
= nominalbitrate
;
163 if (props
.containsKey("ogg.bitrate.max.bps")) maxbitrate
= ((Integer
)props
.get("ogg.bitrate.max.bps")).intValue();
164 if (props
.containsKey("ogg.bitrate.min.bps")) minbitrate
= ((Integer
)props
.get("ogg.bitrate.min.bps")).intValue();
165 if (props
.containsKey("ogg.version")) version
= ((Integer
)props
.get("ogg.version")).intValue();
166 if (props
.containsKey("ogg.serial")) serial
= ((Integer
)props
.get("ogg.serial")).intValue();
167 if (props
.containsKey("ogg.comment.encodedby")) vendor
= (String
)props
.get("ogg.comment.encodedby");
169 if (props
.containsKey("copyright")) comments
.add((String
)props
.get("copyright"));
170 if (props
.containsKey("title")) title
= (String
)props
.get("title");
171 if (props
.containsKey("author")) artist
= (String
)props
.get("author");
172 if (props
.containsKey("album")) album
= (String
)props
.get("album");
173 if (props
.containsKey("date")) year
= (String
)props
.get("date");
174 if (props
.containsKey("comment")) comments
.add((String
)props
.get("comment"));
175 if (props
.containsKey("duration")) totalms
= (long) Math
.round((((Long
)props
.get("duration")).longValue())/1000000);
176 if (props
.containsKey("ogg.comment.genre")) genre
= (String
)props
.get("ogg.comment.genre");
177 if (props
.containsKey("ogg.comment.track"))
181 track
= Integer
.parseInt((String
)props
.get("ogg.comment.track"));
183 catch (NumberFormatException e1
)
188 if (props
.containsKey("ogg.comment.ext.1")) comments
.add((String
)props
.get("ogg.comment.ext.1"));
189 if (props
.containsKey("ogg.comment.ext.2")) comments
.add((String
)props
.get("ogg.comment.ext.2"));
190 if (props
.containsKey("ogg.comment.ext.3")) comments
.add((String
)props
.get("ogg.comment.ext.3"));
195 * Load extended info from AudioFileFormat.
197 * @throws IOException
198 * @throws UnsupportedAudioFileException
200 protected void loadExtendedInfo(AudioFileFormat aff
) throws IOException
, UnsupportedAudioFileException
202 String type
= aff
.getType().toString();
203 if (!type
.equalsIgnoreCase("ogg")) throw new UnsupportedAudioFileException("Not Ogg Vorbis audio format");
204 if (aff
instanceof TAudioFileFormat
)
206 Map props
= ((TAudioFileFormat
) aff
).properties();
207 // How to load icecast meta data (if any) ??
211 public int getSerial()
216 public int getChannels()
221 public int getVersion()
226 public int getMinBitrate()
231 public int getMaxBitrate()
236 public int getAverageBitrate()
238 return averagebitrate
;
241 public long getSize()
246 public String
getVendor()
251 public String
getLocation()
256 /*-- TagInfo Implementation --*/
258 public int getSamplingRate()
263 public int getBitRate()
265 return nominalbitrate
;
268 public long getPlayTime()
273 public String
getTitle()
278 public String
getArtist()
283 public String
getAlbum()
288 public int getTrack()
293 public String
getGenre()
298 public Vector
getComment()
303 public String
getYear()
308 public int getFirstFrameOffset() {