1 ////////////////////////////////////////////////////////////
\r
3 // SFML - Simple and Fast Multimedia Library
\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
\r
6 // This software is provided 'as-is', without any express or implied warranty.
\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.
\r
9 // Permission is granted to anyone to use this software for any purpose,
\r
10 // including commercial applications, and to alter it and redistribute it freely,
\r
11 // subject to the following restrictions:
\r
13 // 1. The origin of this software must not be misrepresented;
\r
14 // you must not claim that you wrote the original software.
\r
15 // If you use this software in a product, an acknowledgment
\r
16 // in the product documentation would be appreciated but is not required.
\r
18 // 2. Altered source versions must be plainly marked as such,
\r
19 // and must not be misrepresented as being the original software.
\r
21 // 3. This notice may not be removed or altered from any source distribution.
\r
23 ////////////////////////////////////////////////////////////
\r
25 #ifndef SFML_SOUNDSTREAM_HPP
\r
26 #define SFML_SOUNDSTREAM_HPP
\r
28 ////////////////////////////////////////////////////////////
\r
30 ////////////////////////////////////////////////////////////
\r
31 #include <SFML/Audio/Sound.hpp>
\r
32 #include <SFML/System/Thread.hpp>
\r
38 ////////////////////////////////////////////////////////////
\r
39 /// SoundStream is a streamed sound, ie samples are acquired
\r
40 /// while the sound is playing. Use it for big sounds that would
\r
41 /// require hundreds of MB in memory (see Music),
\r
42 /// or for streaming sound from the network
\r
43 ////////////////////////////////////////////////////////////
\r
44 class SFML_API SoundStream : private Thread, private Sound
\r
48 using Sound::Status;
\r
49 using Sound::Stopped;
\r
50 using Sound::Paused;
\r
51 using Sound::Playing;
\r
53 using Sound::SetPitch;
\r
54 using Sound::SetVolume;
\r
55 using Sound::SetPosition;
\r
56 using Sound::SetRelativeToListener;
\r
57 using Sound::SetMinDistance;
\r
58 using Sound::SetAttenuation;
\r
59 using Sound::GetPitch;
\r
60 using Sound::GetVolume;
\r
61 using Sound::GetPosition;
\r
62 using Sound::IsRelativeToListener;
\r
63 using Sound::GetMinDistance;
\r
64 using Sound::GetAttenuation;
\r
66 ////////////////////////////////////////////////////////////
\r
67 /// Structure defining a chunk of audio data to stream
\r
68 ////////////////////////////////////////////////////////////
\r
71 const Int16* Samples; ///< Pointer to the audio samples
\r
72 std::size_t NbSamples; ///< Number of samples pointed by Samples
\r
75 ////////////////////////////////////////////////////////////
\r
76 /// Virtual destructor
\r
78 ////////////////////////////////////////////////////////////
\r
79 virtual ~SoundStream();
\r
81 ////////////////////////////////////////////////////////////
\r
82 /// Start playing the audio stream
\r
84 ////////////////////////////////////////////////////////////
\r
87 ////////////////////////////////////////////////////////////
\r
88 /// Stop playing the audio stream
\r
90 ////////////////////////////////////////////////////////////
\r
93 ////////////////////////////////////////////////////////////
\r
94 /// Return the number of channels (1 = mono, 2 = stereo)
\r
96 /// \return Number of channels
\r
98 ////////////////////////////////////////////////////////////
\r
99 unsigned int GetChannelsCount() const;
\r
101 ////////////////////////////////////////////////////////////
\r
102 /// Get the stream sample rate
\r
104 /// \return Stream frequency (number of samples per second)
\r
106 ////////////////////////////////////////////////////////////
\r
107 unsigned int GetSampleRate() const;
\r
109 ////////////////////////////////////////////////////////////
\r
110 /// Get the status of the stream (stopped, paused, playing)
\r
112 /// \return Current status of the sound
\r
114 ////////////////////////////////////////////////////////////
\r
115 Status GetStatus() const;
\r
117 ////////////////////////////////////////////////////////////
\r
118 /// Get the current playing position of the stream
\r
120 /// \return Current playing position, expressed in seconds
\r
122 ////////////////////////////////////////////////////////////
\r
123 float GetPlayingOffset() const;
\r
125 ////////////////////////////////////////////////////////////
\r
126 /// Set the stream loop state.
\r
127 /// This parameter is disabled by default
\r
129 /// \param Loop : True to play in loop, false to play once
\r
131 ////////////////////////////////////////////////////////////
\r
132 void SetLoop(bool Loop);
\r
134 ////////////////////////////////////////////////////////////
\r
135 /// Tell whether or not the stream is looping
\r
137 /// \return True if the music is looping, false otherwise
\r
139 ////////////////////////////////////////////////////////////
\r
140 bool GetLoop() const;
\r
144 ////////////////////////////////////////////////////////////
\r
145 /// Default constructor
\r
147 ////////////////////////////////////////////////////////////
\r
150 ////////////////////////////////////////////////////////////
\r
151 /// Set the audio stream parameters, you must call it before Play()
\r
153 /// \param ChannelsCount : Number of channels
\r
154 /// \param SampleRate : Sample rate
\r
156 ////////////////////////////////////////////////////////////
\r
157 void Initialize(unsigned int ChannelsCount, unsigned int SampleRate);
\r
161 ////////////////////////////////////////////////////////////
\r
162 /// /see Thread::Run
\r
164 ////////////////////////////////////////////////////////////
\r
165 virtual void Run();
\r
167 ////////////////////////////////////////////////////////////
\r
168 /// Called when the sound restarts
\r
170 /// \return If false is returned, the playback is aborted
\r
172 ////////////////////////////////////////////////////////////
\r
173 virtual bool OnStart();
\r
175 ////////////////////////////////////////////////////////////
\r
176 /// Called each time new audio data is needed to feed the stream
\r
178 /// \param Data : New chunk of data to stream
\r
180 /// \return True to continue playback, false to stop
\r
182 ////////////////////////////////////////////////////////////
\r
183 virtual bool OnGetData(Chunk& Data) = 0;
\r
185 ////////////////////////////////////////////////////////////
\r
186 /// Fill a new buffer with audio data, and push it to the
\r
189 /// \param Buffer : Number of the buffer to fill (in [0, BuffersCount])
\r
191 /// \return True if the derived class has requested to stop
\r
193 ////////////////////////////////////////////////////////////
\r
194 bool FillAndPushBuffer(unsigned int BufferNum);
\r
196 ////////////////////////////////////////////////////////////
\r
197 /// Fill the buffers queue with all available buffers
\r
199 /// \return True if the derived class has requested to stop
\r
201 ////////////////////////////////////////////////////////////
\r
204 ////////////////////////////////////////////////////////////
\r
205 /// Clear the queue of any remaining buffers
\r
207 ////////////////////////////////////////////////////////////
\r
210 enum {BuffersCount = 3};
\r
212 ////////////////////////////////////////////////////////////
\r
214 ////////////////////////////////////////////////////////////
\r
215 bool myIsStreaming; ///< Streaming state (true = playing, false = stopped)
\r
216 unsigned int myBuffers[BuffersCount]; ///< Sound buffers used to store temporary audio data
\r
217 unsigned int myChannelsCount; ///< Number of channels (1 = mono, 2 = stereo, ...)
\r
218 unsigned int mySampleRate; ///< Frequency (samples / second)
\r
219 unsigned long myFormat; ///< Format of the internal sound buffers
\r
220 bool myLoop; ///< Loop flag (true to loop, false to play once)
\r
221 unsigned int mySamplesProcessed; ///< Number of buffers processed since beginning of the stream
\r
222 bool myEndBuffers[BuffersCount]; ///< Each buffer is marked as "end buffer" or not, for proper duration calculation
\r
228 #endif // SFML_SOUNDSTREAM_HPP
\r