jdk/src/java.desktop/share/classes/javax/sound/sampled/AudioSystem.java
author serb
Thu, 07 Jan 2016 23:50:00 +0300
changeset 35686 1c21a27682a5
parent 35684 900e28dae561
child 45648 87c997b74bb8
permissions -rw-r--r--
8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway Reviewed-by: prr, amenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
     2
 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.sound.sampled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
    28
import java.io.EOFException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.File;
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    30
import java.io.IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.URL;
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    34
import java.util.ArrayList;
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
    35
import java.util.Collections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.List;
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
    38
import java.util.Objects;
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    39
import java.util.Properties;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    43
import javax.sound.sampled.spi.AudioFileReader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.sound.sampled.spi.AudioFileWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.sound.sampled.spi.FormatConversionProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.sound.sampled.spi.MixerProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import com.sun.media.sound.JDK13Services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/* $fb TODO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * - consistent usage of (typed) collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
    56
 * The {@code AudioSystem} class acts as the entry point to the sampled-audio
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
    57
 * system resources. This class lets you query and access the mixers that are
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
    58
 * installed on the system. {@code AudioSystem} includes a number of methods for
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
    59
 * converting audio data between different formats, and for translating between
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
    60
 * audio files and streams. It also provides a method for obtaining a
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
    61
 * {@link Line} directly from the {@code AudioSystem} without dealing explicitly
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * with mixers.
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
    63
 * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
    64
 * Properties can be used to specify the default mixer for specific line types.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
    65
 * Both system properties and a properties file are considered. The
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    66
 * "sound.properties" properties file is read from an implementation-specific
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 26037
diff changeset
    67
 * location (typically it is the {@code conf} directory in the Java installation
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    68
 * directory). If a property exists both as a system property and in the
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    69
 * properties file, the system property takes precedence. If none is specified,
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    70
 * a suitable default is chosen among the available devices. The syntax of the
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    71
 * properties file is specified in
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    72
 * {@link Properties#load(InputStream) Properties.load}. The following table
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    73
 * lists the available property keys and which methods consider them:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <table border=0>
19207
3448b0cb4077 8022174: Fix doclint warnings in javax.sound
darcy
parents: 15263
diff changeset
    76
 *  <caption>Audio System Property Keys</caption>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *  <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *   <th>Property Key</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *   <th>Interface</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *   <th>Affected Method(s)</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *  </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *  <tr>
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
    83
 *   <td>{@code javax.sound.sampled.Clip}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *   <td>{@link Clip}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *   <td>{@link #getLine}, {@link #getClip}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *  </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *  <tr>
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
    88
 *   <td>{@code javax.sound.sampled.Port}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *   <td>{@link Port}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *   <td>{@link #getLine}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *  </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *  <tr>
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
    93
 *   <td>{@code javax.sound.sampled.SourceDataLine}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *   <td>{@link SourceDataLine}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *   <td>{@link #getLine}, {@link #getSourceDataLine}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *  </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *  <tr>
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
    98
 *   <td>{@code javax.sound.sampled.TargetDataLine}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *   <td>{@link TargetDataLine}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *   <td>{@link #getLine}, {@link #getTargetDataLine}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *  </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   104
 * The property value consists of the provider class name and the mixer name,
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   105
 * separated by the hash mark (&quot;#&quot;). The provider class name is the
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   106
 * fully-qualified name of a concrete {@link MixerProvider mixer provider}
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   107
 * class. The mixer name is matched against the {@code String} returned by the
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   108
 * {@code getName} method of {@code Mixer.Info}. Either the class name, or the
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   109
 * mixer name may be omitted. If only the class name is specified, the trailing
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   110
 * hash mark is optional.
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   111
 * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   112
 * If the provider class is specified, and it can be successfully retrieved from
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   113
 * the installed providers, the list of {@code Mixer.Info} objects is retrieved
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   114
 * from the provider. Otherwise, or when these mixers do not provide a
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   115
 * subsequent match, the list is retrieved from {@link #getMixerInfo} to contain
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   116
 * all available {@code Mixer.Info} objects.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   117
 * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   118
 * If a mixer name is specified, the resulting list of {@code Mixer.Info}
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   119
 * objects is searched: the first one with a matching name, and whose
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   120
 * {@code Mixer} provides the respective line interface, will be returned. If no
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   121
 * matching {@code Mixer.Info} object is found, or the mixer name is not
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   122
 * specified, the first mixer from the resulting list, which provides the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * respective line interface, will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   125
 * For example, the property {@code javax.sound.sampled.Clip} with a value
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   126
 * {@code "com.sun.media.sound.MixerProvider#SunClip"}
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   127
 * will have the following consequences when {@code getLine} is called
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   128
 * requesting a {@code Clip} instance: if the class
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   129
 * {@code com.sun.media.sound.MixerProvider} exists in the list of installed
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   130
 * mixer providers, the first {@code Clip} from the first mixer with name
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   131
 * {@code "SunClip"} will be returned. If it cannot be found, the
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   132
 * first {@code Clip} from the first mixer of the specified provider will be
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   133
 * returned, regardless of name. If there is none, the first {@code Clip} from
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   134
 * the first {@code Mixer} with name {@code "SunClip"} in the list of
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   135
 * all mixers (as returned by {@code getMixerInfo}) will be returned, or, if not
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   136
 * found, the first {@code Clip} of the first {@code Mixer} that can be found in
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   137
 * the list of all mixers is returned. If that fails, too, an
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   138
 * {@code IllegalArgumentException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * @author Matthias Pfisterer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * @author Kevin P. Smith
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * @see AudioFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * @see AudioInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * @see Mixer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * @see Line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * @see Line.Info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
public class AudioSystem {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   154
     * An integer that stands for an unknown numeric value. This value is
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   155
     * appropriate only for signed quantities that do not normally take negative
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   156
     * values. Examples include file sizes, frame sizes, buffer sizes, and
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   157
     * sample rates. A number of Java Sound constructors accept a value of
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   158
     * {@code NOT_SPECIFIED} for such parameters. Other methods may also accept
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   159
     * or return this value, as documented.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public static final int NOT_SPECIFIED = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Private no-args constructor for ensuring against instantiation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private AudioSystem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   170
     * Obtains an array of mixer info objects that represents the set of audio
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   171
     * mixers that are currently installed on the system.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   172
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   173
     * @return an array of info objects for the currently installed mixers. If
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   174
     *         no mixers are available on the system, an array of length 0 is
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   175
     *         returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @see #getMixer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public static Mixer.Info[] getMixerInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   180
        List<Mixer.Info> infos = getMixerInfoList();
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   181
        Mixer.Info[] allInfos = infos.toArray(new Mixer.Info[infos.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return allInfos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Obtains the requested audio mixer.
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   187
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   188
     * @param  info a {@code Mixer.Info} object representing the desired mixer,
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   189
     *         or {@code null} for the system default mixer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return the requested mixer
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   191
     * @throws SecurityException if the requested mixer is unavailable because
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   192
     *         of security restrictions
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   193
     * @throws IllegalArgumentException if the info object does not represent a
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   194
     *         mixer installed on the system
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @see #getMixerInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   197
    public static Mixer getMixer(final Mixer.Info info) {
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   198
        for (final MixerProvider provider : getMixerProviders()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            try {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   200
                return provider.getMixer(info);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   201
            } catch (IllegalArgumentException | NullPointerException ignored) {
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   202
                // The MixerProvider.getMixer(null) should return default Mixer,
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   203
                // This behaviour was assumed from the beginning, but strictly
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   204
                // specified only in the jdk9. Since the jdk1.1.5 we skipped
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   205
                // NPE for some reason and therefore skipped some
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   206
                // implementations of MixerProviders, which throw NPE. To keep
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   207
                // support of such implementations, we still ignore NPE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   210
        throw new IllegalArgumentException(
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   211
                String.format("Mixer not supported: %s", info));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   214
    //$$fb 2002-11-26: fix for 4757930: DOC: AudioSystem.getTarget/SourceLineInfo() is ambiguous
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   217
     * Obtains information about all source lines of a particular type that are
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   218
     * supported by the installed mixers.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   220
     * @param  info a {@code Line.Info} object that specifies the kind of lines
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   221
     *         about which information is requested
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   222
     * @return an array of {@code Line.Info} objects describing source lines
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   223
     *         matching the type requested. If no matching source lines are
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   224
     *         supported, an array of length 0 is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @see Mixer#getSourceLineInfo(Line.Info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public static Line.Info[] getSourceLineInfo(Line.Info info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   229
        Vector<Line.Info> vector = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        Line.Info[] currentInfoArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        Mixer mixer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        Line.Info fullInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        Mixer.Info[] infoArray = getMixerInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        for (int i = 0; i < infoArray.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            mixer = getMixer(infoArray[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            currentInfoArray = mixer.getSourceLineInfo(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            for (int j = 0; j < currentInfoArray.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                vector.addElement(currentInfoArray[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        Line.Info[] returnedArray = new Line.Info[vector.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        for (int i = 0; i < returnedArray.length; i++) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   249
            returnedArray[i] = vector.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return returnedArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   256
     * Obtains information about all target lines of a particular type that are
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   257
     * supported by the installed mixers.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   259
     * @param  info a {@code Line.Info} object that specifies the kind of lines
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   260
     *         about which information is requested
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   261
     * @return an array of {@code Line.Info} objects describing target lines
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   262
     *         matching the type requested. If no matching target lines are
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   263
     *         supported, an array of length 0 is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @see Mixer#getTargetLineInfo(Line.Info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public static Line.Info[] getTargetLineInfo(Line.Info info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   268
        Vector<Line.Info> vector = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        Line.Info[] currentInfoArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        Mixer mixer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        Line.Info fullInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        Mixer.Info[] infoArray = getMixerInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        for (int i = 0; i < infoArray.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            mixer = getMixer(infoArray[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            currentInfoArray = mixer.getTargetLineInfo(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            for (int j = 0; j < currentInfoArray.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                vector.addElement(currentInfoArray[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        Line.Info[] returnedArray = new Line.Info[vector.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        for (int i = 0; i < returnedArray.length; i++) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   288
            returnedArray[i] = vector.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return returnedArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   295
     * Indicates whether the system supports any lines that match the specified
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   296
     * {@code Line.Info} object. A line is supported if any installed mixer
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   297
     * supports it.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   299
     * @param  info a {@code Line.Info} object describing the line for which
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   300
     *         support is queried
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   301
     * @return {@code true} if at least one matching line is supported,
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   302
     *         otherwise {@code false}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @see Mixer#isLineSupported(Line.Info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public static boolean isLineSupported(Line.Info info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        Mixer mixer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        Mixer.Info[] infoArray = getMixerInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        for (int i = 0; i < infoArray.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if( infoArray[i] != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                mixer = getMixer(infoArray[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                if (mixer.isLineSupported(info)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Obtains a line that matches the description in the specified
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   325
     * {@code Line.Info} object.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   326
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   327
     * If a {@code DataLine} is requested, and {@code info} is an instance of
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   328
     * {@code DataLine.Info} specifying at least one fully qualified audio
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   329
     * format, the last one will be used as the default format of the returned
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   330
     * {@code DataLine}.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   331
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   332
     * If system properties
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   333
     * {@code javax.sound.sampled.Clip},
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   334
     * {@code javax.sound.sampled.Port},
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   335
     * {@code javax.sound.sampled.SourceDataLine} and
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   336
     * {@code javax.sound.sampled.TargetDataLine} are defined or they are
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   337
     * defined in the file "sound.properties", they are used to retrieve default
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   338
     * lines. For details, refer to the {@link AudioSystem class description}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   340
     * If the respective property is not set, or the mixer requested in the
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   341
     * property is not installed or does not provide the requested line, all
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   342
     * installed mixers are queried for the requested line type. A Line will be
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   343
     * returned from the first mixer providing the requested line type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   345
     * @param  info a {@code Line.Info} object describing the desired kind of
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   346
     *         line
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   347
     * @return a line of the requested kind
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   348
     * @throws LineUnavailableException if a matching line is not available due
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   349
     *         to resource restrictions
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   350
     * @throws SecurityException if a matching line is not available due to
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   351
     *         security restrictions
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   352
     * @throws IllegalArgumentException if the system does not support at least
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   353
     *         one line matching the specified {@code Line.Info} object through
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   354
     *         any installed mixer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public static Line getLine(Line.Info info) throws LineUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        LineUnavailableException lue = null;
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   358
        List<MixerProvider> providers = getMixerProviders();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        // 1: try from default mixer for this line class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            Mixer mixer = getDefaultMixer(providers, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            if (mixer != null && mixer.isLineSupported(info)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                return mixer.getLine(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        } catch (LineUnavailableException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            lue = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            // must not happen... but better to catch it here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            // if plug-ins are badly written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        // 2: if that doesn't work, try to find any mixing mixer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        for(int i = 0; i < providers.size(); i++) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   377
            MixerProvider provider = providers.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            Mixer.Info[] infos = provider.getMixerInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            for (int j = 0; j < infos.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    Mixer mixer = provider.getMixer(infos[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    // see if this is an appropriate mixer which can mix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    if (isAppropriateMixer(mixer, info, true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                        return mixer.getLine(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                } catch (LineUnavailableException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    lue = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    // must not happen... but better to catch it here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    // if plug-ins are badly written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        // 3: if that didn't work, try to find any non-mixing mixer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        for(int i = 0; i < providers.size(); i++) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   399
            MixerProvider provider = providers.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            Mixer.Info[] infos = provider.getMixerInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            for (int j = 0; j < infos.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    Mixer mixer = provider.getMixer(infos[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    // see if this is an appropriate mixer which can mix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    if (isAppropriateMixer(mixer, info, false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                        return mixer.getLine(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                } catch (LineUnavailableException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    lue = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    // must not happen... but better to catch it here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    // if plug-ins are badly written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        // if this line was supported but was not available, throw the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // LineUnavailableException we got (??).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (lue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            throw lue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        // otherwise, the requested line was not supported, so throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        // an Illegal argument exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        throw new IllegalArgumentException("No line matching " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                                           info.toString() + " is supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   430
     * Obtains a clip that can be used for playing back an audio file or an
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   431
     * audio stream. The returned clip will be provided by the default system
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   432
     * mixer, or, if not possible, by any other mixer installed in the system
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   433
     * that supports a {@code Clip} object.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   434
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   435
     * The returned clip must be opened with the {@code open(AudioFormat)} or
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   436
     * {@code open(AudioInputStream)} method.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   437
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   438
     * This is a high-level method that uses {@code getMixer} and
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   439
     * {@code getLine} internally.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   440
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   441
     * If the system property {@code javax.sound.sampled.Clip} is defined or it
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   442
     * is defined in the file "sound.properties", it is used to retrieve the
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   443
     * default clip. For details, refer to the
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   444
     * {@link AudioSystem class description}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @return the desired clip object
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   447
     * @throws LineUnavailableException if a clip object is not available due to
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   448
     *         resource restrictions
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   449
     * @throws SecurityException if a clip object is not available due to
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   450
     *         security restrictions
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   451
     * @throws IllegalArgumentException if the system does not support at least
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   452
     *         one clip instance through any installed mixer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @see #getClip(Mixer.Info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    public static Clip getClip() throws LineUnavailableException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                                             AudioSystem.NOT_SPECIFIED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                             16, 2, 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                                             AudioSystem.NOT_SPECIFIED, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        DataLine.Info info = new DataLine.Info(Clip.class, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        return (Clip) AudioSystem.getLine(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   466
     * Obtains a clip from the specified mixer that can be used for playing back
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   467
     * an audio file or an audio stream.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   468
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   469
     * The returned clip must be opened with the {@code open(AudioFormat)} or
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   470
     * {@code open(AudioInputStream)} method.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   471
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   472
     * This is a high-level method that uses {@code getMixer} and
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   473
     * {@code getLine} internally.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   475
     * @param  mixerInfo a {@code Mixer.Info} object representing the desired
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   476
     *         mixer, or {@code null} for the system default mixer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @return a clip object from the specified mixer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   479
     * @throws LineUnavailableException if a clip is not available from this
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   480
     *         mixer due to resource restrictions
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   481
     * @throws SecurityException if a clip is not available from this mixer due
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   482
     *         to security restrictions
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   483
     * @throws IllegalArgumentException if the system does not support at least
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   484
     *         one clip through the specified mixer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @see #getClip()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    public static Clip getClip(Mixer.Info mixerInfo) throws LineUnavailableException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                                             AudioSystem.NOT_SPECIFIED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                                             16, 2, 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                             AudioSystem.NOT_SPECIFIED, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        DataLine.Info info = new DataLine.Info(Clip.class, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        Mixer mixer = AudioSystem.getMixer(mixerInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        return (Clip) mixer.getLine(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   499
     * Obtains a source data line that can be used for playing back audio data
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   500
     * in the format specified by the {@code AudioFormat} object. The returned
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   501
     * line will be provided by the default system mixer, or, if not possible,
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   502
     * by any other mixer installed in the system that supports a matching
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   503
     * {@code SourceDataLine} object.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   504
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   505
     * The returned line should be opened with the {@code open(AudioFormat)} or
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   506
     * {@code open(AudioFormat, int)} method.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   507
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   508
     * This is a high-level method that uses {@code getMixer} and
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   509
     * {@code getLine} internally.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   510
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   511
     * The returned {@code SourceDataLine}'s default audio format will be
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   512
     * initialized with {@code format}.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   513
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   514
     * If the system property {@code javax.sound.sampled.SourceDataLine} is
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   515
     * defined or it is defined in the file "sound.properties", it is used to
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   516
     * retrieve the default source data line. For details, refer to the
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   517
     * {@link AudioSystem class description}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   519
     * @param  format an {@code AudioFormat} object specifying the supported
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   520
     *         audio format of the returned line, or {@code null} for any audio
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   521
     *         format
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   522
     * @return the desired {@code SourceDataLine} object
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   523
     * @throws LineUnavailableException if a matching source data line is not
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   524
     *         available due to resource restrictions
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   525
     * @throws SecurityException if a matching source data line is not available
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   526
     *         due to security restrictions
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   527
     * @throws IllegalArgumentException if the system does not support at least
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   528
     *         one source data line supporting the specified audio format
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   529
     *         through any installed mixer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @see #getSourceDataLine(AudioFormat, Mixer.Info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    public static SourceDataLine getSourceDataLine(AudioFormat format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        throws LineUnavailableException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        return (SourceDataLine) AudioSystem.getLine(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   540
     * Obtains a source data line that can be used for playing back audio data
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   541
     * in the format specified by the {@code AudioFormat} object, provided by
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   542
     * the mixer specified by the {@code Mixer.Info} object.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   543
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   544
     * The returned line should be opened with the {@code open(AudioFormat)} or
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   545
     * {@code open(AudioFormat, int)} method.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   546
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   547
     * This is a high-level method that uses {@code getMixer} and
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   548
     * {@code getLine} internally.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   549
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   550
     * The returned {@code SourceDataLine}'s default audio format will be
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   551
     * initialized with {@code format}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   553
     * @param  format an {@code AudioFormat} object specifying the supported
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   554
     *         audio format of the returned line, or {@code null} for any audio
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   555
     *         format
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   556
     * @param  mixerinfo a {@code Mixer.Info} object representing the desired
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   557
     *         mixer, or {@code null} for the system default mixer
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   558
     * @return the desired {@code SourceDataLine} object
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   559
     * @throws LineUnavailableException if a matching source data line is not
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   560
     *         available from the specified mixer due to resource restrictions
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   561
     * @throws SecurityException if a matching source data line is not available
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   562
     *         from the specified mixer due to security restrictions
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   563
     * @throws IllegalArgumentException if the specified mixer does not support
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   564
     *         at least one source data line supporting the specified audio
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   565
     *         format
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @see #getSourceDataLine(AudioFormat)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    public static SourceDataLine getSourceDataLine(AudioFormat format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                                                   Mixer.Info mixerinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        throws LineUnavailableException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        Mixer mixer = AudioSystem.getMixer(mixerinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        return (SourceDataLine) mixer.getLine(info);
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   575
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   578
     * Obtains a target data line that can be used for recording audio data in
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   579
     * the format specified by the {@code AudioFormat} object. The returned line
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   580
     * will be provided by the default system mixer, or, if not possible, by any
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   581
     * other mixer installed in the system that supports a matching
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   582
     * {@code TargetDataLine} object.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   583
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   584
     * The returned line should be opened with the {@code open(AudioFormat)} or
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   585
     * {@code open(AudioFormat, int)} method.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   586
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   587
     * This is a high-level method that uses {@code getMixer} and
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   588
     * {@code getLine} internally.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   589
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   590
     * The returned {@code TargetDataLine}'s default audio format will be
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   591
     * initialized with {@code format}.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   592
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   593
     * If the system property {@code javax.sound.sampled.TargetDataLine} is
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   594
     * defined or it is defined in the file "sound.properties", it is used to
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   595
     * retrieve the default target data line. For details, refer to the
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   596
     * {@link AudioSystem class description}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   598
     * @param  format an {@code AudioFormat} object specifying the supported
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   599
     *         audio format of the returned line, or {@code null} for any audio
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   600
     *         format
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   601
     * @return the desired {@code TargetDataLine} object
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   602
     * @throws LineUnavailableException if a matching target data line is not
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   603
     *         available due to resource restrictions
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   604
     * @throws SecurityException if a matching target data line is not available
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   605
     *         due to security restrictions
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   606
     * @throws IllegalArgumentException if the system does not support at least
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   607
     *         one target data line supporting the specified audio format
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   608
     *         through any installed mixer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @see #getTargetDataLine(AudioFormat, Mixer.Info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @see AudioPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    public static TargetDataLine getTargetDataLine(AudioFormat format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        throws LineUnavailableException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        return (TargetDataLine) AudioSystem.getLine(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   621
     * Obtains a target data line that can be used for recording audio data in
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   622
     * the format specified by the {@code AudioFormat} object, provided by the
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   623
     * mixer specified by the {@code Mixer.Info} object.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   624
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   625
     * The returned line should be opened with the {@code open(AudioFormat)} or
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   626
     * {@code open(AudioFormat, int)} method.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   627
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   628
     * This is a high-level method that uses {@code getMixer} and
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   629
     * {@code getLine} internally.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   630
     * <p>
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   631
     * The returned {@code TargetDataLine}'s default audio format will be
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   632
     * initialized with {@code format}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   634
     * @param  format an {@code AudioFormat} object specifying the supported
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   635
     *         audio format of the returned line, or {@code null} for any audio
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   636
     *         format
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   637
     * @param  mixerinfo a {@code Mixer.Info} object representing the desired
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   638
     *         mixer, or {@code null} for the system default mixer
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   639
     * @return the desired {@code TargetDataLine} object
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   640
     * @throws LineUnavailableException if a matching target data line is not
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   641
     *         available from the specified mixer due to resource restrictions
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   642
     * @throws SecurityException if a matching target data line is not available
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   643
     *         from the specified mixer due to security restrictions
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   644
     * @throws IllegalArgumentException if the specified mixer does not support
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   645
     *         at least one target data line supporting the specified audio
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   646
     *         format
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @see #getTargetDataLine(AudioFormat)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @see AudioPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    public static TargetDataLine getTargetDataLine(AudioFormat format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                                                   Mixer.Info mixerinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        throws LineUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        Mixer mixer = AudioSystem.getMixer(mixerinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return (TargetDataLine) mixer.getLine(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   660
    // $$fb 2002-04-12: fix for 4662082: behavior of AudioSystem.getTargetEncodings() methods doesn't match the spec
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   663
     * Obtains the encodings that the system can obtain from an audio input
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   664
     * stream with the specified encoding using the set of installed format
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   665
     * converters.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   666
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   667
     * @param  sourceEncoding the encoding for which conversion support is
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   668
     *         queried
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   669
     * @return array of encodings. If {@code sourceEncoding}is not supported, an
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   670
     *         array of length 0 is returned. Otherwise, the array will have a
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   671
     *         length of at least 1, representing {@code sourceEncoding}
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   672
     *         (no conversion).
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   673
     * @throws NullPointerException if {@code sourceEncoding} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat.Encoding sourceEncoding) {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   676
        Objects.requireNonNull(sourceEncoding);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   678
        List<FormatConversionProvider> codecs = getFormatConversionProviders();
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   679
        Vector<AudioFormat.Encoding> encodings = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        AudioFormat.Encoding encs[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        // gather from all the codecs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        for(int i=0; i<codecs.size(); i++ ) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   685
            FormatConversionProvider codec = codecs.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            if( codec.isSourceEncodingSupported( sourceEncoding ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                encs = codec.getTargetEncodings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                for (int j = 0; j < encs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                    encodings.addElement( encs[j] );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   693
        if (!encodings.contains(sourceEncoding)) {
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   694
            encodings.addElement(sourceEncoding);
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   695
        }
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   696
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   697
        return encodings.toArray(new AudioFormat.Encoding[encodings.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   700
    // $$fb 2002-04-12: fix for 4662082: behavior of AudioSystem.getTargetEncodings() methods doesn't match the spec
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   703
     * Obtains the encodings that the system can obtain from an audio input
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   704
     * stream with the specified format using the set of installed format
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   705
     * converters.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   706
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   707
     * @param  sourceFormat the audio format for which conversion is queried
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   708
     * @return array of encodings. If {@code sourceFormat}is not supported, an
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   709
     *         array of length 0 is returned. Otherwise, the array will have a
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   710
     *         length of at least 1, representing the encoding of
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   711
     *         {@code sourceFormat} (no conversion).
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   712
     * @throws NullPointerException if {@code sourceFormat} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat) {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   715
        Objects.requireNonNull(sourceFormat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   717
        List<FormatConversionProvider> codecs = getFormatConversionProviders();
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   718
        List<AudioFormat.Encoding> encs = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        // gather from all the codecs
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   721
        for (final FormatConversionProvider codec : codecs) {
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   722
            Collections.addAll(encs, codec.getTargetEncodings(sourceFormat));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   725
        if (!encs.contains(sourceFormat.getEncoding())) {
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   726
            encs.add(sourceFormat.getEncoding());
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   727
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   729
        return encs.toArray(new AudioFormat.Encoding[encs.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   733
     * Indicates whether an audio input stream of the specified encoding can be
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   734
     * obtained from an audio input stream that has the specified format.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   735
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   736
     * @param  targetEncoding the desired encoding after conversion
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   737
     * @param  sourceFormat the audio format before conversion
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   738
     * @return {@code true} if the conversion is supported, otherwise
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   739
     *         {@code false}
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   740
     * @throws NullPointerException if {@code targetEncoding} or
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   741
     *         {@code sourceFormat} are {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    public static boolean isConversionSupported(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat) {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   744
        Objects.requireNonNull(targetEncoding);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   745
        Objects.requireNonNull(sourceFormat);
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   746
        if (sourceFormat.getEncoding().equals(targetEncoding)) {
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   747
            return true;
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   748
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   750
        List<FormatConversionProvider> codecs = getFormatConversionProviders();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        for(int i=0; i<codecs.size(); i++ ) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   753
            FormatConversionProvider codec = codecs.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            if(codec.isConversionSupported(targetEncoding,sourceFormat) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   762
     * Obtains an audio input stream of the indicated encoding, by converting
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   763
     * the provided audio input stream.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   764
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   765
     * @param  targetEncoding the desired encoding after conversion
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   766
     * @param  sourceStream the stream to be converted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @return an audio input stream of the indicated encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * @throws IllegalArgumentException if the conversion is not supported
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   769
     * @throws NullPointerException if {@code targetEncoding} or
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   770
     *         {@code sourceStream} are {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * @see #getTargetEncodings(AudioFormat.Encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @see #getTargetEncodings(AudioFormat)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @see #isConversionSupported(AudioFormat.Encoding, AudioFormat)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * @see #getAudioInputStream(AudioFormat, AudioInputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    public static AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                                                       AudioInputStream sourceStream) {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   778
        Objects.requireNonNull(targetEncoding);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   779
        Objects.requireNonNull(sourceStream);
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   780
        if (sourceStream.getFormat().getEncoding().equals(targetEncoding)) {
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   781
            return sourceStream;
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   782
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   784
        List<FormatConversionProvider> codecs = getFormatConversionProviders();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        for(int i = 0; i < codecs.size(); i++) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   787
            FormatConversionProvider codec = codecs.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            if( codec.isConversionSupported( targetEncoding, sourceStream.getFormat() ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                return codec.getAudioInputStream( targetEncoding, sourceStream );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        // we ran out of options, throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        throw new IllegalArgumentException("Unsupported conversion: " + targetEncoding + " from " + sourceStream.getFormat());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   797
     * Obtains the formats that have a particular encoding and that the system
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   798
     * can obtain from a stream of the specified format using the set of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * installed format converters.
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   800
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   801
     * @param  targetEncoding the desired encoding after conversion
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   802
     * @param  sourceFormat the audio format before conversion
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   803
     * @return array of formats. If no formats of the specified encoding are
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   804
     *         supported, an array of length 0 is returned.
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   805
     * @throws NullPointerException if {@code targetEncoding} or
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   806
     *         {@code sourceFormat} are {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    public static AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat) {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   809
        Objects.requireNonNull(targetEncoding);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   810
        Objects.requireNonNull(sourceFormat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   812
        List<FormatConversionProvider> codecs = getFormatConversionProviders();
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   813
        List<AudioFormat> formats = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   815
        boolean matchFound = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        // gather from all the codecs
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   817
        for (final FormatConversionProvider codec : codecs) {
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   818
            AudioFormat[] elements = codec
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   819
                    .getTargetFormats(targetEncoding, sourceFormat);
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   820
            for (AudioFormat format : elements) {
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   821
                formats.add(format);
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   822
                if (sourceFormat.matches(format)) {
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   823
                    matchFound = true;
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   824
                }
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   825
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   828
        if (targetEncoding.equals(sourceFormat.getEncoding())) {
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   829
            if (!matchFound) {
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   830
                formats.add(sourceFormat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   833
        return formats.toArray(new AudioFormat[formats.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   837
     * Indicates whether an audio input stream of a specified format can be
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   838
     * obtained from an audio input stream of another specified format.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   839
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   840
     * @param  targetFormat the desired audio format after conversion
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   841
     * @param  sourceFormat the audio format before conversion
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   842
     * @return {@code true} if the conversion is supported, otherwise
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   843
     *         {@code false}
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   844
     * @throws NullPointerException if {@code targetFormat} or
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   845
     *         {@code sourceFormat} are {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    public static boolean isConversionSupported(AudioFormat targetFormat, AudioFormat sourceFormat) {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   848
        Objects.requireNonNull(targetFormat);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   849
        Objects.requireNonNull(sourceFormat);
35684
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   850
        if (sourceFormat.matches(targetFormat)) {
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   851
            return true;
900e28dae561 8146144: Incorrect behaviour of AudioSystem.getTargetFormats/getTargetEncodings/isConversionSupported
serb
parents: 35683
diff changeset
   852
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   854
        List<FormatConversionProvider> codecs = getFormatConversionProviders();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        for(int i=0; i<codecs.size(); i++ ) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   857
            FormatConversionProvider codec = codecs.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            if(codec.isConversionSupported(targetFormat, sourceFormat) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * Obtains an audio input stream of the indicated format, by converting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * provided audio input stream.
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   868
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   869
     * @param  targetFormat the desired audio format after conversion
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   870
     * @param  sourceStream the stream to be converted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * @return an audio input stream of the indicated format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @throws IllegalArgumentException if the conversion is not supported
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   873
     * @throws NullPointerException if {@code targetFormat} or
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   874
     *         {@code sourceStream} are {@code null}
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   875
     * @see #getTargetEncodings(AudioFormat)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @see #getTargetFormats(AudioFormat.Encoding, AudioFormat)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * @see #isConversionSupported(AudioFormat, AudioFormat)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * @see #getAudioInputStream(AudioFormat.Encoding, AudioInputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    public static AudioInputStream getAudioInputStream(AudioFormat targetFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                                                       AudioInputStream sourceStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        if (sourceStream.getFormat().matches(targetFormat)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            return sourceStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   886
        List<FormatConversionProvider> codecs = getFormatConversionProviders();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        for(int i = 0; i < codecs.size(); i++) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
   889
            FormatConversionProvider codec = codecs.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            if(codec.isConversionSupported(targetFormat,sourceStream.getFormat()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                return codec.getAudioInputStream(targetFormat,sourceStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        // we ran out of options...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        throw new IllegalArgumentException("Unsupported conversion: " + targetFormat + " from " + sourceStream.getFormat());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   900
     * Obtains the audio file format of the provided input stream. The stream
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   901
     * must point to valid audio file data. The implementation of this method
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   902
     * may require multiple parsers to examine the stream to determine whether
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   903
     * they support it. These parsers must be able to mark the stream, read
32663
1279321775f1 8133677: Specification of AudioFileReader should be clarifed
serb
parents: 27565
diff changeset
   904
     * enough data to determine whether they support the stream, and reset the
1279321775f1 8133677: Specification of AudioFileReader should be clarifed
serb
parents: 27565
diff changeset
   905
     * stream's read pointer to its original position. If the input stream does
1279321775f1 8133677: Specification of AudioFileReader should be clarifed
serb
parents: 27565
diff changeset
   906
     * not support these operations, this method may fail with an
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   907
     * {@code IOException}.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   908
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   909
     * @param  stream the input stream from which file format information should
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   910
     *         be extracted
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   911
     * @return an {@code AudioFileFormat} object describing the stream's audio
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   912
     *         file format
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   913
     * @throws UnsupportedAudioFileException if the stream does not point to
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   914
     *         valid audio file data recognized by the system
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * @throws IOException if an input/output exception occurs
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   916
     * @throws NullPointerException if {@code stream} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @see InputStream#markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * @see InputStream#mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     */
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
   920
    public static AudioFileFormat getAudioFileFormat(final InputStream stream)
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   921
            throws UnsupportedAudioFileException, IOException {
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   922
        Objects.requireNonNull(stream);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
   924
        for (final AudioFileReader reader : getAudioFileReaders()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            try {
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
   926
                return reader.getAudioFileFormat(stream);
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
   927
            } catch (final UnsupportedAudioFileException ignored) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        }
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
   930
        throw new UnsupportedAudioFileException("Stream of unsupported format");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   934
     * Obtains the audio file format of the specified URL. The URL must point to
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   935
     * valid audio file data.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   936
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   937
     * @param  url the URL from which file format information should be
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   938
     *         extracted
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   939
     * @return an {@code AudioFileFormat} object describing the audio file
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   940
     *         format
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   941
     * @throws UnsupportedAudioFileException if the URL does not point to valid
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   942
     *         audio file data recognized by the system
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * @throws IOException if an input/output exception occurs
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   944
     * @throws NullPointerException if {@code url} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     */
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
   946
    public static AudioFileFormat getAudioFileFormat(final URL url)
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   947
            throws UnsupportedAudioFileException, IOException {
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   948
        Objects.requireNonNull(url);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
   950
        for (final AudioFileReader reader : getAudioFileReaders()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            try {
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
   952
                return reader.getAudioFileFormat(url);
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
   953
            } catch (final UnsupportedAudioFileException ignored) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        }
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
   956
        throw new UnsupportedAudioFileException("URL of unsupported format");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   960
     * Obtains the audio file format of the specified {@code File}. The
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   961
     * {@code File} must point to valid audio file data.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   962
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   963
     * @param  file the {@code File} from which file format information should
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   964
     *         be extracted
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   965
     * @return an {@code AudioFileFormat} object describing the audio file
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   966
     *         format
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   967
     * @throws UnsupportedAudioFileException if the {@code File} does not point
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   968
     *         to valid audio file data recognized by the system
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * @throws IOException if an I/O exception occurs
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   970
     * @throws NullPointerException if {@code file} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     */
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
   972
    public static AudioFileFormat getAudioFileFormat(final File file)
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   973
            throws UnsupportedAudioFileException, IOException {
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
   974
        Objects.requireNonNull(file);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
   976
        for (final AudioFileReader reader : getAudioFileReaders()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            try {
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
   978
                return reader.getAudioFileFormat(file);
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
   979
            } catch (final UnsupportedAudioFileException ignored) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        }
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
   982
        throw new UnsupportedAudioFileException("File of unsupported format");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   986
     * Obtains an audio input stream from the provided input stream. The stream
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   987
     * must point to valid audio file data. The implementation of this method
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   988
     * may require multiple parsers to examine the stream to determine whether
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   989
     * they support it. These parsers must be able to mark the stream, read
32663
1279321775f1 8133677: Specification of AudioFileReader should be clarifed
serb
parents: 27565
diff changeset
   990
     * enough data to determine whether they support the stream, and reset the
1279321775f1 8133677: Specification of AudioFileReader should be clarifed
serb
parents: 27565
diff changeset
   991
     * stream's read pointer to its original position. If the input stream does
1279321775f1 8133677: Specification of AudioFileReader should be clarifed
serb
parents: 27565
diff changeset
   992
     * not support these operation, this method may fail with an
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   993
     * {@code IOException}.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   994
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   995
     * @param  stream the input stream from which the {@code AudioInputStream}
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   996
     *         should be constructed
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   997
     * @return an {@code AudioInputStream} object based on the audio file data
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   998
     *         contained in the input stream
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
   999
     * @throws UnsupportedAudioFileException if the stream does not point to
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1000
     *         valid audio file data recognized by the system
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * @throws IOException if an I/O exception occurs
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1002
     * @throws NullPointerException if {@code stream} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * @see InputStream#markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * @see InputStream#mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     */
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
  1006
    public static AudioInputStream getAudioInputStream(final InputStream stream)
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1007
            throws UnsupportedAudioFileException, IOException {
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1008
        Objects.requireNonNull(stream);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
  1010
        for (final AudioFileReader reader : getAudioFileReaders()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            try {
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
  1012
                return reader.getAudioInputStream(stream);
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
  1013
            } catch (final UnsupportedAudioFileException ignored) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
  1016
        throw new UnsupportedAudioFileException("Stream of unsupported format");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1020
     * Obtains an audio input stream from the URL provided. The URL must point
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1021
     * to valid audio file data.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1022
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1023
     * @param  url the URL for which the {@code AudioInputStream} should be
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1024
     *         constructed
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1025
     * @return an {@code AudioInputStream} object based on the audio file data
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1026
     *         pointed to by the URL
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1027
     * @throws UnsupportedAudioFileException if the URL does not point to valid
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1028
     *         audio file data recognized by the system
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @throws IOException if an I/O exception occurs
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1030
     * @throws NullPointerException if {@code url} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     */
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
  1032
    public static AudioInputStream getAudioInputStream(final URL url)
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1033
            throws UnsupportedAudioFileException, IOException {
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1034
        Objects.requireNonNull(url);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
  1036
        for (final AudioFileReader reader : getAudioFileReaders()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            try {
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
  1038
                return reader.getAudioInputStream(url);
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
  1039
            } catch (final UnsupportedAudioFileException ignored) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        }
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
  1042
        throw new UnsupportedAudioFileException("URL of unsupported format");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1046
     * Obtains an audio input stream from the provided {@code File}. The
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1047
     * {@code File} must point to valid audio file data.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1048
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1049
     * @param  file the {@code File} for which the {@code AudioInputStream}
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1050
     *         should be constructed
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1051
     * @return an {@code AudioInputStream} object based on the audio file data
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1052
     *         pointed to by the {@code File}
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1053
     * @throws UnsupportedAudioFileException if the {@code File} does not point
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1054
     *         to valid audio file data recognized by the system
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * @throws IOException if an I/O exception occurs
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1056
     * @throws NullPointerException if {@code file} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     */
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
  1058
    public static AudioInputStream getAudioInputStream(final File file)
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1059
            throws UnsupportedAudioFileException, IOException {
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1060
        Objects.requireNonNull(file);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
  1062
        for (final AudioFileReader reader : getAudioFileReaders()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            try {
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
  1064
                return reader.getAudioInputStream(file);
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
  1065
            } catch (final UnsupportedAudioFileException ignored) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        }
35683
d8a9be6bd7db 8131974: AudioFileReader incorrectly handle EOFException
serb
parents: 34412
diff changeset
  1068
        throw new UnsupportedAudioFileException("File of unsupported format");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1072
     * Obtains the file types for which file writing support is provided by the
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1073
     * system.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1074
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1075
     * @return array of unique file types. If no file types are supported, an
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1076
     *         array of length 0 is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    public static AudioFileFormat.Type[] getAudioFileTypes() {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1079
        List<AudioFileWriter> providers = getAudioFileWriters();
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1080
        Set<AudioFileFormat.Type> returnTypesSet = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        for(int i=0; i < providers.size(); i++) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1083
            AudioFileWriter writer = providers.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            AudioFileFormat.Type[] fileTypes = writer.getAudioFileTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            for(int j=0; j < fileTypes.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                returnTypesSet.add(fileTypes[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        }
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1089
        AudioFileFormat.Type returnTypes[] =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            returnTypesSet.toArray(new AudioFileFormat.Type[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        return returnTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1095
     * Indicates whether file writing support for the specified file type is
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1096
     * provided by the system.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1097
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1098
     * @param  fileType the file type for which write capabilities are queried
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1099
     * @return {@code true} if the file type is supported, otherwise
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1100
     *         {@code false}
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1101
     * @throws NullPointerException if {@code fileType} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    public static boolean isFileTypeSupported(AudioFileFormat.Type fileType) {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1104
        Objects.requireNonNull(fileType);
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1105
        List<AudioFileWriter> providers = getAudioFileWriters();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        for(int i=0; i < providers.size(); i++) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1108
            AudioFileWriter writer = providers.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            if (writer.isFileTypeSupported(fileType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1117
     * Obtains the file types that the system can write from the audio input
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1118
     * stream specified.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1119
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1120
     * @param  stream the audio input stream for which audio file type
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1121
     *         support is queried
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1122
     * @return array of file types. If no file types are supported, an array of
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1123
     *         length 0 is returned.
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1124
     * @throws NullPointerException if {@code stream} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    public static AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1127
        Objects.requireNonNull(stream);
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1128
        List<AudioFileWriter> providers = getAudioFileWriters();
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1129
        Set<AudioFileFormat.Type> returnTypesSet = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        for(int i=0; i < providers.size(); i++) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1132
            AudioFileWriter writer = providers.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            AudioFileFormat.Type[] fileTypes = writer.getAudioFileTypes(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            for(int j=0; j < fileTypes.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                returnTypesSet.add(fileTypes[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        }
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1138
        AudioFileFormat.Type returnTypes[] =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            returnTypesSet.toArray(new AudioFileFormat.Type[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        return returnTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * Indicates whether an audio file of the specified file type can be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * from the indicated audio input stream.
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1146
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1147
     * @param  fileType the file type for which write capabilities are queried
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1148
     * @param  stream the stream for which file-writing support is queried
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1149
     * @return {@code true} if the file type is supported for this audio input
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1150
     *         stream, otherwise {@code false}
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1151
     * @throws NullPointerException if {@code fileType} or {@code stream} are
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1152
     *         {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    public static boolean isFileTypeSupported(AudioFileFormat.Type fileType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                                              AudioInputStream stream) {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1156
        Objects.requireNonNull(fileType);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1157
        Objects.requireNonNull(stream);
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1158
        List<AudioFileWriter> providers = getAudioFileWriters();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        for(int i=0; i < providers.size(); i++) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1161
            AudioFileWriter writer = providers.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            if(writer.isFileTypeSupported(fileType, stream)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1170
     * Writes a stream of bytes representing an audio file of the specified file
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1171
     * type to the output stream provided. Some file types require that the
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1172
     * length be written into the file header; such files cannot be written from
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1173
     * start to finish unless the length is known in advance. An attempt to
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1174
     * write a file of such a type will fail with an IOException if the length
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1175
     * in the audio file type is {@code AudioSystem.NOT_SPECIFIED}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     *
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1177
     * @param  stream the audio input stream containing audio data to be written
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1178
     *         to the file
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1179
     * @param  fileType the kind of audio file to write
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1180
     * @param  out the stream to which the file data should be written
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * @return the number of bytes written to the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * @throws IOException if an input/output exception occurs
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1183
     * @throws IllegalArgumentException if the file type is not supported by the
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1184
     *         system
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1185
     * @throws NullPointerException if {@code stream} or {@code fileType} or
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1186
     *         {@code out} are {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * @see #isFileTypeSupported
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1188
     * @see #getAudioFileTypes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     */
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1190
    public static int write(final AudioInputStream stream,
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1191
                            final AudioFileFormat.Type fileType,
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1192
                            final OutputStream out) throws IOException {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1193
        Objects.requireNonNull(stream);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1194
        Objects.requireNonNull(fileType);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1195
        Objects.requireNonNull(out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1197
        for (final AudioFileWriter writer : getAudioFileWriters()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            try {
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1199
                return writer.write(stream, fileType, out);
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1200
            } catch (final IllegalArgumentException ignored) {
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1201
                // thrown if this provider cannot write the stream, try next
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        }
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1204
        // "File type " + type + " not supported."
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1205
        throw new IllegalArgumentException(
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1206
                "could not write audio file: file type not supported: "
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1207
                        + fileType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1211
     * Writes a stream of bytes representing an audio file of the specified file
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1212
     * type to the external file provided.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1213
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1214
     * @param  stream the audio input stream containing audio data to be written
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1215
     *         to the file
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1216
     * @param  fileType the kind of audio file to write
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1217
     * @param  out the external file to which the file data should be written
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * @return the number of bytes written to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * @throws IOException if an I/O exception occurs
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1220
     * @throws IllegalArgumentException if the file type is not supported by the
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1221
     *         system
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1222
     * @throws NullPointerException if {@code stream} or {@code fileType} or
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1223
     *         {@code out} are {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * @see #isFileTypeSupported
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1225
     * @see #getAudioFileTypes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     */
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1227
    public static int write(final AudioInputStream stream,
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1228
                            final AudioFileFormat.Type fileType,
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1229
                            final File out) throws IOException {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1230
        Objects.requireNonNull(stream);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1231
        Objects.requireNonNull(fileType);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32663
diff changeset
  1232
        Objects.requireNonNull(out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1234
        for (final AudioFileWriter writer : getAudioFileWriters()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            try {
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1236
                return writer.write(stream, fileType, out);
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1237
            } catch (final IllegalArgumentException ignored) {
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1238
                // thrown if this provider cannot write the stream, try next
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        }
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1241
        throw new IllegalArgumentException(
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1242
                "could not write audio file: file type not supported: "
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35684
diff changeset
  1243
                        + fileType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    // METHODS FOR INTERNAL IMPLEMENTATION USE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * Obtains the set of MixerProviders on the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     */
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1251
    @SuppressWarnings("unchecked")
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1252
    private static List<MixerProvider> getMixerProviders() {
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1253
        return (List<MixerProvider>) getProviders(MixerProvider.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1257
     * Obtains the set of format converters (codecs, transcoders, etc.) that are
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1258
     * currently installed on the system.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1259
     *
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
  1260
     * @return an array of {@link FormatConversionProvider} objects representing
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
  1261
     *         the available format converters. If no format converters readers
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
  1262
     *         are available on the system, an array of length 0 is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     */
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1264
    @SuppressWarnings("unchecked")
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1265
    private static List<FormatConversionProvider> getFormatConversionProviders() {
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1266
        return (List<FormatConversionProvider>) getProviders(FormatConversionProvider.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1270
     * Obtains the set of audio file readers that are currently installed on the
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1271
     * system.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1272
     *
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
  1273
     * @return a List of {@link AudioFileReader} objects representing the
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
  1274
     *         installed audio file readers. If no audio file readers are
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
  1275
     *         available on the system, an empty List is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     */
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1277
    @SuppressWarnings("unchecked")
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1278
    private static List<AudioFileReader> getAudioFileReaders() {
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1279
        return (List<AudioFileReader>)getProviders(AudioFileReader.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1283
     * Obtains the set of audio file writers that are currently installed on the
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1284
     * system.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1285
     *
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
  1286
     * @return a List of {@link AudioFileWriter} objects representing the
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
  1287
     *         available audio file writers. If no audio file writers are
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
  1288
     *         available on the system, an empty List is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     */
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1290
    @SuppressWarnings("unchecked")
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1291
    private static List<AudioFileWriter> getAudioFileWriters() {
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1292
        return (List<AudioFileWriter>)getProviders(AudioFileWriter.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1295
    /**
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1296
     * Attempts to locate and return a default Mixer that provides lines of the
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1297
     * specified type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     *
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1299
     * @param  providers the installed mixer providers
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1300
     * @param  info The requested line type TargetDataLine.class, Clip.class or
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1301
     *         Port.class
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1302
     * @return a Mixer that matches the requirements, or null if no default
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1303
     *         mixer found
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     */
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1305
    private static Mixer getDefaultMixer(List<MixerProvider> providers, Line.Info info) {
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1306
        Class<?> lineClass = info.getLineClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        String providerClassName = JDK13Services.getDefaultProviderClassName(lineClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        String instanceName = JDK13Services.getDefaultInstanceName(lineClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        Mixer mixer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        if (providerClassName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            MixerProvider defaultProvider = getNamedProvider(providerClassName, providers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            if (defaultProvider != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                if (instanceName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                    mixer = getNamedMixer(instanceName, defaultProvider, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                    if (mixer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                        return mixer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                    mixer = getFirstMixer(defaultProvider, info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                                          false /* mixing not required*/);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                    if (mixer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                        return mixer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        /* Provider class not specified or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
           provider class cannot be found, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
           provider class and instance specified and instance cannot be found or is not appropriate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        if (instanceName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            mixer = getNamedMixer(instanceName, providers, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            if (mixer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                return mixer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        /* No default are specified, or if something is specified, everything
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
           failed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1346
    /**
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1347
     * Return a MixerProvider of a given class from the list of MixerProviders.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1348
     * This method never requires the returned Mixer to do mixing.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1349
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1350
     * @param  providerClassName The class name of the provider to be returned
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1351
     * @param  providers The list of MixerProviders that is searched
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1352
     * @return A MixerProvider of the requested class, or null if none is found
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    private static MixerProvider getNamedProvider(String providerClassName,
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1355
                                                  List<MixerProvider> providers) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        for(int i = 0; i < providers.size(); i++) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1357
            MixerProvider provider = providers.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            if (provider.getClass().getName().equals(providerClassName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1365
    /**
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1366
     * Return a Mixer with a given name from a given MixerProvider. This method
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1367
     * never requires the returned Mixer to do mixing.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1368
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1369
     * @param  mixerName The name of the Mixer to be returned
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1370
     * @param  provider The MixerProvider to check for Mixers
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1371
     * @param  info The type of line the returned Mixer is required to support
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1372
     * @return A Mixer matching the requirements, or null if none is found
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    private static Mixer getNamedMixer(String mixerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                                       MixerProvider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                                       Line.Info info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        Mixer.Info[] infos = provider.getMixerInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        for (int i = 0; i < infos.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            if (infos[i].getName().equals(mixerName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                Mixer mixer = provider.getMixer(infos[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                if (isAppropriateMixer(mixer, info, false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                    return mixer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1389
    /**
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1390
     * From a List of MixerProviders, return a Mixer with a given name. This
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1391
     * method never requires the returned Mixer to do mixing.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1392
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1393
     * @param  mixerName The name of the Mixer to be returned
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1394
     * @param  providers The List of MixerProviders to check for Mixers
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1395
     * @param  info The type of line the returned Mixer is required to support
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1396
     * @return A Mixer matching the requirements, or null if none is found
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
    private static Mixer getNamedMixer(String mixerName,
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1399
                                       List<MixerProvider> providers,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                                       Line.Info info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        for(int i = 0; i < providers.size(); i++) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1402
            MixerProvider provider = providers.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            Mixer mixer = getNamedMixer(mixerName, provider, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
            if (mixer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                return mixer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1411
    /**
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1412
     * From a given MixerProvider, return the first appropriate Mixer.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1413
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1414
     * @param  provider The MixerProvider to check for Mixers
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1415
     * @param  info The type of line the returned Mixer is required to support
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1416
     * @param  isMixingRequired If true, only Mixers that support mixing are
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1417
     *         returned for line types of SourceDataLine and Clip
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1418
     * @return A Mixer that is considered appropriate, or null if none is found
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    private static Mixer getFirstMixer(MixerProvider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                                       Line.Info info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                                       boolean isMixingRequired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        Mixer.Info[] infos = provider.getMixerInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        for (int j = 0; j < infos.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
            Mixer mixer = provider.getMixer(infos[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            if (isAppropriateMixer(mixer, info, isMixingRequired)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                return mixer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1433
    /**
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1434
     * Checks if a Mixer is appropriate. A Mixer is considered appropriate if it
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1435
     * support the given line type. If isMixingRequired is true and the line
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1436
     * type is an output one (SourceDataLine, Clip), the mixer is appropriate if
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1437
     * it supports at least 2 (concurrent) lines of the given type.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1438
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1439
     * @return {@code true} if the mixer is considered appropriate according to
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1440
     *         the rules given above, {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    private static boolean isAppropriateMixer(Mixer mixer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                                              Line.Info lineInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                                              boolean isMixingRequired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        if (! mixer.isLineSupported(lineInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        }
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1448
        Class<?> lineClass = lineInfo.getLineClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        if (isMixingRequired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            && (SourceDataLine.class.isAssignableFrom(lineClass) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                Clip.class.isAssignableFrom(lineClass))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            int maxLines = mixer.getMaxLines(lineInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            return ((maxLines == NOT_SPECIFIED) || (maxLines > 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1459
     * Like getMixerInfo, but return List.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     */
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1461
    private static List<Mixer.Info> getMixerInfoList() {
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1462
        List<MixerProvider> providers = getMixerProviders();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        return getMixerInfoList(providers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1467
     * Like getMixerInfo, but return List.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     */
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1469
    private static List<Mixer.Info> getMixerInfoList(List<MixerProvider> providers) {
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1470
        List<Mixer.Info> infos = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        Mixer.Info[] someInfos; // per-mixer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        Mixer.Info[] allInfos;  // for all mixers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        for(int i = 0; i < providers.size(); i++ ) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1476
            someInfos = providers.get(i).getMixerInfo();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            for (int j = 0; j < someInfos.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                infos.add(someInfos[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        return infos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    /**
25128
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1487
     * Obtains the set of services currently installed on the system using the
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1488
     * SPI mechanism in 1.3.
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1489
     *
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1490
     * @return a List of instances of providers for the requested service. If no
2dfdfa369071 8043979: Javadoc cleanup of javax.sound.sampled package
serb
parents: 22959
diff changeset
  1491
     *         providers are available, a vector of length 0 will be returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     */
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 25128
diff changeset
  1493
    private static List<?> getProviders(Class<?> providerClass) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        return JDK13Services.getProviders(providerClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
}