001 /*
002 * Copyright 1999-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.sound.sampled;
027
028 /**
029 * The <code>LineEvent</code> class encapsulates information that a line
030 * sends its listeners whenever the line opens, closes, starts, or stops.
031 * Each of these four state changes is represented by a corresponding
032 * type of event. A listener receives the event as a parameter to its
033 * {@link LineListener#update update} method. By querying the event,
034 * the listener can learn the type of event, the line responsible for
035 * the event, and how much data the line had processed when the event occurred.
036 *
037 * <p>Although this class implements Serializable, attempts to
038 * serialize a <code>LineEvent</code> object will fail.
039 *
040 * @author Kara Kytle
041 * @version 1.33, 07/05/05
042 *
043 * @see Line
044 * @see LineListener#update
045 * @since 1.3
046 *
047 * @serial exclude
048 */
049 public class LineEvent extends java.util.EventObject {
050
051 // INSTANCE VARIABLES
052
053 /**
054 * The kind of line event (<code>OPEN</code>, <code>CLOSE</code>,
055 * <code>START</code>, or <code>STOP</code>).
056 * @see #getType
057 * @serial
058 */
059 private final Type type;
060
061 /**
062 * The media position when the event occurred, expressed in sample frames.
063 * Note that this field is only relevant to certain events generated by
064 * data lines, such as <code>START</code> and <code>STOP</code>. For
065 * events generated by lines that do not count sample frames, and for any
066 * other events for which this value is not known, the position value
067 * should be {@link AudioSystem#NOT_SPECIFIED}.
068 * @serial
069 * @see #getFramePosition
070 */
071 private final long position;
072
073 /**
074 * Constructs a new event of the specified type, originating from the specified line.
075 * @param line the source of this event
076 * @param type the event type (<code>OPEN</code>, <code>CLOSE</code>, <code>START</code>, or <code>STOP</code>)
077 * @param position the number of sample frames that the line had already processed when the event occurred,
078 * or {@link AudioSystem#NOT_SPECIFIED}
079 *
080 * @throws IllegalArgumentException if <code>line</code> is
081 * <code>null</code>.
082 */
083 public LineEvent(Line line, Type type, long position) {
084
085 super (line);
086 this .type = type;
087 this .position = position;
088 }
089
090 /**
091 * Obtains the audio line that is the source of this event.
092 * @return the line responsible for this event
093 */
094 public final Line getLine() {
095
096 return (Line) getSource();
097 }
098
099 /**
100 * Obtains the event's type.
101 * @return this event's type ({@link Type#OPEN}, {@link Type#CLOSE},
102 * {@link Type#START}, or {@link Type#STOP})
103 */
104 public final Type getType() {
105
106 return type;
107 }
108
109 /**
110 * Obtains the position in the line's audio data when the event occurred, expressed in sample frames.
111 * For example, if a source line had already played back 14 sample frames at the time it was
112 * paused, the pause event would report the line's position as 14. The next frame to be processed
113 * would be frame number 14 using zero-based numbering, or 15 using one-based numbering.
114 * <p>
115 * Note that this field is relevant only to certain events generated by
116 * data lines, such as <code>START</code> and <code>STOP</code>. For
117 * events generated by lines that do not count sample frames, and for any
118 * other events for which this value is not known, the position value
119 * should be {@link AudioSystem#NOT_SPECIFIED}.
120 *
121 * @return the line's position as a sample frame number
122 */
123 /*
124 * $$kk: 04.20.99: note to myself: should make sure our implementation is consistent with this.
125 * which is a reasonable definition....
126 */
127 public final long getFramePosition() {
128
129 return position;
130 }
131
132 /**
133 * Obtains a string representation of the event. The contents of the string may vary
134 * between implementations of Java Sound.
135 * @return a string describing the event.
136 */
137 public String toString() {
138 String sType = "";
139 if (type != null)
140 sType = type.toString() + " ";
141 String sLine;
142 if (getLine() == null) {
143 sLine = "null";
144 } else {
145 sLine = getLine().toString();
146 }
147 return new String(sType + "event from line " + sLine);
148 }
149
150 /**
151 * The LineEvent.Type inner class identifies what kind of event occurred on a line.
152 * Static instances are provided for the common types (OPEN, CLOSE, START, and STOP).
153 *
154 * @see LineEvent#getType()
155 */
156 public static class Type {
157
158 /**
159 * Type name.
160 */
161 // $$kk: 03.25.99: why can't this be final??
162 private/*final*/String name;
163
164 /**
165 * Constructs a new event type.
166 * @param name name of the type
167 */
168 protected Type(String name) {
169 this .name = name;
170 }
171
172 //$$fb 2002-11-26: fix for 4695001: SPEC: description of equals() method contains typo
173 /**
174 * Indicates whether the specified object is equal to this event type,
175 * returning <code>true</code> if the objects are identical.
176 * @param obj the reference object with which to compare
177 * @return <code>true</code> if this event type is the same as
178 * <code>obj</code>; <code>false</code> otherwise
179 */
180 public final boolean equals(Object obj) {
181 return super .equals(obj);
182 }
183
184 /**
185 * Finalizes the hashcode method.
186 */
187 public final int hashCode() {
188 return super .hashCode();
189 }
190
191 /**
192 * Returns the type name as the string representation.
193 */
194 public String toString() {
195 return name;
196 }
197
198 // LINE EVENT TYPE DEFINES
199
200 /**
201 * A type of event that is sent when a line opens, reserving system
202 * resources for itself.
203 * @see #CLOSE
204 * @see Line#open
205 */
206 public static final Type OPEN = new Type("Open");
207
208 /**
209 * A type of event that is sent when a line closes, freeing the system
210 * resources it had obtained when it was opened.
211 * @see #OPEN
212 * @see Line#close
213 */
214 public static final Type CLOSE = new Type("Close");
215
216 /**
217 * A type of event that is sent when a line begins to engage in active
218 * input or output of audio data in response to a
219 * {@link DataLine#start start} request.
220 * @see #STOP
221 * @see DataLine#start
222 */
223 public static final Type START = new Type("Start");
224
225 /**
226 * A type of event that is sent when a line ceases active input or output
227 * of audio data in response to a {@link DataLine#stop stop} request,
228 * or because the end of media has been reached.
229 * @see #START
230 * @see DataLine#stop
231 */
232 public static final Type STOP = new Type("Stop");
233
234 /**
235 * A type of event that is sent when a line ceases to engage in active
236 * input or output of audio data because the end of media has been reached.
237 */
238 /*
239 * ISSUE: we may want to get rid of this. Is JavaSound
240 * responsible for reporting this??
241 *
242 * [If it's decided to keep this API, the docs will need to be updated to include mention
243 * of EOM events elsewhere.]
244 */
245 //public static final Type EOM = new Type("EOM");
246
247 /**
248 * A type of event that is sent when a line begins to engage in active
249 * input or output of audio data. Examples of when this happens are
250 * when a source line begins or resumes writing data to its mixer, and
251 * when a target line begins or resumes reading data from its mixer.
252 * @see #STOP
253 * @see SourceDataLine#write
254 * @see TargetDataLine#read
255 * @see DataLine#start
256 */
257 //public static final Type ACTIVE = new Type("ACTIVE");
258
259 /**
260 * A type of event that is sent when a line ceases active input or output
261 * of audio data.
262 * @see #START
263 * @see DataLine#stop
264 */
265 //public static final Type INACTIVE = new Type("INACTIVE");
266 } // class Type
267
268 } // class LineEvent
|