LMMS
Loading...
Searching...
No Matches
juce_AudioFormatManager.cpp
Go to the documentation of this file.
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
26namespace juce
27{
28
31
32//==============================================================================
33void 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)
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
85
87AudioFormat* AudioFormatManager::getKnownFormat (int index) const { return knownFormats[index]; }
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!
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
135AudioFormatReader* 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!
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
Definition juce_AiffAudioFormat.h:38
Definition juce_AudioFormat.h:39
const String & getFormatName() const
Definition juce_AudioFormat.cpp:52
OwnedArray< AudioFormat > knownFormats
Definition juce_AudioFormatManager.h:144
AudioFormatReader * createReaderFor(const File &audioFile)
Definition juce_AudioFormatManager.cpp:120
AudioFormatManager()
Definition juce_AudioFormatManager.cpp:29
AudioFormat * getKnownFormat(int index) const
Definition juce_AudioFormatManager.cpp:87
void registerFormat(AudioFormat *newFormat, bool makeThisTheDefaultFormat)
Definition juce_AudioFormatManager.cpp:33
int defaultFormatIndex
Definition juce_AudioFormatManager.h:145
~AudioFormatManager()
Definition juce_AudioFormatManager.cpp:30
AudioFormat * getDefaultFormat() const
Definition juce_AudioFormatManager.cpp:88
AudioFormat * findFormatForFileExtension(const String &fileExtension) const
Definition juce_AudioFormatManager.cpp:90
int getNumKnownFormats() const
Definition juce_AudioFormatManager.cpp:86
String getWildcardForAllFormats() const
Definition juce_AudioFormatManager.cpp:102
void clearFormats()
Definition juce_AudioFormatManager.cpp:80
void registerBasicFormats()
Definition juce_AudioFormatManager.cpp:54
Definition juce_AudioFormatReader.h:44
Definition juce_File.h:45
Definition juce_StringArray.h:35
String joinIntoString(StringRef separatorString, int startIndex=0, int numberOfElements=-1) const
Definition juce_StringArray.cpp:289
void removeEmptyStrings(bool removeWhitespaceStrings=true)
Definition juce_StringArray.cpp:250
void addArray(const StringArray &other, int startIndex=0, int numElementsToAdd=-1)
Definition juce_StringArray.cpp:159
void removeDuplicates(bool ignoreCase)
Definition juce_StringArray.cpp:411
void trim()
Definition juce_StringArray.cpp:266
Definition juce_String.h:53
bool startsWithChar(juce_wchar character) const noexcept
Definition juce_String.cpp:1403
Definition juce_WavAudioFormat.h:38
* e
Definition inflate.c:1404
#define jassert(expression)
#define jassertfalse
float in
Definition lilv_test.c:1460
Definition carla_juce.cpp:31
int r
Definition crypt.c:458
struct zdirent * file
Definition win32.c:1500