]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/data/AudioClip.java
2e5f5ee729968c921e360fd9e470bea6ca9277b1
[GpsPrune.git] / src / tim / prune / data / AudioClip.java
1 package tim.prune.data;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.File;
5 import javax.sound.sampled.AudioFileFormat;
6 import javax.sound.sampled.AudioSystem;
7
8 /**
9  * Class to represent an audio clip for correlation
10  */
11 public class AudioClip extends MediaObject
12 {
13         /** length of current audio clip in seconds */
14         private int _lengthInSeconds = LENGTH_UNKNOWN;
15
16         private static final int LENGTH_UNKNOWN = -1;
17         private static final int LENGTH_NOT_AVAILABLE = -2;
18
19         /**
20          * Constructor
21          * @param inFile file object
22          */
23         public AudioClip(File inFile)
24         {
25                 // Timestamp is always just taken from the file modification stamp
26                 super(inFile, new TimestampUtc(inFile.lastModified()));
27         }
28
29         /**
30          * Constructor
31          * @param inData byte array of data
32          * @param inName name of source file
33          * @param inUrl url from which it came (or null)
34          */
35         public AudioClip(byte[] inData, String inName, String inUrl)
36         {
37                 super(inData, inName, inUrl);
38         }
39
40         /**
41          * @return length of this audio clip in seconds
42          */
43         public int getLengthInSeconds()
44         {
45                 if (_lengthInSeconds == LENGTH_UNKNOWN)
46                 {
47                         try {
48                                 AudioFileFormat format = null;
49                                 if (getFile() != null)
50                                         format = AudioSystem.getAudioFileFormat(getFile());
51                                 else
52                                         format = AudioSystem.getAudioFileFormat(new ByteArrayInputStream(_data));
53                                 _lengthInSeconds = (int) (format.getFrameLength() / format.getFormat().getFrameRate());
54                         }
55                         catch (Exception e) {
56                                 _lengthInSeconds = LENGTH_NOT_AVAILABLE;
57                         }
58                 }
59                 return _lengthInSeconds;
60         }
61 }