]> git.sesse.net Git - casparcg/blob - dependencies/SFML-1.6/include/SFML/Audio/SoundStream.hpp
Subtree merge of old SVN "dependencies" folder into the "master" git branch. You...
[casparcg] / dependencies / SFML-1.6 / include / SFML / Audio / SoundStream.hpp
1 ////////////////////////////////////////////////////////////\r
2 //\r
3 // SFML - Simple and Fast Multimedia Library\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)\r
5 //\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
8 //\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
12 //\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
17 //\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
20 //\r
21 // 3. This notice may not be removed or altered from any source distribution.\r
22 //\r
23 ////////////////////////////////////////////////////////////\r
24 \r
25 #ifndef SFML_SOUNDSTREAM_HPP\r
26 #define SFML_SOUNDSTREAM_HPP\r
27 \r
28 ////////////////////////////////////////////////////////////\r
29 // Headers\r
30 ////////////////////////////////////////////////////////////\r
31 #include <SFML/Audio/Sound.hpp>\r
32 #include <SFML/System/Thread.hpp>\r
33 #include <cstdlib>\r
34 \r
35 \r
36 namespace sf\r
37 {\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
45 {\r
46 public :\r
47 \r
48     using Sound::Status;\r
49     using Sound::Stopped;\r
50     using Sound::Paused;\r
51     using Sound::Playing;\r
52     using Sound::Pause;\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
65 \r
66     ////////////////////////////////////////////////////////////\r
67     /// Structure defining a chunk of audio data to stream\r
68     ////////////////////////////////////////////////////////////\r
69     struct Chunk\r
70     {\r
71         const Int16* Samples;   ///< Pointer to the audio samples\r
72         std::size_t  NbSamples; ///< Number of samples pointed by Samples\r
73     };\r
74 \r
75     ////////////////////////////////////////////////////////////\r
76     /// Virtual destructor\r
77     ///\r
78     ////////////////////////////////////////////////////////////\r
79     virtual ~SoundStream();\r
80 \r
81     ////////////////////////////////////////////////////////////\r
82     /// Start playing the audio stream\r
83     ///\r
84     ////////////////////////////////////////////////////////////\r
85     void Play();\r
86 \r
87     ////////////////////////////////////////////////////////////\r
88     /// Stop playing the audio stream\r
89     ///\r
90     ////////////////////////////////////////////////////////////\r
91     void Stop();\r
92 \r
93     ////////////////////////////////////////////////////////////\r
94     /// Return the number of channels (1 = mono, 2 = stereo)\r
95     ///\r
96     /// \return Number of channels\r
97     ///\r
98     ////////////////////////////////////////////////////////////\r
99     unsigned int GetChannelsCount() const;\r
100 \r
101     ////////////////////////////////////////////////////////////\r
102     /// Get the stream sample rate\r
103     ///\r
104     /// \return Stream frequency (number of samples per second)\r
105     ///\r
106     ////////////////////////////////////////////////////////////\r
107     unsigned int GetSampleRate() const;\r
108 \r
109     ////////////////////////////////////////////////////////////\r
110     /// Get the status of the stream (stopped, paused, playing)\r
111     ///\r
112     /// \return Current status of the sound\r
113     ///\r
114     ////////////////////////////////////////////////////////////\r
115     Status GetStatus() const;\r
116 \r
117     ////////////////////////////////////////////////////////////\r
118     /// Get the current playing position of the stream\r
119     ///\r
120     /// \return Current playing position, expressed in seconds\r
121     ///\r
122     ////////////////////////////////////////////////////////////\r
123     float GetPlayingOffset() const;\r
124 \r
125     ////////////////////////////////////////////////////////////\r
126     /// Set the stream loop state.\r
127     /// This parameter is disabled by default\r
128     ///\r
129     /// \param Loop : True to play in loop, false to play once\r
130     ///\r
131     ////////////////////////////////////////////////////////////\r
132     void SetLoop(bool Loop);\r
133 \r
134     ////////////////////////////////////////////////////////////\r
135     /// Tell whether or not the stream is looping\r
136     ///\r
137     /// \return True if the music is looping, false otherwise\r
138     ///\r
139     ////////////////////////////////////////////////////////////\r
140     bool GetLoop() const;\r
141 \r
142 protected :\r
143 \r
144     ////////////////////////////////////////////////////////////\r
145     /// Default constructor\r
146     ///\r
147     ////////////////////////////////////////////////////////////\r
148     SoundStream();\r
149 \r
150     ////////////////////////////////////////////////////////////\r
151     /// Set the audio stream parameters, you must call it before Play()\r
152     ///\r
153     /// \param ChannelsCount : Number of channels\r
154     /// \param SampleRate :    Sample rate\r
155     ///\r
156     ////////////////////////////////////////////////////////////\r
157     void Initialize(unsigned int ChannelsCount, unsigned int SampleRate);\r
158 \r
159 private :\r
160 \r
161     ////////////////////////////////////////////////////////////\r
162     /// /see Thread::Run\r
163     ///\r
164     ////////////////////////////////////////////////////////////\r
165     virtual void Run();\r
166 \r
167     ////////////////////////////////////////////////////////////\r
168     /// Called when the sound restarts\r
169     ///\r
170     /// \return If false is returned, the playback is aborted\r
171     ///\r
172     ////////////////////////////////////////////////////////////\r
173     virtual bool OnStart();\r
174 \r
175     ////////////////////////////////////////////////////////////\r
176     /// Called each time new audio data is needed to feed the stream\r
177     ///\r
178     /// \param Data : New chunk of data to stream\r
179     ///\r
180     /// \return True to continue playback, false to stop\r
181     ///\r
182     ////////////////////////////////////////////////////////////\r
183     virtual bool OnGetData(Chunk& Data) = 0;\r
184 \r
185     ////////////////////////////////////////////////////////////\r
186     /// Fill a new buffer with audio data, and push it to the\r
187     /// playing queue\r
188     ///\r
189     /// \param Buffer : Number of the buffer to fill (in [0, BuffersCount])\r
190     ///\r
191     /// \return True if the derived class has requested to stop\r
192     ///\r
193     ////////////////////////////////////////////////////////////\r
194     bool FillAndPushBuffer(unsigned int BufferNum);\r
195 \r
196     ////////////////////////////////////////////////////////////\r
197     /// Fill the buffers queue with all available buffers\r
198     ///\r
199     /// \return True if the derived class has requested to stop\r
200     ///\r
201     ////////////////////////////////////////////////////////////\r
202     bool FillQueue();\r
203 \r
204     ////////////////////////////////////////////////////////////\r
205     /// Clear the queue of any remaining buffers\r
206     ///\r
207     ////////////////////////////////////////////////////////////\r
208     void ClearQueue();\r
209 \r
210     enum {BuffersCount = 3};\r
211 \r
212     ////////////////////////////////////////////////////////////\r
213     // Member data\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
223 };\r
224 \r
225 } // namespace sf\r
226 \r
227 \r
228 #endif // SFML_SOUNDSTREAM_HPP\r