2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// SFML - Simple and Fast Multimedia Library
|
2013-04-06 15:34:44 +08:00
|
|
|
// Copyright (C) 2007-2013 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_SOUND_HPP
|
|
|
|
#define SFML_SOUND_HPP
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-01-21 17:34:17 +08:00
|
|
|
#include <SFML/Audio/Export.hpp>
|
2009-10-31 00:14:26 +08:00
|
|
|
#include <SFML/Audio/SoundSource.hpp>
|
2012-01-20 06:51:06 +08:00
|
|
|
#include <SFML/System/Time.hpp>
|
2009-01-29 00:18:34 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
|
|
|
|
namespace sf
|
|
|
|
{
|
|
|
|
class SoundBuffer;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \brief Regular sound that can be played in the audio environment
|
|
|
|
///
|
2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
2012-01-21 17:34:17 +08:00
|
|
|
class SFML_AUDIO_API Sound : public SoundSource
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
public :
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \brief Default constructor
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
Sound();
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2011-07-23 04:31:27 +08:00
|
|
|
/// \brief Construct the sound with a buffer
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2011-07-23 04:31:27 +08:00
|
|
|
/// \param buffer Sound buffer containing the audio data to play with the sound
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-03-27 23:13:46 +08:00
|
|
|
explicit Sound(const SoundBuffer& buffer);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \brief Copy constructor
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \param copy Instance to copy
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-07-12 06:17:24 +08:00
|
|
|
Sound(const Sound& copy);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \brief Destructor
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
~Sound();
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \brief Start or resume playing the sound
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2010-10-07 05:10:25 +08:00
|
|
|
/// This function starts the stream if it was stopped, resumes
|
|
|
|
/// it if it was paused, and restarts it from beginning if it
|
|
|
|
/// was it already playing.
|
2009-10-31 00:14:26 +08:00
|
|
|
/// This function uses its own thread so that it doesn't block
|
|
|
|
/// the rest of the program while the sound is played.
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2012-08-23 18:34:51 +08:00
|
|
|
/// \see pause, stop
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-03-12 02:10:37 +08:00
|
|
|
void play();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \brief Pause the sound
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-31 00:14:26 +08:00
|
|
|
/// This function pauses the sound if it was playing,
|
|
|
|
/// otherwise (sound already paused or stopped) it has no effect.
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2012-08-23 18:34:51 +08:00
|
|
|
/// \see play, stop
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-03-12 02:10:37 +08:00
|
|
|
void pause();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-08-23 18:34:51 +08:00
|
|
|
/// \brief stop playing the sound
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-31 00:14:26 +08:00
|
|
|
/// This function stops the sound if it was playing or paused,
|
|
|
|
/// and does nothing if it was already stopped.
|
2012-03-12 02:10:37 +08:00
|
|
|
/// It also resets the playing position (unlike pause()).
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2012-08-23 18:34:51 +08:00
|
|
|
/// \see play, pause
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-03-12 02:10:37 +08:00
|
|
|
void stop();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \brief Set the source buffer containing the audio data to play
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-31 00:14:26 +08:00
|
|
|
/// It is important to note that the sound buffer is not copied,
|
|
|
|
/// thus the sf::SoundBuffer instance must remain alive as long
|
|
|
|
/// as it is attached to the sound.
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \param buffer Sound buffer to attach to the sound
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2012-08-23 18:34:51 +08:00
|
|
|
/// \see getBuffer
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-03-12 02:10:37 +08:00
|
|
|
void setBuffer(const SoundBuffer& buffer);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2009-03-23 02:36:03 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \brief Set whether or not the sound should loop after reaching the end
|
2009-03-23 02:36:03 +08:00
|
|
|
///
|
2009-10-31 00:14:26 +08:00
|
|
|
/// If set, the sound will restart from beginning after
|
|
|
|
/// reaching the end and so on, until it is stopped or
|
2012-03-12 02:10:37 +08:00
|
|
|
/// setLoop(false) is called.
|
2009-10-31 00:14:26 +08:00
|
|
|
/// The default looping state for sound is false.
|
2009-03-23 02:36:03 +08:00
|
|
|
///
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \param loop True to play in loop, false to play once
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2012-03-12 02:10:37 +08:00
|
|
|
/// \see getLoop
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-03-12 02:10:37 +08:00
|
|
|
void setLoop(bool loop);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \brief Change the current playing position of the sound
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-31 00:14:26 +08:00
|
|
|
/// The playing position can be changed when the sound is
|
|
|
|
/// either paused or playing.
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2012-01-20 06:51:06 +08:00
|
|
|
/// \param timeOffset New playing position, from the beginning of the sound
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2012-03-12 02:10:37 +08:00
|
|
|
/// \see getPlayingOffset
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-03-12 02:10:37 +08:00
|
|
|
void setPlayingOffset(Time timeOffset);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \brief Get the audio buffer attached to the sound
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \return Sound buffer attached to the sound (can be NULL)
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-03-12 02:10:37 +08:00
|
|
|
const SoundBuffer* getBuffer() const;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \brief Tell whether or not the sound is in loop mode
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
/// \return True if the sound is looping, false otherwise
|
|
|
|
///
|
2012-03-12 02:10:37 +08:00
|
|
|
/// \see setLoop
|
2009-03-23 02:36:03 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-03-12 02:10:37 +08:00
|
|
|
bool getLoop() const;
|
2009-03-23 02:36:03 +08:00
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \brief Get the current playing position of the sound
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2012-01-20 06:51:06 +08:00
|
|
|
/// \return Current playing position, from the beginning of the sound
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2012-03-12 02:10:37 +08:00
|
|
|
/// \see setPlayingOffset
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-03-12 02:10:37 +08:00
|
|
|
Time getPlayingOffset() const;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \brief Get the current status of the sound (stopped, paused, playing)
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
/// \return Current status of the sound
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-03-12 02:10:37 +08:00
|
|
|
Status getStatus() const;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \brief Overload of assignment operator
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \param right Instance to assign
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \return Reference to self
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-10-31 00:14:26 +08:00
|
|
|
Sound& operator =(const Sound& right);
|
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 Reset the internal buffer of the sound
|
2010-01-26 17:49:03 +08:00
|
|
|
///
|
|
|
|
/// This function is for internal use only, you don't have
|
2010-01-26 18:52:36 +08:00
|
|
|
/// to use it. It is called by the sf::SoundBuffer that
|
|
|
|
/// this sound uses, when it is destroyed in order to prevent
|
|
|
|
/// the sound from using a dead buffer.
|
2010-01-26 17:49:03 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-03-12 02:10:37 +08:00
|
|
|
void resetBuffer();
|
2010-01-26 17:49:03 +08:00
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
private :
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Member data
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-03-09 08:22:47 +08:00
|
|
|
const SoundBuffer* m_buffer; ///< Sound buffer bound to the source
|
2009-01-29 00:18:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sf
|
|
|
|
|
|
|
|
|
|
|
|
#endif // SFML_SOUND_HPP
|
2009-10-31 00:14:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// \class sf::Sound
|
2010-07-23 05:45:58 +08:00
|
|
|
/// \ingroup audio
|
2009-10-31 00:14:26 +08:00
|
|
|
///
|
|
|
|
/// sf::Sound is the class to use to play sounds.
|
|
|
|
/// It provides:
|
|
|
|
/// \li Control (play, pause, stop)
|
|
|
|
/// \li Ability to modify output parameters in real-time (pitch, volume, ...)
|
|
|
|
/// \li 3D spatial features (position, attenuation, ...).
|
|
|
|
///
|
|
|
|
/// sf::Sound is perfect for playing short sounds that can
|
|
|
|
/// fit in memory and require no latency, like foot steps or
|
|
|
|
/// gun shots. For longer sounds, like background musics
|
|
|
|
/// or long speeches, rather see sf::Music (which is based
|
|
|
|
/// on streaming).
|
|
|
|
///
|
|
|
|
/// In order to work, a sound must be given a buffer of audio
|
|
|
|
/// data to play. Audio data (samples) is stored in sf::SoundBuffer,
|
2012-03-12 02:10:37 +08:00
|
|
|
/// and attached to a sound with the setBuffer() function.
|
2009-10-31 00:14:26 +08:00
|
|
|
/// The buffer object attached to a sound must remain alive
|
|
|
|
/// as long as the sound uses it. Note that multiple sounds
|
|
|
|
/// can use the same sound buffer at the same time.
|
|
|
|
///
|
|
|
|
/// Usage example:
|
|
|
|
/// \code
|
|
|
|
/// sf::SoundBuffer buffer;
|
2012-03-12 02:10:37 +08:00
|
|
|
/// buffer.loadFromFile("sound.wav");
|
2009-10-31 00:14:26 +08:00
|
|
|
///
|
|
|
|
/// sf::Sound sound;
|
2012-03-12 02:10:37 +08:00
|
|
|
/// sound.setBuffer(buffer);
|
|
|
|
/// sound.play();
|
2009-10-31 00:14:26 +08:00
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// \see sf::SoundBuffer, sf::Music
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|