1 /*
   2  * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.sound.sampled;
  27 
  28 /**
  29  * The <code>LineEvent</code> class encapsulates information that a line
  30  * sends its listeners whenever the line opens, closes, starts, or stops.
  31  * Each of these four state changes is represented by a corresponding
  32  * type of event.  A listener receives the event as a parameter to its
  33  * {@link LineListener#update update} method.  By querying the event,
  34  * the listener can learn the type of event, the line responsible for
  35  * the event, and how much data the line had processed when the event occurred.
  36  *
  37  * <p>Although this class implements Serializable, attempts to
  38  * serialize a <code>LineEvent</code> object will fail.
  39  *
  40  * @author Kara Kytle
  41  *
  42  * @see Line
  43  * @see LineListener#update
  44  * @since 1.3
  45  *
  46  * @serial exclude
  47  */
  48 public class LineEvent extends java.util.EventObject {
  49     private static final long serialVersionUID = -1274246333383880410L;
  50 
  51     // INSTANCE VARIABLES
  52 
  53     /**
  54      * The kind of line event (<code>OPEN</code>, <code>CLOSE</code>,
  55      * <code>START</code>, or <code>STOP</code>).
  56      * @see #getType
  57      * @serial
  58      */
  59     private final Type type;
  60 
  61     /**
  62      * The media position when the event occurred, expressed in sample frames.
  63      * Note that this field is only relevant to certain events generated by
  64      * data lines, such as <code>START</code> and <code>STOP</code>.  For
  65      * events generated by lines that do not count sample frames, and for any
  66      * other events for which this value is not known, the position value
  67      * should be {@link AudioSystem#NOT_SPECIFIED}.
  68      * @serial
  69      * @see #getFramePosition
  70      */
  71     private final long position;
  72 
  73 
  74     /**
  75      * Constructs a new event of the specified type, originating from the specified line.
  76      * @param line the source of this event
  77      * @param type the event type (<code>OPEN</code>, <code>CLOSE</code>, <code>START</code>, or <code>STOP</code>)
  78      * @param position the number of sample frames that the line had already processed when the event occurred,
  79      * or {@link AudioSystem#NOT_SPECIFIED}
  80      *
  81      * @throws IllegalArgumentException if <code>line</code> is
  82      * <code>null</code>.
  83      */
  84     public LineEvent(Line line, Type type, long position) {
  85 
  86         super(line);
  87         this.type = type;
  88         this.position = position;
  89     }
  90 
  91     /**
  92      * Obtains the audio line that is the source of this event.
  93      * @return the line responsible for this event
  94      */
  95     public final Line getLine() {
  96 
  97         return (Line)getSource();
  98     }
  99 
 100 
 101     /**
 102      * Obtains the event's type.
 103      * @return this event's type ({@link Type#OPEN}, {@link Type#CLOSE},
 104      * {@link Type#START}, or {@link Type#STOP})
 105      */
 106     public final Type getType() {
 107 
 108         return type;
 109     }
 110 
 111     /**
 112      * Obtains the position in the line's audio data when the event occurred, expressed in sample frames.
 113      * For example, if a source line had already played back 14 sample frames at the time it was
 114      * paused, the pause event would report the line's position as 14.  The next frame to be processed
 115      * would be frame number 14 using zero-based numbering, or 15 using one-based numbering.
 116      * <p>
 117      * Note that this field is relevant only to certain events generated by
 118      * data lines, such as <code>START</code> and <code>STOP</code>.  For
 119      * events generated by lines that do not count sample frames, and for any
 120      * other events for which this value is not known, the position value
 121      * should be {@link AudioSystem#NOT_SPECIFIED}.
 122      *
 123      * @return the line's position as a sample frame number
 124      */
 125     /*
 126      * $$kk: 04.20.99: note to myself: should make sure our implementation is consistent with this.
 127      * which is a reasonable definition....
 128      */
 129     public final long getFramePosition() {
 130 
 131         return position;
 132     }
 133 
 134     /**
 135      * Obtains a string representation of the event.  The contents of the string may vary
 136      * between implementations of Java Sound.
 137      * @return a string describing the event.
 138      */
 139     public String toString() {
 140         String sType = "";
 141         if (type != null) sType = type.toString()+" ";
 142         String sLine;
 143         if (getLine() == null) {
 144             sLine = "null";
 145         } else {
 146             sLine = getLine().toString();
 147         }
 148         return new String(sType + "event from line " + sLine);
 149     }
 150 
 151 
 152     /**
 153      * The LineEvent.Type inner class identifies what kind of event occurred on a line.
 154      * Static instances are provided for the common types (OPEN, CLOSE, START, and STOP).
 155      *
 156      * @see LineEvent#getType()
 157      */
 158     public static class Type {
 159 
 160 
 161         /**
 162          * Type name.
 163          */
 164         // $$kk: 03.25.99: why can't this be final??
 165         private /*final*/ String name;
 166 
 167         /**
 168          * Constructs a new event type.
 169          * @param name name of the type
 170          */
 171         protected Type(String name) {
 172             this.name = name;
 173         }
 174 
 175 
 176         //$$fb 2002-11-26: fix for 4695001: SPEC: description of equals() method contains typo
 177         /**
 178          * Indicates whether the specified object is equal to this event type,
 179          * returning <code>true</code> if the objects are identical.
 180          * @param obj the reference object with which to compare
 181          * @return <code>true</code> if this event type is the same as
 182          * <code>obj</code>; <code>false</code> otherwise
 183          */
 184         public final boolean equals(Object obj) {
 185             return super.equals(obj);
 186         }
 187 
 188 
 189         /**
 190          * Finalizes the hashcode method.
 191          */
 192         public final int hashCode() {
 193             return super.hashCode();
 194         }
 195 
 196 
 197         /**
 198          * Returns the type name as the string representation.
 199          */
 200         public String toString() {
 201             return name;
 202         }
 203 
 204 
 205         // LINE EVENT TYPE DEFINES
 206 
 207         /**
 208          * A type of event that is sent when a line opens, reserving system
 209          * resources for itself.
 210          * @see #CLOSE
 211          * @see Line#open
 212          */
 213         public static final Type OPEN   = new Type("Open");
 214 
 215 
 216         /**
 217          * A type of event that is sent when a line closes, freeing the system
 218          * resources it had obtained when it was opened.
 219          * @see #OPEN
 220          * @see Line#close
 221          */
 222         public static final Type CLOSE  = new Type("Close");
 223 
 224 
 225         /**
 226          * A type of event that is sent when a line begins to engage in active
 227          * input or output of audio data in response to a
 228          * {@link DataLine#start start} request.
 229          * @see #STOP
 230          * @see DataLine#start
 231          */
 232         public static final Type START  = new Type("Start");
 233 
 234 
 235         /**
 236          * A type of event that is sent when a line ceases active input or output
 237          * of audio data in response to a {@link DataLine#stop stop} request,
 238          * or because the end of media has been reached.
 239          * @see #START
 240          * @see DataLine#stop
 241          */
 242         public static final Type STOP   = new Type("Stop");
 243 
 244 
 245         /**
 246          * A type of event that is sent when a line ceases to engage in active
 247          * input or output of audio data because the end of media has been reached.
 248          */
 249         /*
 250          * ISSUE: we may want to get rid of this.  Is JavaSound
 251          * responsible for reporting this??
 252          *
 253          * [If it's decided to keep this API, the docs will need to be updated to include mention
 254          * of EOM events elsewhere.]
 255          */
 256         //public static final Type EOM  = new Type("EOM");
 257 
 258 
 259         /**
 260          * A type of event that is sent when a line begins to engage in active
 261          * input or output of audio data.  Examples of when this happens are
 262          * when a source line begins or resumes writing data to its mixer, and
 263          * when a target line begins or resumes reading data from its mixer.
 264          * @see #STOP
 265          * @see SourceDataLine#write
 266          * @see TargetDataLine#read
 267          * @see DataLine#start
 268          */
 269         //public static final Type ACTIVE       = new Type("ACTIVE");
 270 
 271 
 272         /**
 273          * A type of event that is sent when a line ceases active input or output
 274          * of audio data.
 275          * @see #START
 276          * @see DataLine#stop
 277          */
 278         //public static final Type INACTIVE     = new Type("INACTIVE");
 279 
 280     } // class Type
 281 
 282 } // class LineEvent