Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:58:13

0001 ////////////////////////////////////////////////////////////
0002 //
0003 // SFML - Simple and Fast Multimedia Library
0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
0005 //
0006 // This software is provided 'as-is', without any express or implied warranty.
0007 // In no event will the authors be held liable for any damages arising from the use of this software.
0008 //
0009 // Permission is granted to anyone to use this software for any purpose,
0010 // including commercial applications, and to alter it and redistribute it freely,
0011 // subject to the following restrictions:
0012 //
0013 // 1. The origin of this software must not be misrepresented;
0014 //    you must not claim that you wrote the original software.
0015 //    If you use this software in a product, an acknowledgment
0016 //    in the product documentation would be appreciated but is not required.
0017 //
0018 // 2. Altered source versions must be plainly marked as such,
0019 //    and must not be misrepresented as being the original software.
0020 //
0021 // 3. This notice may not be removed or altered from any source distribution.
0022 //
0023 ////////////////////////////////////////////////////////////
0024 
0025 #ifndef SFML_SOUND_HPP
0026 #define SFML_SOUND_HPP
0027 
0028 ////////////////////////////////////////////////////////////
0029 // Headers
0030 ////////////////////////////////////////////////////////////
0031 #include <SFML/Audio/Export.hpp>
0032 #include <SFML/Audio/SoundSource.hpp>
0033 #include <SFML/System/Time.hpp>
0034 #include <cstdlib>
0035 
0036 
0037 namespace sf
0038 {
0039 class SoundBuffer;
0040 
0041 ////////////////////////////////////////////////////////////
0042 /// \brief Regular sound that can be played in the audio environment
0043 ///
0044 ////////////////////////////////////////////////////////////
0045 class SFML_AUDIO_API Sound : public SoundSource
0046 {
0047 public:
0048 
0049     ////////////////////////////////////////////////////////////
0050     /// \brief Default constructor
0051     ///
0052     ////////////////////////////////////////////////////////////
0053     Sound();
0054 
0055     ////////////////////////////////////////////////////////////
0056     /// \brief Construct the sound with a buffer
0057     ///
0058     /// \param buffer Sound buffer containing the audio data to play with the sound
0059     ///
0060     ////////////////////////////////////////////////////////////
0061     explicit Sound(const SoundBuffer& buffer);
0062 
0063     ////////////////////////////////////////////////////////////
0064     /// \brief Copy constructor
0065     ///
0066     /// \param copy Instance to copy
0067     ///
0068     ////////////////////////////////////////////////////////////
0069     Sound(const Sound& copy);
0070 
0071     ////////////////////////////////////////////////////////////
0072     /// \brief Destructor
0073     ///
0074     ////////////////////////////////////////////////////////////
0075     ~Sound();
0076 
0077     ////////////////////////////////////////////////////////////
0078     /// \brief Start or resume playing the sound
0079     ///
0080     /// This function starts the stream if it was stopped, resumes
0081     /// it if it was paused, and restarts it from beginning if it
0082     /// was it already playing.
0083     /// This function uses its own thread so that it doesn't block
0084     /// the rest of the program while the sound is played.
0085     ///
0086     /// \see pause, stop
0087     ///
0088     ////////////////////////////////////////////////////////////
0089     void play();
0090 
0091     ////////////////////////////////////////////////////////////
0092     /// \brief Pause the sound
0093     ///
0094     /// This function pauses the sound if it was playing,
0095     /// otherwise (sound already paused or stopped) it has no effect.
0096     ///
0097     /// \see play, stop
0098     ///
0099     ////////////////////////////////////////////////////////////
0100     void pause();
0101 
0102     ////////////////////////////////////////////////////////////
0103     /// \brief stop playing the sound
0104     ///
0105     /// This function stops the sound if it was playing or paused,
0106     /// and does nothing if it was already stopped.
0107     /// It also resets the playing position (unlike pause()).
0108     ///
0109     /// \see play, pause
0110     ///
0111     ////////////////////////////////////////////////////////////
0112     void stop();
0113 
0114     ////////////////////////////////////////////////////////////
0115     /// \brief Set the source buffer containing the audio data to play
0116     ///
0117     /// It is important to note that the sound buffer is not copied,
0118     /// thus the sf::SoundBuffer instance must remain alive as long
0119     /// as it is attached to the sound.
0120     ///
0121     /// \param buffer Sound buffer to attach to the sound
0122     ///
0123     /// \see getBuffer
0124     ///
0125     ////////////////////////////////////////////////////////////
0126     void setBuffer(const SoundBuffer& buffer);
0127 
0128     ////////////////////////////////////////////////////////////
0129     /// \brief Set whether or not the sound should loop after reaching the end
0130     ///
0131     /// If set, the sound will restart from beginning after
0132     /// reaching the end and so on, until it is stopped or
0133     /// setLoop(false) is called.
0134     /// The default looping state for sound is false.
0135     ///
0136     /// \param loop True to play in loop, false to play once
0137     ///
0138     /// \see getLoop
0139     ///
0140     ////////////////////////////////////////////////////////////
0141     void setLoop(bool loop);
0142 
0143     ////////////////////////////////////////////////////////////
0144     /// \brief Change the current playing position of the sound
0145     ///
0146     /// The playing position can be changed when the sound is
0147     /// either paused or playing. Changing the playing position
0148     /// when the sound is stopped has no effect, since playing
0149     /// the sound will reset its position.
0150     ///
0151     /// \param timeOffset New playing position, from the beginning of the sound
0152     ///
0153     /// \see getPlayingOffset
0154     ///
0155     ////////////////////////////////////////////////////////////
0156     void setPlayingOffset(Time timeOffset);
0157 
0158     ////////////////////////////////////////////////////////////
0159     /// \brief Get the audio buffer attached to the sound
0160     ///
0161     /// \return Sound buffer attached to the sound (can be NULL)
0162     ///
0163     ////////////////////////////////////////////////////////////
0164     const SoundBuffer* getBuffer() const;
0165 
0166     ////////////////////////////////////////////////////////////
0167     /// \brief Tell whether or not the sound is in loop mode
0168     ///
0169     /// \return True if the sound is looping, false otherwise
0170     ///
0171     /// \see setLoop
0172     ///
0173     ////////////////////////////////////////////////////////////
0174     bool getLoop() const;
0175 
0176     ////////////////////////////////////////////////////////////
0177     /// \brief Get the current playing position of the sound
0178     ///
0179     /// \return Current playing position, from the beginning of the sound
0180     ///
0181     /// \see setPlayingOffset
0182     ///
0183     ////////////////////////////////////////////////////////////
0184     Time getPlayingOffset() const;
0185 
0186     ////////////////////////////////////////////////////////////
0187     /// \brief Get the current status of the sound (stopped, paused, playing)
0188     ///
0189     /// \return Current status of the sound
0190     ///
0191     ////////////////////////////////////////////////////////////
0192     Status getStatus() const;
0193 
0194     ////////////////////////////////////////////////////////////
0195     /// \brief Overload of assignment operator
0196     ///
0197     /// \param right Instance to assign
0198     ///
0199     /// \return Reference to self
0200     ///
0201     ////////////////////////////////////////////////////////////
0202     Sound& operator =(const Sound& right);
0203 
0204     ////////////////////////////////////////////////////////////
0205     /// \brief Reset the internal buffer of the sound
0206     ///
0207     /// This function is for internal use only, you don't have
0208     /// to use it. It is called by the sf::SoundBuffer that
0209     /// this sound uses, when it is destroyed in order to prevent
0210     /// the sound from using a dead buffer.
0211     ///
0212     ////////////////////////////////////////////////////////////
0213     void resetBuffer();
0214 
0215 private:
0216 
0217     ////////////////////////////////////////////////////////////
0218     // Member data
0219     ////////////////////////////////////////////////////////////
0220     const SoundBuffer* m_buffer; //!< Sound buffer bound to the source
0221 };
0222 
0223 } // namespace sf
0224 
0225 
0226 #endif // SFML_SOUND_HPP
0227 
0228 
0229 ////////////////////////////////////////////////////////////
0230 /// \class sf::Sound
0231 /// \ingroup audio
0232 ///
0233 /// sf::Sound is the class to use to play sounds.
0234 /// It provides:
0235 /// \li Control (play, pause, stop)
0236 /// \li Ability to modify output parameters in real-time (pitch, volume, ...)
0237 /// \li 3D spatial features (position, attenuation, ...).
0238 ///
0239 /// sf::Sound is perfect for playing short sounds that can
0240 /// fit in memory and require no latency, like foot steps or
0241 /// gun shots. For longer sounds, like background musics
0242 /// or long speeches, rather see sf::Music (which is based
0243 /// on streaming).
0244 ///
0245 /// In order to work, a sound must be given a buffer of audio
0246 /// data to play. Audio data (samples) is stored in sf::SoundBuffer,
0247 /// and attached to a sound with the setBuffer() function.
0248 /// The buffer object attached to a sound must remain alive
0249 /// as long as the sound uses it. Note that multiple sounds
0250 /// can use the same sound buffer at the same time.
0251 ///
0252 /// Usage example:
0253 /// \code
0254 /// sf::SoundBuffer buffer;
0255 /// buffer.loadFromFile("sound.wav");
0256 ///
0257 /// sf::Sound sound;
0258 /// sound.setBuffer(buffer);
0259 /// sound.play();
0260 /// \endcode
0261 ///
0262 /// \see sf::SoundBuffer, sf::Music
0263 ///
0264 ////////////////////////////////////////////////////////////