Path: blob/master/src/java.desktop/share/classes/javax/sound/sampled/LineEvent.java
41159 views
/*1* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sound.sampled;2627import java.io.Serial;28import java.util.EventObject;2930/**31* The {@code LineEvent} class encapsulates information that a line sends its32* listeners whenever the line opens, closes, starts, or stops. Each of these33* four state changes is represented by a corresponding type of event. A34* listener receives the event as a parameter to its35* {@link LineListener#update update} method. By querying the event, the36* listener can learn the type of event, the line responsible for the event, and37* how much data the line had processed when the event occurred.38* <p>39* Although this class implements Serializable, attempts to serialize a40* {@code LineEvent} object will fail.41*42* @author Kara Kytle43* @see Line44* @see LineListener#update45* @since 1.346*47* @serial exclude48*/49public class LineEvent extends EventObject {5051/**52* Use serialVersionUID from JDK 1.3 for interoperability.53*/54@Serial55private static final long serialVersionUID = -1274246333383880410L;5657/**58* The kind of line event ({@code OPEN}, {@code CLOSE}, {@code START}, or59* {@code STOP}).60*61* @see #getType62* @serial63*/64@SuppressWarnings("serial") // Not statically typed as Serializable65private final Type type;6667/**68* The media position when the event occurred, expressed in sample frames.69* Note that this field is only relevant to certain events generated by data70* lines, such as {@code START} and {@code STOP}. For events generated by71* lines that do not count sample frames, and for any other events for which72* this value is not known, the position value should be73* {@link AudioSystem#NOT_SPECIFIED}.74*75* @see #getFramePosition76* @serial77*/78private final long position;7980/**81* Constructs a new event of the specified type, originating from the82* specified line.83*84* @param line the source of this event85* @param type the event type ({@code OPEN}, {@code CLOSE}, {@code START},86* or {@code STOP})87* @param position the number of sample frames that the line had already88* processed when the event occurred, or89* {@link AudioSystem#NOT_SPECIFIED}90* @throws IllegalArgumentException if {@code line} is {@code null}91*/92public LineEvent(Line line, Type type, long position) {9394super(line);95this.type = type;96this.position = position;97}9899/**100* Obtains the audio line that is the source of this event.101*102* @return the line responsible for this event103*/104public final Line getLine() {105return (Line)getSource();106}107108/**109* Obtains the event's type.110*111* @return this event's type ({@link Type#OPEN}, {@link Type#CLOSE},112* {@link Type#START}, or {@link Type#STOP})113*/114public final Type getType() {115return type;116}117118/**119* Obtains the position in the line's audio data when the event occurred,120* expressed in sample frames. For example, if a source line had already121* played back 14 sample frames at the time it was paused, the pause event122* would report the line's position as 14. The next frame to be processed123* would be frame number 14 using zero-based numbering, or 15 using124* one-based numbering.125* <p>126* Note that this field is relevant only to certain events generated by data127* lines, such as {@code START} and {@code STOP}. For events generated by128* lines that do not count sample frames, and for any other events for which129* this value is not known, the position value should be130* {@link AudioSystem#NOT_SPECIFIED}.131*132* @return the line's position as a sample frame number133*/134/*135* $$kk: 04.20.99: note to myself: should make sure our implementation is136* consistent with this.137* which is a reasonable definition....138*/139public final long getFramePosition() {140return position;141}142143/**144* Returns a string representation of the event.145*146* @return a string representation of the event147*/148@Override149public String toString() {150return String.format("%s event from line %s", type, getLine());151}152153/**154* The LineEvent.Type inner class identifies what kind of event occurred on155* a line. Static instances are provided for the common types (OPEN, CLOSE,156* START, and STOP).157*158* @see LineEvent#getType()159*/160public static class Type {161162/**163* Type name.164*/165private final String name;166167/**168* Constructs a new event type.169*170* @param name name of the type171*/172protected Type(String name) {173this.name = name;174}175176//$$fb 2002-11-26: fix for 4695001: SPEC: description of equals() method contains typo177178/**179* Indicates whether the specified object is equal to this event type,180* returning {@code true} if the objects are the same.181*182* @param obj the reference object with which to compare183* @return {@code true} if the specified object is equal to this event184* type; {@code false} otherwise185*/186@Override187public final boolean equals(Object obj) {188return super.equals(obj);189}190191/**192* Returns a hash code value for this event type.193*194* @return a hash code value for this event type195*/196@Override197public final int hashCode() {198return super.hashCode();199}200201/**202* Returns type's name as the string representation of the event type.203*204* @return a string representation of the event type205*/206@Override207public String toString() {208return name;209}210211// LINE EVENT TYPE DEFINES212213/**214* A type of event that is sent when a line opens, reserving system215* resources for itself.216*217* @see #CLOSE218* @see Line#open219*/220public static final Type OPEN = new Type("Open");221222/**223* A type of event that is sent when a line closes, freeing the system224* resources it had obtained when it was opened.225*226* @see #OPEN227* @see Line#close228*/229public static final Type CLOSE = new Type("Close");230231/**232* A type of event that is sent when a line begins to engage in active233* input or output of audio data in response to a234* {@link DataLine#start start} request.235*236* @see #STOP237* @see DataLine#start238*/239public static final Type START = new Type("Start");240241/**242* A type of event that is sent when a line ceases active input or243* output of audio data in response to a {@link DataLine#stop stop}244* request, or because the end of media has been reached.245*246* @see #START247* @see DataLine#stop248*/249public static final Type STOP = new Type("Stop");250251/**252* A type of event that is sent when a line ceases to engage in active253* input or output of audio data because the end of media has been254* reached.255*/256/*257* ISSUE: we may want to get rid of this. Is JavaSound responsible for258* reporting this??259*260* [If it's decided to keep this API, the docs will need to be updated261* to include mention of EOM events elsewhere.]262*/263//public static final Type EOM = new Type("EOM");264265/**266* A type of event that is sent when a line begins to engage in active267* input or output of audio data. Examples of when this happens are when268* a source line begins or resumes writing data to its mixer, and when a269* target line begins or resumes reading data from its mixer.270*271* @see #STOP272* @see SourceDataLine#write273* @see TargetDataLine#read274* @see DataLine#start275*/276//public static final Type ACTIVE = new Type("ACTIVE");277278/**279* A type of event that is sent when a line ceases active input or280* output of audio data.281*282* @see #START283* @see DataLine#stop284*/285//public static final Type INACTIVE = new Type("INACTIVE");286}287}288289290