28#include <SFML/Audio/SoundStream.hpp>
29#include <SFML/Audio/AudioDevice.hpp>
30#include <SFML/Audio/OpenAL.hpp>
31#include <SFML/System/Sleep.hpp>
66 myChannelsCount = ChannelsCount;
67 mySampleRate = SampleRate;
70 myFormat = priv::AudioDevice::GetInstance().GetFormatFromChannelsCount(ChannelsCount);
77 std::cerr <<
"Unsupported number of channels (" << myChannelsCount <<
")" << std::endl;
90 std::cerr <<
"Failed to play audio stream : sound parameters have not been initialized (call Initialize first)" << std::endl;
105 mySamplesProcessed = 0;
106 myIsStreaming =
true;
118 myIsStreaming =
false;
128 return myChannelsCount;
189void SoundStream::Run()
192 ALCheck(alGenBuffers(BuffersCount, myBuffers));
193 for (
int i = 0; i < BuffersCount; ++i)
194 myEndBuffers[i] =
false;
197 bool RequestStop = FillQueue();
202 while (myIsStreaming)
215 myIsStreaming =
false;
221 ALCheck(alGetSourcei(Sound::mySource, AL_BUFFERS_PROCESSED, &NbProcessed));
223 while (NbProcessed--)
227 ALCheck(alSourceUnqueueBuffers(Sound::mySource, 1, &Buffer));
230 unsigned int BufferNum = 0;
231 for (
int i = 0; i < BuffersCount; ++i)
232 if (myBuffers[i] == Buffer)
239 if (myEndBuffers[BufferNum])
242 mySamplesProcessed = 0;
243 myEndBuffers[BufferNum] =
false;
248 ALCheck(alGetBufferi(Buffer, AL_SIZE, &Size));
249 mySamplesProcessed += Size /
sizeof(Int16);
255 if (FillAndPushBuffer(BufferNum))
272 ALCheck(alSourcei(Sound::mySource, AL_BUFFER, 0));
273 ALCheck(alDeleteBuffers(BuffersCount, myBuffers));
281bool SoundStream::FillAndPushBuffer(
unsigned int BufferNum)
283 bool RequestStop =
false;
286 Chunk Data = {NULL, 0};
287 if (!OnGetData(Data))
290 myEndBuffers[BufferNum] =
true;
293 if (myLoop && OnStart())
296 if (!Data.Samples || (Data.NbSamples == 0))
298 return FillAndPushBuffer(BufferNum);
309 if (Data.Samples && Data.NbSamples)
311 unsigned int Buffer = myBuffers[BufferNum];
314 ALsizei Size =
static_cast<ALsizei
>(Data.NbSamples) *
sizeof(Int16);
315 ALCheck(alBufferData(Buffer, myFormat, Data.Samples, Size, mySampleRate));
318 ALCheck(alSourceQueueBuffers(Sound::mySource, 1, &Buffer));
328bool SoundStream::FillQueue()
331 bool RequestStop =
false;
332 for (
int i = 0; (i < BuffersCount) && !RequestStop; ++i)
334 if (FillAndPushBuffer(i))
345void SoundStream::ClearQueue()
349 ALCheck(alGetSourcei(Sound::mySource, AL_BUFFERS_QUEUED, &NbQueued));
353 for (ALint i = 0; i < NbQueued; ++i)
354 ALCheck(alSourceUnqueueBuffers(Sound::mySource, 1, &Buffer));
361bool SoundStream::OnStart()
void SetLoop(bool Loop)
Set the stream loop state.
@ Playing
Sound is playing.
Status
Enumeration of the sound states.
@ Stopped
Sound is not playing.
void Play()
Start playing the audio stream.
void Stop()
Stop playing the audio stream.
float GetPlayingOffset() const
Get the current playing position of the stream.
virtual ~SoundStream()
Virtual destructor.
Status GetStatus() const
Get the status of the stream (stopped, paused, playing).
SoundStream()
Default constructor.
unsigned int GetSampleRate() const
Get the stream sample rate.
bool GetLoop() const
Tell whether or not the stream is looping.
void Initialize(unsigned int ChannelsCount, unsigned int SampleRate)
Set the audio stream parameters, you must call it before Play().
unsigned int GetChannelsCount() const
Return the number of channels (1 = mono, 2 = stereo).
float GetPlayingOffset() const
Get the current playing position of the sound.
Status
Enumeration of the sound states.
void Stop()
Stop the sound.
Status GetStatus() const
Get the status of the sound (stopped, paused, playing).
void Play()
Play the sound.
void Launch()
Create and run the thread.
void Wait()
Wait until the thread finishes.
Structure defining a chunk of audio data to stream.