1. /*
  2. * @(#)LineEvent.java 1.26 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.sound.sampled;
  8. /**
  9. * The <code>LineEvent</code> class encapsulates information that a line
  10. * sends its listeners whenever the line opens, closes, starts, or stops.
  11. * Each of these four state changes is represented by a corresponding
  12. * type of event. A listener receives the event as a parameter to its
  13. * {@link LineListener#update update} method. By querying the event,
  14. * the listener can learn the type of event, the line responsible for
  15. * the event, and how much data the line had processed when the event occurred.
  16. *
  17. * <p>Although this class implements Serializable, attempts to
  18. * serialize a <code>LineEvent</code> object will fail.
  19. *
  20. * @author Kara Kytle
  21. * @version 1.26, 03/12/19
  22. *
  23. * @see Line
  24. * @see LineListener#update
  25. * @since 1.3
  26. *
  27. * @serial exclude
  28. */
  29. public class LineEvent extends java.util.EventObject {
  30. // INSTANCE VARIABLES
  31. /**
  32. * The kind of line event (<code>OPEN</code>, <code>CLOSE</code>,
  33. * <code>START</code>, or <code>STOP</code>).
  34. * @see #getType
  35. * @serial
  36. */
  37. private final Type type;
  38. /**
  39. * The media position when the event occurred, expressed in sample frames.
  40. * Note that this field is only relevant to certain events generated by
  41. * data lines, such as <code>START</code> and <code>STOP</code>. For
  42. * events generated by lines that do not count sample frames, and for any
  43. * other events for which this value is not known, the position value
  44. * should be {@link AudioSystem#NOT_SPECIFIED}.
  45. * @serial
  46. * @see #getFramePosition
  47. */
  48. private final long position;
  49. /**
  50. * Constructs a new event of the specified type, originating from the specified line.
  51. * @param line the source of this event
  52. * @param type the event type (<code>OPEN</code>, <code>CLOSE</code>, <code>START</code>, or <code>STOP</code>)
  53. * @param position the number of sample frames that the line had already processed when the event occurred,
  54. * or {@link AudioSystem#NOT_SPECIFIED}
  55. *
  56. * @throws IllegalArgumentException if <code>line</code> is
  57. * <code>null</code>.
  58. */
  59. public LineEvent(Line line, Type type, long position) {
  60. super(line);
  61. this.type = type;
  62. this.position = position;
  63. }
  64. /**
  65. * Obtains the audio line that is the source of this event.
  66. * @return the line responsible for this event
  67. */
  68. public final Line getLine() {
  69. return (Line)getSource();
  70. }
  71. /**
  72. * Obtains the event's type.
  73. * @return this event's type ({@link Type#OPEN}, {@link Type#CLOSE},
  74. * {@link Type#START}, or {@link Type#STOP})
  75. */
  76. public final Type getType() {
  77. return type;
  78. }
  79. /**
  80. * Obtains the position in the line's audio data when the event occurred, expressed in sample frames.
  81. * For example, if a source line had already played back 14 sample frames at the time it was
  82. * paused, the pause event would report the line's position as 14. The next frame to be processed
  83. * would be frame number 14 using zero-based numbering, or 15 using one-based numbering.
  84. * <p>
  85. * Note that this field is relevant only to certain events generated by
  86. * data lines, such as <code>START</code> and <code>STOP</code>. For
  87. * events generated by lines that do not count sample frames, and for any
  88. * other events for which this value is not known, the position value
  89. * should be {@link AudioSystem#NOT_SPECIFIED}.
  90. *
  91. * @return the line's position as a sample frame number
  92. */
  93. /*
  94. * $$kk: 04.20.99: note to myself: should make sure our implementation is consistent with this.
  95. * which is a reasonable definition....
  96. */
  97. public final long getFramePosition() {
  98. return position;
  99. }
  100. /**
  101. * Obtains a string representation of the event. The contents of the string may vary
  102. * between implementations of Java Sound.
  103. * @return a string describing the event.
  104. */
  105. public String toString() {
  106. String sType = "";
  107. if (type != null) sType = type.toString()+" ";
  108. String sLine;
  109. if (getLine() == null) {
  110. sLine = "null";
  111. } else {
  112. sLine = getLine().toString();
  113. }
  114. return new String(sType + "event from line " + sLine);
  115. }
  116. /**
  117. * The LineEvent.Type inner class identifies what kind of event occurred on a line.
  118. * Static instances are provided for the common types (OPEN, CLOSE, START, and STOP).
  119. *
  120. * @see LineEvent#getType()
  121. */
  122. public static class Type {
  123. /**
  124. * Type name.
  125. */
  126. // $$kk: 03.25.99: why can't this be final??
  127. private /*final*/ String name;
  128. /**
  129. * Constructs a new event type.
  130. * @param name name of the type
  131. */
  132. protected Type(String name) {
  133. this.name = name;
  134. }
  135. //$$fb 2002-11-26: fix for 4695001: SPEC: description of equals() method contains typo
  136. /**
  137. * Indicates whether the specified object is equal to this event type,
  138. * returning <code>true</code> if the objects are identical.
  139. * @param obj the reference object with which to compare
  140. * @return <code>true</code> if this event type is the same as
  141. * <code>obj</code> <code>false</code> otherwise
  142. */
  143. public final boolean equals(Object obj) {
  144. return super.equals(obj);
  145. }
  146. /**
  147. * Finalizes the hashcode method.
  148. */
  149. public final int hashCode() {
  150. return super.hashCode();
  151. }
  152. /**
  153. * Returns the type name as the string representation.
  154. */
  155. public String toString() {
  156. return name;
  157. }
  158. // LINE EVENT TYPE DEFINES
  159. /**
  160. * A type of event that is sent when a line opens, reserving system
  161. * resources for itself.
  162. * @see #CLOSE
  163. * @see Line#open
  164. */
  165. public static final Type OPEN = new Type("Open");
  166. /**
  167. * A type of event that is sent when a line closes, freeing the system
  168. * resources it had obtained when it was opened.
  169. * @see #OPEN
  170. * @see Line#close
  171. */
  172. public static final Type CLOSE = new Type("Close");
  173. /**
  174. * A type of event that is sent when a line begins to engage in active
  175. * input or output of audio data in response to a
  176. * {@link DataLine#start start} request.
  177. * @see #STOP
  178. * @see DataLine#start
  179. */
  180. public static final Type START = new Type("Start");
  181. /**
  182. * A type of event that is sent when a line ceases active input or output
  183. * of audio data in response to a {@link DataLine#stop stop} request,
  184. * or because the end of media has been reached.
  185. * @see #START
  186. * @see DataLine#stop
  187. */
  188. public static final Type STOP = new Type("Stop");
  189. /**
  190. * A type of event that is sent when a line ceases to engage in active
  191. * input or output of audio data because the end of media has been reached.
  192. */
  193. /*
  194. * ISSUE: we may want to get rid of this. Is JavaSound
  195. * responsible for reporting this??
  196. *
  197. * [If it's decided to keep this API, the docs will need to be updated to include mention
  198. * of EOM events elsewhere.]
  199. */
  200. //public static final Type EOM = new Type("EOM");
  201. /**
  202. * A type of event that is sent when a line begins to engage in active
  203. * input or output of audio data. Examples of when this happens are
  204. * when a source line begins or resumes writing data to its mixer, and
  205. * when a target line begins or resumes reading data from its mixer.
  206. * @see #STOP
  207. * @see SourceDataLine#write
  208. * @see TargetDataLine#read
  209. * @see DataLine#start
  210. */
  211. //public static final Type ACTIVE = new Type("ACTIVE");
  212. /**
  213. * A type of event that is sent when a line ceases active input or output
  214. * of audio data.
  215. * @see #START
  216. * @see DataLine#stop
  217. */
  218. //public static final Type INACTIVE = new Type("INACTIVE");
  219. } // class Type
  220. } // class LineEvent