SoundBuffer.hpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
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 //
24 
25 #ifndef CPP3DS_SOUNDBUFFER_HPP
26 #define CPP3DS_SOUNDBUFFER_HPP
27 
29 // Headers
31 #ifndef EMULATION
32 #include <cpp3ds/System/LinearAllocator.hpp>
33 #endif
34 #include <cpp3ds/Audio/AlResource.hpp>
35 #include <cpp3ds/System/Time.hpp>
36 #include <string>
37 #include <vector>
38 #include <set>
39 
40 
41 namespace cpp3ds
42 {
43 class Sound;
44 class InputSoundFile;
45 class InputStream;
46 
52 {
53 public:
54 
59  SoundBuffer();
60 
67  SoundBuffer(const SoundBuffer& copy);
68 
73  ~SoundBuffer();
74 
88  bool loadFromFile(const std::string& filename);
89 
104  bool loadFromMemory(const void* data, std::size_t sizeInBytes);
105 
119  bool loadFromStream(InputStream& stream);
120 
137  bool loadFromSamples(const Int16* samples, Uint64 sampleCount, unsigned int channelCount, unsigned int sampleRate);
138 
152  bool saveToFile(const std::string& filename) const;
153 
166  const Int16* getSamples() const;
167 
179  Uint64 getSampleCount() const;
180 
193  unsigned int getSampleRate() const;
194 
206  unsigned int getChannelCount() const;
207 
216  Time getDuration() const;
217 
226  SoundBuffer& operator =(const SoundBuffer& right);
227 
228 private:
229 
230  friend class Sound;
231 
240  bool initialize(InputSoundFile& file);
241 
251  bool update(unsigned int channelCount, unsigned int sampleRate);
252 
259  void attachSound(Sound* sound) const;
260 
267  void detachSound(Sound* sound) const;
268 
270  // Types
272  typedef std::set<Sound*> SoundList;
273 
275  // Member data
277  Time m_duration;
278  mutable SoundList m_sounds;
279 
280  unsigned int m_sampleRate;
281  unsigned int m_channelCount;
282  #ifdef EMULATION
283  unsigned int m_buffer;
284  std::vector<Int16> m_samples;
285  #else
286  std::vector<Int16, LinearAllocator<Int16>> m_samples;
287  #endif
288 };
289 
290 } // namespace cpp3ds
291 
292 
293 #endif // CPP3DS_SOUNDBUFFER_HPP
294 
295 
unsigned int getSampleRate() const
Get the sample rate of the sound.
Abstract class for custom file input streams.
Definition: InputStream.hpp:40
SoundBuffer & operator=(const SoundBuffer &right)
Overload of assignment operator.
const Int16 * getSamples() const
Get the array of audio samples stored in the buffer.
bool loadFromMemory(const void *data, std::size_t sizeInBytes)
Load the sound buffer from a file in memory.
bool loadFromFile(const std::string &filename)
Load the sound buffer from a file.
bool loadFromSamples(const Int16 *samples, Uint64 sampleCount, unsigned int channelCount, unsigned int sampleRate)
Load the sound buffer from an array of audio samples.
Represents a time value.
Definition: Time.hpp:37
bool saveToFile(const std::string &filename) const
Save the sound buffer to an audio file.
Provide read access to sound files.
Storage for audio samples defining a sound.
Definition: SoundBuffer.hpp:51
Regular sound that can be played in the audio environment.
Definition: Sound.hpp:47
Time getDuration() const
Get the total duration of the sound.
Uint64 getSampleCount() const
Get the number of samples stored in the buffer.
SoundBuffer()
Default constructor.
bool loadFromStream(InputStream &stream)
Load the sound buffer from a custom stream.
unsigned int getChannelCount() const
Get the number of channels used by the sound.
~SoundBuffer()
Destructor.
Base class for classes that require an OpenAL context.
Definition: AlResource.hpp:39