OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
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
29 AudioFormatManager::AudioFormatManager() {}
30 AudioFormatManager::~AudioFormatManager() {}
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
54 void AudioFormatManager::registerBasicFormats()
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
80 void AudioFormatManager::clearFormats()
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
90 AudioFormat* AudioFormatManager::findFormatForFileExtension (const String& fileExtension) const
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
102 String AudioFormatManager::getWildcardForAllFormats() const
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//==============================================================================
120 AudioFormatReader* AudioFormatManager::createReaderFor (const File& file)
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
juce::AiffAudioFormat
Definition juce_AiffAudioFormat.h:38
juce::AudioFormatManager::createReaderFor
AudioFormatReader * createReaderFor(const File &audioFile)
Definition juce_AudioFormatManager.cpp:120
juce::AudioFormatManager::AudioFormatManager
AudioFormatManager()
Definition juce_AudioFormatManager.cpp:29
juce::AudioFormatManager::getKnownFormat
AudioFormat * getKnownFormat(int index) const
Definition juce_AudioFormatManager.cpp:87
juce::AudioFormatManager::registerFormat
void registerFormat(AudioFormat *newFormat, bool makeThisTheDefaultFormat)
Definition juce_AudioFormatManager.cpp:33
juce::AudioFormatManager::~AudioFormatManager
~AudioFormatManager()
Definition juce_AudioFormatManager.cpp:30
juce::AudioFormatManager::getDefaultFormat
AudioFormat * getDefaultFormat() const
Definition juce_AudioFormatManager.cpp:88
juce::AudioFormatManager::findFormatForFileExtension
AudioFormat * findFormatForFileExtension(const String &fileExtension) const
Definition juce_AudioFormatManager.cpp:90
juce::AudioFormatManager::getNumKnownFormats
int getNumKnownFormats() const
Definition juce_AudioFormatManager.cpp:86
juce::AudioFormatManager::getWildcardForAllFormats
String getWildcardForAllFormats() const
Definition juce_AudioFormatManager.cpp:102
juce::AudioFormatManager::clearFormats
void clearFormats()
Definition juce_AudioFormatManager.cpp:80
juce::AudioFormatManager::registerBasicFormats
void registerBasicFormats()
Definition juce_AudioFormatManager.cpp:54
juce::AudioFormatReader
Definition juce_AudioFormatReader.h:44
juce::AudioFormat
Definition juce_AudioFormat.h:39
juce::AudioFormat::getFormatName
const String & getFormatName() const
Definition juce_AudioFormat.cpp:52
juce::File
Definition juce_File.h:45
juce::File::createInputStream
std::unique_ptr< FileInputStream > createInputStream() const
Definition juce_File.cpp:732
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:289
juce::StringArray::removeEmptyStrings
void removeEmptyStrings(bool removeWhitespaceStrings=true)
Definition juce_StringArray.cpp:250
juce::StringArray::addArray
void addArray(const StringArray &other, int startIndex=0, int numElementsToAdd=-1)
Definition juce_StringArray.cpp:159
juce::StringArray::removeDuplicates
void removeDuplicates(bool ignoreCase)
Definition juce_StringArray.cpp:411
juce::StringArray::trim
void trim()
Definition juce_StringArray.cpp:266
juce::String
Definition juce_String.h:53
juce::String::startsWithChar
bool startsWithChar(juce_wchar character) const noexcept
Definition juce_String.cpp:1403
juce::WavAudioFormat
Definition juce_WavAudioFormat.h:38