The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
5.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_AudioFormatManager.h"
  21. #include "juce_AiffAudioFormat.h"
  22. #include "juce_WavAudioFormat.h"
  23. #include "juce_FlacAudioFormat.h"
  24. #include "juce_OggVorbisAudioFormat.h"
  25. #include "../../io/files/juce_FileInputStream.h"
  26. #include "../../memory/juce_ScopedPointer.h"
  27. //==============================================================================
  28. AudioFormatManager::AudioFormatManager()
  29. : defaultFormatIndex (0)
  30. {
  31. }
  32. AudioFormatManager::~AudioFormatManager()
  33. {
  34. }
  35. //==============================================================================
  36. void AudioFormatManager::registerFormat (AudioFormat* newFormat, const bool makeThisTheDefaultFormat)
  37. {
  38. jassert (newFormat != 0);
  39. if (newFormat != 0)
  40. {
  41. #if JUCE_DEBUG
  42. for (int i = getNumKnownFormats(); --i >= 0;)
  43. {
  44. if (getKnownFormat (i)->getFormatName() == newFormat->getFormatName())
  45. {
  46. jassertfalse; // trying to add the same format twice!
  47. }
  48. }
  49. #endif
  50. if (makeThisTheDefaultFormat)
  51. defaultFormatIndex = getNumKnownFormats();
  52. knownFormats.add (newFormat);
  53. }
  54. }
  55. void AudioFormatManager::registerBasicFormats()
  56. {
  57. #if JUCE_MAC
  58. registerFormat (new AiffAudioFormat(), true);
  59. registerFormat (new WavAudioFormat(), false);
  60. #else
  61. registerFormat (new WavAudioFormat(), true);
  62. registerFormat (new AiffAudioFormat(), false);
  63. #endif
  64. #if JUCE_USE_FLAC
  65. registerFormat (new FlacAudioFormat(), false);
  66. #endif
  67. #if JUCE_USE_OGGVORBIS
  68. registerFormat (new OggVorbisAudioFormat(), false);
  69. #endif
  70. }
  71. void AudioFormatManager::clearFormats()
  72. {
  73. knownFormats.clear();
  74. defaultFormatIndex = 0;
  75. }
  76. int AudioFormatManager::getNumKnownFormats() const
  77. {
  78. return knownFormats.size();
  79. }
  80. AudioFormat* AudioFormatManager::getKnownFormat (const int index) const
  81. {
  82. return knownFormats [index];
  83. }
  84. AudioFormat* AudioFormatManager::getDefaultFormat() const
  85. {
  86. return getKnownFormat (defaultFormatIndex);
  87. }
  88. AudioFormat* AudioFormatManager::findFormatForFileExtension (const String& fileExtension) const
  89. {
  90. String e (fileExtension);
  91. if (! e.startsWithChar ('.'))
  92. e = "." + e;
  93. for (int i = 0; i < getNumKnownFormats(); ++i)
  94. if (getKnownFormat(i)->getFileExtensions().contains (e, true))
  95. return getKnownFormat(i);
  96. return 0;
  97. }
  98. const String AudioFormatManager::getWildcardForAllFormats() const
  99. {
  100. StringArray allExtensions;
  101. int i;
  102. for (i = 0; i < getNumKnownFormats(); ++i)
  103. allExtensions.addArray (getKnownFormat (i)->getFileExtensions());
  104. allExtensions.trim();
  105. allExtensions.removeEmptyStrings();
  106. String s;
  107. for (i = 0; i < allExtensions.size(); ++i)
  108. {
  109. s << '*';
  110. if (! allExtensions[i].startsWithChar ('.'))
  111. s << '.';
  112. s << allExtensions[i];
  113. if (i < allExtensions.size() - 1)
  114. s << ';';
  115. }
  116. return s;
  117. }
  118. //==============================================================================
  119. AudioFormatReader* AudioFormatManager::createReaderFor (const File& file)
  120. {
  121. // you need to actually register some formats before the manager can
  122. // use them to open a file!
  123. jassert (getNumKnownFormats() > 0);
  124. for (int i = 0; i < getNumKnownFormats(); ++i)
  125. {
  126. AudioFormat* const af = getKnownFormat(i);
  127. if (af->canHandleFile (file))
  128. {
  129. InputStream* const in = file.createInputStream();
  130. if (in != 0)
  131. {
  132. AudioFormatReader* const r = af->createReaderFor (in, true);
  133. if (r != 0)
  134. return r;
  135. }
  136. }
  137. }
  138. return 0;
  139. }
  140. AudioFormatReader* AudioFormatManager::createReaderFor (InputStream* audioFileStream)
  141. {
  142. // you need to actually register some formats before the manager can
  143. // use them to open a file!
  144. jassert (getNumKnownFormats() > 0);
  145. ScopedPointer <InputStream> in (audioFileStream);
  146. if (in != 0)
  147. {
  148. const int64 originalStreamPos = in->getPosition();
  149. for (int i = 0; i < getNumKnownFormats(); ++i)
  150. {
  151. AudioFormatReader* const r = getKnownFormat(i)->createReaderFor (in, false);
  152. if (r != 0)
  153. {
  154. in.release();
  155. return r;
  156. }
  157. in->setPosition (originalStreamPos);
  158. // the stream that is passed-in must be capable of being repositioned so
  159. // that all the formats can have a go at opening it.
  160. jassert (in->getPosition() == originalStreamPos);
  161. }
  162. }
  163. return 0;
  164. }
  165. END_JUCE_NAMESPACE