summaryrefslogtreecommitdiffstats
path: root/songdbj/MpegInfo.java
blob: 6f578798838f0abf82b081ca10af66e85d90dc96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * MpegInfo.
 * 
 * JavaZOOM : jlgui@javazoom.net
 *            http://www.javazoom.net
 * 
 *-----------------------------------------------------------------------
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as published
 *   by the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *----------------------------------------------------------------------
 */

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;

import org.tritonus.share.sampled.file.TAudioFileFormat;

/**
 * This class gives information (audio format and comments) about MPEG file or URL.
 */
public class MpegInfo implements TagInfo
{
  protected int channels = -1;
  protected String channelsMode = null;
  protected String version = null;
  protected int rate = 0;
  protected String layer = null;
  protected String emphasis = null;
  protected int nominalbitrate = 0;
  protected long total = 0;
  protected String vendor = null;
  protected String location = null;
  protected long size = 0;
  protected boolean copyright = false;
  protected boolean crc = false;
  protected boolean original = false;
  protected boolean priv = false;
  protected boolean vbr = false;
  protected int track = -1;
  protected int offset = 0;
  protected String year = null;
  protected String genre = null;
  protected String title = null;
  protected String artist = null;
  protected String album = null;
  protected Vector comments = null;
  
  /**
   * Constructor.
   */
  public MpegInfo()
  {
  	super();
  }
  
  /**
   * Load and parse MPEG info from File.
   * @param input
   * @throws IOException
   */
  public void load(File input) throws IOException, UnsupportedAudioFileException
  {
    size = input.length();
    location = input.getPath();
    loadInfo(input);
  }

  /**
   * Load and parse MPEG info from URL.
   * @param input
   * @throws IOException
   * @throws UnsupportedAudioFileException
   */
  public void load(URL input) throws IOException, UnsupportedAudioFileException
  {
	location = input.toString();
	loadInfo(input);
  }

  /**
   * Load and parse MPEG info from InputStream.
   * @param input
   * @throws IOException
   * @throws UnsupportedAudioFileException
   */
  public void load(InputStream input) throws IOException, UnsupportedAudioFileException
  {
	loadInfo(input);
  }

  /**
   * Load info from input stream.
   * @param input
   * @throws IOException
   * @throws UnsupportedAudioFileException
   */
  protected void loadInfo(InputStream input) throws IOException, UnsupportedAudioFileException
  {
	AudioFileFormat aff = AudioSystem.getAudioFileFormat(input);
	loadInfo(aff);
  }

  /**
   * Load MP3 info from file.
   * @param file
   * @throws IOException
   * @throws UnsupportedAudioFileException
   */
  protected void loadInfo(File file) throws IOException, UnsupportedAudioFileException
  {
 		InputStream in = new BufferedInputStream(new FileInputStream(file));
		loadInfo(in);
		in.close();
  }
  
  /**
   * Load info from AudioFileFormat.
   * @param aff
   */
  protected void loadInfo(AudioFileFormat aff) throws UnsupportedAudioFileException
  {
		String type = aff.getType().toString();
		if (!type.equalsIgnoreCase("mp3")) throw new UnsupportedAudioFileException("Not MP3 audio format");
		if (aff instanceof TAudioFileFormat)
		{
			Map props = ((TAudioFileFormat) aff).properties();
			if (props.containsKey("mp3.channels")) channels = ((Integer)props.get("mp3.channels")).intValue();
			if (props.containsKey("mp3.frequency.hz")) rate = ((Integer)props.get("mp3.frequency.hz")).intValue();
			if (props.containsKey("mp3.bitrate.nominal.bps")) nominalbitrate = ((Integer)props.get("mp3.bitrate.nominal.bps")).intValue();
			if (props.containsKey("mp3.version.layer")) layer = "Layer "+(String)props.get("mp3.version.layer");
			if (props.containsKey("mp3.version.mpeg"))
			{
				version = (String)props.get("mp3.version.mpeg");
				if (version.equals("1")) version = "MPEG1"; 
				else if (version.equals("2")) version = "MPEG2-LSF";
				else if (version.equals("2.5")) version = "MPEG2.5-LSF";
			}
			if (props.containsKey("mp3.mode"))
			{
				int mode = ((Integer)props.get("mp3.mode")).intValue();
				if (mode==0) channelsMode = "Stereo";
				else if (mode==1) channelsMode = "Joint Stereo"; 
				else if (mode==2) channelsMode = "Dual Channel";
				else if (mode==3) channelsMode = "Single Channel";
			}
			if (props.containsKey("mp3.crc")) crc = ((Boolean)props.get("mp3.crc")).booleanValue();
			if (props.containsKey("mp3.vbr")) vbr = ((Boolean)props.get("mp3.vbr")).booleanValue();
			if (props.containsKey("mp3.copyright")) copyright = ((Boolean)props.get("mp3.copyright")).booleanValue();
			if (props.containsKey("mp3.original")) original = ((Boolean)props.get("mp3.original")).booleanValue();
			emphasis="none";

			if (props.containsKey("title")) title = (String)props.get("title");
			if (props.containsKey("author")) artist = (String)props.get("author");
			if (props.containsKey("album")) album = (String)props.get("album");
			if (props.containsKey("date")) year = (String)props.get("date");
			if (props.containsKey("duration")) total = (long) Math.round((((Long)props.get("duration")).longValue())/1000000);
			if (props.containsKey("mp3.id3tag.genre")) genre = (String)props.get("mp3.id3tag.genre");
			
			if (props.containsKey("mp3.header.pos")) {
			  offset = ((Integer)props.get("mp3.header.pos")).intValue();
			}
			else
				offset = 0;
			if (props.containsKey("mp3.id3tag.track"))
			{
				try
				{
					track = Integer.parseInt((String)props.get("mp3.id3tag.track"));
				}
				catch (NumberFormatException e1)
				{
					// Not a number
				}
			}		 
		} 
  }

