1. /*
  2. * @(#)FormatConversionProvider.java 1.29 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.spi;
  8. import java.io.InputStream;
  9. import javax.sound.sampled.AudioFormat;
  10. import javax.sound.sampled.AudioInputStream;
  11. /**
  12. * A format conversion provider provides format conversion services
  13. * from one or more input formats to one or more output formats.
  14. * Converters include codecs, which encode and/or decode audio data,
  15. * as well as transcoders, etc. Format converters provide methods for
  16. * determining what conversions are supported and for obtaining an audio
  17. * stream from which converted data can be read.
  18. * <p>
  19. * The source format represents the format of the incoming
  20. * audio data, which will be converted.
  21. * <p>
  22. * The target format represents the format of the processed, converted
  23. * audio data. This is the format of the data that can be read from
  24. * the stream returned by one of the <code>getAudioInputStream</code> methods.
  25. *
  26. * @author Kara Kytle
  27. * @version 1.29, 03/12/19
  28. * @since 1.3
  29. */
  30. public abstract class FormatConversionProvider {
  31. // NEW METHODS
  32. /**
  33. * Obtains the set of source format encodings from which format
  34. * conversion services are provided by this provider.
  35. * @return array of source format encodings. The array will always
  36. * have a length of at least 1.
  37. */
  38. public abstract AudioFormat.Encoding[] getSourceEncodings();
  39. /**
  40. * Obtains the set of target format encodings to which format
  41. * conversion services are provided by this provider.
  42. * @return array of target format encodings. The array will always
  43. * have a length of at least 1.
  44. */
  45. public abstract AudioFormat.Encoding[] getTargetEncodings();
  46. /**
  47. * Indicates whether the format converter supports conversion from the
  48. * specified source format encoding.
  49. * @param sourceEncoding the source format encoding for which support is queried
  50. * @return <code>true</code> if the encoding is supported, otherwise <code>false</code>
  51. */
  52. public boolean isSourceEncodingSupported(AudioFormat.Encoding sourceEncoding){
  53. AudioFormat.Encoding sourceEncodings[] = getSourceEncodings();
  54. for(int i=0; i<sourceEncodings.length; i++) {
  55. if( sourceEncoding.equals( sourceEncodings[i]) ) {
  56. return true;
  57. }
  58. }
  59. return false;
  60. }
  61. /**
  62. * Indicates whether the format converter supports conversion to the
  63. * specified target format encoding.
  64. * @param targetEncoding the target format encoding for which support is queried
  65. * @return <code>true</code> if the encoding is supported, otherwise <code>false</code>
  66. */
  67. public boolean isTargetEncodingSupported(AudioFormat.Encoding targetEncoding){
  68. AudioFormat.Encoding targetEncodings[] = getTargetEncodings();
  69. for(int i=0; i<targetEncodings.length; i++) {
  70. if( targetEncoding.equals( targetEncodings[i]) ) {
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. /**
  77. * Obtains the set of target format encodings supported by the format converter
  78. * given a particular source format.
  79. * If no target format encodings are supported for this source format,
  80. * an array of length 0 is returned.
  81. * @return array of supported target format encodings.
  82. */
  83. public abstract AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat);
  84. /**
  85. * Indicates whether the format converter supports conversion to a particular encoding
  86. * from a particular format.
  87. * @param targetEncoding desired encoding of the outgoing data
  88. * @param sourceFormat format of the incoming data
  89. * @return <code>true</code> if the conversion is supported, otherwise <code>false</code>
  90. */
  91. public boolean isConversionSupported(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){
  92. AudioFormat.Encoding targetEncodings[] = getTargetEncodings(sourceFormat);
  93. for(int i=0; i<targetEncodings.length; i++) {
  94. if( targetEncoding.equals( targetEncodings[i]) ) {
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. /**
  101. * Obtains the set of target formats with the encoding specified
  102. * supported by the format converter
  103. * If no target formats with the specified encoding are supported
  104. * for this source format, an array of length 0 is returned.
  105. * @return array of supported target formats.
  106. */
  107. public abstract AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat);
  108. /**
  109. * Indicates whether the format converter supports conversion to one
  110. * particular format from another.
  111. * @param targetFormat desired format of outgoing data
  112. * @param sourceFormat format of the incoming data
  113. * @return <code>true</code> if the conversion is supported, otherwise <code>false</code>
  114. */
  115. public boolean isConversionSupported(AudioFormat targetFormat, AudioFormat sourceFormat){
  116. AudioFormat targetFormats[] = getTargetFormats( targetFormat.getEncoding(), sourceFormat );
  117. for(int i=0; i<targetFormats.length; i++) {
  118. if( targetFormat.matches( targetFormats[i] ) ) {
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. /**
  125. * Obtains an audio input stream with the specified encoding from the given audio
  126. * input stream.
  127. * @param targetEncoding desired encoding of the stream after processing
  128. * @param sourceStream stream from which data to be processed should be read
  129. * @return stream from which processed data with the specified target encoding may be read
  130. * @throws IllegalArgumentException if the format combination supplied is
  131. * not supported.
  132. */
  133. public abstract AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream);
  134. /**
  135. * Obtains an audio input stream with the specified format from the given audio
  136. * input stream.
  137. * @param targetFormat desired data format of the stream after processing
  138. * @param sourceStream stream from which data to be processed should be read
  139. * @return stream from which processed data with the specified format may be read
  140. * @throws IllegalArgumentException if the format combination supplied is
  141. * not supported.
  142. */
  143. public abstract AudioInputStream getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream);
  144. }