commit | author | age
|
37eadf
|
1 |
/* |
U |
2 |
Mediacenter - Central Media Store and Raspberry Pi remote control |
|
3 |
Copyright (C) 2013, 2014 Ulrich Hilger, http://uhilger.de |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as published by |
|
7 |
the Free Software Foundation, either version 3 of the License, or |
|
8 |
(at your option) any later version. |
|
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 Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
|
|
18 |
*/ |
|
19 |
|
|
20 |
|
|
21 |
package de.uhilger.mediaz.store; |
|
22 |
|
|
23 |
import java.io.File; |
|
24 |
import java.util.HashMap; |
|
25 |
import java.util.Map; |
|
26 |
import javax.sound.sampled.AudioFileFormat; |
|
27 |
import javax.sound.sampled.AudioFormat; |
|
28 |
import javax.sound.sampled.AudioSystem; |
|
29 |
import org.tritonus.share.sampled.TAudioFormat; |
|
30 |
import org.tritonus.share.sampled.file.TAudioFileFormat; |
|
31 |
|
|
32 |
/** |
|
33 |
* Class <code>Track</code> models an audio file including ID3 metadata. |
|
34 |
* |
|
35 |
* @author Ulrich Hilger, <a href="http://dev.uhilger.de" target="_blank">http://dev.uhilger.de</a> |
|
36 |
* @author Published under the terms and conditions of |
|
37 |
* the <a href="http://www.gnu.org/licenses/" target="_blank">GNU Affero General Public License</a> |
|
38 |
* |
|
39 |
* @version 3, February 19, 2012 |
|
40 |
*/ |
|
41 |
|
|
42 |
public class Track { |
|
43 |
|
|
44 |
/** reference to file of audio track */ |
|
45 |
private File trackFile; |
|
46 |
|
|
47 |
/** the time at which playing this track has started */ |
|
48 |
private long playStart; |
|
49 |
|
|
50 |
/** reference to metadata information */ |
|
51 |
private Map<String, Object> properties; |
|
52 |
|
|
53 |
/** |
|
54 |
* Create a new object of class <code>Track</code> |
|
55 |
* @param trackFile the audio file this track object refers to |
|
56 |
*/ |
|
57 |
public Track(File trackFile) { |
|
58 |
super(); |
|
59 |
this.playStart = -1; |
|
60 |
this.trackFile = trackFile; |
|
61 |
} |
|
62 |
|
|
63 |
/** |
|
64 |
* get the time at which the playing of this track has started |
|
65 |
* @return number of milliseconds since Jan 1, 1970 denoting the time this tack has |
|
66 |
* started playing or -1 if it does not play |
|
67 |
*/ |
|
68 |
public long getPlayStart() { |
|
69 |
return playStart; |
|
70 |
} |
|
71 |
|
|
72 |
/** |
|
73 |
* set the time at which the playing of this track has started |
|
74 |
* @param playStart number of milliseconds since Jan 1, 1970 denoting the time this tack has |
|
75 |
* started playing or -1 if it does not play |
|
76 |
*/ |
|
77 |
public void setPlayStart(long playStart) { |
|
78 |
this.playStart = playStart; |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* get the file this track refers to |
|
83 |
* @return the audio file reference |
|
84 |
*/ |
|
85 |
public File getTrackFile() { |
|
86 |
return trackFile; |
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* set the file this track refers to |
|
91 |
* @param trackFile the audio file reference to set this track to |
|
92 |
*/ |
|
93 |
public void setTrackFile(File trackFile) { |
|
94 |
this.trackFile = trackFile; |
|
95 |
} |
|
96 |
|
|
97 |
/** |
|
98 |
* get the metadata of a given audio file |
|
99 |
* |
|
100 |
* <p>Example<br><pre> |
|
101 |
* properties: {mp3.frequency.hz=44100, title=All Cried Out, |
|
102 |
* mp3.length.bytes=6301706, comment=, mp3.channels=2, date=1984, |
|
103 |
* mp3.version.layer=3, mp3.framesize.bytes=414, mp3.id3tag.track=6, |
|
104 |
* mp3.version.mpeg=1, mp3.bitrate.nominal.bps=128000, mp3.vbr.scale=0, |
|
105 |
* bitrate=128000, mp3.length.frames=15112, mp3.crc=false, mp3.vbr=false, |
|
106 |
* album=Alf, mp3.framerate.fps=38.28125, mp3.copyright=false, |
|
107 |
* mp3.id3tag.v2=java.io.ByteArrayInputStream@7efa96, mp3.version.encoding=MPEG1L3, |
|
108 |
* year=1984, mp3.id3tag.genre=Rock, mp3.header.pos=136, mp3.mode=0, |
|
109 |
* mp3.original=true, vbr=false, mp3.padding=true, duration=394762000, |
|
110 |
* author=Alison Moyet} |
|
111 |
* </pre></p> |
|
112 |
* |
|
113 |
* @param file File the audio file to get metadata for |
|
114 |
* @return Map the properties Map with information such as in above example |
|
115 |
*/ |
|
116 |
@SuppressWarnings("unchecked") |
|
117 |
private Map<String, Object> getMetadata(File file) { |
|
118 |
Map<String, Object> map = new HashMap<String, Object>(); |
|
119 |
try { |
|
120 |
AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(file); |
|
121 |
if (audioFileFormat instanceof TAudioFileFormat) { |
|
122 |
map.putAll(((TAudioFileFormat) audioFileFormat).properties()); |
|
123 |
} |
|
124 |
AudioFormat audioFormat = audioFileFormat.getFormat(); |
|
125 |
if (audioFormat instanceof TAudioFormat) { |
|
126 |
map.putAll(((TAudioFormat) audioFormat).properties()); |
|
127 |
} |
|
128 |
} catch (Exception ex) { |
|
129 |
ex.printStackTrace(); |
|
130 |
} |
|
131 |
return map; |
|
132 |
} |
|
133 |
|
|
134 |
/** |
|
135 |
* get the metadata of the audio file this track refers to |
|
136 |
* |
|
137 |
* <p>Example<br><pre> |
|
138 |
* properties: {mp3.frequency.hz=44100, title=All Cried Out, |
|
139 |
* mp3.length.bytes=6301706, comment=, mp3.channels=2, date=1984, |
|
140 |
* mp3.version.layer=3, mp3.framesize.bytes=414, mp3.id3tag.track=6, |
|
141 |
* mp3.version.mpeg=1, mp3.bitrate.nominal.bps=128000, mp3.vbr.scale=0, |
|
142 |
* bitrate=128000, mp3.length.frames=15112, mp3.crc=false, mp3.vbr=false, |
|
143 |
* album=Alf, mp3.framerate.fps=38.28125, mp3.copyright=false, |
|
144 |
* mp3.id3tag.v2=java.io.ByteArrayInputStream@7efa96, mp3.version.encoding=MPEG1L3, |
|
145 |
* year=1984, mp3.id3tag.genre=Rock, mp3.header.pos=136, mp3.mode=0, |
|
146 |
* mp3.original=true, vbr=false, mp3.padding=true, duration=394762000, |
|
147 |
* author=Alison Moyet} |
|
148 |
* </pre></p> |
|
149 |
* |
|
150 |
* @return Map the properties Map with information such as in above example |
|
151 |
*/ |
|
152 |
public Map<String, Object> getMetadata() { |
|
153 |
if (properties == null) { |
|
154 |
properties = getMetadata(trackFile); |
|
155 |
} |
|
156 |
return properties; |
|
157 |
} |
|
158 |
|
|
159 |
/** |
|
160 |
* Get the bitrate of this track, i.e. bits per second |
|
161 |
* @return the bitrate |
|
162 |
*/ |
|
163 |
public long getBitrate() { |
|
164 |
long bitrate = -1; |
|
165 |
Object o = getMetadata().get("bitrate"); |
|
166 |
if (o != null) { |
|
167 |
bitrate = Long.parseLong(o.toString()); |
|
168 |
} |
|
169 |
return bitrate; |
|
170 |
} |
|
171 |
|
|
172 |
/** |
|
173 |
* get the artist of the currently playing song |
|
174 |
* @return String the artist of the song |
|
175 |
*/ |
|
176 |
public String getArtist() { |
|
177 |
String artist = ""; |
|
178 |
Object o = getMetadata().get("author"); |
|
179 |
if (o != null) { |
|
180 |
artist = o.toString().trim(); |
|
181 |
} |
|
182 |
return artist; |
|
183 |
} |
|
184 |
|
|
185 |
/** |
|
186 |
* get the album of the currently playing song |
|
187 |
* @return String the album of the song |
|
188 |
*/ |
|
189 |
public String getAlbum() { |
|
190 |
String album = ""; |
|
191 |
//if (properties != null) { |
|
192 |
Object o = getMetadata().get("album"); |
|
193 |
if (o != null) { |
|
194 |
album = o.toString().trim(); |
|
195 |
} |
|
196 |
//} |
|
197 |
return album; |
|
198 |
} |
|
199 |
|
|
200 |
/** |
|
201 |
* get the title of the currently playing song |
|
202 |
* @return String the song title |
|
203 |
*/ |
|
204 |
public String getTitle() { |
|
205 |
String title = ""; |
|
206 |
Object o = getMetadata().get("title"); |
|
207 |
if (o != null) { |
|
208 |
title = o.toString().trim(); |
|
209 |
} else { |
|
210 |
title = trackFile.getName(); |
|
211 |
} |
|
212 |
return title; |
|
213 |
} |
|
214 |
|
|
215 |
/** |
|
216 |
* get the duration of the currently playing song |
|
217 |
* |
|
218 |
* @return long duration in milliseconds |
|
219 |
*/ |
|
220 |
public long getDuration() { |
|
221 |
long duration = -1; |
|
222 |
Object o = getMetadata().get("duration"); |
|
223 |
if (o != null) { |
|
224 |
duration = Long.parseLong(o.toString()); |
|
225 |
} |
|
226 |
return duration; |
|
227 |
} |
|
228 |
|
|
229 |
/** |
|
230 |
* Overridden from class <code>Object</code> |
|
231 |
* @return the hash code |
|
232 |
* @see java.lang.Object#hashCode() |
|
233 |
*/ |
|
234 |
public int hashCode() { |
|
235 |
int hash = 7; |
|
236 |
hash = 97 * hash + (this.trackFile != null ? this.trackFile.hashCode() : 0); |
|
237 |
return hash; |
|
238 |
} |
|
239 |
|
|
240 |
/** |
|
241 |
* Overridden from class <code>Object</code> |
|
242 |
* @return true, when this object is equal to the given object |
|
243 |
* @see java.lang.Object#equals(java.lang.Object) |
|
244 |
*/ |
|
245 |
public boolean equals(Object obj) { |
|
246 |
boolean isEqual = false; |
|
247 |
if(obj != null && obj instanceof Track) { |
|
248 |
Track otherTrack = (Track) obj; |
|
249 |
File otherFile = otherTrack.getTrackFile(); |
|
250 |
isEqual = otherFile.equals(this.getTrackFile()); |
|
251 |
} |
|
252 |
return isEqual; |
|
253 |
} |
|
254 |
|
|
255 |
} |