Redesigned the audio encoding/decoding classes to get rid of libsndfile

This commit is contained in:
Laurent Gomila 2014-07-20 17:48:40 +02:00 committed by Mario Liebisch
parent 34692d5a39
commit f0608eaed8
68 changed files with 4523 additions and 2241 deletions

View file

@ -35,6 +35,11 @@
#include <SFML/Audio/Sound.hpp>
#include <SFML/Audio/SoundBuffer.hpp>
#include <SFML/Audio/SoundBufferRecorder.hpp>
#include <SFML/Audio/InputSoundFile.hpp>
#include <SFML/Audio/OutputSoundFile.hpp>
#include <SFML/Audio/SoundFileFactory.hpp>
#include <SFML/Audio/SoundFileReader.hpp>
#include <SFML/Audio/SoundFileWriter.hpp>
#include <SFML/Audio/SoundRecorder.hpp>
#include <SFML/Audio/SoundStream.hpp>

View file

@ -0,0 +1,250 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_INPUTSOUNDFILE_HPP
#define SFML_INPUTSOUNDFILE_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio/Export.hpp>
#include <SFML/System/NonCopyable.hpp>
#include <SFML/System/Time.hpp>
#include <string>
namespace sf
{
class InputStream;
class SoundFileReader;
////////////////////////////////////////////////////////////
/// \brief Provide read access to sound files
///
////////////////////////////////////////////////////////////
class SFML_AUDIO_API InputSoundFile : NonCopyable
{
public :
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
////////////////////////////////////////////////////////////
InputSoundFile();
////////////////////////////////////////////////////////////
/// \brief Destructor
///
////////////////////////////////////////////////////////////
~InputSoundFile();
////////////////////////////////////////////////////////////
/// \brief Open a sound file from the disk for reading
///
/// The supported audio formats are: WAV, OGG/Vorbis, FLAC.
///
/// \param filename Path of the sound file to load
///
/// \return True if the file was successfully opened
///
////////////////////////////////////////////////////////////
bool openFromFile(const std::string& filename);
////////////////////////////////////////////////////////////
/// \brief Open a sound file in memory for reading
///
/// The supported audio formats are: WAV, OGG/Vorbis, FLAC.
///
/// \param data Pointer to the file data in memory
/// \param sizeInBytes Size of the data to load, in bytes
///
/// \return True if the file was successfully opened
///
////////////////////////////////////////////////////////////
bool openFromMemory(const void* data, std::size_t sizeInBytes);
////////////////////////////////////////////////////////////
/// \brief Open a sound file from a custom stream for reading
///
/// The supported audio formats are: WAV, OGG/Vorbis, FLAC.
///
/// \param stream Source stream to read from
///
/// \return True if the file was successfully opened
///
////////////////////////////////////////////////////////////
bool openFromStream(InputStream& stream);
////////////////////////////////////////////////////////////
/// \brief Open the sound file from the disk for writing
///
/// \param filename Path of the sound file to write
/// \param channelCount Number of channels in the sound
/// \param sampleRate Sample rate of the sound
///
/// \return True if the file was successfully opened
///
////////////////////////////////////////////////////////////
bool openForWriting(const std::string& filename, unsigned int channelCount, unsigned int sampleRate);
////////////////////////////////////////////////////////////
/// \brief Get the total number of audio samples in the file
///
/// \return Number of samples
///
////////////////////////////////////////////////////////////
Uint64 getSampleCount() const;
////////////////////////////////////////////////////////////
/// \brief Get the number of channels used by the sound
///
/// \return Number of channels (1 = mono, 2 = stereo)
///
////////////////////////////////////////////////////////////
unsigned int getChannelCount() const;
////////////////////////////////////////////////////////////
/// \brief Get the sample rate of the sound
///
/// \return Sample rate, in samples per second
///
////////////////////////////////////////////////////////////
unsigned int getSampleRate() const;
////////////////////////////////////////////////////////////
/// \brief Get the total duration of the sound file
///
/// This function is provided for convenience, the duration is
/// deduced from the other sound file attributes.
///
/// \return Duration of the sound file
///
////////////////////////////////////////////////////////////
Time getDuration() const;
////////////////////////////////////////////////////////////
/// \brief Change the current read position to the given sample offset
///
/// This function takes a sample offset to provide maximum
/// precision. If you need to jump to a given time, use the
/// other overload.
///
/// If the given offset exceeds to total number of samples,
/// this function jumps to the end of the sound file.
///
/// \param sampleOffset Index of the sample to jump to, relative to the beginning
///
////////////////////////////////////////////////////////////
void seek(Uint64 sampleOffset);
////////////////////////////////////////////////////////////
/// \brief Change the current read position to the given time offset
///
/// Using a time offset is handy but unprecise. If you need an accurate
/// result, consider using the overload which takes a sample offset.
///
/// If the given time exceeds to total duration, this function jumps
/// to the end of the sound file.
///
/// \param timeOffset Time to jump to, relative to the beginning
///
////////////////////////////////////////////////////////////
void seek(Time timeOffset);
////////////////////////////////////////////////////////////
/// \brief Read audio samples from the open file
///
/// \param samples Pointer to the sample array to fill
/// \param maxCount Maximum number of samples to read
///
/// \return Number of samples actually read (may be less than \a maxCount)
///
////////////////////////////////////////////////////////////
Uint64 read(Int16* samples, Uint64 maxCount);
private:
////////////////////////////////////////////////////////////
/// \brief Close the current file
///
////////////////////////////////////////////////////////////
void close();
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
SoundFileReader* m_reader; ///< Reader that handles I/O on the file's format
InputStream* m_stream; ///< Input stream used to access the file's data
bool m_streamOwned; ///< Is the stream internal or external?
Uint64 m_sampleCount; ///< Total number of samples in the file
unsigned int m_channelCount; ///< Number of channels of the sound
unsigned int m_sampleRate; ///< Number of samples per second
};
} // namespace sf
#endif // SFML_INPUTSOUNDFILE_HPP
////////////////////////////////////////////////////////////
/// \class sf::InputSoundFile
/// \ingroup audio
///
/// This class decodes audio samples from a sound file. It is
/// used internally by higher-level classes such as sf::SoundBuffer
/// and sf::Music, but can also be useful if you want to process
/// or analyze audio files without playing them, or if you want to
/// implement your own version of sf::Music with more specific
/// features.
///
/// Usage example:
/// \code
/// // Open a sound file
/// sf::InputSoundFile file;
/// if (!file.openFromFile("music.ogg"))
/// /* error */;
///
/// // Print the sound attributes
/// std::cout << "duration: " << file.getDuration().asSeconds() << std::endl;
/// std::cout << "channels: " << file.getChannelCount() << std::endl;
/// std::cout << "sample rate: " << file.getSampleRate() << std::endl;
/// std::cout << "sample count: " << file.getSampleCount() << std::endl;
///
/// // Read and process batches of samples until the end of file is reached
/// sf::Int16 samples[1024];
/// sf::Uint64 count;
/// do
/// {
/// count = file.read(samples, 1024);
///
/// // process, analyse, play, convert, or whatever
/// // you want to do with the samples...
/// }
/// while (count > 0);
/// \endcode
///
/// \see sf::SoundFileReader, sf::OutputSoundFile
///
////////////////////////////////////////////////////////////

