Audio plugin host https://kx.studio/carla
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.

185 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2015 ROLI Ltd.
  5. Copyright (C) 2018 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of either:
  7. a) the GPL v2 (or any later version)
  8. b) the Affero GPL v3
  9. Details of these licenses can be found at: www.gnu.org/licenses
  10. Water 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. */
  15. #include "AudioFormatManager.h"
  16. #include "../audiocodecs/WavAudioFormat.h"
  17. #include "../files/FileInputStream.h"
  18. namespace water {
  19. AudioFormatManager::AudioFormatManager() : defaultFormatIndex (0) {}
  20. AudioFormatManager::~AudioFormatManager() {}
  21. //==============================================================================
  22. void AudioFormatManager::registerFormat (AudioFormat* newFormat, const bool makeThisTheDefaultFormat)
  23. {
  24. jassert (newFormat != nullptr);
  25. if (newFormat != nullptr)
  26. {
  27. #if DEBUG
  28. for (int i = getNumKnownFormats(); --i >= 0;)
  29. {
  30. if (getKnownFormat (i)->getFormatName() == newFormat->getFormatName())
  31. {
  32. jassertfalse; // trying to add the same format twice!
  33. }
  34. }
  35. #endif
  36. if (makeThisTheDefaultFormat)
  37. defaultFormatIndex = getNumKnownFormats();
  38. knownFormats.add (newFormat);
  39. }
  40. }
  41. void AudioFormatManager::registerBasicFormats()
  42. {
  43. registerFormat (new WavAudioFormat(), true);
  44. #if 0
  45. registerFormat (new AiffAudioFormat(), false);
  46. #endif
  47. #if WATER_USE_FLAC
  48. registerFormat (new FlacAudioFormat(), false);
  49. #endif
  50. #if WATER_USE_OGGVORBIS
  51. registerFormat (new OggVorbisAudioFormat(), false);
  52. #endif
  53. #if WATER_MAC || WATER_IOS
  54. registerFormat (new CoreAudioFormat(), false);
  55. #endif
  56. #if WATER_USE_MP3AUDIOFORMAT
  57. registerFormat (new MP3AudioFormat(), false);
  58. #endif
  59. #if WATER_USE_WINDOWS_MEDIA_FORMAT
  60. registerFormat (new WindowsMediaAudioFormat(), false);
  61. #endif
  62. }
  63. void AudioFormatManager::clearFormats()
  64. {
  65. knownFormats.clear();
  66. defaultFormatIndex = 0;
  67. }
  68. int AudioFormatManager::getNumKnownFormats() const
  69. {
  70. return knownFormats.size();
  71. }
  72. AudioFormat* AudioFormatManager::getKnownFormat (const int index) const
  73. {
  74. return knownFormats [index];
  75. }
  76. AudioFormat* AudioFormatManager::getDefaultFormat() const
  77. {
  78. return getKnownFormat (defaultFormatIndex);
  79. }
  80. AudioFormat* AudioFormatManager::findFormatForFileExtension (const String& fileExtension) const
  81. {
  82. if (! fileExtension.startsWithChar ('.'))
  83. return findFormatForFileExtension ("." + fileExtension);
  84. for (int i = 0; i < getNumKnownFormats(); ++i)
  85. if (getKnownFormat(i)->getFileExtensions().contains (fileExtension, true))
  86. return getKnownFormat(i);
  87. return nullptr;
  88. }
  89. String AudioFormatManager::getWildcardForAllFormats() const
  90. {
  91. StringArray extensions;
  92. for (int i = 0; i < getNumKnownFormats(); ++i)
  93. extensions.addArray (getKnownFormat(i)->getFileExtensions());
  94. extensions.trim();
  95. extensions.removeEmptyStrings();
  96. for (int i = 0; i < extensions.size(); ++i)
  97. extensions.set (i, (extensions[i].startsWithChar ('.') ? "*" : "*.") + extensions[i]);
  98. extensions.removeDuplicates (true);
  99. return extensions.joinIntoString (";");
  100. }
  101. //==============================================================================
  102. AudioFormatReader* AudioFormatManager::createReaderFor (const File& file)
  103. {
  104. // you need to actually register some formats before the manager can
  105. // use them to open a file!
  106. jassert (getNumKnownFormats() > 0);
  107. for (int i = 0; i < getNumKnownFormats(); ++i)
  108. {
  109. AudioFormat* const af = getKnownFormat(i);
  110. if (af->canHandleFile (file))
  111. if (InputStream* const in = file.createInputStream())
  112. if (AudioFormatReader* const r = af->createReaderFor (in, true))
  113. return r;
  114. }
  115. return nullptr;
  116. }
  117. AudioFormatReader* AudioFormatManager::createReaderFor (InputStream* audioFileStream)
  118. {
  119. // you need to actually register some formats before the manager can
  120. // use them to open a file!
  121. jassert (getNumKnownFormats() > 0);
  122. ScopedPointer<InputStream> in (audioFileStream);
  123. if (in != nullptr)
  124. {
  125. const int64 originalStreamPos = in->getPosition();
  126. for (int i = 0; i < getNumKnownFormats(); ++i)
  127. {
  128. if (AudioFormatReader* const r = getKnownFormat(i)->createReaderFor (in, false))
  129. {
  130. in.release();
  131. return r;
  132. }
  133. in->setPosition (originalStreamPos);
  134. // the stream that is passed-in must be capable of being repositioned so
  135. // that all the formats can have a go at opening it.
  136. jassert (in->getPosition() == originalStreamPos);
  137. }
  138. }
  139. return nullptr;
  140. }
  141. }