]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/AudioFile.java
43089ab85570526ad833bff0dcb68283165c167b
[GpsPrune.git] / tim / prune / data / AudioFile.java
1 package tim.prune.data;
2
3 import java.io.File;
4 import javax.sound.sampled.AudioFileFormat;
5 import javax.sound.sampled.AudioSystem;
6
7 /**
8  * Class to represent an audio file for correlation
9  */
10 public class AudioFile extends MediaFile
11 {
12         /** length of current audio file in seconds */
13         private int _lengthInSeconds = LENGTH_UNKNOWN;
14
15         private static final int LENGTH_UNKNOWN = -1;
16         private static final int LENGTH_NOT_AVAILABLE = -2;
17
18         /**
19          * Constructor
20          * @param inFile file object
21          */
22         public AudioFile(File inFile)
23         {
24                 // Timestamp is always just taken from the file modification stamp
25                 super(inFile, new Timestamp(inFile.lastModified()));
26         }
27
28         /**
29          * @return length of this audio file in seconds
30          */
31         public int getLengthInSeconds()
32         {
33                 if (_lengthInSeconds == LENGTH_UNKNOWN)
34                 {
35                         try {
36                                 AudioFileFormat format = AudioSystem.getAudioFileFormat(getFile());
37                                 _lengthInSeconds = (int) (format.getFrameLength() / format.getFormat().getFrameRate());
38                         }
39                         catch (Exception e) {
40                                 _lengthInSeconds = LENGTH_NOT_AVAILABLE;
41                         }
42                 }
43                 return _lengthInSeconds;
44         }
45 }