View Javadoc

1   /*
2    * Entagged Audio Tag library
3    * Copyright (c) 2003-2005 Rapha�l Slinckx <raphael@slinckx.net>
4    * 
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *  
10   * This library 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 GNU
13   * Lesser General Public License for more details.
14   * 
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package entagged.audioformats;
20  
21  import java.io.File;
22  
23  import entagged.audioformats.exceptions.CannotWriteException;
24  import entagged.audioformats.generic.GenericTag;
25  
26  /***
27   *	<p>This is the main object manipulated by the user representing an audiofile, its properties and its tag.</p>
28   *	<p>The prefered way to obtain an <code>AudioFile</code> is to use the <code>AudioFileIO.read(File)</code> method.</p>
29   *	<p>The <code>AudioFile</code> contains every properties associated with the file itself (no meta-data), like the bitrate, the sampling rate, the encoding infos, etc.</p>
30   *	<p>To get the meta-data contained in this file you have to get the <code>Tag</code> of this <code>AudioFile</code></p>
31   *
32   *@author	Raphael Slinckx
33   *@version	$Id: AudioFile.java,v 1.1 2007/03/23 14:16:56 nicov1 Exp $
34   *@since	v0.01
35   *@see	AudioFileIO
36   *@see	Tag
37   */
38  public class AudioFile extends File {
39  	
40  	private EncodingInfo info;
41  	private Tag tag;
42  	private int id;
43  	
44  	/***
45  	 *	<p>These constructors are used by the different readers, users should not use them, but use the <code>AudioFileIO.read(File)</code> method instead !.</p>
46  	 *	<p>Create the AudioFile representing file f, the encodinginfos and containing an empty tag</p>
47  	 *
48  	 *@param	f	The file of the audiofile
49  	 *@param	info	the encoding infos over this file
50  	 */
51  	public AudioFile(File f, EncodingInfo info) {
52  		super(f.getAbsolutePath());
53  		this.info = info;
54  		this.tag = new GenericTag();
55  	}
56  	
57  	/***
58  	 *	<p>These constructors are used by the different readers, users should not use them, but use the <code>AudioFileIO.read(File)</code> method instead !.</p>
59  	 *	<p>Create the AudioFile representing file f, the encodinginfos and containing the tag</p>
60  	 *
61  	 *@param	f	The file of the audiofile
62  	 *@param	info	the encoding infos over this file
63  	 *@param	tag	the tag contained in this file
64  	 */
65  	public AudioFile(File f, EncodingInfo info, Tag tag) {
66  		super(f.getAbsolutePath());
67  		this.info = info;
68  		this.tag = tag;
69  	}
70  	
71  	/***
72  	 *	<p>Returns the bitrate of this AufioFile in kilobytes per second (KB/s). Example: 192 KB/s</p>
73  	 *
74  	 *@return	Returns the bitrate of this AufioFile
75  	 */
76  	public int getBitrate() {
77  		return info.getBitrate();
78  	}
79  	
80  	/***
81  	 *	<p>Returns the number of audio channels contained in this AudioFile, 2 for example means stereo</p>
82  	 *
83  	 *@return	Returns the number of audio channels contained in this AudioFile
84  	 */
85  	public int getChannelNumber() {
86  		return info.getChannelNumber();
87  	}
88  
89  	/***
90  	 *	<p>Returns the encoding type of this AudioFile, this needs to be precisely specified in the future</p>
91  	 *
92  	 *@return	Returns the encoding type of this AudioFile
93  	 *@todo		This method needs to be fully specified
94  	 */
95  	public String getEncodingType() {
96  		return info.getEncodingType();
97  	}
98  
99  	/***
100 	 *	<p>Returns the extra encoding infos of this AudioFile, this needs to be precisely specified in the future</p>
101 	 *
102 	 *@return	Returns the extra encoding infos of this AudioFile
103 	 *@todo		This method needs to be fully specified
104 	 */
105 	public String getExtraEncodingInfos() {
106 		return info.getExtraEncodingInfos();
107 	}
108 
109 	/***
110 	 *	<p>Returns the length (duration) in seconds (s) of this AudioFile.Example: 241 seconds</p>
111 	 *
112 	 *@return	Returns the length (duration) of this AudioFile
113 	 */
114 	public int getLength() {
115 		return info.getLength();
116 	}
117 	
118 	/***
119 	 * Returns the length (duration) in seconds with fractions.<br>
120 	 * 
121 	 * @return The duration in seconds.
122 	 */
123 	public float getPreciseLength() {
124 		return info.getPreciseLength();
125 	}
126 	
127 	/***
128 	 *	<p>Returns the sampling rate of this AudioFile in Hertz (Hz). Example: 44100 Hz for most of the audio files</p>
129 	 *
130 	 *@return	Returns the sampling rate of this AudioFile
131 	 */
132 	public int getSamplingRate() {
133 		return info.getSamplingRate();
134 	}
135 	
136 	/***
137 	 *	<p>Returns the tag contained in this AudioFile, the <code>Tag</code> contains any useful meta-data, like artist, album, title, etc.</p>
138 	 *	<p>If the file does not contain any tag, a new empty tag is returned</p>
139 	 *
140 	 *@return	Returns the tag contained in this AudioFile, or a new one if file hasn't any tag.
141 	 */
142 	public Tag getTag() {
143 		return (tag == null) ? new GenericTag() : tag;
144 	}
145 	
146 	/***
147 	 * <p>Checks if this file is a VBR (variable bitrate) or a Constant Bitrate one</p>
148 	 * <p>True means VBR, false means CBR</p>
149 	 * <p>This has only meaning with MP3 and MPC files, other formats are always VBR
150 	 * 	since it offers a better compression ratio (and lossless compression is by nature VBR</p>
151 	 */
152 	public boolean isVbr() {
153 	    return info.isVbr();
154 	}
155 	
156 	/***
157 	 *	<p>Returns a multi-line string with the file path, the encoding informations, and the tag contents.</p>
158 	 *
159 	 *@return	A multi-line string with the file path, the encoding informations, and the tag contents.
160 	 *@todo		Maybe this can be changed ?
161 	 */
162 	public String toString() {
163 		return "AudioFile "+getAbsolutePath()+"  --------\n"+info.toString()+"\n"+ ( (tag == null) ? "" : tag.toString())+"\n-------------------";
164 	}
165 	
166 	/***
167 	 * 
168 	 * @param id
169 	 */
170 	public void setID(int id) {
171 		this.id = id;
172 	}
173 	
174 	/***
175 	 * 
176 	 * @return
177 	 */
178 	public int getID() {
179 		return id;
180 	}
181 }