KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > files > model > MP3File


1 /*
2  * Created on May 7, 2004
3  *
4  */

5 package com.dotmarketing.portlets.files.model;
6
7 import java.io.IOException JavaDoc;
8 import java.lang.reflect.InvocationTargetException JavaDoc;
9
10 import org.apache.commons.beanutils.BeanUtils;
11 import org.farng.mp3.AbstractMP3Tag;
12 import org.farng.mp3.TagException;
13
14 import com.dotmarketing.portlets.files.factories.FileFactory;
15 import com.dotmarketing.util.UtilMethods;
16
17 /**
18  * @author rocco
19  *
20  */

21 public class MP3File extends File {
22
23     /**
24      *
25      */

26
27     String JavaDoc[] genres = new String JavaDoc[] { "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge", "Hip-Hop",
28             "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno",
29             "Industrial", "Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient",
30             "Trip-Hop", "Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical", "Instrumental", "Acid", "House", "Game",
31             "Sound Clip", "Gospel", "Noise", "AlternRock", "Bass", "Soul", "Punk", "Space", "Meditative",
32             "Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic", "Darkwave", "Techno-Industrial", "Electronic",
33             "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta", "Top 40", "Christian Rap",
34             "Pop/Funk", "Jungle", "Native American", "Cabaret", "New Wave", "Psychadelic", "Rave", "Showtunes",
35             "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro", "Musical", "Rock & Roll",
36             "Hard Rock", "Folk", "Folk-Rock", "National Folk", "Swing", "Fast Fusion", "Bebob", "Latin", "Revival",
37             "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock", "Progressive Rock", "Psychedelic Rock",
38             "Symphonic Rock", "Slow Rock", "Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech",
39             "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass", "Primus", "Porn Groove", "Satire",
40             "Slow Jam", "Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle",
41             "Duet", "Punk Rock", "Drum Solo", "A capella", "Euro-House", "Dance Hall" };
42
43     private static final long serialVersionUID = 1L;
44
45     String JavaDoc title;
46
47     String JavaDoc artist;
48
49     String JavaDoc album;
50
51     String JavaDoc year;
52
53     int bitrate;
54
55     double frequency;
56
57     long duration;
58
59     String JavaDoc genre;
60
61     public MP3File(File file) throws IOException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc, TagException {
62
63         if (!file.getFileName().toLowerCase().endsWith("mp3")) {
64             throw new IOException JavaDoc("Not an mp3 file");
65         }
66         BeanUtils.copyProperties(this, file);
67
68         java.io.File JavaDoc ioFile = new java.io.File JavaDoc(FileFactory.getRealAssetPath(file));
69         org.farng.mp3.MP3File mp3 = new org.farng.mp3.MP3File(ioFile);
70
71         if (ioFile.length() > 0 && mp3.getBitRate() > 0) {
72             duration = (long) ((double) ioFile.length() / ((double) mp3.getBitRate() / 8.0));
73         }
74         bitrate = mp3.getBitRate();
75         frequency = mp3.getFrequency();
76         // get the id3 tags
77
AbstractMP3Tag tag = null;
78         if (mp3.hasID3v1Tag()) {
79             tag = mp3.getID3v1Tag();
80
81             title = tag.getSongTitle();
82             if(!"New Artist".equals( tag.getLeadArtist())){
83                 artist = tag.getLeadArtist();
84             }
85             if(!"New Title".equals( tag.getAlbumTitle())){
86                 album = tag.getAlbumTitle();
87             }
88             year = tag.getYearReleased();
89             genre = lookupGenre(tag.getSongGenre());
90
91         }
92         if (mp3.hasID3v2Tag()) {
93             tag = mp3.getID3v2Tag();
94             if (title == null)
95                 title = tag.getSongTitle();
96             if (artist == null && !"New Artist".equals( tag.getLeadArtist()))
97                 artist = tag.getLeadArtist();
98             if (album == null && !"New Title".equals( tag.getAlbumTitle()))
99                 album = tag.getAlbumTitle();
100             if (year == null)
101                 year = tag.getYearReleased();
102             if (genre == null)
103                 genre = lookupGenre(tag.getSongGenre());
104
105         }
106     }
107
108     public String JavaDoc getAlbum() {
109         return album;
110     }
111
112     public void setAlbum(String JavaDoc album) {
113         this.album = album;
114     }
115
116     public String JavaDoc getArtist() {
117         return artist;
118     }
119
120     public void setArtist(String JavaDoc artist) {
121         this.artist = artist;
122     }
123
124     public long getDuration() {
125         return duration;
126     }
127
128     public void setDuration(long duration) {
129         this.duration = duration;
130     }
131
132     public int getBitrate() {
133         return bitrate;
134     }
135
136     public void setBitrate(int bitrate) {
137         this.bitrate = bitrate;
138     }
139
140     public double getFrequency() {
141         return frequency;
142     }
143
144     public void setFrequency(double frequency) {
145         this.frequency = frequency;
146     }
147
148     public String JavaDoc getGenre() {
149         return genre;
150     }
151
152     public void setGenre(String JavaDoc genre) {
153         this.genre = genre;
154     }
155
156     public String JavaDoc getTitle() {
157         return title;
158     }
159
160     public void setTitle(String JavaDoc title) {
161         this.title = title;
162     }
163
164     public String JavaDoc getYear() {
165         return year;
166     }
167
168     public void setYear(String JavaDoc year) {
169         this.year = year;
170     }
171
172     private String JavaDoc lookupGenre(String JavaDoc x) {
173
174         try {
175             x = x.replaceAll("[()]", "");
176             int y = Integer.parseInt(x);
177             return genres[y];
178         } catch (Exception JavaDoc e) {
179             return null;
180         }
181     }
182
183 }
184
Popular Tags