File: AudioClip.java

package info (click to toggle)
gpsprune 19.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,516 kB
  • sloc: java: 42,704; sh: 25; makefile: 24; python: 15
file content (61 lines) | stat: -rw-r--r-- 1,509 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
	}
}