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.

226 lines
6.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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 "../../containers/juce_ScopedPointer.h"
  27. //==============================================================================
  28. AudioFormatManager::AudioFormatManager()
  29. : defaultFormatIndex (0)
  30. {
  31. }
  32. AudioFormatManager::~AudioFormatManager()
  33. {
  34. clearFormats();
  35. clearSingletonInstance();
  36. }
  37. juce_ImplementSingleton (AudioFormatManager);
  38. //==============================================================================
  39. void AudioFormatManager::registerFormat (AudioFormat* newFormat,
  40. const bool makeThisTheDefaultFormat)
  41. {
  42. jassert (newFormat != 0);
  43. if (newFormat != 0)
  44. {
  45. #ifdef JUCE_DEBUG
  46. for (int i = getNumKnownFormats(); --i >= 0;)
  47. {
  48. if (getKnownFormat (i)->getFormatName() == newFormat->getFormatName())
  49. {
  50. jassertfalse // trying to add the same format twice!
  51. }
  52. }
  53. #endif
  54. if (makeThisTheDefaultFormat)
  55. defaultFormatIndex = knownFormats.size();
  56. knownFormats.add (newFormat);
  57. }
  58. }
  59. void AudioFormatManager::registerBasicFormats()
  60. {
  61. #if JUCE_MAC
  62. registerFormat (new AiffAudioFormat(), true);
  63. registerFormat (new WavAudioFormat(), false);
  64. #else
  65. registerFormat (new WavAudioFormat(), true);
  66. registerFormat (new AiffAudioFormat(), false);
  67. #endif
  68. #if JUCE_USE_FLAC
  69. registerFormat (new FlacAudioFormat(), false);
  70. #endif
  71. #if JUCE_USE_OGGVORBIS
  72. registerFormat (new OggVorbisAudioFormat(), false);
  73. #endif
  74. }
  75. void AudioFormatManager::clearFormats()
  76. {
  77. for (int i = getNumKnownFormats(); --i >= 0;)
  78. delete getKnownFormat(i);
  79. knownFormats.clear();
  80. defaultFormatIndex = 0;
  81. }
  82. int AudioFormatManager::getNumKnownFormats() const
  83. {
  84. return knownFormats.size();
  85. }
  86. AudioFormat* AudioFormatManager::getKnownFormat (const int index) const
  87. {
  88. return (AudioFormat*) knownFormats [index];
  89. }
  90. AudioFormat* AudioFormatManager::getDefaultFormat() const
  91. {
  92. return getKnownFormat (defaultFormatIndex);
  93. }
  94. AudioFormat* AudioFormatManager::findFormatForFileExtension (const String& fileExtension) const
  95. {
  96. String e (fileExtension);
  97. if (! e.startsWithChar (T('.')))
  98. e = T(".") + e;
  99. for (int i = 0; i < getNumKnownFormats(); ++i)
  100. if (getKnownFormat(i)->getFileExtensions().contains (e, true))
  101. return getKnownFormat(i);
  102. return 0;
  103. }
  104. const String AudioFormatManager::getWildcardForAllFormats() const
  105. {
  106. StringArray allExtensions;
  107. int i;
  108. for (i = 0; i < getNumKnownFormats(); ++i)
  109. allExtensions.addArray (getKnownFormat (i)->getFileExtensions());
  110. allExtensions.trim();
  111. allExtensions.removeEmptyStrings();
  112. String s;
  113. for (i = 0; i < allExtensions.size(); ++i)
  114. {
  115. s << T('*');
  116. if (! allExtensions[i].startsWithChar (T('.')))
  117. s << T('.');
  118. s << allExtensions[i];
  119. if (i < allExtensions.size() - 1)
  120. s << T(';');
  121. }
  122. return s;
  123. }
  124. //==============================================================================
  125. AudioFormatReader* AudioFormatManager::createReaderFor (const File& file)
  126. {
  127. // you need to actually register some formats before the manager can
  128. // use them to open a file!
  129. jassert (knownFormats.size() > 0);
  130. for (int i = 0; i < getNumKnownFormats(); ++i)
  131. {
  132. AudioFormat* const af = getKnownFormat(i);
  133. if (af->canHandleFile (file))
  134. {
  135. InputStream* const in = file.createInputStream();
  136. if (in != 0)
  137. {
  138. AudioFormatReader* const r = af->createReaderFor (in, true);
  139. if (r != 0)
  140. return r;
  141. }
  142. }
  143. }
  144. return 0;
  145. }
  146. AudioFormatReader* AudioFormatManager::createReaderFor (InputStream* audioFileStream)
  147. {
  148. // you need to actually register some formats before the manager can
  149. // use them to open a file!
  150. jassert (knownFormats.size() > 0);
  151. ScopedPointer <InputStream> in (audioFileStream);
  152. if (in != 0)
  153. {
  154. const int64 originalStreamPos = in->getPosition();
  155. for (int i = 0; i < getNumKnownFormats(); ++i)
  156. {
  157. AudioFormatReader* const r = getKnownFormat(i)->createReaderFor (in, false);
  158. if (r != 0)
  159. {
  160. in.release();
  161. return r;
  162. }
  163. in->setPosition (originalStreamPos);
  164. // the stream that is passed-in must be capable of being repositioned so
  165. // that all the formats can have a go at opening it.
  166. jassert (in->getPosition() == originalStreamPos);
  167. }
  168. }
  169. return 0;
  170. }
  171. END_JUCE_NAMESPACE