2
* Entagged Audio Tag library
3
* Copyright (c) 2003-2005 Raphaël Slinckx <raphael@slinckx.net>
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.
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.
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
19
package org.jaudiotagger.audio;
22
import java.io.FileNotFoundException;
23
import java.io.RandomAccessFile;
24
import java.util.logging.Logger;
25
import java.util.ArrayList;
27
import org.jaudiotagger.audio.exceptions.CannotWriteException;
28
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
29
import org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture;
30
import org.jaudiotagger.audio.generic.GenericTag;
31
import org.jaudiotagger.audio.asf.tag.AsfTag;
32
import org.jaudiotagger.audio.wav.WavTag;
33
import org.jaudiotagger.audio.real.RealTag;
34
import org.jaudiotagger.tag.Tag;
35
import org.jaudiotagger.tag.mp4.Mp4Tag;
36
import org.jaudiotagger.tag.vorbiscomment.VorbisCommentTag;
37
import org.jaudiotagger.tag.flac.FlacTag;
40
* <p>This is the main object manipulated by the user representing an audiofile, its properties and its tag.</p>
41
* <p>The prefered way to obtain an <code>AudioFile</code> is to use the <code>AudioFileIO.read(File)</code> method.</p>
42
* <p>The <code>AudioFile</code> contains every properties associated with the file itself (no meta-data), like the bitrate, the sampling rate, the encoding audioHeaders, etc.</p>
43
* <p>To get the meta-data contained in this file you have to get the <code>Tag</code> of this <code>AudioFile</code></p>
45
* @author Raphael Slinckx
46
* @version $Id: AudioFile.java,v 1.15 2009/11/12 15:43:00 paultaylor Exp $
51
public class AudioFile
54
public static Logger logger = Logger.getLogger("org.jaudiotagger.audio");
57
* The physical file that this instance represents.
62
* The Audio header info
64
protected AudioHeader audioHeader;
77
* <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>
78
* <p>Create the AudioFile representing file f, the encodingaudioHeaders and containing the tag</p>
80
* @param f The file of the audiofile
81
* @param audioHeader the encoding audioHeaders over this file
82
* @param tag the tag contained in this file or null if no tag exists
84
public AudioFile(File f, AudioHeader audioHeader, Tag tag)
87
this.audioHeader = audioHeader;
93
* <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>
94
* <p>Create the AudioFile representing file denoted by pathname s, the encodingaudioHeaders and containing the tag</p>
96
* @param s The pathname of the audiofile
97
* @param audioHeader the encoding audioHeaders over this file
98
* @param tag the tag contained in this file
100
public AudioFile(String s, AudioHeader audioHeader, Tag tag)
102
this.file = new File(s);
103
this.audioHeader = audioHeader;
108
* <p>Write the tag contained in this AudioFile in the actual file on the disk, this is the same as calling the <code>AudioFileIO.write(this)</code> method.</p>
110
* @throws CannotWriteException If the file could not be written/accessed, the extension wasn't recognized, or other IO error occured.
113
public void commit() throws CannotWriteException
115
AudioFileIO.write(this);
119
* Set the file to store the info in
123
public void setFile(File file)
129
* Retrieve the physical file
133
public File getFile()
138
public void setTag(Tag tag)
144
* Return audio header
147
public AudioHeader getAudioHeader()
153
* <p>Returns the tag contained in this AudioFile, the <code>Tag</code> contains any useful meta-data, like
154
* artist, album, title, etc. If the file does not contain any tag the null is returned. Some audio formats do
155
* not allow there to be no tag so in this case the reader would return an empty tag whereas for others such
156
* as mp3 it is purely optional.
158
* @return Returns the tag contained in this AudioFile, or null if no tag exists.
166
* <p>Returns a multi-line string with the file path, the encoding audioHeaderrmations, and the tag contents.</p>
168
* @return A multi-line string with the file path, the encoding audioHeaderrmations, and the tag contents.
169
* TODO Maybe this can be changed ?
171
public String toString()
173
return "AudioFile " + getFile().getAbsolutePath()
174
+ " --------\n" + audioHeader.toString() + "\n" + ((tag == null) ? "" : tag.toString()) + "\n-------------------";
178
* Checks the file is accessible with the correct permissions, otherwise exception occurs
182
* @throws ReadOnlyFileException
183
* @throws FileNotFoundException
186
protected RandomAccessFile checkFilePermissions(File file, boolean readOnly) throws ReadOnlyFileException, FileNotFoundException
188
RandomAccessFile newFile;
190
logger.info("Reading file:" + "path" + file.getPath() + ":abs:" + file.getAbsolutePath());
193
logger.severe("Unable to find:" + file.getPath());
194
throw new FileNotFoundException("Unable to find:" + file.getPath());
197
// Unless opened as readonly the file must be writable
200
newFile = new RandomAccessFile(file, "r");
204
if (!file.canWrite())
206
logger.severe("Unable to write:" + file.getPath());
207
throw new ReadOnlyFileException("Unable to write to:" + file.getPath());
209
newFile = new RandomAccessFile(file, "rws");
215
* Optional debugging method
219
public String displayStructureAsXML()
225
* Optional debugging method
229
public String displayStructureAsPlainText()
235
/** Create Default Tag
239
//TODO might be better to instantiate classes such as Mp4File,FlacFile ecetera
240
//TODO Generic tag is very misleading because soem of these formats cannot actually save the tag
241
public Tag createDefaultTag()
243
if(SupportedFileFormat.FLAC.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
245
return new FlacTag(VorbisCommentTag.createNewTag(), new ArrayList< MetadataBlockDataPicture >());
247
else if(SupportedFileFormat.OGG.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
249
return VorbisCommentTag.createNewTag();
251
else if(SupportedFileFormat.MP4.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
255
else if(SupportedFileFormat.M4A.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
259
else if(SupportedFileFormat.M4P.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
263
else if(SupportedFileFormat.WMA.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
267
else if(SupportedFileFormat.WAV.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
271
else if(SupportedFileFormat.RA.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
273
return new RealTag();
275
else if(SupportedFileFormat.RM.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
277
return new RealTag();
281
throw new RuntimeException("Unable to create default tag for this file format");
287
* Get the tag or if the file doesnt have one at all, create a default tag and return
291
public Tag getTagOrCreateDefault()
296
return createDefaultTag();
302
* Get the tag or if the file doesnt have one at all, create a default tag and set it
306
public Tag getTagOrCreateAndSetDefault()
311
tag = createDefaultTag();
321
* @return filename with audioformat seperator stripped of.
323
public static String getBaseFilename(File file)
325
int index=file.getName().toLowerCase().lastIndexOf(".");
328
return file.getName().substring(0,index);
330
return file.getName();
2
* Entagged Audio Tag library
3
* Copyright (c) 2003-2005 Raphaël Slinckx <raphael@slinckx.net>
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.
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.
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
19
package org.jaudiotagger.audio;
22
import java.io.FileNotFoundException;
23
import java.io.RandomAccessFile;
24
import java.util.logging.Logger;
25
import java.util.ArrayList;
27
import org.jaudiotagger.audio.exceptions.CannotWriteException;
28
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
29
import org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture;
30
import org.jaudiotagger.logging.ErrorMessage;
31
import org.jaudiotagger.tag.asf.AsfTag;
32
import org.jaudiotagger.audio.wav.WavTag;
33
import org.jaudiotagger.audio.real.RealTag;
34
import org.jaudiotagger.tag.Tag;
35
import org.jaudiotagger.tag.mp4.Mp4Tag;
36
import org.jaudiotagger.tag.vorbiscomment.VorbisCommentTag;
37
import org.jaudiotagger.tag.flac.FlacTag;
40
* <p>This is the main object manipulated by the user representing an audiofile, its properties and its tag.</p>
41
* <p>The prefered way to obtain an <code>AudioFile</code> is to use the <code>AudioFileIO.read(File)</code> method.</p>
42
* <p>The <code>AudioFile</code> contains every properties associated with the file itself (no meta-data), like the bitrate, the sampling rate, the encoding audioHeaders, etc.</p>
43
* <p>To get the meta-data contained in this file you have to get the <code>Tag</code> of this <code>AudioFile</code></p>
45
* @author Raphael Slinckx
46
* @version $Id: AudioFile.java 929 2010-11-17 12:36:46Z paultaylor $
51
public class AudioFile
54
public static Logger logger = Logger.getLogger("org.jaudiotagger.audio");
57
* The physical file that this instance represents.
62
* The Audio header info
64
protected AudioHeader audioHeader;
77
* <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>
78
* <p>Create the AudioFile representing file f, the encoding audio headers and containing the tag</p>
80
* @param f The file of the audio file
81
* @param audioHeader the encoding audioHeaders over this file
82
* @param tag the tag contained in this file or null if no tag exists
84
public AudioFile(File f, AudioHeader audioHeader, Tag tag)
87
this.audioHeader = audioHeader;
93
* <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>
94
* <p>Create the AudioFile representing file denoted by pathnames, the encoding audio Headers and containing the tag</p>
96
* @param s The pathname of the audio file
97
* @param audioHeader the encoding audioHeaders over this file
98
* @param tag the tag contained in this file
100
public AudioFile(String s, AudioHeader audioHeader, Tag tag)
102
this.file = new File(s);
103
this.audioHeader = audioHeader;
108
* <p>Write the tag contained in this AudioFile in the actual file on the disk, this is the same as calling the <code>AudioFileIO.write(this)</code> method.</p>
110
* @throws CannotWriteException If the file could not be written/accessed, the extension wasn't recognized, or other IO error occured.
113
public void commit() throws CannotWriteException
115
AudioFileIO.write(this);
119
* Set the file to store the info in
123
public void setFile(File file)
129
* Retrieve the physical file
133
public File getFile()
138
public void setTag(Tag tag)
144
* Return audio header
147
public AudioHeader getAudioHeader()
153
* <p>Returns the tag contained in this AudioFile, the <code>Tag</code> contains any useful meta-data, like
154
* artist, album, title, etc. If the file does not contain any tag the null is returned. Some audio formats do
155
* not allow there to be no tag so in this case the reader would return an empty tag whereas for others such
156
* as mp3 it is purely optional.
158
* @return Returns the tag contained in this AudioFile, or null if no tag exists.
166
* <p>Returns a multi-line string with the file path, the encoding audioHeader, and the tag contents.</p>
168
* @return A multi-line string with the file path, the encoding audioHeader, and the tag contents.
169
* TODO Maybe this can be changed ?
171
public String toString()
173
return "AudioFile " + getFile().getAbsolutePath()
174
+ " --------\n" + audioHeader.toString() + "\n" + ((tag == null) ? "" : tag.toString()) + "\n-------------------";
178
* Check does file exist
181
* @throws FileNotFoundException
183
public void checkFileExists(File file)throws FileNotFoundException
185
logger.info("Reading file:" + "path" + file.getPath() + ":abs:" + file.getAbsolutePath());
188
logger.severe("Unable to find:" + file.getPath());
189
throw new FileNotFoundException(ErrorMessage.UNABLE_TO_FIND_FILE.getMsg(file.getPath()));
194
* Checks the file is accessible with the correct permissions, otherwise exception occurs
198
* @throws ReadOnlyFileException
199
* @throws FileNotFoundException
202
protected RandomAccessFile checkFilePermissions(File file, boolean readOnly) throws ReadOnlyFileException, FileNotFoundException
204
RandomAccessFile newFile;
206
checkFileExists(file);
208
// Unless opened as readonly the file must be writable
211
newFile = new RandomAccessFile(file, "r");
215
if (!file.canWrite())
217
logger.severe("Unable to write:" + file.getPath());
218
throw new ReadOnlyFileException(ErrorMessage.NO_PERMISSIONS_TO_WRITE_TO_FILE.getMsg(file.getPath()));
220
newFile = new RandomAccessFile(file, "rws");
226
* Optional debugging method
230
public String displayStructureAsXML()
236
* Optional debugging method
240
public String displayStructureAsPlainText()
246
/** Create Default Tag
250
//TODO might be better to instantiate classes such as Mp4File,FlacFile ecetera
251
//TODO Generic tag is very misleading because soem of these formats cannot actually save the tag
252
public Tag createDefaultTag()
254
if(SupportedFileFormat.FLAC.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
256
return new FlacTag(VorbisCommentTag.createNewTag(), new ArrayList< MetadataBlockDataPicture >());
258
else if(SupportedFileFormat.OGG.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
260
return VorbisCommentTag.createNewTag();
262
else if(SupportedFileFormat.MP4.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
266
else if(SupportedFileFormat.M4A.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
270
else if(SupportedFileFormat.M4P.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
274
else if(SupportedFileFormat.WMA.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
278
else if(SupportedFileFormat.WAV.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
282
else if(SupportedFileFormat.RA.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
284
return new RealTag();
286
else if(SupportedFileFormat.RM.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
288
return new RealTag();
292
throw new RuntimeException("Unable to create default tag for this file format");
298
* Get the tag or if the file doesn't have one at all, create a default tag and return
302
public Tag getTagOrCreateDefault()
307
return createDefaultTag();
313
* Get the tag or if the file doesn't have one at all, create a default tag and set it
317
public Tag getTagOrCreateAndSetDefault()
322
tag = createDefaultTag();
332
* @return filename with audioFormat separator stripped of.
334
public static String getBaseFilename(File file)
336
int index=file.getName().toLowerCase().lastIndexOf(".");
339
return file.getName().substring(0,index);
341
return file.getName();