X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Fdata%2FAudioClip.java;fp=src%2Ftim%2Fprune%2Fdata%2FAudioClip.java;h=2e5f5ee729968c921e360fd9e470bea6ca9277b1;hp=0000000000000000000000000000000000000000;hb=ce6f2161b8596f7018d6a76bff79bc9e571f35fd;hpb=2d8cb72e84d5cc1089ce77baf1e34ea3ea2f8465 diff --git a/src/tim/prune/data/AudioClip.java b/src/tim/prune/data/AudioClip.java new file mode 100644 index 0000000..2e5f5ee --- /dev/null +++ b/src/tim/prune/data/AudioClip.java @@ -0,0 +1,61 @@ +package tim.prune.data; + +import java.io.ByteArrayInputStream; +import java.io.File; +import javax.sound.sampled.AudioFileFormat; +import javax.sound.sampled.AudioSystem; + +/** + * Class to represent an audio clip for correlation + */ +public class AudioClip extends MediaObject +{ + /** length of current audio clip in seconds */ + private int _lengthInSeconds = LENGTH_UNKNOWN; + + private static final int LENGTH_UNKNOWN = -1; + private static final int LENGTH_NOT_AVAILABLE = -2; + + /** + * Constructor + * @param inFile file object + */ + public AudioClip(File inFile) + { + // Timestamp is always just taken from the file modification stamp + super(inFile, new TimestampUtc(inFile.lastModified())); + } + + /** + * Constructor + * @param inData byte array of data + * @param inName name of source file + * @param inUrl url from which it came (or null) + */ + public AudioClip(byte[] inData, String inName, String inUrl) + { + super(inData, inName, inUrl); + } + + /** + * @return length of this audio clip in seconds + */ + public int getLengthInSeconds() + { + if (_lengthInSeconds == LENGTH_UNKNOWN) + { + try { + AudioFileFormat format = null; + if (getFile() != null) + format = AudioSystem.getAudioFileFormat(getFile()); + else + format = AudioSystem.getAudioFileFormat(new ByteArrayInputStream(_data)); + _lengthInSeconds = (int) (format.getFrameLength() / format.getFormat().getFrameRate()); + } + catch (Exception e) { + _lengthInSeconds = LENGTH_NOT_AVAILABLE; + } + } + return _lengthInSeconds; + } +}