OpenShot Audio Library | OpenShotAudio  0.6.0
juce_AudioFormatManager.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11  Agreement and JUCE Privacy Policy.
12 
13  End User License Agreement: www.juce.com/juce-7-licence
14  Privacy Policy: www.juce.com/juce-privacy-policy
15 
16  Or: You may also use this code under the terms of the GPL v3 (see
17  www.gnu.org/licenses).
18 
19  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21  DISCLAIMED.
22 
23  ==============================================================================
24 */
25 
26 namespace juce
27 {
28 
31 
32 //==============================================================================
33 void AudioFormatManager::registerFormat (AudioFormat* newFormat, bool makeThisTheDefaultFormat)
34 {
35  jassert (newFormat != nullptr);
36 
37  if (newFormat != nullptr)
38  {
39  #if JUCE_DEBUG
40  for (auto* af : knownFormats)
41  {
42  if (af->getFormatName() == newFormat->getFormatName())
43  jassertfalse; // trying to add the same format twice!
44  }
45  #endif
46 
47  if (makeThisTheDefaultFormat)
48  defaultFormatIndex = getNumKnownFormats();
49 
50  knownFormats.add (newFormat);
51  }
52 }
53 
55 {
56  registerFormat (new WavAudioFormat(), true);
57  registerFormat (new AiffAudioFormat(), false);
58 
59  #if JUCE_USE_FLAC
60  registerFormat (new FlacAudioFormat(), false);
61  #endif
62 
63  #if JUCE_USE_OGGVORBIS
64  registerFormat (new OggVorbisAudioFormat(), false);
65  #endif
66 
67  #if JUCE_MAC || JUCE_IOS
68  registerFormat (new CoreAudioFormat(), false);
69  #endif
70 
71  #if JUCE_USE_MP3AUDIOFORMAT
72  registerFormat (new MP3AudioFormat(), false);
73  #endif
74 
75  #if JUCE_USE_WINDOWS_MEDIA_FORMAT
76  registerFormat (new WindowsMediaAudioFormat(), false);
77  #endif
78 }
79 
81 {
82  knownFormats.clear();
83  defaultFormatIndex = 0;
84 }
85 
86 int AudioFormatManager::getNumKnownFormats() const { return knownFormats.size(); }
87 AudioFormat* AudioFormatManager::getKnownFormat (int index) const { return knownFormats[index]; }
88 AudioFormat* AudioFormatManager::getDefaultFormat() const { return getKnownFormat (defaultFormatIndex); }
89 
91 {
92  if (! fileExtension.startsWithChar ('.'))
93  return findFormatForFileExtension ("." + fileExtension);
94 
95  for (auto* af : knownFormats)
96  if (af->getFileExtensions().contains (fileExtension, true))
97  return af;
98 
99  return nullptr;
100 }
101 
103 {
104  StringArray extensions;
105 
106  for (auto* af : knownFormats)
107  extensions.addArray (af->getFileExtensions());
108 
109  extensions.trim();
110  extensions.removeEmptyStrings();
111 
112  for (auto& e : extensions)
113  e = (e.startsWithChar ('.') ? "*" : "*.") + e;
114 
115  extensions.removeDuplicates (true);
116  return extensions.joinIntoString (";");
117 }
118 
119 //==============================================================================
121 {
122  // you need to actually register some formats before the manager can
123  // use them to open a file!
124  jassert (getNumKnownFormats() > 0);
125 
126  for (auto* af : knownFormats)
127  if (af->canHandleFile (file))
128  if (auto in = file.createInputStream())
129  if (auto* r = af->createReaderFor (in.release(), true))
130  return r;
131 
132  return nullptr;
133 }
134 
135 AudioFormatReader* AudioFormatManager::createReaderFor (std::unique_ptr<InputStream> audioFileStream)
136 {
137  // you need to actually register some formats before the manager can
138  // use them to open a file!
139  jassert (getNumKnownFormats() > 0);
140 
141  if (audioFileStream != nullptr)
142  {
143  auto originalStreamPos = audioFileStream->getPosition();
144 
145  for (auto* af : knownFormats)
146  {
147  if (auto* r = af->createReaderFor (audioFileStream.get(), false))
148  {
149  audioFileStream.release();
150  return r;
151  }
152 
153  audioFileStream->setPosition (originalStreamPos);
154 
155  // the stream that is passed-in must be capable of being repositioned so
156  // that all the formats can have a go at opening it.
157  jassert (audioFileStream->getPosition() == originalStreamPos);
158  }
159  }
160 
161  return nullptr;
162 }
163 
164 } // namespace juce
AudioFormatReader * createReaderFor(const File &audioFile)
AudioFormat * getKnownFormat(int index) const
void registerFormat(AudioFormat *newFormat, bool makeThisTheDefaultFormat)
AudioFormat * getDefaultFormat() const
AudioFormat * findFormatForFileExtension(const String &fileExtension) const
const String & getFormatName() const
std::unique_ptr< FileInputStream > createInputStream() const
Definition: juce_File.cpp:732
String joinIntoString(StringRef separatorString, int startIndex=0, int numberOfElements=-1) const
void removeEmptyStrings(bool removeWhitespaceStrings=true)
void addArray(const StringArray &other, int startIndex=0, int numElementsToAdd=-1)
void removeDuplicates(bool ignoreCase)
bool startsWithChar(juce_wchar character) const noexcept