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
juce::AudioFormat::getFormatName
const String & getFormatName() const
Returns the name of this format.
Definition: juce_AudioFormat.cpp:53
juce::StringArray
A special array for holding a list of strings.
Definition: juce_StringArray.h:38
juce::WindowsMediaAudioFormat
Audio format which uses the Windows Media codecs (Windows only).
Definition: juce_WindowsMediaAudioFormat.h:42
juce::AiffAudioFormat
Reads and Writes AIFF format audio files.
Definition: juce_AiffAudioFormat.h:42
juce::AudioFormatManager::~AudioFormatManager
~AudioFormatManager()
Destructor.
Definition: juce_AudioFormatManager.cpp:31
juce::WavAudioFormat
Reads and Writes WAV format audio files.
Definition: juce_WavAudioFormat.h:42
juce::AudioFormatManager::clearFormats
void clearFormats()
Clears the list of known formats.
Definition: juce_AudioFormatManager.cpp:81
juce::StringArray::trim
void trim()
Deletes any whitespace characters from the starts and ends of all the strings.
Definition: juce_StringArray.cpp:265
juce::StringArray::removeEmptyStrings
void removeEmptyStrings(bool removeWhitespaceStrings=true)
Removes empty strings from the array.
Definition: juce_StringArray.cpp:249
juce::AudioFormatManager::createReaderFor
AudioFormatReader * createReaderFor(const File &audioFile)
Searches through the known formats to try to create a suitable reader for this file.
Definition: juce_AudioFormatManager.cpp:121
juce::AudioFormatManager::registerBasicFormats
void registerBasicFormats()
Handy method to make it easy to register the formats that come with JUCE.
Definition: juce_AudioFormatManager.cpp:55
juce::InputStream
The base class for streams that read data.
Definition: juce_InputStream.h:40
juce::FlacAudioFormat
Reads and writes the lossless-compression FLAC audio format.
Definition: juce_FlacAudioFormat.h:46
juce::AudioFormat
Subclasses of AudioFormat are used to read and write different audio file formats.
Definition: juce_AudioFormat.h:43
juce::AudioFormatManager::registerFormat
void registerFormat(AudioFormat *newFormat, bool makeThisTheDefaultFormat)
Adds a format to the manager's list of available file types.
Definition: juce_AudioFormatManager.cpp:34
juce::StringArray::addArray
void addArray(const StringArray &other, int startIndex=0, int numElementsToAdd=-1)
Appends some strings from another array to the end of this one.
Definition: juce_StringArray.cpp:158
juce::File
Represents a local file or directory.
Definition: juce_File.h:44
juce::AudioFormatManager::getDefaultFormat
AudioFormat * getDefaultFormat() const
Returns the format which has been set as the default one.
Definition: juce_AudioFormatManager.cpp:89
juce::CoreAudioFormat
OSX and iOS only - This uses the AudioToolbox framework to read any audio format that the system has ...
Definition: juce_CoreAudioFormat.h:47
juce::OggVorbisAudioFormat
Reads and writes the Ogg-Vorbis audio format.
Definition: juce_OggVorbisAudioFormat.h:46
juce::AudioFormatManager::getNumKnownFormats
int getNumKnownFormats() const
Returns the number of currently registered file formats.
Definition: juce_AudioFormatManager.cpp:87
juce::File::createInputStream
FileInputStream * createInputStream() const
Creates a stream to read from this file.
Definition: juce_File.cpp:709
juce::MP3AudioFormat
Software-based MP3 decoding format (doesn't currently provide an encoder).
Definition: juce_MP3AudioFormat.h:52
juce::String
The JUCE String class!
Definition: juce_String.h:42
juce::AudioFormatManager::findFormatForFileExtension
AudioFormat * findFormatForFileExtension(const String &fileExtension) const
Looks for which of the known formats is listed as being for a given file extension.
Definition: juce_AudioFormatManager.cpp:91
juce::AudioFormatManager::AudioFormatManager
AudioFormatManager()
Creates an empty format manager.
Definition: juce_AudioFormatManager.cpp:30
juce::AudioFormatManager::getWildcardForAllFormats
String getWildcardForAllFormats() const
Returns a set of wildcards for file-matching that contains the extensions for all known formats.
Definition: juce_AudioFormatManager.cpp:103
juce::String::startsWithChar
bool startsWithChar(juce_wchar character) const noexcept
Tests whether the string begins with a particular character.
Definition: juce_String.cpp:1409
juce::AudioFormatReader
Reads samples from an audio file stream.
Definition: juce_AudioFormatReader.h:48
juce::AudioFormatManager::getKnownFormat
AudioFormat * getKnownFormat(int index) const
Returns one of the registered file formats.
Definition: juce_AudioFormatManager.cpp:88