SoundStream.hpp
1 //
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 //
24 
25 #ifndef SFML_SOUNDSTREAM_HPP
26 #define SFML_SOUNDSTREAM_HPP
27 
29 // Headers
31 #include <SFML/Audio/Sound.hpp>
32 #include <SFML/System/Thread.hpp>
33 #include <cstdlib>
34 
35 
36 namespace sf
37 {
44 class SFML_API SoundStream : private Thread, private Sound
45 {
46 public :
47 
48  using Sound::Status;
49  using Sound::Stopped;
50  using Sound::Paused;
51  using Sound::Playing;
52  using Sound::Pause;
53  using Sound::SetPitch;
54  using Sound::SetVolume;
55  using Sound::SetPosition;
59  using Sound::GetPitch;
60  using Sound::GetVolume;
61  using Sound::GetPosition;
65 
69  struct Chunk
70  {
71  const Int16* Samples;
72  std::size_t NbSamples;
73  };
74 
79  virtual ~SoundStream();
80 
85  void Play();
86 
91  void Stop();
92 
99  unsigned int GetChannelsCount() const;
100 
107  unsigned int GetSampleRate() const;
108 
115  Status GetStatus() const;
116 
123  float GetPlayingOffset() const;
124 
132  void SetLoop(bool Loop);
133 
140  bool GetLoop() const;
141 
142 protected :
143 
148  SoundStream();
149 
157  void Initialize(unsigned int ChannelsCount, unsigned int SampleRate);
158 
159 private :
160 
165  virtual void Run();
166 
173  virtual bool OnStart();
174 
183  virtual bool OnGetData(Chunk& Data) = 0;
184 
194  bool FillAndPushBuffer(unsigned int BufferNum);
195 
202  bool FillQueue();
203 
208  void ClearQueue();
209 
210  enum {BuffersCount = 3};
211 
213  // Member data
215  bool myIsStreaming;
216  unsigned int myBuffers[BuffersCount];
217  unsigned int myChannelsCount;
218  unsigned int mySampleRate;
219  unsigned long myFormat;
220  bool myLoop;
221  unsigned int mySamplesProcessed;
222  bool myEndBuffers[BuffersCount];
223 };
224 
225 } // namespace sf
226 
227 
228 #endif // SFML_SOUNDSTREAM_HPP
void SetPitch(float Pitch)
Set the sound pitch.
Definition: Sound.cpp:158
void SetRelativeToListener(bool Relative)
Make the sound's position relative to the listener's position, or absolute.
Definition: Sound.cpp:197
void SetPosition(float X, float Y, float Z)
Set the sound position (take 3 values).
Definition: Sound.cpp:176
Sound is not playing.
Definition: Sound.hpp:54
Sound defines the properties of a sound such as position, volume, pitch, etc.
Definition: Sound.hpp:45
void SetAttenuation(float Attenuation)
Set the attenuation factor - the higher the attenuation, the more the sound will be attenuated with d...
Definition: Sound.cpp:219
bool IsRelativeToListener() const
Tell if the sound's position is relative to the listener's position, or if it's absolute.
Definition: Sound.cpp:295
Thread defines an easy way to manipulate a thread.
Vector3f GetPosition() const
Get the sound position.
Definition: Sound.cpp:282
const Int16 * Samples
Pointer to the audio samples.
Definition: SoundStream.hpp:71
Sound is paused.
Definition: Sound.hpp:55
void Pause()
Pause the sound.
Definition: Sound.cpp:112
SoundStream is a streamed sound, ie samples are acquired while the sound is playing.
Definition: SoundStream.hpp:44
float GetMinDistance() const
Get the minimum distance.
Definition: Sound.cpp:307
Sound is playing.
Definition: Sound.hpp:56
Structure defining a chunk of audio data to stream.
Definition: SoundStream.hpp:69
void SetVolume(float Volume)
Set the sound volume.
Definition: Sound.cpp:167
std::size_t NbSamples
Number of samples pointed by Samples.
Definition: SoundStream.hpp:72
float GetAttenuation() const
Get the attenuation factor.
Definition: Sound.cpp:319
Status
Enumeration of the sound states.
Definition: Sound.hpp:52
float GetVolume() const
Get the volume.
Definition: Sound.cpp:270
void SetMinDistance(float MinDistance)
Set the minimum distance - closer than this distance, the listener will hear the sound at its maximum...
Definition: Sound.cpp:208
float GetPitch() const
Get the pitch.
Definition: Sound.cpp:258