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