OpenShot Library | libopenshot-audio  0.2.0
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
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
int getNumKnownFormats() const
Returns the number of currently registered file formats.
Reads and Writes AIFF format audio files.
void registerFormat(AudioFormat *newFormat, bool makeThisTheDefaultFormat)
Adds a format to the manager&#39;s list of available file types.
void addArray(const StringArray &other, int startIndex=0, int numElementsToAdd=-1)
Appends some strings from another array to the end of this one.
void registerBasicFormats()
Handy method to make it easy to register the formats that come with JUCE.
Reads and writes the Ogg-Vorbis audio format.
The base class for streams that read data.
void removeEmptyStrings(bool removeWhitespaceStrings=true)
Removes empty strings from the array.
void clearFormats()
Clears the list of known formats.
A special array for holding a list of strings.
The JUCE String class!
Definition: juce_String.h:42
FileInputStream * createInputStream() const
Creates a stream to read from this file.
Definition: juce_File.cpp:709
String getWildcardForAllFormats() const
Returns a set of wildcards for file-matching that contains the extensions for all known formats...
Audio format which uses the Windows Media codecs (Windows only).
Reads and writes the lossless-compression FLAC audio format.
void trim()
Deletes any whitespace characters from the starts and ends of all the strings.
Reads and Writes WAV format audio files.
AudioFormat * getDefaultFormat() const
Returns the format which has been set as the default one.
AudioFormatManager()
Creates an empty format manager.
Subclasses of AudioFormat are used to read and write different audio file formats.
OSX and iOS only - This uses the AudioToolbox framework to read any audio format that the system has ...
AudioFormatReader * createReaderFor(const File &audioFile)
Searches through the known formats to try to create a suitable reader for this file.
Represents a local file or directory.
Definition: juce_File.h:44
Software-based MP3 decoding format (doesn&#39;t currently provide an encoder).
AudioFormat * getKnownFormat(int index) const
Returns one of the registered file formats.
bool startsWithChar(juce_wchar character) const noexcept
Tests whether the string begins with a particular character.
AudioFormat * findFormatForFileExtension(const String &fileExtension) const
Looks for which of the known formats is listed as being for a given file extension.
Reads samples from an audio file stream.
const String & getFormatName() const
Returns the name of this format.