AudioDeviceBase.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * 11/19/04 1.0 moved to LGPL.
  3. * 29/01/00 Initial version. mdm@techie.com
  4. *-----------------------------------------------------------------------
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Library General Public License as published
  7. * by the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *----------------------------------------------------------------------
  19. */
  20. package javazoom.jl.player;
  21. import javazoom.jl.decoder.Decoder;
  22. import javazoom.jl.decoder.JavaLayerException;
  23. /**
  24. * The <code>AudioDeviceBase</code> class provides a simple thread-safe
  25. * implementation of the <code>AudioDevice</code> interface.
  26. * Template methods are provided for subclasses to override and
  27. * in doing so provide the implementation for the main operations
  28. * of the <code>AudioDevice</code> interface.
  29. *
  30. * @since 0.0.8
  31. * @author Mat McGowan
  32. */
  33. /*
  34. * REVIEW: It is desirable to be able to use the decoder whe
  35. * in the implementation of open(), but the decoder
  36. * has not yet read a frame, and so much of the
  37. * desired information (sample rate, channels etc.)
  38. * are not available.
  39. */
  40. public abstract class AudioDeviceBase implements AudioDevice
  41. {
  42. private boolean open = false;
  43. private Decoder decoder = null;
  44. /**
  45. * Opens this audio device.
  46. *
  47. * @param decoder The decoder that will provide audio data
  48. * to this audio device.
  49. */
  50. public synchronized void open(Decoder decoder) throws JavaLayerException
  51. {
  52. if (!isOpen())
  53. {
  54. this.decoder = decoder;
  55. openImpl();
  56. setOpen(true);
  57. }
  58. }
  59. /**
  60. * Template method to provide the
  61. * implementation for the opening of the audio device.
  62. */
  63. protected void openImpl() throws JavaLayerException
  64. {
  65. }
  66. /**
  67. * Sets the open state for this audio device.
  68. */
  69. protected void setOpen(boolean open)
  70. {
  71. this.open = open;
  72. }
  73. /**
  74. * Determines if this audio device is open or not.
  75. *
  76. * @return <code>true</code> if the audio device is open,
  77. * <code>false</code> if it is not.
  78. */
  79. public synchronized boolean isOpen()
  80. {
  81. return open;
  82. }
  83. /**
  84. * Closes this audio device. If the device is currently playing
  85. * audio, playback is stopped immediately without flushing
  86. * any buffered audio data.
  87. */
  88. public synchronized void close()
  89. {
  90. if (isOpen())
  91. {
  92. closeImpl();
  93. setOpen(false);
  94. decoder = null;
  95. }
  96. }
  97. /**
  98. * Template method to provide the implementation for
  99. * closing the audio device.
  100. */
  101. protected void closeImpl()
  102. {
  103. }
  104. /**
  105. * Writes audio data to this audio device. Audio data is
  106. * assumed to be in the output format of the decoder. This
  107. * method may return before the data has actually been sounded
  108. * by the device if the device buffers audio samples.
  109. *
  110. * @param samples The samples to write to the audio device.
  111. * @param offs The offset into the array of the first sample to write.
  112. * @param len The number of samples from the array to write.
  113. * @throws JavaLayerException if the audio data could not be
  114. * written to the audio device.
  115. * If the audio device is not open, this method does nthing.
  116. */
  117. public void write(short[] samples, int offs, int len)
  118. throws JavaLayerException
  119. {
  120. if (isOpen())
  121. {
  122. writeImpl(samples, offs, len);
  123. }
  124. }
  125. /**
  126. * Template method to provide the implementation for
  127. * writing audio samples to the audio device.
  128. */
  129. protected void writeImpl(short[] samples, int offs, int len)
  130. throws JavaLayerException
  131. {
  132. }
  133. /**
  134. * Waits for any buffered audio samples to be played by the
  135. * audio device. This method should only be called prior
  136. * to closing the device.
  137. */
  138. public void flush()
  139. {
  140. if (isOpen())
  141. {
  142. flushImpl();
  143. }
  144. }
  145. /**
  146. * Template method to provide the implementation for
  147. * flushing any buffered audio data.
  148. */
  149. protected void flushImpl()
  150. {
  151. }
  152. /**
  153. * Retrieves the decoder that provides audio data to this
  154. * audio device.
  155. *
  156. * @return The associated decoder.
  157. */
  158. protected Decoder getDecoder()
  159. {
  160. return decoder;
  161. }
  162. }