28 #include <SFML/Audio/SoundStream.hpp>
29 #include <SFML/Audio/AudioDevice.hpp>
30 #include <SFML/Audio/OpenAL.hpp>
31 #include <SFML/System/Sleep.hpp>
40 myIsStreaming (false),
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;
149 if ((Status ==
Stopped) && myIsStreaming)
189 void 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));
281 bool 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));
328 bool SoundStream::FillQueue()
331 bool RequestStop =
false;
332 for (
int i = 0; (i < BuffersCount) && !RequestStop; ++i)
334 if (FillAndPushBuffer(i))
345 void 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));
361 bool SoundStream::OnStart()
void Stop()
Stop the sound.
void Play()
Play the sound.
float GetPlayingOffset() const
Get the current playing position of the stream.
unsigned int GetSampleRate() const
Get the stream sample rate.
Status GetStatus() const
Get the status of the sound (stopped, paused, playing)
void Wait()
Wait until the thread finishes.
void SetLoop(bool Loop)
Set the stream loop state.
SoundStream()
Default constructor.
unsigned int GetChannelsCount() const
Return the number of channels (1 = mono, 2 = stereo)
float GetPlayingOffset() const
Get the current playing position of the sound.
void Initialize(unsigned int ChannelsCount, unsigned int SampleRate)
Set the audio stream parameters, you must call it before Play()
Status GetStatus() const
Get the status of the stream (stopped, paused, playing)
void Stop()
Stop playing the audio stream.
bool GetLoop() const
Tell whether or not the stream is looping.
Status
Enumeration of the sound states.
virtual ~SoundStream()
Virtual destructor.
void Launch()
Create and run the thread.
void Play()
Start playing the audio stream.