2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// SFML - Simple and Fast Multimedia Library
|
2009-02-13 01:48:35 +08:00
|
|
|
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
|
2009-01-29 00:18:34 +08:00
|
|
|
//
|
|
|
|
// 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_SOUNDBUFFER_HPP
|
|
|
|
#define SFML_SOUNDBUFFER_HPP
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-01-21 17:34:17 +08:00
|
|
|
#include <SFML/Audio/Export.hpp>
|
2012-01-20 06:51:06 +08:00
|
|
|
#include <SFML/System/Time.hpp>
|
2009-01-29 00:18:34 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2010-01-26 17:49:03 +08:00
|
|
|
#include <set>
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
namespace sf
|
|
|
|
{
|
2011-07-17 18:21:47 +08:00
|
|
|
namespace priv
|
|
|
|
{
|
|
|
|
class SoundFile;
|
|
|
|
}
|
|
|
|
|
2010-01-26 17:49:03 +08:00
|
|
|
class Sound;
|
2011-07-17 18:21:47 +08:00
|
|
|
class InputStream;
|
2010-01-26 17:49:03 +08:00
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \brief Storage for audio samples defining a sound
|
|
|
|
///
|
2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
2012-01-21 17:34:17 +08:00
|
|
|
class SFML_AUDIO_API SoundBuffer
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
public :
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \brief Default constructor
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
SoundBuffer();
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \brief Copy constructor
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \param copy Instance to copy
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-07-12 06:17:24 +08:00
|
|
|
SoundBuffer(const SoundBuffer& copy);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \brief Destructor
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
~SoundBuffer();
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \brief Load the sound buffer from a file
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-30 00:05:22 +08:00
|
|
|
/// 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.
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \param filename Path of the sound file to load
|
|
|
|
///
|
|
|
|
/// \return True if loading succeeded, false if it failed
|
|
|
|
///
|
2011-07-17 18:21:47 +08:00
|
|
|
/// \see LoadFromMemory, LoadFromStream, LoadFromSamples, SaveToFile
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-07-12 06:17:24 +08:00
|
|
|
bool LoadFromFile(const std::string& filename);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \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.
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \param data Pointer to the file data in memory
|
|
|
|
/// \param sizeInBytes Size of the data to load, in bytes
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \return True if loading succeeded, false if it failed
|
|
|
|
///
|
2011-07-17 18:21:47 +08:00
|
|
|
/// \see LoadFromFile, LoadFromStream, LoadFromSamples
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2010-02-17 22:43:09 +08:00
|
|
|
bool LoadFromMemory(const void* data, std::size_t sizeInBytes);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2011-07-17 18:21:47 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// \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.
|
|
|
|
///
|
|
|
|
/// \param stream Source stream to read from
|
|
|
|
///
|
|
|
|
/// \return True if loading succeeded, false if it failed
|
|
|
|
///
|
|
|
|
/// \see LoadFromFile, LoadFromMemory, LoadFromSamples
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
bool LoadFromStream(InputStream& stream);
|
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \brief Load the sound buffer from an array of audio samples
|
|
|
|
///
|
|
|
|
/// The assumed format of the audio samples is 16 bits signed integer
|
|
|
|
/// (sf::Int16).
|
|
|
|
///
|
2011-12-26 06:42:43 +08:00
|
|
|
/// \param samples Pointer to the array of samples in memory
|
|
|
|
/// \param sampleCount Number of samples in the array
|
|
|
|
/// \param channelCount Number of channels (1 = mono, 2 = stereo, ...)
|
|
|
|
/// \param sampleRate Sample rate (number of samples to play per second)
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \return True if loading succeeded, false if it failed
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \see LoadFromFile, LoadFromMemory, SaveToFile
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2011-12-26 06:42:43 +08:00
|
|
|
bool LoadFromSamples(const Int16* samples, std::size_t sampleCount, unsigned int channelCount, unsigned int sampleRate);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \brief Save the sound buffer to an audio file
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-30 00:05:22 +08:00
|
|
|
/// 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.
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \param filename Path of the sound file to write
|
|
|
|
///
|
|
|
|
/// \return True if saving succeeded, false if it failed
|
|
|
|
///
|
|
|
|
/// \see LoadFromFile, LoadFromMemory, LoadFromSamples
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-07-12 06:17:24 +08:00
|
|
|
bool SaveToFile(const std::string& filename) const;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \brief Get the array of audio samples stored in the buffer
|
|
|
|
///
|
|
|
|
/// The format of the returned samples is 16 bits signed integer
|
|
|
|
/// (sf::Int16). The total number of samples in this array
|
2011-12-26 06:42:43 +08:00
|
|
|
/// is given by the GetSampleCount() function.
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \return Read-only pointer to the array of sound samples
|
|
|
|
///
|
2011-12-26 06:42:43 +08:00
|
|
|
/// \see GetSampleCount
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
const Int16* GetSamples() const;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \brief Get the number of samples stored in the buffer
|
|
|
|
///
|
|
|
|
/// The array of samples can be accessed with the GetSamples()
|
|
|
|
/// function.
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
/// \return Number of samples
|
|
|
|
///
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \see GetSamples
|
|
|
|
///
|
2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
2011-12-26 06:42:43 +08:00
|
|
|
std::size_t GetSampleCount() const;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \brief Get the sample rate of the sound
|
|
|
|
///
|
|
|
|
/// The sample rate is the number of samples played per second.
|
|
|
|
/// The higher, the better the quality (for example, 44100
|
|
|
|
/// samples/s is CD quality).
|
|
|
|
///
|
|
|
|
/// \return Sample rate (number of samples per second)
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2011-12-26 06:42:43 +08:00
|
|
|
/// \see GetChannelCount, GetDuration
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
unsigned int GetSampleRate() const;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \brief Get the number of channels used by the sound
|
|
|
|
///
|
2011-09-03 01:26:18 +08:00
|
|
|
/// If the sound is mono then the number of channels will
|
2009-10-30 00:05:22 +08:00
|
|
|
/// be 1, 2 for stereo, etc.
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
/// \return Number of channels
|
|
|
|
///
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \see GetSampleRate, GetDuration
|
|
|
|
///
|
2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
2011-12-26 06:42:43 +08:00
|
|
|
unsigned int GetChannelCount() const;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \brief Get the total duration of the sound
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2012-01-20 06:51:06 +08:00
|
|
|
/// \return Sound duration
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2011-12-26 06:42:43 +08:00
|
|
|
/// \see GetSampleRate, GetChannelCount
|
2009-10-30 00:05:22 +08:00
|
|
|
///
|
2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
2012-01-20 06:51:06 +08:00
|
|
|
Time GetDuration() const;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \brief Overload of assignment operator
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \param right Instance to assign
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \return Reference to self
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
SoundBuffer& operator =(const SoundBuffer& right);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
private :
|
|
|
|
|
|
|
|
friend class Sound;
|
|
|
|
|
2011-07-17 18:21:47 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// \brief Initialize the internal state after loading a new sound
|
|
|
|
///
|
|
|
|
/// \param file Sound file providing access to the new loaded sound
|
|
|
|
///
|
|
|
|
/// \return True on succesful initialization, false on failure
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
bool Initialize(priv::SoundFile& file);
|
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \brief Update the internal buffer with the cached audio samples
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2011-12-26 06:42:43 +08:00
|
|
|
/// \param channelCount Number of channels
|
|
|
|
/// \param sampleRate Sample rate (number of samples per second)
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-30 00:05:22 +08:00
|
|
|
/// \return True on success, false if any error happened
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2011-12-26 06:42:43 +08:00
|
|
|
bool Update(unsigned int channelCount, unsigned int sampleRate);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-01-26 17:49:03 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
2010-01-26 18:52:36 +08:00
|
|
|
/// \brief Add a sound to the list of sounds that use this buffer
|
2010-01-26 17:49:03 +08:00
|
|
|
///
|
2010-01-26 18:52:36 +08:00
|
|
|
/// \param sound Sound instance to attach
|
2010-01-26 17:49:03 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2010-01-26 18:52:36 +08:00
|
|
|
void AttachSound(Sound* sound) const;
|
2010-01-26 17:49:03 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2010-01-26 18:52:36 +08:00
|
|
|
/// \brief Remove a sound from the list of sounds that use this buffer
|
2010-01-26 17:49:03 +08:00
|
|
|
///
|
2010-01-26 18:52:36 +08:00
|
|
|
/// \param sound Sound instance to detach
|
2010-01-26 17:49:03 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2010-01-26 18:52:36 +08:00
|
|
|
void DetachSound(Sound* sound) const;
|
2010-01-26 17:49:03 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Types
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
typedef std::set<Sound*> SoundList; ///< Set of unique sound instances
|
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Member data
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-03-09 08:22:47 +08:00
|
|
|
unsigned int m_buffer; ///< OpenAL buffer identifier
|
|
|
|
std::vector<Int16> m_samples; ///< Samples buffer
|
|
|
|
Time m_duration; ///< Sound duration
|
|
|
|
mutable SoundList m_sounds; ///< List of sounds that are using this buffer
|
2009-01-29 00:18:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sf
|
|
|
|
|
|
|
|
|
|
|
|
#endif // SFML_SOUNDBUFFER_HPP
|
2009-10-30 00:05:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// \class sf::SoundBuffer
|
2010-07-23 05:45:58 +08:00
|
|
|
/// \ingroup audio
|
2009-10-30 00:05:22 +08:00
|
|
|
///
|
|
|
|
/// A sound buffer holds the data of a sound, which is
|
|
|
|
/// an array of audio samples. A sample is a 16 bits signed integer
|
|
|
|
/// that defines the amplitude of the sound at a given time.
|
|
|
|
/// The sound is then restituted by playing these samples at
|
|
|
|
/// a high rate (for example, 44100 samples per second is the
|
|
|
|
/// standard rate used for playing CDs). In short, audio samples
|
2011-07-23 04:31:27 +08:00
|
|
|
/// are like texture pixels, and a sf::SoundBuffer is similar to
|
|
|
|
/// a sf::Texture.
|
2009-10-30 00:05:22 +08:00
|
|
|
///
|
|
|
|
/// A sound buffer can be loaded from a file (see LoadFromFile()
|
2011-07-17 18:21:47 +08:00
|
|
|
/// for the complete list of supported formats), from memory, from
|
|
|
|
/// a custom stream (see sf::InputStream) or directly from an array
|
|
|
|
/// of samples. It can also be saved back to a file.
|
2009-10-30 00:05:22 +08:00
|
|
|
///
|
|
|
|
/// Sound buffers alone are not very useful: they hold the audio data
|
|
|
|
/// but cannot be played. To do so, you need to use the sf::Sound class,
|
|
|
|
/// which provides functions to play/pause/stop the sound as well as
|
|
|
|
/// changing the way it is outputted (volume, pitch, 3D position, ...).
|
|
|
|
/// This separation allows more flexibility and better performances:
|
|
|
|
/// indeed a sf::SoundBuffer is a heavy resource, and any operation on it
|
|
|
|
/// is slow (often too slow for real-time applications). On the other
|
|
|
|
/// side, a sf::Sound is a lightweight object, which can use the audio data
|
|
|
|
/// of a sound buffer and change the way it is played without actually
|
|
|
|
/// modifying that data. Note that it is also possible to bind
|
|
|
|
/// several sf::Sound instances to the same sf::SoundBuffer.
|
|
|
|
///
|
|
|
|
/// It is important to note that the sf::Sound instance doesn't
|
|
|
|
/// copy the buffer that it uses, it only keeps a reference to it.
|
|
|
|
/// Thus, a sf::SoundBuffer must not be destructed while it is
|
|
|
|
/// used by a sf::Sound (i.e. never write a function that
|
|
|
|
/// uses a local sf::SoundBuffer instance for loading a sound).
|
|
|
|
///
|
|
|
|
/// Usage example:
|
|
|
|
/// \code
|
|
|
|
/// // Declare a new sound buffer
|
|
|
|
/// sf::SoundBuffer buffer;
|
|
|
|
///
|
|
|
|
/// // Load it from a file
|
|
|
|
/// if (!buffer.LoadFromFile("sound.wav"))
|
|
|
|
/// {
|
|
|
|
/// // error...
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Create a sound source and bind it to the buffer
|
|
|
|
/// sf::Sound sound1;
|
|
|
|
/// sound1.SetBuffer(buffer);
|
|
|
|
///
|
|
|
|
/// // Play the sound
|
|
|
|
/// sound1.Play();
|
|
|
|
///
|
|
|
|
/// // Create another sound source bound to the same buffer
|
|
|
|
/// sf::Sound sound2;
|
|
|
|
/// sound2.SetBuffer(buffer);
|
|
|
|
///
|
|
|
|
/// // Play it with a higher pitch -- the first sound remains unchanged
|
|
|
|
/// sound2.SetPitch(2);
|
|
|
|
/// sound2.Play();
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// \see sf::Sound, sf::SoundBufferRecorder
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|