1 ////////////////////////////////////////////////////////////
\r
3 // SFML - Simple and Fast Multimedia Library
\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
\r
6 // This software is provided 'as-is', without any express or implied warranty.
\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.
\r
9 // Permission is granted to anyone to use this software for any purpose,
\r
10 // including commercial applications, and to alter it and redistribute it freely,
\r
11 // subject to the following restrictions:
\r
13 // 1. The origin of this software must not be misrepresented;
\r
14 // you must not claim that you wrote the original software.
\r
15 // If you use this software in a product, an acknowledgment
\r
16 // in the product documentation would be appreciated but is not required.
\r
18 // 2. Altered source versions must be plainly marked as such,
\r
19 // and must not be misrepresented as being the original software.
\r
21 // 3. This notice may not be removed or altered from any source distribution.
\r
23 ////////////////////////////////////////////////////////////
\r
25 #ifndef SFML_SOUNDBUFFER_HPP
\r
26 #define SFML_SOUNDBUFFER_HPP
\r
28 ////////////////////////////////////////////////////////////
\r
30 ////////////////////////////////////////////////////////////
\r
31 #include <SFML/System/Resource.hpp>
\r
32 #include <SFML/Audio/AudioResource.hpp>
\r
42 ////////////////////////////////////////////////////////////
\r
43 /// SoundBuffer is the low-level for loading and manipulating
\r
45 ////////////////////////////////////////////////////////////
\r
46 class SFML_API SoundBuffer : public AudioResource, public Resource<SoundBuffer>
\r
50 ////////////////////////////////////////////////////////////
\r
51 /// Default constructor
\r
53 ////////////////////////////////////////////////////////////
\r
56 ////////////////////////////////////////////////////////////
\r
57 /// Copy constructor
\r
59 /// \param Copy : Instance to copy
\r
61 ////////////////////////////////////////////////////////////
\r
62 SoundBuffer(const SoundBuffer& Copy);
\r
64 ////////////////////////////////////////////////////////////
\r
67 ////////////////////////////////////////////////////////////
\r
70 ////////////////////////////////////////////////////////////
\r
71 /// Load the sound buffer from a file
\r
73 /// \param Filename : Path of the sound file to load
\r
75 /// \return True if loading has been successful
\r
77 ////////////////////////////////////////////////////////////
\r
78 bool LoadFromFile(const std::string& Filename);
\r
80 ////////////////////////////////////////////////////////////
\r
81 /// Load the sound buffer from a file in memory
\r
83 /// \param Data : Pointer to the file data in memory
\r
84 /// \param SizeInBytes : Size of the data to load, in bytes
\r
86 /// \return True if loading has been successful
\r
88 ////////////////////////////////////////////////////////////
\r
89 bool LoadFromMemory(const char* Data, std::size_t SizeInBytes);
\r
91 ////////////////////////////////////////////////////////////
\r
92 /// Load the sound buffer from an array of samples - assumed format for
\r
93 /// samples is 16 bits signed integer
\r
95 /// \param Samples : Pointer to the samples in memory
\r
96 /// \param SamplesCount : Number of samples pointed by Samples
\r
97 /// \param ChannelsCount : Number of channels (1 = mono, 2 = stereo, ...)
\r
98 /// \param SampleRate : Frequency (number of samples to play per second)
\r
100 /// \return True if loading has been successful
\r
102 ////////////////////////////////////////////////////////////
\r
103 bool LoadFromSamples(const Int16* Samples, std::size_t SamplesCount, unsigned int ChannelsCount, unsigned int SampleRate);
\r
105 ////////////////////////////////////////////////////////////
\r
106 /// Save the sound buffer to a file
\r
108 /// \param Filename : Path of the sound file to write
\r
110 /// \return True if saving has been successful
\r
112 ////////////////////////////////////////////////////////////
\r
113 bool SaveToFile(const std::string& Filename) const;
\r
115 ////////////////////////////////////////////////////////////
\r
116 /// Return the sound samples
\r
118 /// \return Pointer to the array of sound samples, in 16 bits signed integer format
\r
120 ////////////////////////////////////////////////////////////
\r
121 const Int16* GetSamples() const;
\r
123 ////////////////////////////////////////////////////////////
\r
124 /// Return the samples count
\r
126 /// \return Number of samples
\r
128 ////////////////////////////////////////////////////////////
\r
129 std::size_t GetSamplesCount() const;
\r
131 ////////////////////////////////////////////////////////////
\r
132 /// Get the sample rate
\r
134 /// \return Sound frequency (number of samples per second)
\r
136 ////////////////////////////////////////////////////////////
\r
137 unsigned int GetSampleRate() const;
\r
139 ////////////////////////////////////////////////////////////
\r
140 /// Return the number of channels (1 = mono, 2 = stereo, ...)
\r
142 /// \return Number of channels
\r
144 ////////////////////////////////////////////////////////////
\r
145 unsigned int GetChannelsCount() const;
\r
147 ////////////////////////////////////////////////////////////
\r
148 /// Get the sound duration
\r
150 /// \return Sound duration, in seconds
\r
152 ////////////////////////////////////////////////////////////
\r
153 float GetDuration() const;
\r
155 ////////////////////////////////////////////////////////////
\r
156 /// Assignment operator
\r
158 /// \param Other : Instance to assign
\r
160 /// \return Reference to the sound buffer
\r
162 ////////////////////////////////////////////////////////////
\r
163 SoundBuffer& operator =(const SoundBuffer& Other);
\r
167 friend class Sound;
\r
169 ////////////////////////////////////////////////////////////
\r
170 /// Update the internal buffer with the audio samples
\r
172 /// \param ChannelsCount : Number of channels
\r
173 /// \param SampleRate : Sample rate
\r
175 /// \return True on success
\r
177 ////////////////////////////////////////////////////////////
\r
178 bool Update(unsigned int ChannelsCount, unsigned int SampleRate);
\r
180 ////////////////////////////////////////////////////////////
\r
181 /// Add a sound to the list of sounds that use this buffer
\r
183 /// \param Instance : Sound object to attach
\r
185 ////////////////////////////////////////////////////////////
\r
186 void AttachSound(Sound* Instance) const;
\r
188 ////////////////////////////////////////////////////////////
\r
189 /// Remove a sound from the list of sounds that use this buffer
\r
191 /// \param Instance : Sound object to detach
\r
193 ////////////////////////////////////////////////////////////
\r
194 void DetachSound(Sound* Instance) const;
\r
196 ////////////////////////////////////////////////////////////
\r
198 ////////////////////////////////////////////////////////////
\r
199 typedef std::set<Sound*> SoundList; ///< Set of unique sound instances
\r
201 ////////////////////////////////////////////////////////////
\r
203 ////////////////////////////////////////////////////////////
\r
204 unsigned int myBuffer; ///< OpenAL buffer identifier
\r
205 std::vector<Int16> mySamples; ///< Samples buffer
\r
206 float myDuration; ///< Sound duration, in seconds
\r
207 mutable SoundList mySounds; ///< List of sounds that are using this buffer
\r
213 #endif // SFML_SOUNDBUFFER_HPP
\r