1 package tim.prune.data;
3 import java.io.ByteArrayInputStream;
5 import javax.sound.sampled.AudioFileFormat;
6 import javax.sound.sampled.AudioSystem;
9 * Class to represent an audio clip for correlation
11 public class AudioClip extends MediaObject
13 /** length of current audio clip in seconds */
14 private int _lengthInSeconds = LENGTH_UNKNOWN;
16 private static final int LENGTH_UNKNOWN = -1;
17 private static final int LENGTH_NOT_AVAILABLE = -2;
21 * @param inFile file object
23 public AudioClip(File inFile)
25 // Timestamp is always just taken from the file modification stamp
26 super(inFile, new Timestamp(inFile.lastModified()));
31 * @param inData byte array of data
32 * @param inName name of source file
33 * @param inUrl url from which it came (or null)
35 public AudioClip(byte[] inData, String inName, String inUrl)
37 super(inData, inName, inUrl);
41 * @return length of this audio clip in seconds
43 public int getLengthInSeconds()
45 if (_lengthInSeconds == LENGTH_UNKNOWN)
48 AudioFileFormat format = null;
49 if (getFile() != null)
50 format = AudioSystem.getAudioFileFormat(getFile());
52 format = AudioSystem.getAudioFileFormat(new ByteArrayInputStream(_data));
53 _lengthInSeconds = (int) (format.getFrameLength() / format.getFormat().getFrameRate());
56 _lengthInSeconds = LENGTH_NOT_AVAILABLE;
59 return _lengthInSeconds;