View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program 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
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.io;
21  
22  import org.xnap.XNap;
23  import org.xnap.util.Formatter;
24  
25  /***
26   * MP3 header format documented at:
27   * http://mp3tech.free.fr/programmers/frame_header.html
28   */
29  public class MP3MetaInfo {
30      
31      // --- Constant(s) ---
32  
33      // --- Data Field(s) ---
34  
35  	private MetaInfoFile file;
36  	
37      // --- Constructor(s) ---
38  
39      public MP3MetaInfo(MetaInfoFile file)
40      {
41  		this.file = file;
42      }
43  
44      // --- Method(s) ---
45      
46      public int getBitrate() 
47      { 
48  		return ((Integer)file.get(MetaInfoFile.BITRATE)).intValue(); 
49      }
50  
51      public int getFrequency() 
52      { 
53  		return ((Integer)file.get(MetaInfoFile.FREQUENCY)).intValue(); 
54      }
55  
56      /***
57       * Returns the length of the file in seconds.
58       */
59      public long getPlayingTime() 
60      { 
61  		return ((Long)file.get(MetaInfoFile.LENGTH)).longValue(); 
62      }
63  
64      public String getInfo()
65      {
66  		return XNap.tr("{0} bit, {1}", new Integer(getBitrate()),
67  					   Formatter.formatLength(getPlayingTime()));
68      }
69  
70      public static boolean hasInfo(MetaInfoFile file)
71      {
72      	return "true".equals(file.get("provider:mp3"));
73      }
74      
75  	/***
76  	 *  @see org.xnap.io.MetaInfoProvider#handle(org.xnap.io.MetaInfoFile)
77  	 */
78  	public static boolean handle(MetaInfoFile file)
79  	{
80  		try {
81  			helliker.id3.MP3File m = new helliker.id3.MP3File(file);
82  			
83  			file.put(MetaInfoFile.BITRATE, new Integer(m.getBitRate()));
84  			file.put(MetaInfoFile.FREQUENCY, new Integer(m.getSampleRate()));
85  			file.put(MetaInfoFile.LENGTH, new Long(m.getPlayingTime()));
86  			file.put("provider:mp3", "true");
87  			return true;
88  		}
89  		catch (Exception e) {
90  			return false;
91  		}
92  	}
93  
94      public static class Provider implements MetaInfoProvider {
95  
96  		/***
97  		 *  @see org.xnap.io.MetaInfoProvider#handle(org.xnap.io.MetaInfoFile)
98  		 */
99  		public boolean handle(MetaInfoFile file)
100 		{
101 			return MP3MetaInfo.handle(file);
102 		} 
103     
104     }
105     
106 }