View file

@ -30,6 +30,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Audio/Export.hpp>
#include <SFML/Audio/SoundStream.hpp>
#include <SFML/Audio/InputSoundFile.hpp>
#include <SFML/System/Mutex.hpp>
#include <SFML/System/Time.hpp>
#include <string>
@ -38,11 +39,6 @@
namespace sf
{
namespace priv
{
class SoundFile;
}
class InputStream;
////////////////////////////////////////////////////////////
@ -51,7 +47,7 @@ class InputStream;
////////////////////////////////////////////////////////////
class SFML_AUDIO_API Music : public SoundStream
{
public:
public :
////////////////////////////////////////////////////////////
/// \brief Default constructor
@ -70,9 +66,8 @@ public:
///
/// This function doesn't start playing the music (call play()
/// to do so).
/// Here is a complete list of all the supported audio formats:
/// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam,
/// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
/// See the documentation of sf::InputSoundFile for the list
/// of supported formats.
///
/// \param filename Path of the music file to open
///
@ -88,12 +83,11 @@ public:
///
/// This function doesn't start playing the music (call play()
/// to do so).
/// Here is a complete list of all the supported audio formats:
/// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam,
/// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
/// See the documentation of sf::InputSoundFile for the list
/// of supported formats.
/// Since the music is not loaded completely but rather streamed
/// continuously, the \a data must remain available as long as the
/// music is playing (i.e. you can't deallocate it right after calling
/// music is playing (ie. you can't deallocate it right after calling
/// this function).
///
/// \param data Pointer to the file data in memory
@ -111,12 +105,11 @@ public:
///
/// This function doesn't start playing the music (call play()
/// to do so).
/// Here is a complete list of all the supported audio formats:
/// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam,
/// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
/// See the documentation of sf::InputSoundFile for the list
/// of supported formats.
/// Since the music is not loaded completely but rather streamed
/// continuously, the \a stream must remain alive as long as the
/// music is playing (i.e. you can't destroy it right after calling
/// music is playing (ie. you can't destroy it right after calling
/// this function).
///
/// \param stream Source stream to read from
@ -136,7 +129,7 @@ public:
////////////////////////////////////////////////////////////
Time getDuration() const;
protected:
protected :
////////////////////////////////////////////////////////////
/// \brief Request a new chunk of audio samples from the stream source
@ -159,7 +152,7 @@ protected:
////////////////////////////////////////////////////////////
virtual void onSeek(Time timeOffset);
private:
private :
////////////////////////////////////////////////////////////
/// \brief Initialize the internal state after loading a new music
@ -170,7 +163,7 @@ private:
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
priv::SoundFile* m_file; ///< Sound file
InputSoundFile m_file; ///< The streamed music file
Time m_duration; ///< Music duration
std::vector<Int16> m_samples; ///< Temporary buffer of samples
Mutex m_mutex; ///< Mutex protecting the data
@ -186,13 +179,13 @@ private:
/// \class sf::Music
/// \ingroup audio
///
/// Music objects are sounds that are streamed rather than
/// completely loaded in memory. This is especially useful for
/// compressed music that usually takes hundreds of MB when it is
/// Musics are sounds that are streamed rather than completely
/// loaded in memory. This is especially useful for compressed
/// musics that usually take hundreds of MB when they are
/// uncompressed: by streaming it instead of loading it entirely,
/// you avoid saturating the memory and have almost no loading delay.
///
/// Apart from that, sf::Music has almost the same features as
/// Apart from that, a sf::Music has almost the same features as
/// the sf::SoundBuffer / sf::Sound pair: you can play/pause/stop
/// it, request its parameters (channels, sample rate), change
/// the way it is played (pitch, volume, 3D position, ...), etc.

View file

@ -0,0 +1,133 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_OUTPUTSOUNDFILE_HPP
#define SFML_OUTPUTSOUNDFILE_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio/Export.hpp>
#include <SFML/System/NonCopyable.hpp>
#include <string>
namespace sf
{
class SoundFileWriter;
////////////////////////////////////////////////////////////
/// \brief Provide write access to sound files
///
////////////////////////////////////////////////////////////
class SFML_AUDIO_API OutputSoundFile : NonCopyable
{
public :
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
////////////////////////////////////////////////////////////
OutputSoundFile();
////////////////////////////////////////////////////////////
/// \brief Destructor
///
/// Closes the file if it was still open.
///
////////////////////////////////////////////////////////////
~OutputSoundFile();
////////////////////////////////////////////////////////////
/// \brief Open the sound file from the disk for writing
///
/// The supported audio formats are: WAV, OGG/Vorbis, FLAC.
///
/// \param filename Path of the sound file to write
/// \param sampleRate Sample rate of the sound
/// \param channelCount Number of channels in the sound
///
/// \return True if the file was successfully opened
///
////////////////////////////////////////////////////////////
bool openFromFile(const std::string& filename, unsigned int sampleRate, unsigned int channelCount);
////////////////////////////////////////////////////////////
/// \brief Write audio samples to the file
///
/// \param samples Pointer to the sample array to write
/// \param sampleCount Number of samples to write
///
////////////////////////////////////////////////////////////
void write(const Int16* samples, Uint64 count);
private:
////////////////////////////////////////////////////////////
/// \brief Close the current file
///
////////////////////////////////////////////////////////////
void close();
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
SoundFileWriter* m_writer; ///< Writer that handles I/O on the file's format
};
} // namespace sf
#endif // SFML_OUTPUTSOUNDFILE_HPP
////////////////////////////////////////////////////////////
/// \class sf::OutputSoundFile
/// \ingroup audio
///
/// This class encodes audio samples to a sound file. It is
/// used internally by higher-level classes such as sf::SoundBuffer,
/// but can also be useful if you want to create audio files from
/// custom data sources, like generated audio samples.
///
/// Usage example:
/// \code
/// // Create a sound file, ogg/vorbis format, 44100 Hz, stereo
/// sf::OutputSoundFile file;
/// if (!file.openFromFile("music.ogg", 44100, 2))
/// /* error */;
///
/// while (...)
/// {
/// // Read or generate audio samples from your custom source
/// std::vector<sf::Int16> samples = ...;
///
/// // Write them to the file
/// file.write(samples.data(), samples.size());
/// }
/// \endcode
///
/// \see sf::SoundFileWriter, sf::InputSoundFile
///
////////////////////////////////////////////////////////////

View file

@ -38,12 +38,8 @@
namespace sf
{
namespace priv
{
class SoundFile;
}
class Sound;
class InputSoundFile;
class InputStream;
////////////////////////////////////////////////////////////
@ -52,7 +48,7 @@ class InputStream;
////////////////////////////////////////////////////////////
class SFML_AUDIO_API SoundBuffer : AlResource
{
public:
public :
////////////////////////////////////////////////////////////
/// \brief Default constructor
@ -77,9 +73,8 @@ public:
////////////////////////////////////////////////////////////
/// \brief Load the sound buffer from a file
///
/// Here is a complete list of all the supported audio formats:
/// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam,
/// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
/// See the documentation of sf::InputSoundFile for the list
/// of supported formats.
///
/// \param filename Path of the sound file to load
///
@ -93,9 +88,8 @@ public:
////////////////////////////////////////////////////////////
/// \brief Load the sound buffer from a file in memory
///
/// Here is a complete list of all the supported audio formats:
/// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam,
/// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
/// See the documentation of sf::InputSoundFile for the list
/// of supported formats.
///
/// \param data Pointer to the file data in memory
/// \param sizeInBytes Size of the data to load, in bytes
@ -110,9 +104,8 @@ public:
////////////////////////////////////////////////////////////
/// \brief Load the sound buffer from a custom stream
///
/// Here is a complete list of all the supported audio formats:
/// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam,
/// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
/// See the documentation of sf::InputSoundFile for the list
/// of supported formats.
///
/// \param stream Source stream to read from
///
@ -139,14 +132,13 @@ public:
/// \see loadFromFile, loadFromMemory, saveToFile
///
////////////////////////////////////////////////////////////
bool loadFromSamples(const Int16* samples, std::size_t sampleCount, unsigned int channelCount, unsigned int sampleRate);
bool loadFromSamples(const Int16* samples, Uint64 sampleCount, unsigned int channelCount, unsigned int sampleRate);
////////////////////////////////////////////////////////////
/// \brief Save the sound buffer to an audio file
///
/// Here is a complete list of all the supported audio formats:
/// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam,
/// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
/// See the documentation of sf::OutputSoundFile for the list
/// of supported formats.
///
/// \param filename Path of the sound file to write
///
@ -182,7 +174,7 @@ public:
/// \see getSamples
///
////////////////////////////////////////////////////////////
std::size_t getSampleCount() const;
Uint64 getSampleCount() const;
////////////////////////////////////////////////////////////
/// \brief Get the sample rate of the sound
@ -231,7 +223,7 @@ public:
////////////////////////////////////////////////////////////
SoundBuffer& operator =(const SoundBuffer& right);
private:
private :
friend class Sound;
@ -243,7 +235,7 @@ private:
/// \return True on succesful initialization, false on failure
///
////////////////////////////////////////////////////////////
bool initialize(priv::SoundFile& file);
bool initialize(InputSoundFile& file);
////////////////////////////////////////////////////////////
/// \brief Update the internal buffer with the cached audio samples

View file

@ -0,0 +1,189 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_SOUNDFILEFACTORY_HPP
#define SFML_SOUNDFILEFACTORY_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio/Export.hpp>
#include <string>
#include <vector>
namespace sf
{
class InputStream;
class SoundFileReader;
class SoundFileWriter;
////////////////////////////////////////////////////////////
/// \brief Manages and instanciates sound file readers and writers
///
////////////////////////////////////////////////////////////
class SFML_AUDIO_API SoundFileFactory
{
public :
////////////////////////////////////////////////////////////
/// \brief Register a new reader
///
/// \see unregisterReader
///
////////////////////////////////////////////////////////////
template <typename T>
static void registerReader();
////////////////////////////////////////////////////////////
/// \brief Unregister a reader
///
/// \see registerReader
///
////////////////////////////////////////////////////////////
template <typename T>
static void unregisterReader();
////////////////////////////////////////////////////////////
/// \brief Register a new writer
///
/// \see unregisterWriter
///
////////////////////////////////////////////////////////////
template <typename T>
static void registerWriter();
////////////////////////////////////////////////////////////
/// \brief Unregister a writer
///
/// \see registerWriter
///
////////////////////////////////////////////////////////////
template <typename T>
static void unregisterWriter();
////////////////////////////////////////////////////////////
/// \brief Instanciate the right reader for the given file on disk
///
/// \param filename Path of the sound file
///
/// \return A new sound file reader that can read the given file, or null if no reader can handle it
///
/// \see createReaderFromMemory, createReaderFromStream
///
////////////////////////////////////////////////////////////
static SoundFileReader* createReaderFromFilename(const std::string& filename);
////////////////////////////////////////////////////////////
/// \brief Instanciate the right codec for the given file in memory
///
/// \param data Pointer to the file data in memory
/// \param sizeInBytes Total size of the file data, in bytes
///
/// \return A new sound file codec that can read the given file, or null if no codec can handle it
///
/// \see createReaderFromFilename, createReaderFromStream
///
////////////////////////////////////////////////////////////
static SoundFileReader* createReaderFromMemory(const void* data, std::size_t sizeInBytes);
////////////////////////////////////////////////////////////
/// \brief Instanciate the right codec for the given file in stream
///
/// \param stream Source stream to read from
///
/// \return A new sound file codec that can read the given file, or null if no codec can handle it
///
/// \see createReaderFromFilename, createReaderFromMemory
///
////////////////////////////////////////////////////////////
static SoundFileReader* createReaderFromStream(InputStream& stream);
////////////////////////////////////////////////////////////
/// \brief Instanciate the right writer for the given file on disk
///
/// \param filename Path of the sound file
///
/// \return A new sound file writer that can write given file, or null if no writer can handle it
///
////////////////////////////////////////////////////////////
static SoundFileWriter* createWriterFromFilename(const std::string& filename);
private:
////////////////////////////////////////////////////////////
// Types
////////////////////////////////////////////////////////////
struct ReaderFactory
{
bool (*check)(InputStream&);
SoundFileReader* (*create)();
};
typedef std::vector<ReaderFactory> ReaderFactoryArray;
struct WriterFactory
{
bool (*check)(const std::string&);
SoundFileWriter* (*create)();
};
typedef std::vector<WriterFactory> WriterFactoryArray;
////////////////////////////////////////////////////////////
// Static member data
////////////////////////////////////////////////////////////
static ReaderFactoryArray s_readers; ///< List of all registered readers
static WriterFactoryArray s_writers; ///< List of all registered writers
};
} // namespace sf
#include <SFML/Audio/SoundFileFactory.inl>
#endif // SFML_SOUNDFILEFACTORY_HPP
////////////////////////////////////////////////////////////
/// \class sf::SoundFileFactory
/// \ingroup audio
///
/// This class is where all the sound file readers and writers are
/// registered. You should normally only need to use its regitration
/// and unregistration functions; readers/writers creation and manipulation
/// are wrapped into the higher-level classes sf::InputSoundFile and
/// sf::OutputSoundFile.
///
/// To register a new reader (writer) use the sf::SoundFileFactory::registerReader
/// (registerWriter) static function. You don't have to call the unregisterReader
/// (unregisterWriter) function, unless you want to unregister a format before your
/// application ends (typically, when a plugin is unloaded).
///
/// Usage example:
/// \code
/// sf::SoundFileFactory::registerReader<MySoundFileReader>();
/// sf::SoundFileFactory::registerWriter<MySoundFileWriter>();
/// \endcode
///
/// \see sf::InputSoundFile, sf::OutputSoundFile, sf::SoundFileReader, sf::SoundFileWriter
///
////////////////////////////////////////////////////////////

View file

@ -0,0 +1,100 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
namespace sf
{
namespace priv
{
template <typename T> SoundFileReader* createReader() {return new T;}
template <typename T> SoundFileWriter* createWriter() {return new T;}
}
////////////////////////////////////////////////////////////
template <typename T>
void SoundFileFactory::registerReader()
{
// Make sure the same class won't be registered twice
unregisterReader<T>();
// Create a new factory with the functions provided by the class
ReaderFactory factory;
factory.check = &T::check;
factory.create = &priv::createReader<T>;
// Add it
s_readers.push_back(factory);
}
////////////////////////////////////////////////////////////
template <typename T>
void SoundFileFactory::unregisterReader()
{
// Remove the instance(s) of the reader from the array of factories
for (ReaderFactoryArray::iterator it = s_readers.begin(); it != s_readers.end(); )
{
if (it->create == &priv::createReader<T>)
it = s_readers.erase(it);
else
++it;
}
}
////////////////////////////////////////////////////////////
template <typename T>
void SoundFileFactory::registerWriter()
{
// Make sure the same class won't be registered twice
unregisterWriter<T>();
// Create a new factory with the functions provided by the class
WriterFactory factory;
factory.check = &T::check;
factory.create = &priv::createWriter<T>;
// Add it
s_writers.push_back(factory);
}
////////////////////////////////////////////////////////////
template <typename T>
void SoundFileFactory::unregisterWriter()
{
// Remove the instance(s) of the writer from the array of factories
for (WriterFactoryArray::iterator it = s_writers.begin(); it != s_writers.end(); )
{
if (it->create == &priv::createWriter<T>)
it = s_writers.erase(it);
else
++it;
}
}
} // namespace sf

View file

@ -0,0 +1,161 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_SOUNDFILEREADER_HPP
#define SFML_SOUNDFILEREADER_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio/Export.hpp>
#include <string>
namespace sf
{
class InputStream;
////////////////////////////////////////////////////////////
/// \brief Abstract base class for sound file decoding
///
////////////////////////////////////////////////////////////
class SFML_AUDIO_API SoundFileReader
{
public :
////////////////////////////////////////////////////////////
/// \brief Structure holding the audio properties of a sound file
///
////////////////////////////////////////////////////////////
struct Info
{
Uint64 sampleCount; ///< Total number of samples in the file
unsigned int channelCount; ///< Number of channels of the sound
unsigned int sampleRate; ///< Samples rate of the sound, in samples per second
};
////////////////////////////////////////////////////////////
/// \brief Virtual destructor
///
////////////////////////////////////////////////////////////
virtual ~SoundFileReader() {}
////////////////////////////////////////////////////////////
/// \brief Open a sound file for reading
///
/// The provided stream reference is valid as long as the
/// SoundFileReader is alive, so it is safe to use/store it
/// during the whole lifetime of the reader.
///
/// \param stream Source stream to read from
/// \param info Structure to fill with the properties of the loaded sound
///
/// \return True if the file was successfully opened
///
////////////////////////////////////////////////////////////
virtual bool open(InputStream& stream, Info& info) = 0;
////////////////////////////////////////////////////////////
/// \brief Change the current read position to the given sample offset
///
/// If the given offset exceeds to total number of samples,
/// this function must jump to the end of the file.
///
/// \param sampleOffset Index of the sample to jump to, relative to the beginning
///
////////////////////////////////////////////////////////////
virtual void seek(Uint64 sampleOffset) = 0;
////////////////////////////////////////////////////////////
/// \brief Read audio samples from the open file
///
/// \param samples Pointer to the sample array to fill
/// \param maxCount Maximum number of samples to read
///
/// \return Number of samples actually read (may be less than \a maxCount)
///
////////////////////////////////////////////////////////////
virtual Uint64 read(Int16* samples, Uint64 maxCount) = 0;
};
} // namespace sf
#endif // SFML_SOUNDFILEREADER_HPP
////////////////////////////////////////////////////////////
/// \class sf::SoundFileReader
/// \ingroup audio
///
/// This class allows users to read audio file formats not natively
/// supported by SFML, and thus extend the set of supported readable
/// audio formats.
///
/// A valid sound file reader must override the open, seek and write functions,
/// as well as providing a static check function; the latter is used by
/// SFML to find a suitable writer for a given input file.
///
/// To register a new reader, use the sf::SoundFileFactory::registerReader
/// template function.
///
/// Usage example:
/// \code
/// class MySoundFileReader : public sf::SoundFileReader
/// {
/// public:
///
/// static bool check(sf::InputStream& stream)
/// {
/// // typically, read the first few header bytes and check fields that identify the format
/// // return true if the reader can handle the format
/// }
///
/// virtual bool open(sf::InputStream& stream, Info& info)
/// {
/// // read the sound file header and fill the sound attributes
/// // (channel count, sample count and sample rate)
/// // return true on success
/// }
///
/// virtual void seek(sf::Uint64 sampleOffset)
/// {
/// // advance to the sampleOffset-th sample from the beginning of the sound
/// }
///
/// virtual sf::Uint64 read(sf::Int16* samples, sf::Uint64 maxCount)
/// {
/// // read up to 'maxCount' samples into the 'samples' array,
/// // convert them (for example from normalized float) if they are not stored
/// // as 16-bits signed integers in the file
/// // return the actual number of samples read
/// }
/// };
///
/// sf::SoundFileFactory::registerReader<MySoundFileReader>();
/// \endcode
///
/// \see sf::InputSoundFile, sf::SoundFileFactory, sf::SoundFileWriter
///
////////////////////////////////////////////////////////////

View file

@ -0,0 +1,125 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_SOUNDFILEWRITER_HPP
#define SFML_SOUNDFILEWRITER_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio/Export.hpp>
#include <string>
namespace sf
{
////////////////////////////////////////////////////////////
/// \brief Abstract base class for sound file encoding
///
////////////////////////////////////////////////////////////
class SFML_AUDIO_API SoundFileWriter
{
public :
////////////////////////////////////////////////////////////
/// \brief Virtual destructor
///
////////////////////////////////////////////////////////////
virtual ~SoundFileWriter() {}
////////////////////////////////////////////////////////////
/// \brief Open a sound file for writing
///
/// \param filename Path of the file to open
/// \param sampleRate Sample rate of the sound
/// \param channelCount Number of channels of the sound
///
/// \return True if the file was successfully opened
///
////////////////////////////////////////////////////////////
virtual bool open(const std::string& filename, unsigned int sampleRate, unsigned int channelCount) = 0;
////////////////////////////////////////////////////////////
/// \brief Write audio samples to the open file
///
/// \param samples Pointer to the sample array to write
/// \param count Number of samples to write
///
////////////////////////////////////////////////////////////
virtual void write(const Int16* samples, Uint64 count) = 0;
};
} // namespace sf
#endif // SFML_SOUNDFILEWRITER_HPP
////////////////////////////////////////////////////////////
/// \class sf::SoundFileWriter
/// \ingroup audio
///
/// This class allows users to write audio file formats not natively
/// supported by SFML, and thus extend the set of supported writable
/// audio formats.
///
/// A valid sound file writer must override the open and write functions,
/// as well as providing a static check function; the latter is used by
/// SFML to find a suitable writer for a given filename.
///
/// To register a new writer, use the sf::SoundFileFactory::registerWriter
/// template function.
///
/// Usage example:
/// \code
/// class MySoundFileWriter : public sf::SoundFileWriter
/// {
/// public:
///
/// static bool check(const std::string& filename)
/// {
/// // typically, check the extension
/// // return true if the writer can handle the format
/// }
///
/// virtual bool open(const std::string& filename, unsigned int sampleRate, unsigned int channelCount)
/// {
/// // open the file 'filename' for writing,
/// // write the given sample rate and channel count to the file header
/// // return true on success
/// }
///
/// virtual void write(const sf::Int16* samples, sf::Uint64 count)
/// {
/// // write 'count' samples stored at address 'samples',
/// // convert them (for example to normalized float) if the format requires it
/// }
/// };
///
/// sf::SoundFileFactory::registerWriter<MySoundFileWriter>();
/// \endcode
///
/// \see sf::OutputSoundFile, sf::SoundFileFactory, sf::SoundFileReader
///
////////////////////////////////////////////////////////////

View file

@ -0,0 +1,141 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_FILEINPUTSTREAM_HPP
#define SFML_FILEINPUTSTREAM_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/InputStream.hpp>
#include <fstream>
namespace sf
{
////////////////////////////////////////////////////////////
/// \brief Implementation of input stream based on a file
///
////////////////////////////////////////////////////////////
class FileInputStream : public InputStream
{
public :
////////////////////////////////////////////////////////////
/// \brief Open the stream from a file path
///
/// \param filename Name of the file to open
///
/// \return True on success, false on error
///
////////////////////////////////////////////////////////////
bool open(const std::string& filename);
////////////////////////////////////////////////////////////
/// \brief Read data from the stream
///
/// After reading, the stream's reading position must be
/// advanced by the amount of bytes read.
///
/// \param data Buffer where to copy the read data
/// \param size Desired number of bytes to read
///
/// \return The number of bytes actually read, or -1 on error
///
////////////////////////////////////////////////////////////
virtual Int64 read(void* data, Int64 size);
////////////////////////////////////////////////////////////
/// \brief Change the current reading position
///
/// \param position The position to seek to, from the beginning
///
/// \return The position actually sought to, or -1 on error
///
////////////////////////////////////////////////////////////
virtual Int64 seek(Int64 position);
////////////////////////////////////////////////////////////
/// \brief Get the current reading position in the stream
///
/// \return The current position, or -1 on error.
///
////////////////////////////////////////////////////////////
virtual Int64 tell();
////////////////////////////////////////////////////////////
/// \brief Return the size of the stream
///
/// \return The total number of bytes available in the stream, or -1 on error
///
////////////////////////////////////////////////////////////
virtual Int64 getSize();
private:
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
std::ifstream m_file; ///< Standard file stream
};
} // namespace sf
#endif // SFML_FILEINPUTSTREAM_HPP
////////////////////////////////////////////////////////////
/// \class FileInputStream
/// \ingroup system
///
/// This class is a specialization of InputStream that
/// reads from a file on disk.
///
/// It wraps a file in the common InputStream interface
/// and therefore allows to use generic classes or functions
/// that accept such a stream, with a file on disk as the data
/// source.
///
/// In addition to the virtual functions inherited from
/// InputStream, FileInputStream adds a function to
/// specify the file to open.
///
/// SFML resource classes can usually be loaded directly from
/// a filename, so this class shouldn't be useful to you unless
/// you create your own algorithms that operate on a InputStream.
///
/// Usage example:
/// \code
/// void process(InputStream& stream);
///
/// FileStream stream;
/// if (stream.open("some_file.dat"))
/// process(stream);
/// \endcode
///
/// InputStream, MemoryStream
///
////////////////////////////////////////////////////////////

View file

@ -29,6 +29,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/Export.hpp>
namespace sf
@ -37,7 +38,7 @@ namespace sf
/// \brief Abstract class for custom file input streams
///
////////////////////////////////////////////////////////////
class InputStream
class SFML_SYSTEM_API InputStream
{
public:
@ -70,7 +71,7 @@ public:
///
////////////////////////////////////////////////////////////
virtual Int64 seek(Int64 position) = 0;
////////////////////////////////////////////////////////////
/// \brief Get the current reading position in the stream
///
@ -115,17 +116,17 @@ public:
/// class ZipStream : public sf::InputStream
/// {
/// public:
///
///
/// ZipStream(std::string archive);
///
/// bool open(std::string filename);
///
/// Int64 read(void* data, Int64 size);
///
///
/// Int64 seek(Int64 position);
///
///
/// Int64 tell();
///
///
/// Int64 getSize();
///
/// private:

View file

@ -0,0 +1,147 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_MEMORYINPUTSTREAM_HPP
#define SFML_MEMORYINPUTSTREAM_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/InputStream.hpp>
#include <cstdlib>
namespace sf
{
////////////////////////////////////////////////////////////
/// \brief Implementation of input stream based on a memory chunk
///
////////////////////////////////////////////////////////////
class MemoryInputStream : public InputStream
{
public :
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
////////////////////////////////////////////////////////////
MemoryInputStream();
////////////////////////////////////////////////////////////
/// \brief Open the stream from its data
///
/// \param data Pointer to the data in memory
/// \param sizeInBytes Size of the data, in bytes
///
////////////////////////////////////////////////////////////
void open(const void* data, std::size_t sizeInBytes);
////////////////////////////////////////////////////////////
/// \brief Read data from the stream
///
/// After reading, the stream's reading position must be
/// advanced by the amount of bytes read.
///
/// \param data Buffer where to copy the read data
/// \param size Desired number of bytes to read
///
/// \return The number of bytes actually read, or -1 on error
///
////////////////////////////////////////////////////////////
virtual Int64 read(void* data, Int64 size);
////////////////////////////////////////////////////////////
/// \brief Change the current reading position
///
/// \param position The position to seek to, from the beginning
///
/// \return The position actually sought to, or -1 on error
///
////////////////////////////////////////////////////////////
virtual Int64 seek(Int64 position);
////////////////////////////////////////////////////////////
/// \brief Get the current reading position in the stream
///
/// \return The current position, or -1 on error.
///
////////////////////////////////////////////////////////////
virtual Int64 tell();
////////////////////////////////////////////////////////////
/// \brief Return the size of the stream
///
/// \return The total number of bytes available in the stream, or -1 on error
///
////////////////////////////////////////////////////////////
virtual Int64 getSize();
private:
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
const char* m_data; ///< Pointer to the data in memory
Int64 m_size; ///< Total size of the data
Int64 m_offset; ///< Current reading position
};
} // namespace sf
#endif // SFML_MEMORYINPUTSTREAM_HPP
////////////////////////////////////////////////////////////
/// \class MemoryeInputStream
/// \ingroup system
///
/// This class is a specialization of InputStream that
/// reads from data in memory.
///
/// It wraps a memory chunk in the common InputStream interface
/// and therefore allows to use generic classes or functions
/// that accept such a stream, with content already loaded in memory.
///
/// In addition to the virtual functions inherited from
/// InputStream, MemoryInputStream adds a function to
/// specify the pointer and size of the data in memory.
///
/// SFML resource classes can usually be loaded directly from
/// memory, so this class shouldn't be useful to you unless
/// you create your own algorithms that operate on a InputStream.
///
/// Usage example:
/// \code
/// void process(InputStream& stream);
///
/// MemoryStream stream;
/// stream.open(thePtr, theSize);
/// process(stream);
/// \endcode
///
/// InputStream, FileStream
///
////////////////////////////////////////////////////////////