  /**
   * Load MP3 info from URL.
   * @param input
   * @throws IOException
   * @throws UnsupportedAudioFileException
   */
  protected void loadInfo(URL input) throws IOException, UnsupportedAudioFileException
  {
		AudioFileFormat aff = AudioSystem.getAudioFileFormat(input);
		loadInfo(aff);
		loadShoutastInfo(aff);
  }
  
  /**
   * Load Shoutcast info from AudioFileFormat.
   * @param aff
   * @throws IOException
   * @throws UnsupportedAudioFileException
   */
  protected void loadShoutastInfo(AudioFileFormat aff) throws IOException, UnsupportedAudioFileException
  {
		String type = aff.getType().toString();
		if (!type.equalsIgnoreCase("mp3")) throw new UnsupportedAudioFileException("Not MP3 audio format");
		if (aff instanceof TAudioFileFormat)
		{
			Map props = ((TAudioFileFormat) aff).properties();
			// Try shoutcast meta data (if any).
			Iterator it = props.keySet().iterator();
			comments = new Vector();
			while (it.hasNext())
			{
				String key = (String) it.next();
				if (key.startsWith("mp3.shoutcast.metadata."))
				{
					String value = (String) props.get(key);
					key = key.substring(23,key.length());
					if (key.equalsIgnoreCase("icy-name"))
					{
						title = value;
					}
					else if (key.equalsIgnoreCase("icy-genre"))
					{
						genre = value;
					}
					else
					{
						comments.add(key+"="+value);
					}
				}
			}
		} 
  }

  public boolean getVBR()
  {
    return vbr;
  }

  public int getChannels()
  {
    return channels;
  }

  public String getVersion()
  {
    return version;
  }

  public String getEmphasis()
  {
    return emphasis;
  }

  public boolean getCopyright()
  {
    return copyright;
  }

  public boolean getCRC()
  {
    return crc;
  }

  public boolean getOriginal()
  {
    return original;
  }

  public String getLayer()
  {
    return layer;
  }

  public long getSize()
  {
    return size;
  }

  public String getLocation()
  {
    return location;
  }

  /*-- TagInfo Implementation --*/

  public int getSamplingRate()
  {
    return rate;
  }

  public int getBitRate()
  {
    return nominalbitrate;
  }

  public long getPlayTime()
  {
    return total;
  }

  public String getTitle()
  {
    return title;
  }

  public String getArtist()
  {
    return artist;
  }

  public String getAlbum()
  {
    return album;
  }

  public int getTrack()
  {
    return track;
  }

  public String getGenre()
  {
    return genre;
  }

  public Vector getComment()
  {
    return comments;
  }

  public String getYear()
  {
    return year;
  }

  /**
   * Get channels mode.
   * @return
   */
  public String getChannelsMode()
  {
	return channelsMode;
  }
  
  public int getFirstFrameOffset() {
  	return offset;
  }

}