/extlibs/SFML/include/SFML/Audio/SoundBuffer.hpp

https://bitbucket.org/hugoruscitti/pilascpp · C++ Header · 327 lines · 39 code · 30 blank · 258 comment · 0 complexity · 6db38f3763076b394731d2bf103f807b MD5 · raw file

  1. ////////////////////////////////////////////////////////////
  2. //
  3. // SFML - Simple and Fast Multimedia Library
  4. // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
  5. //
  6. // This software is provided 'as-is', without any express or implied warranty.
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it freely,
  11. // subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented;
  14. // you must not claim that you wrote the original software.
  15. // If you use this software in a product, an acknowledgment
  16. // in the product documentation would be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such,
  19. // and must not be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source distribution.
  22. //
  23. ////////////////////////////////////////////////////////////
  24. #ifndef SFML_SOUNDBUFFER_HPP
  25. #define SFML_SOUNDBUFFER_HPP
  26. ////////////////////////////////////////////////////////////
  27. // Headers
  28. ////////////////////////////////////////////////////////////
  29. #include <SFML/Config.hpp>
  30. #include <SFML/System/Resource.hpp>
  31. #include <string>
  32. #include <vector>
  33. #include <set>
  34. namespace sf
  35. {
  36. class Sound;
  37. ////////////////////////////////////////////////////////////
  38. /// \brief Storage for audio samples defining a sound
  39. ///
  40. ////////////////////////////////////////////////////////////
  41. class SFML_API SoundBuffer : public Resource<SoundBuffer>
  42. {
  43. public :
  44. ////////////////////////////////////////////////////////////
  45. /// \brief Default constructor
  46. ///
  47. ////////////////////////////////////////////////////////////
  48. SoundBuffer();
  49. ////////////////////////////////////////////////////////////
  50. /// \brief Copy constructor
  51. ///
  52. /// \param copy Instance to copy
  53. ///
  54. ////////////////////////////////////////////////////////////
  55. SoundBuffer(const SoundBuffer& copy);
  56. ////////////////////////////////////////////////////////////
  57. /// \brief Destructor
  58. ///
  59. ////////////////////////////////////////////////////////////
  60. ~SoundBuffer();
  61. ////////////////////////////////////////////////////////////
  62. /// \brief Load the sound buffer from a file
  63. ///
  64. /// Here is a complete list of all the supported audio formats:
  65. /// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam,
  66. /// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
  67. ///
  68. /// \param filename Path of the sound file to load
  69. ///
  70. /// \return True if loading succeeded, false if it failed
  71. ///
  72. /// \see LoadFromMemory, LoadFromSamples, SaveToFile
  73. ///
  74. ////////////////////////////////////////////////////////////
  75. bool LoadFromFile(const std::string& filename);
  76. ////////////////////////////////////////////////////////////
  77. /// \brief Load the sound buffer from a file in memory
  78. ///
  79. /// Here is a complete list of all the supported audio formats:
  80. /// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam,
  81. /// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
  82. ///
  83. /// \param data Pointer to the file data in memory
  84. /// \param sizeInBytes Size of the data to load, in bytes
  85. ///
  86. /// \return True if loading succeeded, false if it failed
  87. ///
  88. /// \see LoadFromFile, LoadFromSamples, SaveToFile
  89. ///
  90. ////////////////////////////////////////////////////////////
  91. bool LoadFromMemory(const void* data, std::size_t sizeInBytes);
  92. ////////////////////////////////////////////////////////////
  93. /// \brief Load the sound buffer from an array of audio samples
  94. ///
  95. /// The assumed format of the audio samples is 16 bits signed integer
  96. /// (sf::Int16).
  97. ///
  98. /// \param samples Pointer to the array of samples in memory
  99. /// \param samplesCount Number of samples in the array
  100. /// \param channelsCount Number of channels (1 = mono, 2 = stereo, ...)
  101. /// \param sampleRate Sample rate (number of samples to play per second)
  102. ///
  103. /// \return True if loading succeeded, false if it failed
  104. ///
  105. /// \see LoadFromFile, LoadFromMemory, SaveToFile
  106. ///
  107. ////////////////////////////////////////////////////////////
  108. bool LoadFromSamples(const Int16* samples, std::size_t samplesCount, unsigned int channelsCount, unsigned int sampleRate);
  109. ////////////////////////////////////////////////////////////
  110. /// \brief Save the sound buffer to an audio file
  111. ///
  112. /// Here is a complete list of all the supported audio formats:
  113. /// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam,
  114. /// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
  115. ///
  116. /// \param filename Path of the sound file to write
  117. ///
  118. /// \return True if saving succeeded, false if it failed
  119. ///
  120. /// \see LoadFromFile, LoadFromMemory, LoadFromSamples
  121. ///
  122. ////////////////////////////////////////////////////////////
  123. bool SaveToFile(const std::string& filename) const;
  124. ////////////////////////////////////////////////////////////
  125. /// \brief Get the array of audio samples stored in the buffer
  126. ///
  127. /// The format of the returned samples is 16 bits signed integer
  128. /// (sf::Int16). The total number of samples in this array
  129. /// is given by the GetSamplesCount() function.
  130. ///
  131. /// \return Read-only pointer to the array of sound samples
  132. ///
  133. /// \see GetSamplesCount
  134. ///
  135. ////////////////////////////////////////////////////////////
  136. const Int16* GetSamples() const;
  137. ////////////////////////////////////////////////////////////
  138. /// \brief Get the number of samples stored in the buffer
  139. ///
  140. /// The array of samples can be accessed with the GetSamples()
  141. /// function.
  142. ///
  143. /// \return Number of samples
  144. ///
  145. /// \see GetSamples
  146. ///
  147. ////////////////////////////////////////////////////////////
  148. std::size_t GetSamplesCount() const;
  149. ////////////////////////////////////////////////////////////
  150. /// \brief Get the sample rate of the sound
  151. ///
  152. /// The sample rate is the number of samples played per second.
  153. /// The higher, the better the quality (for example, 44100
  154. /// samples/s is CD quality).
  155. ///
  156. /// \return Sample rate (number of samples per second)
  157. ///
  158. /// \see GetChannelsCount, GetDuration
  159. ///
  160. ////////////////////////////////////////////////////////////
  161. unsigned int GetSampleRate() const;
  162. ////////////////////////////////////////////////////////////
  163. /// \brief Get the number of channels used by the sound
  164. ///
  165. /// If the sound is mono then the number ofchannels will
  166. /// be 1, 2 for stereo, etc.
  167. ///
  168. /// \return Number of channels
  169. ///
  170. /// \see GetSampleRate, GetDuration
  171. ///
  172. ////////////////////////////////////////////////////////////
  173. unsigned int GetChannelsCount() const;
  174. ////////////////////////////////////////////////////////////
  175. /// \brief Get the total duration of the sound
  176. ///
  177. /// \return Sound duration, in seconds
  178. ///
  179. /// \see GetSampleRate, GetChannelsCount
  180. ///
  181. ////////////////////////////////////////////////////////////
  182. float GetDuration() const;
  183. ////////////////////////////////////////////////////////////
  184. /// \brief Overload of assignment operator
  185. ///
  186. /// \param right Instance to assign
  187. ///
  188. /// \return Reference to self
  189. ///
  190. ////////////////////////////////////////////////////////////
  191. SoundBuffer& operator =(const SoundBuffer& right);
  192. private :
  193. friend class Sound;
  194. ////////////////////////////////////////////////////////////
  195. /// \brief Update the internal buffer with the cached audio samples
  196. ///
  197. /// \param channelsCount Number of channels
  198. /// \param sampleRate Sample rate (number of samples per second)
  199. ///
  200. /// \return True on success, false if any error happened
  201. ///
  202. ////////////////////////////////////////////////////////////
  203. bool Update(unsigned int channelsCount, unsigned int sampleRate);
  204. ////////////////////////////////////////////////////////////
  205. /// \brief Add a sound to the list of sounds that use this buffer
  206. ///
  207. /// \param sound Sound instance to attach
  208. ///
  209. ////////////////////////////////////////////////////////////
  210. void AttachSound(Sound* sound) const;
  211. ////////////////////////////////////////////////////////////
  212. /// \brief Remove a sound from the list of sounds that use this buffer
  213. ///
  214. /// \param sound Sound instance to detach
  215. ///
  216. ////////////////////////////////////////////////////////////
  217. void DetachSound(Sound* sound) const;
  218. ////////////////////////////////////////////////////////////
  219. // Types
  220. ////////////////////////////////////////////////////////////
  221. typedef std::set<Sound*> SoundList; ///< Set of unique sound instances
  222. ////////////////////////////////////////////////////////////
  223. // Member data
  224. ////////////////////////////////////////////////////////////
  225. unsigned int myBuffer; ///< OpenAL buffer identifier
  226. std::vector<Int16> mySamples; ///< Samples buffer
  227. float myDuration; ///< Sound duration, in seconds
  228. mutable SoundList mySounds; ///< List of sounds that are using this buffer
  229. };
  230. } // namespace sf
  231. #endif // SFML_SOUNDBUFFER_HPP
  232. ////////////////////////////////////////////////////////////
  233. /// \class sf::SoundBuffer
  234. /// \ingroup audio
  235. ///
  236. /// A sound buffer holds the data of a sound, which is
  237. /// an array of audio samples. A sample is a 16 bits signed integer
  238. /// that defines the amplitude of the sound at a given time.
  239. /// The sound is then restituted by playing these samples at
  240. /// a high rate (for example, 44100 samples per second is the
  241. /// standard rate used for playing CDs). In short, audio samples
  242. /// are like image pixels, and a sf::SoundBuffer is similar to
  243. /// a sf::Image.
  244. ///
  245. /// A sound buffer can be loaded from a file (see LoadFromFile()
  246. /// for the complete list of supported formats), from memory
  247. /// or directly from an array of samples. It can also be saved
  248. /// back to a file.
  249. ///
  250. /// Sound buffers alone are not very useful: they hold the audio data
  251. /// but cannot be played. To do so, you need to use the sf::Sound class,
  252. /// which provides functions to play/pause/stop the sound as well as
  253. /// changing the way it is outputted (volume, pitch, 3D position, ...).
  254. /// This separation allows more flexibility and better performances:
  255. /// indeed a sf::SoundBuffer is a heavy resource, and any operation on it
  256. /// is slow (often too slow for real-time applications). On the other
  257. /// side, a sf::Sound is a lightweight object, which can use the audio data
  258. /// of a sound buffer and change the way it is played without actually
  259. /// modifying that data. Note that it is also possible to bind
  260. /// several sf::Sound instances to the same sf::SoundBuffer.
  261. ///
  262. /// It is important to note that the sf::Sound instance doesn't
  263. /// copy the buffer that it uses, it only keeps a reference to it.
  264. /// Thus, a sf::SoundBuffer must not be destructed while it is
  265. /// used by a sf::Sound (i.e. never write a function that
  266. /// uses a local sf::SoundBuffer instance for loading a sound).
  267. ///
  268. /// Usage example:
  269. /// \code
  270. /// // Declare a new sound buffer
  271. /// sf::SoundBuffer buffer;
  272. ///
  273. /// // Load it from a file
  274. /// if (!buffer.LoadFromFile("sound.wav"))
  275. /// {
  276. /// // error...
  277. /// }
  278. ///
  279. /// // Create a sound source and bind it to the buffer
  280. /// sf::Sound sound1;
  281. /// sound1.SetBuffer(buffer);
  282. ///
  283. /// // Play the sound
  284. /// sound1.Play();
  285. ///
  286. /// // Create another sound source bound to the same buffer
  287. /// sf::Sound sound2;
  288. /// sound2.SetBuffer(buffer);
  289. ///
  290. /// // Play it with a higher pitch -- the first sound remains unchanged
  291. /// sound2.SetPitch(2);
  292. /// sound2.Play();
  293. /// \endcode
  294. ///
  295. /// \see sf::Sound, sf::SoundBufferRecorder
  296. ///
  297. ////////////////////////////////////////////////////////////