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