Path: blob/master/src/java.desktop/share/classes/javax/sound/sampled/AudioFileFormat.java
41159 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sound.sampled;2627import java.util.Collections;28import java.util.HashMap;29import java.util.Map;30import java.util.Objects;3132/**33* An instance of the {@code AudioFileFormat} class describes an audio file,34* including the file type, the file's length in bytes, the length in sample35* frames of the audio data contained in the file, and the format of the audio36* data.37* <p>38* The {@link AudioSystem} class includes methods for determining the format of39* an audio file, obtaining an audio input stream from an audio file, and40* writing an audio file from an audio input stream.41* <p>42* An {@code AudioFileFormat} object can include a set of properties. A property43* is a pair of key and value: the key is of type {@code String}, the associated44* property value is an arbitrary object. Properties specify additional45* informational meta data (like a author, copyright, or file duration).46* Properties are optional information, and file reader and file writer47* implementations are not required to provide or recognize properties.48* <p>49* The following table lists some common properties that should be used in50* implementations:51*52* <table class="striped">53* <caption>Audio File Format Properties</caption>54* <thead>55* <tr>56* <th scope="col">Property key57* <th scope="col">Value type58* <th scope="col">Description59* </thead>60* <tbody>61* <tr>62* <th scope="row">"duration"63* <td>{@link Long Long}64* <td>playback duration of the file in microseconds65* <tr>66* <th scope="row">"author"67* <td>{@link String String}68* <td>name of the author of this file69* <tr>70* <th scope="row">"title"71* <td>{@link String String}72* <td>title of this file73* <tr>74* <th scope="row">"copyright"75* <td>{@link String String}76* <td>copyright message77* <tr>78* <th scope="row">"date"79* <td>{@link java.util.Date Date}80* <td>date of the recording or release81* <tr>82* <th scope="row">"comment"83* <td>{@link String String}84* <td>an arbitrary text85* </tbody>86* </table>87*88* @author David Rivas89* @author Kara Kytle90* @author Florian Bomers91* @see AudioInputStream92* @since 1.393*/94public class AudioFileFormat {9596/**97* File type.98*/99private final Type type;100101/**102* File length in bytes.103*/104private final int byteLength;105106/**107* Format of the audio data contained in the file.108*/109private final AudioFormat format;110111/**112* Audio data length in sample frames.113*/114private final int frameLength;115116/**117* The set of properties.118*/119private HashMap<String, Object> properties;120121/**122* Constructs an audio file format object. This protected constructor is123* intended for use by providers of file-reading services when returning124* information about an audio file or about supported audio file formats.125*126* @param type the type of the audio file127* @param byteLength the length of the file in bytes, or128* {@code AudioSystem.NOT_SPECIFIED}129* @param format the format of the audio data contained in the file130* @param frameLength the audio data length in sample frames, or131* {@code AudioSystem.NOT_SPECIFIED}132* @see #getType133*/134protected AudioFileFormat(Type type, int byteLength, AudioFormat format, int frameLength) {135136this.type = type;137this.byteLength = byteLength;138this.format = format;139this.frameLength = frameLength;140this.properties = null;141}142143/**144* Constructs an audio file format object. This public constructor may be145* used by applications to describe the properties of a requested audio146* file.147*148* @param type the type of the audio file149* @param format the format of the audio data contained in the file150* @param frameLength the audio data length in sample frames, or151* {@code AudioSystem.NOT_SPECIFIED}152*/153public AudioFileFormat(Type type, AudioFormat format, int frameLength) {154155156this(type,AudioSystem.NOT_SPECIFIED,format,frameLength);157}158159/**160* Construct an audio file format object with a set of defined properties.161* This public constructor may be used by applications to describe the162* properties of a requested audio file. The properties map will be copied163* to prevent any changes to it.164*165* @param type the type of the audio file166* @param format the format of the audio data contained in the file167* @param frameLength the audio data length in sample frames, or168* {@code AudioSystem.NOT_SPECIFIED}169* @param properties a {@code Map<String, Object>} object with properties170* @since 1.5171*/172public AudioFileFormat(Type type, AudioFormat format,173int frameLength, Map<String, Object> properties) {174this(type,AudioSystem.NOT_SPECIFIED,format,frameLength);175this.properties = new HashMap<>(properties);176}177178/**179* Obtains the audio file type, such as {@code WAVE} or {@code AU}.180*181* @return the audio file type182* @see Type#WAVE183* @see Type#AU184* @see Type#AIFF185* @see Type#AIFC186* @see Type#SND187*/188public Type getType() {189return type;190}191192/**193* Obtains the size in bytes of the entire audio file (not just its audio194* data).195*196* @return the audio file length in bytes197* @see AudioSystem#NOT_SPECIFIED198*/199public int getByteLength() {200return byteLength;201}202203/**204* Obtains the format of the audio data contained in the audio file.205*206* @return the audio data format207*/208public AudioFormat getFormat() {209return format;210}211212/**213* Obtains the length of the audio data contained in the file, expressed in214* sample frames.215*216* @return the number of sample frames of audio data in the file217* @see AudioSystem#NOT_SPECIFIED218*/219public int getFrameLength() {220return frameLength;221}222223/**224* Obtain an unmodifiable map of properties. The concept of properties is225* further explained in the {@link AudioFileFormat class description}.226*227* @return a {@code Map<String, Object>} object containing all properties.228* If no properties are recognized, an empty map is returned.229* @see #getProperty(String)230* @since 1.5231*/232@SuppressWarnings("unchecked") // Cast of result of clone233public Map<String, Object> properties() {234Map<String,Object> ret;235if (properties == null) {236ret = new HashMap<>(0);237} else {238ret = (Map<String,Object>) (properties.clone());239}240return Collections.unmodifiableMap(ret);241}242243/**244* Obtain the property value specified by the key. The concept of properties245* is further explained in the {@link AudioFileFormat class description}.246* <p>247* If the specified property is not defined for a particular file format,248* this method returns {@code null}.249*250* @param key the key of the desired property251* @return the value of the property with the specified key, or {@code null}252* if the property does not exist253* @see #properties()254* @since 1.5255*/256public Object getProperty(String key) {257if (properties == null) {258return null;259}260return properties.get(key);261}262263/**264* Returns a string representation of the audio file format.265*266* @return a string representation of the audio file format267*/268@Override269public String toString() {270String str = "Unknown file format";271//$$fb2002-11-01: fix for 4672864: AudioFileFormat.toString() throws unexpected NullPointerException272if (getType() != null) {273str = getType() + " (." + getType().getExtension() + ") file";274}275if (getByteLength() != AudioSystem.NOT_SPECIFIED) {276str += ", byte length: " + getByteLength();277}278str += ", data format: " + getFormat();279if (getFrameLength() != AudioSystem.NOT_SPECIFIED) {280str += ", frame length: " + getFrameLength();281}282return str;283}284285/**286* An instance of the {@code Type} class represents one of the standard287* types of audio file. Static instances are provided for the common types.288*/289public static class Type {290291// FILE FORMAT TYPE DEFINES292293/**294* Specifies a WAVE file.295*/296public static final Type WAVE = new Type("WAVE", "wav");297298/**299* Specifies an AU file.300*/301public static final Type AU = new Type("AU", "au");302303/**304* Specifies an AIFF file.305*/306public static final Type AIFF = new Type("AIFF", "aif");307308/**309* Specifies an AIFF-C file.310*/311public static final Type AIFC = new Type("AIFF-C", "aifc");312313/**314* Specifies a SND file.315*/316public static final Type SND = new Type("SND", "snd");317318/**319* File type name.320*/321private final String name;322323/**324* File type extension.325*/326private final String extension;327328/**329* Constructs a file type.330*331* @param name the string that names the file type332* @param extension the string that commonly marks the file type333* without leading dot334*/335public Type(final String name, final String extension) {336this.name = name;337this.extension = extension;338}339340/**341* Indicates whether the specified object is equal to this file type,342* returning {@code true} if the objects are equal.343*344* @param obj the reference object with which to compare345* @return {@code true} if the specified object is equal to this file346* type; {@code false} otherwise347*/348@Override349public final boolean equals(final Object obj) {350if (this == obj) {351return true;352}353if (!(obj instanceof Type)) {354return false;355}356return Objects.equals(name, ((Type) obj).name);357}358359/**360* Returns a hash code value for this file type.361*362* @return a hash code value for this file type363*/364@Override365public final int hashCode() {366return name != null ? name.hashCode() : 0;367}368369/**370* Returns type's name as the string representation of the file type.371*372* @return a string representation of the file type373*/374@Override375public final String toString() {376return name;377}378379/**380* Obtains the common file name extension for this file type.381*382* @return file type extension383*/384public String getExtension() {385return extension;386}387}388}389390391