src/share/classes/javax/sound/sampled/LineEvent.java

Print this page




   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


   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 import java.util.EventObject;
  29 
  30 /**
  31  * The {@code LineEvent} class encapsulates information that a line sends its
  32  * listeners whenever the line opens, closes, starts, or stops. Each of these
  33  * four state changes is represented by a corresponding type of event. A
  34  * listener receives the event as a parameter to its
  35  * {@link LineListener#update update} method. By querying the event, the
  36  * listener can learn the type of event, the line responsible for the event, and
  37  * how much data the line had processed when the event occurred.
  38  * <p>
  39  * Although this class implements Serializable, attempts to serialize a
  40  * {@code LineEvent} object will fail.
  41  *
  42  * @author Kara Kytle

  43  * @see Line
  44  * @see LineListener#update
  45  * @since 1.3
  46  *
  47  * @serial exclude
  48  */
  49 public class LineEvent extends EventObject {

  50 
  51     private static final long serialVersionUID = -1274246333383880410L;
  52 
  53     /**
  54      * The kind of line event ({@code OPEN}, {@code CLOSE}, {@code START}, or
  55      * {@code STOP}).
  56      *
  57      * @serial
  58      * @see #getType
  59      */
  60     private final Type type;
  61 
  62     /**
  63      * The media position when the event occurred, expressed in sample frames.
  64      * Note that this field is only relevant to certain events generated by data
  65      * lines, such as {@code START} and {@code STOP}. For events generated by
  66      * lines that do not count sample frames, and for any other events for which
  67      * this value is not known, the position value should be
  68      * {@link AudioSystem#NOT_SPECIFIED}.
  69      *
  70      * @serial
  71      * @see #getFramePosition
  72      */
  73     private final long position;
  74 

  75     /**
  76      * Constructs a new event of the specified type, originating from the
  77      * specified line.



  78      *
  79      * @param  line the source of this event
  80      * @param  type the event type ({@code OPEN}, {@code CLOSE}, {@code START},
  81      *         or {@code STOP})
  82      * @param  position the number of sample frames that the line had already
  83      *         processed when the event occurred, or
  84      *         {@link AudioSystem#NOT_SPECIFIED}
  85      * @throws IllegalArgumentException if {@code line} is {@code null}.
  86      */
  87     public LineEvent(Line line, Type type, long position) {
  88 
  89         super(line);
  90         this.type = type;
  91         this.position = position;
  92     }
  93 
  94     /**
  95      * Obtains the audio line that is the source of this event.
  96      *
  97      * @return the line responsible for this event
  98      */
  99     public final Line getLine() {
 100 
 101         return (Line)getSource();
 102     }
 103 

 104     /**
 105      * Obtains the event's type.
 106      *
 107      * @return this event's type ({@link Type#OPEN}, {@link Type#CLOSE},
 108      *         {@link Type#START}, or {@link Type#STOP})
 109      */
 110     public final Type getType() {
 111 
 112         return type;
 113     }
 114 
 115     /**
 116      * Obtains the position in the line's audio data when the event occurred,
 117      * expressed in sample frames. For example, if a source line had already
 118      * played back 14 sample frames at the time it was paused, the pause event
 119      * would report the line's position as 14. The next frame to be processed
 120      * would be frame number 14 using zero-based numbering, or 15 using
 121      * one-based numbering.
 122      * <p>
 123      * Note that this field is relevant only to certain events generated by data
 124      * lines, such as {@code START} and {@code STOP}. For events generated by
 125      * lines that do not count sample frames, and for any other events for which
 126      * this value is not known, the position value should be
 127      * {@link AudioSystem#NOT_SPECIFIED}.
 128      *
 129      * @return the line's position as a sample frame number
 130      */
 131     /*
 132      * $$kk: 04.20.99: note to myself: should make sure our implementation is
 133      * consistent with this.
 134      * which is a reasonable definition....
 135      */
 136     public final long getFramePosition() {
 137 
 138         return position;
 139     }
 140 
 141     /**
 142      * Obtains a string representation of the event. The contents of the string
 143      * may vary between implementations of Java Sound.
 144      *
 145      * @return a string describing the event
 146      */
 147     @Override
 148     public String toString() {
 149         String sType = "";
 150         if (type != null) sType = type.toString()+" ";
 151         String sLine;
 152         if (getLine() == null) {
 153             sLine = "null";
 154         } else {
 155             sLine = getLine().toString();
 156         }
 157         return new String(sType + "event from line " + sLine);
 158     }
 159 

 160     /**
 161      * The LineEvent.Type inner class identifies what kind of event occurred on
 162      * a line. Static instances are provided for the common types (OPEN, CLOSE,
 163      * START, and STOP).
 164      *
 165      * @see LineEvent#getType()
 166      */
 167     public static class Type {
 168 

 169         /**
 170          * Type name.
 171          */
 172         // $$kk: 03.25.99: why can't this be final??
 173         private /*final*/ String name;
 174 
 175         /**
 176          * Constructs a new event type.
 177          *
 178          * @param  name name of the type
 179          */
 180         protected Type(String name) {
 181             this.name = name;
 182         }
 183 

 184         //$$fb 2002-11-26: fix for 4695001: SPEC: description of equals() method contains typo
 185 
 186         /**
 187          * Indicates whether the specified object is equal to this event type,
 188          * returning {@code true} if the objects are identical.
 189          *
 190          * @param  obj the reference object with which to compare
 191          * @return {@code true} if this event type is the same as {@code obj};
 192          *         {@code false} otherwise
 193          */
 194         @Override
 195         public final boolean equals(Object obj) {
 196             return super.equals(obj);
 197         }
 198 

 199         /**
 200          * Finalizes the hashcode method.
 201          */
 202         @Override
 203         public final int hashCode() {
 204             return super.hashCode();
 205         }
 206 

 207         /**
 208          * Returns the type name as the string representation.
 209          */
 210         @Override
 211         public String toString() {
 212             return name;
 213         }
 214 

 215         // LINE EVENT TYPE DEFINES
 216 
 217         /**
 218          * A type of event that is sent when a line opens, reserving system
 219          * resources for itself.
 220          *
 221          * @see #CLOSE
 222          * @see Line#open
 223          */
 224         public static final Type OPEN   = new Type("Open");
 225 

 226         /**
 227          * A type of event that is sent when a line closes, freeing the system
 228          * resources it had obtained when it was opened.
 229          *
 230          * @see #OPEN
 231          * @see Line#close
 232          */
 233         public static final Type CLOSE  = new Type("Close");
 234 

 235         /**
 236          * A type of event that is sent when a line begins to engage in active
 237          * input or output of audio data in response to a
 238          * {@link DataLine#start start} request.
 239          *
 240          * @see #STOP
 241          * @see DataLine#start
 242          */
 243         public static final Type START  = new Type("Start");
 244 

 245         /**
 246          * A type of event that is sent when a line ceases active input or
 247          * output of audio data in response to a {@link DataLine#stop stop}
 248          * request, or because the end of media has been reached.
 249          *
 250          * @see #START
 251          * @see DataLine#stop
 252          */
 253         public static final Type STOP   = new Type("Stop");
 254 

 255         /**
 256          * A type of event that is sent when a line ceases to engage in active
 257          * input or output of audio data because the end of media has been reached.
 258          */
 259         /*
 260          * ISSUE: we may want to get rid of this.  Is JavaSound
 261          * responsible for reporting this??
 262          *
 263          * [If it's decided to keep this API, the docs will need to be updated to include mention
 264          * of EOM events elsewhere.]
 265          */
 266         //public static final Type EOM  = new Type("EOM");
 267 

 268         /**
 269          * A type of event that is sent when a line begins to engage in active
 270          * input or output of audio data.  Examples of when this happens are
 271          * when a source line begins or resumes writing data to its mixer, and
 272          * when a target line begins or resumes reading data from its mixer.
 273          * @see #STOP
 274          * @see SourceDataLine#write
 275          * @see TargetDataLine#read
 276          * @see DataLine#start
 277          */
 278         //public static final Type ACTIVE       = new Type("ACTIVE");
 279 

 280         /**
 281          * A type of event that is sent when a line ceases active input or output
 282          * of audio data.
 283          * @see #START
 284          * @see DataLine#stop
 285          */
 286         //public static final Type INACTIVE     = new Type("INACTIVE");
 287     }
 288 }