1 package com.melloware.jukes.file.tag;
2
3 import java.io.File;
4 import java.util.Map;
5
6 import org.apache.commons.lang.StringUtils;
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.jaudiotagger.audio.AudioFile;
10 import org.jaudiotagger.audio.AudioFileIO;
11 import org.jaudiotagger.audio.exceptions.CannotReadException;
12 import org.jaudiotagger.audio.exceptions.CannotWriteException;
13 import org.jaudiotagger.tag.FieldDataInvalidException;
14
15 import com.melloware.jukes.exception.MusicTagException;
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public final class AudioFileTag extends MusicTag {
30
31 private static final Log LOG = LogFactory.getLog(AudioFileTag.class);
32 private AudioFile audioFile;
33
34
35
36
37
38
39
40 public AudioFileTag(File aFile) throws MusicTagException {
41 super(aFile);
42
43 try {
44
45 this.audioFile = AudioFileIO.read(aFile);
46
47
48 initializeTags();
49 } catch (CannotReadException ex) {
50 LOG.error(ex.getMessage(), ex);
51 throw new MusicTagException("CannotReadException opening Music File Tag." + aFile.getAbsolutePath() + "\n\n"
52 + ex.getMessage());
53 } catch (Exception ex) {
54 LOG.error(ex.getMessage(), ex);
55 throw new MusicTagException("Unexpected exception opening Music File Tag." + aFile.getAbsolutePath() + "\n\n"
56 + ex.getMessage());
57 }
58
59 }
60
61
62
63
64
65 public String getArtist() {
66 if (StringUtils.isBlank(this.artist)) {
67 this.artist = StringUtils.defaultIfEmpty(audioFile.getTag().getFirstArtist(), NO_TAG).trim();
68 }
69 return this.artist;
70 }
71
72
73
74
75
76 public Long getBitRate() {
77 if (this.bitRate == null) {
78 this.bitRate = Long.valueOf(audioFile.getAudioHeader().getBitRateAsNumber());
79 }
80
81 return this.bitRate;
82 }
83
84
85
86
87
88 public String getComment() {
89 if (StringUtils.isBlank(this.comment)) {
90 this.comment = StringUtils.defaultIfEmpty(audioFile.getTag().getFirstComment(), "").trim();
91 }
92 return this.comment;
93 }
94
95
96
97
98
99 public String getCopyrighted() {
100 return "No";
101 }
102
103
104
105
106
107 public String getDisc() {
108 if (StringUtils.isBlank(this.disc)) {
109 this.disc = StringUtils.defaultIfEmpty(audioFile.getTag().getFirstAlbum(), NO_TAG).trim();
110 }
111 return this.disc;
112 }
113
114
115
116
117
118 public String getEmphasis() {
119 return "None";
120 }
121
122
123
124
125
126 public String getEncodedBy() {
127 if (StringUtils.isBlank(this.encodedBy)) {
128 this.encodedBy = System.getProperty("application.name");
129 }
130 return this.encodedBy;
131 }
132
133
134
135
136
137 public String getFrequency() {
138 return Integer.toString(audioFile.getAudioHeader().getSampleRateAsNumber());
139
140 }
141
142
143
144
145
146 public String getGenre() {
147 if (StringUtils.isBlank(this.genre)) {
148 this.genre = StringUtils.defaultIfEmpty(audioFile.getTag().getFirstGenre(), "Other").trim();
149 }
150 return this.genre;
151 }
152
153
154
155
156
157 public Map getHeader() {
158 return header;
159 }
160
161
162
163
164
165 public String getLayer() {
166 return audioFile.getAudioHeader().getEncodingType();
167 }
168
169
170
171
172
173 public String getMode() {
174 return audioFile.getAudioHeader().getEncodingType();
175 }
176
177
178
179
180
181 public String getTitle() {
182 if (StringUtils.isBlank(this.title)) {
183
184 String temp = StringUtils.defaultIfEmpty(audioFile.getTag().getFirstTitle(), NO_TAG).trim();
185
186
187
188 if ((temp.length() == 30) || (temp.equals(NO_TAG)) || (temp.startsWith("Track"))) {
189 temp = extractTitleFromFilename();
190 LOG.debug("Filename extracted");
191 }
192
193
194 this.title = StringUtils.defaultIfEmpty(temp, NO_TAG).trim();
195 }
196 return this.title;
197 }
198
199
200
201
202
203 public String getTrack() {
204 if (StringUtils.isBlank(this.track)) {
205 int trackNum = 0;
206 String tracknumber = StringUtils.defaultIfEmpty(audioFile.getTag().getFirstTrack(), "X").trim();
207 if (StringUtils.isNumeric(tracknumber)) {
208 trackNum = Integer.parseInt(tracknumber);
209 }
210
211
212 this.track = StringUtils.leftPad(String.valueOf(trackNum), 2, "0").trim();
213
214 }
215 return this.track;
216 }
217
218
219
220
221
222 public long getTrackLength() {
223 if (this.trackLength > 1) {
224 return this.trackLength;
225 }
226 this.trackLength = audioFile.getAudioHeader().getTrackLength();
227 return this.trackLength;
228 }
229
230
231
232
233
234 public String getVersion() {
235 return audioFile.getAudioHeader().getEncodingType();
236 }
237
238
239
240
241
242 public String getYear() {
243 if (StringUtils.isBlank(this.year)) {
244 this.year = StringUtils.defaultIfEmpty(audioFile.getTag().getFirstYear(), CURRENT_YEAR).trim();
245 }
246 return this.year;
247 }
248
249
250
251
252
253 public void setArtist(String aArtist) {
254 this.artist = StringUtils.defaultIfEmpty(aArtist, NO_TAG).trim();
255 try {
256 audioFile.getTag().setArtist(this.artist);
257 } catch (FieldDataInvalidException ex) {
258 LOG.error("FieldDataInvalidException", ex);
259 }
260
261 }
262
263
264
265
266
267 public void setComment(String aComment) {
268 this.comment = StringUtils.defaultIfEmpty(aComment, "").trim();
269 try {
270 audioFile.getTag().setComment(this.comment);
271 } catch (FieldDataInvalidException ex) {
272 LOG.error("FieldDataInvalidException", ex);
273 }
274 }
275
276
277
278
279
280 public void setDisc(String aDisc) {
281 this.disc = StringUtils.defaultIfEmpty(aDisc, NO_TAG).trim();
282 try {
283 audioFile.getTag().setAlbum(this.disc);
284 } catch (FieldDataInvalidException ex) {
285 LOG.error("FieldDataInvalidException", ex);
286 }
287
288 }
289
290
291
292
293
294 public void setEncodedBy(String aEncodedBy) {
295 this.encodedBy = StringUtils.defaultIfEmpty(aEncodedBy, System.getProperty("application.name")).trim();
296 }
297
298
299
300
301
302 public void setGenre(String aGenre) {
303 this.genre = StringUtils.defaultIfEmpty(aGenre, NO_TAG).trim();
304 try {
305 audioFile.getTag().setGenre(this.genre);
306 } catch (FieldDataInvalidException ex) {
307 LOG.error("FieldDataInvalidException", ex);
308 }
309
310 }
311
312
313
314
315
316 public void setTitle(String aTitle) {
317 this.title = StringUtils.defaultIfEmpty(aTitle, NO_TAG).trim();
318
319 try {
320 audioFile.getTag().setTitle(this.title);
321 } catch (FieldDataInvalidException ex) {
322 LOG.error("FieldDataInvalidException", ex);
323 }
324 }
325
326
327
328
329
330 public void setTrack(String aTrack) {
331 setTrack(aTrack, 2);
332 }
333
334
335
336
337
338
339
340 public void setTrack(final String aTrack, final int aPadding) {
341 final String current = StringUtils.defaultIfEmpty(aTrack, "0").trim();
342 this.track = StringUtils.leftPad(current, aPadding, "0").trim();
343 try {
344 audioFile.getTag().setTrack(this.track);
345 } catch (FieldDataInvalidException ex) {
346 LOG.error("FieldDataInvalidException", ex);
347 }
348 }
349
350
351
352
353
354 public void setTrackLength(long aTrackLength) {
355 this.trackLength = aTrackLength;
356 }
357
358
359
360
361
362 public void setYear(String aYear) {
363 this.year = StringUtils.defaultIfEmpty(aYear, CURRENT_YEAR).trim();
364 try {
365 audioFile.getTag().setYear(this.year);
366 } catch (FieldDataInvalidException ex) {
367 LOG.error("FieldDataInvalidException", ex);
368 }
369 }
370
371
372
373
374
375 public boolean isVBR() {
376 return audioFile.getAudioHeader().isVariableBitRate();
377 }
378
379
380
381
382
383 public void removeTags() throws MusicTagException {
384 if (audioFile != null) {
385 try {
386 AudioFileIO.delete(audioFile);
387 } catch (Exception e) {
388 throw new MusicTagException("Error removing AudioFile tag: " + e.getMessage(), e);
389 }
390 initializeTags();
391 }
392 }
393
394
395
396
397
398
399
400
401
402
403
404
405 public boolean renameFile(String aFormat) {
406 boolean result = false;
407 try {
408 final String newFileName = createFilenameFromFormat(aFormat);
409 final File newFile = new File(newFileName);
410
411 audioFile = null;
412
413 result = this.file.renameTo(newFile);
414 if (result) {
415 this.file = newFile;
416 this.audioFile = AudioFileIO.read(newFile);
417 initializeTags();
418 }
419 } catch (Exception ex) {
420 LOG.error("Error renaming file", ex);
421 }
422 return result;
423 }
424
425
426
427
428
429 public void save() throws MusicTagException {
430 if (audioFile != null) {
431 try {
432 audioFile.commit();
433 } catch (CannotWriteException ex) {
434 throw new MusicTagException("Error saving AudioFile tag: " + ex.getMessage());
435 }
436
437 }
438
439 }
440
441
442
443
444 private void initializeTags() {
445
446 this.getDisc();
447 this.getArtist();
448 this.getComment();
449 this.getGenre();
450 this.getTitle();
451 this.getTrack();
452 this.getYear();
453 this.getTrackLength();
454 this.getEncodedBy();
455 }
456
457 }