One more gapless fix for LAME MP3 files.
[kugel-rb.git] / songdbj / MpegInfo.java
blob6f578798838f0abf82b081ca10af66e85d90dc96
1 /*
2 * MpegInfo.
3 *
4 * JavaZOOM : jlgui@javazoom.net
5 * http://www.javazoom.net
6 *
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 *----------------------------------------------------------------------
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.net.URL;
28 import java.util.Iterator;
29 import java.util.Map;
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;
39 /**
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;
69 /**
70 * Constructor.
72 public MpegInfo()
74 super();
77 /**
78 * Load and parse MPEG info from File.
79 * @param input
80 * @throws IOException
82 public void load(File input) throws IOException, UnsupportedAudioFileException
84 size = input.length();
85 location = input.getPath();
86 loadInfo(input);
89 /**
90 * Load and parse MPEG info from URL.
91 * @param input
92 * @throws IOException
93 * @throws UnsupportedAudioFileException
95 public void load(URL input) throws IOException, UnsupportedAudioFileException
97 location = input.toString();
98 loadInfo(input);
102 * Load and parse MPEG info from InputStream.
103 * @param input
104 * @throws IOException
105 * @throws UnsupportedAudioFileException
107 public void load(InputStream input) throws IOException, UnsupportedAudioFileException
109 loadInfo(input);
113 * Load info from input stream.
114 * @param input
115 * @throws IOException
116 * @throws UnsupportedAudioFileException
118 protected void loadInfo(InputStream input) throws IOException, UnsupportedAudioFileException
120 AudioFileFormat aff = AudioSystem.getAudioFileFormat(input);
121 loadInfo(aff);
125 * Load MP3 info from file.
126 * @param file
127 * @throws IOException
128 * @throws UnsupportedAudioFileException
130 protected void loadInfo(File file) throws IOException, UnsupportedAudioFileException
132 InputStream in = new BufferedInputStream(new FileInputStream(file));
133 loadInfo(in);
134 in.close();
138 * Load info from AudioFileFormat.
139 * @param aff
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();
171 emphasis="none";
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();
183 else
184 offset = 0;
185 if (props.containsKey("mp3.id3tag.track"))
189 track = Integer.parseInt((String)props.get("mp3.id3tag.track"));
191 catch (NumberFormatException e1)
193 // Not a number
200 * Load MP3 info from URL.
201 * @param input
202 * @throws IOException
203 * @throws UnsupportedAudioFileException
205 protected void loadInfo(URL input) throws IOException, UnsupportedAudioFileException
207 AudioFileFormat aff = AudioSystem.getAudioFileFormat(input);
208 loadInfo(aff);
209 loadShoutastInfo(aff);
213 * Load Shoutcast info from AudioFileFormat.
214 * @param aff
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();
228 while (it.hasNext())
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"))
237 title = value;
239 else if (key.equalsIgnoreCase("icy-genre"))
241 genre = value;
243 else
245 comments.add(key+"="+value);
252 public boolean getVBR()
254 return vbr;
257 public int getChannels()
259 return channels;
262 public String getVersion()
264 return version;
267 public String getEmphasis()
269 return emphasis;
272 public boolean getCopyright()
274 return copyright;
277 public boolean getCRC()
279 return crc;
282 public boolean getOriginal()
284 return original;
287 public String getLayer()
289 return layer;
292 public long getSize()
294 return size;
297 public String getLocation()
299 return location;
302 /*-- TagInfo Implementation --*/
304 public int getSamplingRate()
306 return rate;
309 public int getBitRate()
311 return nominalbitrate;
314 public long getPlayTime()
316 return total;
319 public String getTitle()
321 return title;
324 public String getArtist()
326 return artist;
329 public String getAlbum()
331 return album;
334 public int getTrack()
336 return track;
339 public String getGenre()
341 return genre;
344 public Vector getComment()
346 return comments;
349 public String getYear()
351 return year;
355 * Get channels mode.
356 * @return
358 public String getChannelsMode()
360 return channelsMode;
363 public int getFirstFrameOffset() {
364 return offset;