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
;
28 import java
.util
.Iterator
;
30 import java
.util
.Vector
;
31 import java
.io
.FileInputStream
;
32 import java
.io
.BufferedInputStream
;
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 MPEG file or URL.
42 public class MpegInfo
implements TagInfo
44 protected int channels
= -1;
45 protected String channelsMode
= null;
46 protected String version
= null;
47 protected int rate
= 0;
48 protected String layer
= null;
49 protected String emphasis
= null;
50 protected int nominalbitrate
= 0;
51 protected long total
= 0;
52 protected String vendor
= null;
53 protected String location
= null;
54 protected long size
= 0;
55 protected boolean copyright
= false;
56 protected boolean crc
= false;
57 protected boolean original
= false;
58 protected boolean priv
= false;
59 protected boolean vbr
= false;
60 protected int track
= -1;
61 protected int offset
= 0;
62 protected String year
= null;
63 protected String genre
= null;
64 protected String title
= null;
65 protected String artist
= null;
66 protected String album
= null;
67 protected Vector comments
= null;
78 * Load and parse MPEG info from File.
82 public void load(File input
) throws IOException
, UnsupportedAudioFileException
84 size
= input
.length();
85 location
= input
.getPath();
90 * Load and parse MPEG info from URL.
93 * @throws UnsupportedAudioFileException
95 public void load(URL input
) throws IOException
, UnsupportedAudioFileException
97 location
= input
.toString();
102 * Load and parse MPEG info from InputStream.
104 * @throws IOException
105 * @throws UnsupportedAudioFileException
107 public void load(InputStream input
) throws IOException
, UnsupportedAudioFileException
113 * Load info from input stream.
115 * @throws IOException
116 * @throws UnsupportedAudioFileException
118 protected void loadInfo(InputStream input
) throws IOException
, UnsupportedAudioFileException
120 AudioFileFormat aff
= AudioSystem
.getAudioFileFormat(input
);
125 * Load MP3 info from file.
127 * @throws IOException
128 * @throws UnsupportedAudioFileException
130 protected void loadInfo(File file
) throws IOException
, UnsupportedAudioFileException
132 InputStream in
= new BufferedInputStream(new FileInputStream(file
));
138 * Load info from AudioFileFormat.
141 protected void loadInfo(AudioFileFormat aff
) throws UnsupportedAudioFileException
143 String type
= aff
.getType().toString();
144 if (!type
.equalsIgnoreCase("mp3")) throw new UnsupportedAudioFileException("Not MP3 audio format");
145 if (aff
instanceof TAudioFileFormat
)
147 Map props
= ((TAudioFileFormat
) aff
).properties();
148 if (props
.containsKey("mp3.channels")) channels
= ((Integer
)props
.get("mp3.channels")).intValue();
149 if (props
.containsKey("mp3.frequency.hz")) rate
= ((Integer
)props
.get("mp3.frequency.hz")).intValue();
150 if (props
.containsKey("mp3.bitrate.nominal.bps")) nominalbitrate
= ((Integer
)props
.get("mp3.bitrate.nominal.bps")).intValue();
151 if (props
.containsKey("mp3.version.layer")) layer
= "Layer "+(String
)props
.get("mp3.version.layer");
152 if (props
.containsKey("mp3.version.mpeg"))
154 version
= (String
)props
.get("mp3.version.mpeg");
155 if (version
.equals("1")) version
= "MPEG1";
156 else if (version
.equals("2")) version
= "MPEG2-LSF";
157 else if (version
.equals("2.5")) version
= "MPEG2.5-LSF";
159 if (props
.containsKey("mp3.mode"))
161 int mode
= ((Integer
)props
.get("mp3.mode")).intValue();
162 if (mode
==0) channelsMode
= "Stereo";
163 else if (mode
==1) channelsMode
= "Joint Stereo";
164 else if (mode
==2) channelsMode
= "Dual Channel";
165 else if (mode
==3) channelsMode
= "Single Channel";
167 if (props
.containsKey("mp3.crc")) crc
= ((Boolean
)props
.get("mp3.crc")).booleanValue();
168 if (props
.containsKey("mp3.vbr")) vbr
= ((Boolean
)props
.get("mp3.vbr")).booleanValue();
169 if (props
.containsKey("mp3.copyright")) copyright
= ((Boolean
)props
.get("mp3.copyright")).booleanValue();
170 if (props
.containsKey("mp3.original")) original
= ((Boolean
)props
.get("mp3.original")).booleanValue();
173 if (props
.containsKey("title")) title
= (String
)props
.get("title");
174 if (props
.containsKey("author")) artist
= (String
)props
.get("author");
175 if (props
.containsKey("album")) album
= (String
)props
.get("album");
176 if (props
.containsKey("date")) year
= (String
)props
.get("date");
177 if (props
.containsKey("duration")) total
= (long) Math
.round((((Long
)props
.get("duration")).longValue())/1000000);
178 if (props
.containsKey("mp3.id3tag.genre")) genre
= (String
)props
.get("mp3.id3tag.genre");
180 if (props
.containsKey("mp3.header.pos")) {
181 offset
= ((Integer
)props
.get("mp3.header.pos")).intValue();
185 if (props
.containsKey("mp3.id3tag.track"))
189 track
= Integer
.parseInt((String
)props
.get("mp3.id3tag.track"));
191 catch (NumberFormatException e1
)
200 * Load MP3 info from URL.
202 * @throws IOException
203 * @throws UnsupportedAudioFileException
205 protected void loadInfo(URL input
) throws IOException
, UnsupportedAudioFileException
207 AudioFileFormat aff
= AudioSystem
.getAudioFileFormat(input
);
209 loadShoutastInfo(aff
);
213 * Load Shoutcast info from AudioFileFormat.
215 * @throws IOException
216 * @throws UnsupportedAudioFileException
218 protected void loadShoutastInfo(AudioFileFormat aff
) throws IOException
, UnsupportedAudioFileException
220 String type
= aff
.getType().toString();
221 if (!type
.equalsIgnoreCase("mp3")) throw new UnsupportedAudioFileException("Not MP3 audio format");
222 if (aff
instanceof TAudioFileFormat
)
224 Map props
= ((TAudioFileFormat
) aff
).properties();
225 // Try shoutcast meta data (if any).
226 Iterator it
= props
.keySet().iterator();
227 comments
= new Vector();
230 String key
= (String
) it
.next();
231 if (key
.startsWith("mp3.shoutcast.metadata."))
233 String value
= (String
) props
.get(key
);
234 key
= key
.substring(23,key
.length());
235 if (key
.equalsIgnoreCase("icy-name"))
239 else if (key
.equalsIgnoreCase("icy-genre"))
245 comments
.add(key
+"="+value
);
252 public boolean getVBR()
257 public int getChannels()
262 public String
getVersion()
267 public String
getEmphasis()
272 public boolean getCopyright()
277 public boolean getCRC()
282 public boolean getOriginal()
287 public String
getLayer()
292 public long getSize()
297 public String
getLocation()
302 /*-- TagInfo Implementation --*/
304 public int getSamplingRate()
309 public int getBitRate()
311 return nominalbitrate
;
314 public long getPlayTime()
319 public String
getTitle()
324 public String
getArtist()
329 public String
getAlbum()
334 public int getTrack()
339 public String
getGenre()
344 public Vector
getComment()
349 public String
getYear()
358 public String
getChannelsMode()
363 public int getFirstFrameOffset() {