Path: blob/master/src/java.desktop/share/classes/javax/sound/midi/MidiFileFormat.java
41159 views
/*1* Copyright (c) 1999, 2017, 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.midi;2627import java.util.Collections;28import java.util.HashMap;29import java.util.Map;3031/**32* A {@code MidiFileFormat} object encapsulates a MIDI file's type, as well as33* its length and timing information.34* <p>35* A {@code MidiFileFormat} object can include a set of properties. A property36* is a pair of key and value: the key is of type {@code String}, the associated37* property value is an arbitrary object. Properties specify additional38* informational meta data (like a author, or copyright). Properties are39* optional information, and file reader and file writer implementations are not40* required to provide or recognize properties.41* <p>42* The following table lists some common properties that should be used in43* implementations:44*45* <table class="striped">46* <caption>MIDI File Format Properties</caption>47* <thead>48* <tr>49* <th scope="col">Property key50* <th scope="col">Value type51* <th scope="col">Description52* </thead>53* <tbody>54* <tr>55* <th scope="row">"author"56* <td>{@link String String}57* <td>name of the author of this file58* <tr>59* <th scope="row">"title"60* <td>{@link String String}61* <td>title of this file62* <tr>63* <th scope="row">"copyright"64* <td>{@link String String}65* <td>copyright message66* <tr>67* <th scope="row">"date"68* <td>{@link java.util.Date Date}69* <td>date of the recording or release70* <tr>71* <th scope="row">"comment"72* <td>{@link String String}73* <td>an arbitrary text74* </tbody>75* </table>76*77* @author Kara Kytle78* @author Florian Bomers79* @see MidiSystem#getMidiFileFormat(java.io.File)80* @see Sequencer#setSequence(java.io.InputStream stream)81*/82public class MidiFileFormat {8384/**85* Represents unknown length.86*87* @see #getByteLength88* @see #getMicrosecondLength89*/90public static final int UNKNOWN_LENGTH = -1;9192/**93* The type of MIDI file.94*/95protected int type;9697/**98* The division type of the MIDI file.99*100* @see Sequence#PPQ101* @see Sequence#SMPTE_24102* @see Sequence#SMPTE_25103* @see Sequence#SMPTE_30DROP104* @see Sequence#SMPTE_30105*/106protected float divisionType;107108/**109* The timing resolution of the MIDI file.110*/111protected int resolution;112113/**114* The length of the MIDI file in bytes.115*/116protected int byteLength;117118/**119* The duration of the MIDI file in microseconds.120*/121protected long microsecondLength;122123/**124* The set of properties.125*/126private HashMap<String, Object> properties;127128/**129* Constructs a {@code MidiFileFormat}.130*131* @param type the MIDI file type (0, 1, or 2)132* @param divisionType the timing division type (PPQ or one of the SMPTE133* types)134* @param resolution the timing resolution135* @param bytes the length of the MIDI file in bytes, or136* {@link #UNKNOWN_LENGTH} if not known137* @param microseconds the duration of the file in microseconds, or138* {@link #UNKNOWN_LENGTH} if not known139* @see #UNKNOWN_LENGTH140* @see Sequence#PPQ141* @see Sequence#SMPTE_24142* @see Sequence#SMPTE_25143* @see Sequence#SMPTE_30DROP144* @see Sequence#SMPTE_30145*/146public MidiFileFormat(int type, float divisionType, int resolution, int bytes, long microseconds) {147148this.type = type;149this.divisionType = divisionType;150this.resolution = resolution;151this.byteLength = bytes;152this.microsecondLength = microseconds;153this.properties = null;154}155156/**157* Construct a {@code MidiFileFormat} with a set of properties.158*159* @param type the MIDI file type (0, 1, or 2)160* @param divisionType the timing division type (PPQ or one of the SMPTE161* types)162* @param resolution the timing resolution163* @param bytes the length of the MIDI file in bytes, or164* {@code UNKNOWN_LENGTH} if not known165* @param microseconds the duration of the file in microseconds, or166* {@code UNKNOWN_LENGTH} if not known167* @param properties a {@code Map<String,Object>} object with properties168* @see #UNKNOWN_LENGTH169* @see Sequence#PPQ170* @see Sequence#SMPTE_24171* @see Sequence#SMPTE_25172* @see Sequence#SMPTE_30DROP173* @see Sequence#SMPTE_30174* @since 1.5175*/176public MidiFileFormat(int type, float divisionType,177int resolution, int bytes,178long microseconds, Map<String, Object> properties) {179this(type, divisionType, resolution, bytes, microseconds);180this.properties = new HashMap<>(properties);181}182183/**184* Obtains the MIDI file type.185*186* @return the file's type (0, 1, or 2)187*/188public int getType() {189return type;190}191192/**193* Obtains the timing division type for the MIDI file.194*195* @return the division type (PPQ or one of the SMPTE types)196* @see Sequence#Sequence(float, int)197* @see Sequence#PPQ198* @see Sequence#SMPTE_24199* @see Sequence#SMPTE_25200* @see Sequence#SMPTE_30DROP201* @see Sequence#SMPTE_30202* @see Sequence#getDivisionType()203*/204public float getDivisionType() {205return divisionType;206}207208/**209* Obtains the timing resolution for the MIDI file. If the division type is210* PPQ, the resolution is specified in ticks per beat. For SMTPE timing, the211* resolution is specified in ticks per frame.212*213* @return the number of ticks per beat (PPQ) or per frame (SMPTE)214* @see #getDivisionType215* @see Sequence#getResolution()216*/217public int getResolution() {218return resolution;219}220221/**222* Obtains the length of the MIDI file, expressed in 8-bit bytes.223*224* @return the number of bytes in the file, or {@code UNKNOWN_LENGTH} if not225* known226* @see #UNKNOWN_LENGTH227*/228public int getByteLength() {229return byteLength;230}231232/**233* Obtains the length of the MIDI file, expressed in microseconds.234*235* @return the file's duration in microseconds, or {@code UNKNOWN_LENGTH} if236* not known237* @see Sequence#getMicrosecondLength()238* @see #getByteLength239* @see #UNKNOWN_LENGTH240*/241public long getMicrosecondLength() {242return microsecondLength;243}244245/**246* Obtain an unmodifiable map of properties. The concept of properties is247* further explained in the {@link MidiFileFormat class description}.248*249* @return a {@code Map<String,Object>} object containing all properties. If250* no properties are recognized, an empty map is returned.251* @see #getProperty(String)252* @since 1.5253*/254@SuppressWarnings("unchecked") // Cast of result of clone255public Map<String,Object> properties() {256Map<String,Object> ret;257if (properties == null) {258ret = new HashMap<>(0);259} else {260ret = (Map<String,Object>) (properties.clone());261}262return Collections.unmodifiableMap(ret);263}264265/**266* Obtain the property value specified by the key. The concept of properties267* is further explained in the {@link MidiFileFormat class description}.268* <p>269* If the specified property is not defined for a particular file format,270* this method returns {@code null}.271*272* @param key the key of the desired property273* @return the value of the property with the specified key, or {@code null}274* if the property does not exist275* @see #properties()276* @since 1.5277*/278public Object getProperty(String key) {279if (properties == null) {280return null;281}282return properties.get(key);283}284}285286287