GstAudioFileReader.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*GstAudioFileReader -- GNU Classpath GStreamer AudioFileReader.
  2. Copyright (C) 2007 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.sound.sampled.gstreamer.io;
  32. import gnu.java.lang.CPStringBuilder;
  33. import gnu.javax.sound.sampled.gstreamer.GStreamerMixer;
  34. import java.io.BufferedInputStream;
  35. import java.io.File;
  36. import java.io.FileInputStream;
  37. import java.io.IOException;
  38. import java.io.InputStream;
  39. import java.net.URL;
  40. import javax.sound.sampled.AudioFileFormat;
  41. import javax.sound.sampled.AudioFormat;
  42. import javax.sound.sampled.AudioInputStream;
  43. import javax.sound.sampled.AudioSystem;
  44. import javax.sound.sampled.UnsupportedAudioFileException;
  45. import javax.sound.sampled.spi.AudioFileReader;
  46. /**
  47. * An implementation of a general AudioFileReader. Uses GStreamer to
  48. * parse and retrieve informations about the file passed as input.
  49. *
  50. * @author Mario Torre <neugens@limasoftware.net>
  51. */
  52. public class GstAudioFileReader
  53. extends AudioFileReader
  54. {
  55. @Override
  56. public AudioFileFormat getAudioFileFormat(File file)
  57. throws UnsupportedAudioFileException, IOException
  58. {
  59. CPStringBuilder name = new CPStringBuilder(file.getName());
  60. String _name = name.substring(name.lastIndexOf(".") + 1);
  61. return getAudioFileFormat(
  62. new BufferedInputStream(new FileInputStream(file)), _name);
  63. }
  64. @Override
  65. public AudioFileFormat getAudioFileFormat(InputStream is)
  66. throws UnsupportedAudioFileException, IOException
  67. {
  68. return getAudioFileFormat(is, null);
  69. }
  70. private AudioFileFormat getAudioFileFormat(InputStream is, String extension)
  71. throws UnsupportedAudioFileException
  72. {
  73. AudioFormat format = null;
  74. try
  75. {
  76. format = GstAudioFileReaderNativePeer.getAudioFormat(is);
  77. }
  78. catch (Exception e)
  79. {
  80. UnsupportedAudioFileException ex =
  81. new UnsupportedAudioFileException("Unsupported encoding.");
  82. ex.initCause(ex.getCause());
  83. throw ex;
  84. }
  85. if (format == null)
  86. throw new UnsupportedAudioFileException("Unsupported encoding.");
  87. String name = format.getProperty(GStreamerMixer.GST_DECODER).toString();
  88. if (extension == null)
  89. {
  90. extension =
  91. format.getProperty(GStreamerMixer.GST_FILE_EXTENSION).toString();
  92. }
  93. AudioFileFormat.Type type =
  94. new AudioFileFormat.Type(name, extension);
  95. // TODO: we should calculate this in some way. We don't need it, but
  96. // application may want to use this data.
  97. return new AudioFileFormat(type, format, AudioSystem.NOT_SPECIFIED);
  98. }
  99. @Override
  100. public AudioFileFormat getAudioFileFormat(URL url)
  101. throws UnsupportedAudioFileException, IOException
  102. {
  103. return getAudioFileFormat(new BufferedInputStream(url.openStream()));
  104. }
  105. @Override
  106. public AudioInputStream getAudioInputStream(File file)
  107. throws UnsupportedAudioFileException, IOException
  108. {
  109. InputStream stream = new FileInputStream(file);
  110. long length = file.length();
  111. AudioFormat format = null;
  112. try
  113. {
  114. format = GstAudioFileReaderNativePeer.getAudioFormat(file);
  115. }
  116. catch (Exception e)
  117. {
  118. UnsupportedAudioFileException ex =
  119. new UnsupportedAudioFileException("Unsupported encoding.");
  120. ex.initCause(ex.getCause());
  121. throw ex;
  122. }
  123. // get the header size
  124. if (format == null)
  125. throw new UnsupportedAudioFileException("Unsupported encoding.");
  126. return new AudioInputStream(stream, format, length);
  127. }
  128. @Override
  129. public AudioInputStream getAudioInputStream(InputStream is)
  130. throws UnsupportedAudioFileException, IOException
  131. {
  132. AudioFormat format = null;
  133. try
  134. {
  135. format = GstAudioFileReaderNativePeer.getAudioFormat(is);
  136. }
  137. catch (Exception e)
  138. {
  139. // TODO Auto-generated catch block
  140. e.printStackTrace();
  141. }
  142. // get the header size
  143. if (format == null)
  144. throw new UnsupportedAudioFileException("Unsupported encoding.");
  145. return new AudioInputStream(is, format, AudioSystem.NOT_SPECIFIED);
  146. }
  147. @Override
  148. public AudioInputStream getAudioInputStream(URL url)
  149. throws UnsupportedAudioFileException, IOException
  150. {
  151. return getAudioInputStream(new BufferedInputStream(url.openStream()));
  152. }
  153. }