SoundStream.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_SOUNDSTREAM_HPP
26 #define CPP3DS_SOUNDSTREAM_HPP
27 
29 // Headers
31 #ifndef EMULATION
32 #include <cpp3ds/System/LinearAllocator.hpp>
33 #endif
34 #include <cpp3ds/Audio/SoundSource.hpp>
35 #include <cpp3ds/System/Thread.hpp>
36 #include <cpp3ds/System/Time.hpp>
37 #include <cpp3ds/System/Mutex.hpp>
38 #include <cstdlib>
39 #include <vector>
40 
41 
42 namespace cpp3ds
43 {
48 class SoundStream : public SoundSource
49 {
50 public:
51 
56  struct Chunk
57  {
58  const Int16* samples;
59  std::size_t sampleCount;
60  };
61 
66  virtual ~SoundStream();
67 
80  void play();
81 
91  void pause();
92 
103  void stop();
104 
113  unsigned int getChannelCount() const;
114 
124  unsigned int getSampleRate() const;
125 
132  Status getStatus() const;
133 
147  void setPlayingOffset(Time timeOffset);
148 
157  Time getPlayingOffset() const;
158 
172  void setLoop(bool loop);
173 
182  bool getLoop() const;
183 
184 protected:
185 
192  SoundStream();
193 
208  void initialize(unsigned int channelCount, unsigned int sampleRate);
209 
227  virtual bool onGetData(Chunk& data) = 0;
228 
238  virtual void onSeek(Time timeOffset) = 0;
239 
240 private:
241 
249  void streamData();
250 
264  bool fillAndPushBuffer(unsigned int bufferNum);
265 
275  bool fillQueue();
276 
283  void clearQueue();
284 
285  enum
286  {
287  BufferCount = 3
288  };
289 
291  // Member data
293  Thread m_thread;
294  mutable Mutex m_threadMutex;
295  Status m_threadStartState;
296  bool m_isStreaming;
297  unsigned int m_channelCount;
298  unsigned int m_sampleRate;
299  Uint32 m_format;
300  bool m_loop;
301  Uint64 m_samplesProcessed;
302  bool m_endBuffers[BufferCount];
303 #ifndef EMULATION
304  ndspWaveBuf m_ndspWaveBuffers[BufferCount];
305  std::vector<Int16, LinearAllocator<Int16>> m_buffers[BufferCount];
306 #else
307  unsigned int m_buffers[BufferCount];
308 #endif
309 };
310 
311 } // namespace cpp3ds
312 
313 
314 #endif // CPP3DS_SOUNDSTREAM_HPP
315 
316 
Utility class to manipulate threads.
Definition: Thread.hpp:53
void setPlayingOffset(Time timeOffset)
Change the current playing position of the stream.
Base class defining a sound's properties.
Definition: SoundSource.hpp:44
Abstract base class for streamed audio sources.
Definition: SoundStream.hpp:48
void stop()
Stop playing the audio stream.
unsigned int getSampleRate() const
Get the stream sample rate of the stream.
virtual bool onGetData(Chunk &data)=0
Request a new chunk of audio samples from the stream source.
virtual void onSeek(Time timeOffset)=0
Change the current playing position in the stream source.
Represents a time value.
Definition: Time.hpp:37
unsigned int getChannelCount() const
Return the number of channels of the stream.
void pause()
Pause the audio stream.
Status
Enumeration of the sound source states.
Definition: SoundSource.hpp:52
Status getStatus() const
Get the current status of the stream (stopped, paused, playing)
void initialize(unsigned int channelCount, unsigned int sampleRate)
Define the audio stream parameters.
bool getLoop() const
Tell whether or not the stream is in loop mode.
Blocks concurrent access to shared resources from multiple threads.
Definition: Mutex.hpp:44
SoundStream()
Default constructor.
Structure defining a chunk of audio data to stream.
Definition: SoundStream.hpp:56
Time getPlayingOffset() const
Get the current playing position of the stream.
void play()
Start or resume playing the audio stream.
void setLoop(bool loop)
Set whether or not the stream should loop after reaching the end.
std::size_t sampleCount
Number of samples pointed by Samples.
Definition: SoundStream.hpp:59
const Int16 * samples
Pointer to the audio samples.
Definition: SoundStream.hpp:58
virtual ~SoundStream()
Destructor.