OpenShot Audio Library | OpenShotAudio: juce_AudioFormatManager.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.3.0

Loading...
Searching...
No Matches
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
27namespace juce
28{
29
30AudioFormatManager::AudioFormatManager() {}
31AudioFormatManager::~AudioFormatManager() {}
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
55 void AudioFormatManager::registerBasicFormats()
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
81 void AudioFormatManager::clearFormats()
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
91 AudioFormat* AudioFormatManager::findFormatForFileExtension (const String& fileExtension) const
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
103 String AudioFormatManager::getWildcardForAllFormats() const
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//==============================================================================
121 AudioFormatReader* AudioFormatManager::createReaderFor (const File& file)
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
136 AudioFormatReader* AudioFormatManager::createReaderFor (InputStream* audioFileStream)
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
juce::AiffAudioFormat
Definition: juce_AiffAudioFormat.h:39
juce::AudioFormatManager::createReaderFor
AudioFormatReader * createReaderFor(const File &audioFile)
Definition: juce_AudioFormatManager.cpp:121
juce::AudioFormatManager::AudioFormatManager
AudioFormatManager()
Definition: juce_AudioFormatManager.cpp:30
juce::AudioFormatManager::getKnownFormat
AudioFormat * getKnownFormat(int index) const
Definition: juce_AudioFormatManager.cpp:88
juce::AudioFormatManager::registerFormat
void registerFormat(AudioFormat *newFormat, bool makeThisTheDefaultFormat)
Definition: juce_AudioFormatManager.cpp:34
juce::AudioFormatManager::~AudioFormatManager
~AudioFormatManager()
Definition: juce_AudioFormatManager.cpp:31
juce::AudioFormatManager::getDefaultFormat
AudioFormat * getDefaultFormat() const
Definition: juce_AudioFormatManager.cpp:89
juce::AudioFormatManager::findFormatForFileExtension
AudioFormat * findFormatForFileExtension(const String &fileExtension) const
Definition: juce_AudioFormatManager.cpp:91
juce::AudioFormatManager::getNumKnownFormats
int getNumKnownFormats() const
Definition: juce_AudioFormatManager.cpp:87
juce::AudioFormatManager::getWildcardForAllFormats
String getWildcardForAllFormats() const
Definition: juce_AudioFormatManager.cpp:103
juce::AudioFormatManager::clearFormats
void clearFormats()
Definition: juce_AudioFormatManager.cpp:81
juce::AudioFormatManager::registerBasicFormats
void registerBasicFormats()
Definition: juce_AudioFormatManager.cpp:55
juce::AudioFormatReader
Definition: juce_AudioFormatReader.h:45
juce::AudioFormat
Definition: juce_AudioFormat.h:40
juce::AudioFormat::getFormatName
const String & getFormatName() const
Definition: juce_AudioFormat.cpp:53
juce::File
Definition: juce_File.h:41
juce::File::createInputStream
FileInputStream * createInputStream() const
Definition: juce_File.cpp:729
juce::InputStream
Definition: juce_InputStream.h:37
juce::StringArray
Definition: juce_StringArray.h:35
juce::StringArray::joinIntoString
String joinIntoString(StringRef separatorString, int startIndex=0, int numberOfElements=-1) const
Definition: juce_StringArray.cpp:288
juce::StringArray::removeEmptyStrings
void removeEmptyStrings(bool removeWhitespaceStrings=true)
Definition: juce_StringArray.cpp:249
juce::StringArray::addArray
void addArray(const StringArray &other, int startIndex=0, int numElementsToAdd=-1)
Definition: juce_StringArray.cpp:158
juce::StringArray::removeDuplicates
void removeDuplicates(bool ignoreCase)
Definition: juce_StringArray.cpp:410
juce::StringArray::trim
void trim()
Definition: juce_StringArray.cpp:265
juce::String
Definition: juce_String.h:39
juce::String::startsWithChar
bool startsWithChar(juce_wchar character) const noexcept
Definition: juce_String.cpp:1414
juce::WavAudioFormat
Definition: juce_WavAudioFormat.h:39