Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/sound/sampled/FloatControl.java
38918 views
/*1* Copyright (c) 1999, 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sound.sampled;2627/**28* A <code>FloatControl</code> object provides control over a range of29* floating-point values. Float controls are often30* represented in graphical user interfaces by continuously31* adjustable objects such as sliders or rotary knobs. Concrete subclasses32* of <code>FloatControl</code> implement controls, such as gain and pan, that33* affect a line's audio signal in some way that an application can manipulate.34* The <code>{@link FloatControl.Type}</code>35* inner class provides static instances of types that are used to36* identify some common kinds of float control.37* <p>38* The <code>FloatControl</code> abstract class provides methods to set and get39* the control's current floating-point value. Other methods obtain the possible40* range of values and the control's resolution (the smallest increment between41* returned values). Some float controls allow ramping to a42* new value over a specified period of time. <code>FloatControl</code> also43* includes methods that return string labels for the minimum, maximum, and midpoint44* positions of the control.45*46* @see Line#getControls47* @see Line#isControlSupported48*49* @author David Rivas50* @author Kara Kytle51* @since 1.352*/53public abstract class FloatControl extends Control {545556// INSTANCE VARIABLES575859// FINAL VARIABLES6061/**62* The minimum supported value.63*/64private float minimum;6566/**67* The maximum supported value.68*/69private float maximum;7071/**72* The control's precision.73*/74private float precision;7576/**77* The smallest time increment in which a value change78* can be effected during a value shift, in microseconds.79*/80private int updatePeriod;818283/**84* A label for the units in which the control values are expressed,85* such as "dB" for decibels.86*/87private final String units;8889/**90* A label for the minimum value, such as "Left."91*/92private final String minLabel;9394/**95* A label for the maximum value, such as "Right."96*/97private final String maxLabel;9899/**100* A label for the mid-point value, such as "Center."101*/102private final String midLabel;103104105// STATE VARIABLES106107/**108* The current value.109*/110private float value;111112113114// CONSTRUCTORS115116117/**118* Constructs a new float control object with the given parameters119*120* @param type the kind of control represented by this float control object121* @param minimum the smallest value permitted for the control122* @param maximum the largest value permitted for the control123* @param precision the resolution or granularity of the control.124* This is the size of the increment between discrete valid values.125* @param updatePeriod the smallest time interval, in microseconds, over which the control126* can change from one discrete value to the next during a {@link #shift(float,float,int) shift}127* @param initialValue the value that the control starts with when constructed128* @param units the label for the units in which the control's values are expressed,129* such as "dB" or "frames per second"130* @param minLabel the label for the minimum value, such as "Left" or "Off"131* @param midLabel the label for the midpoint value, such as "Center" or "Default"132* @param maxLabel the label for the maximum value, such as "Right" or "Full"133*134* @throws IllegalArgumentException if {@code minimum} is greater135* than {@code maximum} or {@code initialValue} does not fall136* within the allowable range137*/138protected FloatControl(Type type, float minimum, float maximum,139float precision, int updatePeriod, float initialValue,140String units, String minLabel, String midLabel, String maxLabel) {141142super(type);143144if (minimum > maximum) {145throw new IllegalArgumentException("Minimum value " + minimum146+ " exceeds maximum value " + maximum + ".");147}148if (initialValue < minimum) {149throw new IllegalArgumentException("Initial value " + initialValue150+ " smaller than allowable minimum value " + minimum + ".");151}152if (initialValue > maximum) {153throw new IllegalArgumentException("Initial value " + initialValue154+ " exceeds allowable maximum value " + maximum + ".");155}156157158this.minimum = minimum;159this.maximum = maximum;160161this.precision = precision;162this.updatePeriod = updatePeriod;163this.value = initialValue;164165this.units = units;166this.minLabel = ( (minLabel == null) ? "" : minLabel);167this.midLabel = ( (midLabel == null) ? "" : midLabel);168this.maxLabel = ( (maxLabel == null) ? "" : maxLabel);169}170171172/**173* Constructs a new float control object with the given parameters.174* The labels for the minimum, maximum, and mid-point values are set175* to zero-length strings.176*177* @param type the kind of control represented by this float control object178* @param minimum the smallest value permitted for the control179* @param maximum the largest value permitted for the control180* @param precision the resolution or granularity of the control.181* This is the size of the increment between discrete valid values.182* @param updatePeriod the smallest time interval, in microseconds, over which the control183* can change from one discrete value to the next during a {@link #shift(float,float,int) shift}184* @param initialValue the value that the control starts with when constructed185* @param units the label for the units in which the control's values are expressed,186* such as "dB" or "frames per second"187*188* @throws IllegalArgumentException if {@code minimum} is greater189* than {@code maximum} or {@code initialValue} does not fall190* within the allowable range191*/192protected FloatControl(Type type, float minimum, float maximum,193float precision, int updatePeriod, float initialValue, String units) {194this(type, minimum, maximum, precision, updatePeriod,195initialValue, units, "", "", "");196}197198199200// METHODS201202203/**204* Sets the current value for the control. The default implementation205* simply sets the value as indicated. If the value indicated is greater206* than the maximum value, or smaller than the minimum value, an207* IllegalArgumentException is thrown.208* Some controls require that their line be open before they can be affected209* by setting a value.210* @param newValue desired new value211* @throws IllegalArgumentException if the value indicated does not fall212* within the allowable range213*/214public void setValue(float newValue) {215216if (newValue > maximum) {217throw new IllegalArgumentException("Requested value " + newValue + " exceeds allowable maximum value " + maximum + ".");218}219220if (newValue < minimum) {221throw new IllegalArgumentException("Requested value " + newValue + " smaller than allowable minimum value " + minimum + ".");222}223224value = newValue;225}226227228/**229* Obtains this control's current value.230* @return the current value231*/232public float getValue() {233return value;234}235236237/**238* Obtains the maximum value permitted.239* @return the maximum allowable value240*/241public float getMaximum() {242return maximum;243}244245246/**247* Obtains the minimum value permitted.248* @return the minimum allowable value249*/250public float getMinimum() {251return minimum;252}253254255/**256* Obtains the label for the units in which the control's values are expressed,257* such as "dB" or "frames per second."258* @return the units label, or a zero-length string if no label259*/260public String getUnits() {261return units;262}263264265/**266* Obtains the label for the minimum value, such as "Left" or "Off."267* @return the minimum value label, or a zero-length string if no label * has been set268*/269public String getMinLabel() {270return minLabel;271}272273274/**275* Obtains the label for the mid-point value, such as "Center" or "Default."276* @return the mid-point value label, or a zero-length string if no label * has been set277*/278public String getMidLabel() {279return midLabel;280}281282283/**284* Obtains the label for the maximum value, such as "Right" or "Full."285* @return the maximum value label, or a zero-length string if no label * has been set286*/287public String getMaxLabel() {288return maxLabel;289}290291292/**293* Obtains the resolution or granularity of the control, in the units294* that the control measures.295* The precision is the size of the increment between discrete valid values296* for this control, over the set of supported floating-point values.297* @return the control's precision298*/299public float getPrecision() {300return precision;301}302303304/**305* Obtains the smallest time interval, in microseconds, over which the control's value can306* change during a shift. The update period is the inverse of the frequency with which307* the control updates its value during a shift. If the implementation does not support value shifting over308* time, it should set the control's value to the final value immediately309* and return -1 from this method.310*311* @return update period in microseconds, or -1 if shifting over time is unsupported312* @see #shift313*/314public int getUpdatePeriod() {315return updatePeriod;316}317318319/**320* Changes the control value from the initial value to the final321* value linearly over the specified time period, specified in microseconds.322* This method returns without blocking; it does not wait for the shift323* to complete. An implementation should complete the operation within the time324* specified. The default implementation simply changes the value325* to the final value immediately.326*327* @param from initial value at the beginning of the shift328* @param to final value after the shift329* @param microseconds maximum duration of the shift in microseconds330*331* @throws IllegalArgumentException if either {@code from} or {@code to}332* value does not fall within the allowable range333*334* @see #getUpdatePeriod335*/336public void shift(float from, float to, int microseconds) {337// test "from" value, "to" value will be tested by setValue()338if (from < minimum) {339throw new IllegalArgumentException("Requested value " + from340+ " smaller than allowable minimum value " + minimum + ".");341}342if (from > maximum) {343throw new IllegalArgumentException("Requested value " + from344+ " exceeds allowable maximum value " + maximum + ".");345}346setValue(to);347}348349350// ABSTRACT METHOD IMPLEMENTATIONS: CONTROL351352353/**354* Provides a string representation of the control355* @return a string description356*/357public String toString() {358return new String(getType() + " with current value: " + getValue() + " " + units +359" (range: " + minimum + " - " + maximum + ")");360}361362363// INNER CLASSES364365366/**367* An instance of the <code>FloatControl.Type</code> inner class identifies one kind of368* float control. Static instances are provided for the369* common types.370*371* @author Kara Kytle372* @since 1.3373*/374public static class Type extends Control.Type {375376377// TYPE DEFINES378379380// GAIN TYPES381382/**383* Represents a control for the overall gain on a line.384* <p>385* Gain is a quantity in decibels (dB) that is added to the intrinsic386* decibel level of the audio signal--that is, the level of387* the signal before it is altered by the gain control. A positive388* gain amplifies (boosts) the signal's volume, and a negative gain389* attenuates (cuts) it.390* The gain setting defaults to a value of 0.0 dB, meaning the signal's391* loudness is unaffected. Note that gain measures dB, not amplitude.392* The relationship between a gain in decibels and the corresponding393* linear amplitude multiplier is:394*395*<CENTER><CODE> linearScalar = pow(10.0, gainDB/20.0) </CODE></CENTER>396* <p>397* The <code>FloatControl</code> class has methods to impose a maximum and398* minimum allowable value for gain. However, because an audio signal might399* already be at a high amplitude, the maximum setting does not guarantee400* that the signal will be undistorted when the gain is applied to it (unless401* the maximum is zero or negative). To avoid numeric overflow from excessively402* large gain settings, a gain control can implement403* clipping, meaning that the signal's amplitude will be limited to the maximum404* value representable by its audio format, instead of wrapping around.405* <p>406* These comments apply to gain controls in general, not just master gain controls.407* A line can have more than one gain control. For example, a mixer (which is408* itself a line) might have a master gain control, an auxiliary return control,409* a reverb return control, and, on each of its source lines, an individual aux410* send and reverb send.411*412* @see #AUX_SEND413* @see #AUX_RETURN414* @see #REVERB_SEND415* @see #REVERB_RETURN416* @see #VOLUME417*/418public static final Type MASTER_GAIN = new Type("Master Gain");419420/**421* Represents a control for the auxiliary send gain on a line.422*423* @see #MASTER_GAIN424* @see #AUX_RETURN425*/426public static final Type AUX_SEND = new Type("AUX Send");427428/**429* Represents a control for the auxiliary return gain on a line.430*431* @see #MASTER_GAIN432* @see #AUX_SEND433*/434public static final Type AUX_RETURN = new Type("AUX Return");435436/**437* Represents a control for the pre-reverb gain on a line.438* This control may be used to affect how much439* of a line's signal is directed to a mixer's internal reverberation unit.440*441* @see #MASTER_GAIN442* @see #REVERB_RETURN443* @see EnumControl.Type#REVERB444*/445public static final Type REVERB_SEND = new Type("Reverb Send");446447/**448* Represents a control for the post-reverb gain on a line.449* This control may be used to control the relative amplitude450* of the signal returned from an internal reverberation unit.451*452* @see #MASTER_GAIN453* @see #REVERB_SEND454*/455public static final Type REVERB_RETURN = new Type("Reverb Return");456457458// VOLUME459460/**461* Represents a control for the volume on a line.462*/463/*464* $$kk: 08.30.99: ISSUE: what units? linear or dB?465*/466public static final Type VOLUME = new Type("Volume");467468469// PAN470471/**472* Represents a control for the relative pan (left-right positioning)473* of the signal. The signal may be mono; the pan setting affects how474* it is distributed by the mixer in a stereo mix. The valid range of values is -1.0475* (left channel only) to 1.0 (right channel476* only). The default is 0.0 (centered).477*478* @see #BALANCE479*/480public static final Type PAN = new Type("Pan");481482483// BALANCE484485/**486* Represents a control for the relative balance of a stereo signal487* between two stereo speakers. The valid range of values is -1.0 (left channel only) to 1.0 (right channel488* only). The default is 0.0 (centered).489*490* @see #PAN491*/492public static final Type BALANCE = new Type("Balance");493494495// SAMPLE RATE496497/**498* Represents a control that changes the sample rate of audio playback. The net effect499* of changing the sample rate depends on the relationship between500* the media's natural rate and the rate that is set via this control.501* The natural rate is the sample rate that is specified in the data line's502* <code>AudioFormat</code> object. For example, if the natural rate503* of the media is 11025 samples per second and the sample rate is set504* to 22050 samples per second, the media will play back at twice the505* normal speed.506* <p>507* Changing the sample rate with this control does not affect the data line's508* audio format. Also note that whenever you change a sound's sample rate, a509* change in the sound's pitch results. For example, doubling the sample510* rate has the effect of doubling the frequencies in the sound's spectrum,511* which raises the pitch by an octave.512*/513public static final Type SAMPLE_RATE = new Type("Sample Rate");514515516// CONSTRUCTOR517518/**519* Constructs a new float control type.520* @param name the name of the new float control type521*/522protected Type(String name) {523super(name);524}525526} // class Type527528} // class FloatControl529530531