1 /* 2 DSFML - The Simple and Fast Multimedia Library for D 3 4 Copyright (c) 2013 - 2015 Jeremy DeHaan (dehaan.jeremiah@gmail.com) 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, including commercial applications, 10 and to alter it and redistribute it freely, subject to the following restrictions: 11 12 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. 13 If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 14 15 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 16 17 3. This notice may not be removed or altered from any source distribution 18 */ 19 20 module dsfml.audio.soundstream; 21 22 23 import core.thread; 24 import core.time; 25 26 import dsfml.audio.soundsource; 27 28 import dsfml.system.vector3; 29 30 import dsfml.system.err; 31 32 33 /++ 34 + Abstract base class for streamed audio sources. 35 + 36 + Unlike audio buffers (see SoundBuffer), audio streams are never completely loaded in memory. 37 + 38 + Instead, the audio data is acquired continuously while the stream is playing. This behaviour allows to play a sound with no loading delay, and keeps the memory consumption very low. 39 + 40 + Sound sources that need to be streamed are usually big files (compressed audio musics that would eat hundreds of MB in memory) or files that would take a lot of time to be received (sounds played over the network). 41 + 42 + SoundStream is a base class that doesn't care about the stream source, which is left to the derived class. SFML provides a built-in specialization for big files (see Music). No network stream source is provided, but you can write your own by combining this class with the network module. 43 + 44 + A derived class has to override two virtual functions: 45 + - onGetData fills a new chunk of audio data to be played. 46 + - onSeek changes the current playing position in the source 47 + 48 + It is important to note that each SoundStream is played in its own separate thread, so that the streaming loop doesn't block the rest of the program. In particular, the OnGetData and OnSeek virtual functions may sometimes be called from this separate thread. It is important to keep this in mind, because you may have to take care of synchronization issues if you share data between threads. 49 + 50 + See_Also: http://sfml-dev.org/documentation/2.0/classsf_1_1SoundStream.php#details 51 + Authors: Laurent Gomila, Jeremy DeHaan 52 +/ 53 class SoundStream:SoundSource 54 { 55 56 package sfSoundStream* sfPtr; 57 58 private SoundStreamCallBacks callBacks; 59 60 protected this() 61 { 62 callBacks = new SoundStreamCallBacks(this); 63 sfPtr = sfSoundStream_construct(callBacks); 64 } 65 66 ~this() 67 { 68 import dsfml.system.config; 69 mixin(destructorOutput); 70 sfSoundStream_destroy(sfPtr); 71 } 72 73 protected void initialize(uint channelCount, uint sampleRate) 74 { 75 import dsfml.system.string; 76 77 sfSoundStream_initialize(sfPtr, channelCount, sampleRate); 78 79 err.write(dsfml.system..string.toString(sfErr_getOutput())); 80 } 81 82 83 84 @property 85 { 86 void pitch(float newPitch) 87 { 88 sfSoundStream_setPitch(sfPtr, newPitch); 89 } 90 91 float pitch() 92 { 93 return sfSoundStream_getPitch(sfPtr); 94 } 95 } 96 97 /** 98 * The volume of the sound. 99 * 100 * The volume is a vlue between 0 (mute) and 100 (full volume). The default value for the volume is 100. 101 */ 102 @property 103 { 104 void volume(float newVolume) 105 { 106 sfSoundStream_setVolume(sfPtr, newVolume); 107 } 108 109 float volume() 110 { 111 return sfSoundStream_getVolume(sfPtr); 112 } 113 } 114 115 /** 116 * The 3D position of the sound in the audio scene. 117 * 118 * Only sounds with one channel (mono sounds) can be spatialized. The default position of a sound is (0, 0, 0). 119 */ 120 @property 121 { 122 void position(Vector3f newPosition) 123 { 124 sfSoundStream_setPosition(sfPtr, newPosition.x, newPosition.y, newPosition.z); 125 } 126 127 Vector3f position() 128 { 129 Vector3f temp; 130 sfSoundStream_getPosition(sfPtr, &temp.x, &temp.y, &temp.z); 131 return temp; 132 } 133 } 134 135 /** 136 * Whether or not the stream should loop after reaching the end. 137 * 138 * If set, the stream will restart from the beginning after reaching the end and so on, until it is stopped or looping is set to false. 139 * 140 * Default looping state for streams is false. 141 */ 142 @property 143 { 144 void isLooping(bool loop) 145 { 146 sfSoundStream_setLoop(sfPtr, loop); 147 } 148 149 bool isLooping() 150 { 151 return sfSoundStream_getLoop(sfPtr); 152 } 153 } 154 155 /** 156 * The current playing position (from the beginning) of the stream. 157 * 158 * The playing position can be changed when the stream is either paused or playing. 159 */ 160 @property 161 { 162 void playingOffset(Duration offset) 163 { 164 sfSoundStream_setPlayingOffset(sfPtr, offset.total!"usecs"); 165 166 } 167 168 Duration playingOffset() 169 { 170 return usecs(sfSoundStream_getPlayingOffset(sfPtr)); 171 } 172 } 173 174 /** 175 * Make the sound's position relative to the listener (true) or absolute (false). 176 * 177 * Making a sound relative to the listener will ensure that it will always be played the same way regardless the position of the listener. This can be useful for non-spatialized sounds, sounds that are produced by the listener, or sounds attached to it. The default value is false (position is absolute). 178 */ 179 @property 180 { 181 void relativeToListener(bool relative) 182 { 183 sfSoundStream_setRelativeToListener(sfPtr, relative); 184 } 185 186 bool relativeToListener() 187 { 188 return sfSoundStream_isRelativeToListener(sfPtr); 189 } 190 } 191 192 /** 193 * The minimum distance of the sound. 194 * 195 * The "minimum distance" of a sound is the maximum distance at which it is heard at its maximum volume. Further than the minimum distance, it will start to fade out according to its attenuation factor. A value of 0 ("inside the head of the listener") is an invalid value and is forbidden. The default value of the minimum distance is 1. 196 */ 197 @property 198 { 199 void minDistance(float distance) 200 { 201 sfSoundStream_setMinDistance(sfPtr, distance); 202 } 203 204 float minDistance() 205 { 206 return sfSoundStream_getMinDistance(sfPtr); 207 } 208 } 209 210 /** 211 * The attenuation factor of the sound. 212 * 213 * The attenuation is a multiplicative factor which makes the sound more or less loud according to its distance from the listener. An attenuation of 0 will produce a non-attenuated sound, i.e. its volume will always be the same whether it is heard from near or from far. 214 * 215 * On the other hand, an attenuation value such as 100 will make the sound fade out very quickly as it gets further from the listener. The default value of the attenuation is 1. 216 */ 217 @property 218 { 219 void attenuation(float newAttenuation) 220 { 221 sfSoundStream_setAttenuation(sfPtr, newAttenuation); 222 } 223 224 float attenuation() 225 { 226 return sfSoundStream_getAttenuation(sfPtr); 227 } 228 } 229 230 231 /** 232 * The number of channels of the stream. 233 * 234 * 1 channel means mono sound, 2 means stereo, etc. 235 * 236 * Returns: Number of channels 237 */ 238 @property 239 { 240 uint channelCount() 241 { 242 return sfSoundStream_getChannelCount(sfPtr); 243 } 244 } 245 246 /** 247 * The stream sample rate of the stream 248 * 249 * The sample rate is the number of audio samples played per second. The higher, the better the quality. 250 * 251 * Returns: Sample rate, in number of samples per second. 252 */ 253 @property 254 { 255 uint sampleRate() 256 { 257 return sfSoundStream_getSampleRate(sfPtr); 258 } 259 } 260 261 @property 262 { 263 Status status() 264 { 265 return cast(Status)sfSoundStream_getStatus(sfPtr); 266 } 267 } 268 269 void play() 270 { 271 import dsfml.system.string; 272 273 sfSoundStream_play(sfPtr); 274 275 err.write(dsfml.system..string.toString(sfErr_getOutput())); 276 } 277 278 void pause() 279 { 280 sfSoundStream_pause(sfPtr); 281 } 282 283 void stop() 284 { 285 sfSoundStream_stop(sfPtr); 286 } 287 288 abstract bool onGetData(ref const(short)[] samples); 289 290 abstract void onSeek(Duration timeOffset); 291 292 293 } 294 295 private extern(C++) 296 { 297 struct Chunk 298 { 299 const(short)* samples; 300 size_t sampleCount; 301 } 302 } 303 304 private extern(C++) interface sfmlSoundStreamCallBacks 305 { 306 public: 307 bool onGetData(Chunk* chunk); 308 void onSeek(long time); 309 } 310 311 312 class SoundStreamCallBacks: sfmlSoundStreamCallBacks 313 { 314 SoundStream m_stream; 315 316 this(SoundStream stream) 317 { 318 m_stream = stream; 319 } 320 321 extern(C++) bool onGetData(Chunk* chunk) 322 { 323 const(short)[] samples; 324 325 auto ret = m_stream.onGetData(samples); 326 327 (*chunk).samples = samples.ptr; 328 (*chunk).sampleCount = samples.length; 329 330 return ret; 331 332 } 333 334 extern(C++) void onSeek(long time) 335 { 336 m_stream.onSeek(usecs(time)); 337 } 338 339 340 341 } 342 343 private extern(C): 344 345 346 347 struct sfSoundStream; 348 349 350 sfSoundStream* sfSoundStream_construct(sfmlSoundStreamCallBacks callBacks); 351 352 void sfSoundStream_destroy(sfSoundStream* soundStream); 353 354 void sfSoundStream_initialize(sfSoundStream* soundStream, uint channelCount, uint sampleRate); 355 356 void sfSoundStream_play(sfSoundStream* soundStream); 357 358 void sfSoundStream_pause(sfSoundStream* soundStream); 359 360 void sfSoundStream_stop(sfSoundStream* soundStream); 361 362 int sfSoundStream_getStatus(const sfSoundStream* soundStream); 363 364 uint sfSoundStream_getChannelCount(const sfSoundStream* soundStream); 365 366 uint sfSoundStream_getSampleRate(const sfSoundStream* soundStream); 367 368 void sfSoundStream_setPitch(sfSoundStream* soundStream, float pitch); 369 370 void sfSoundStream_setVolume(sfSoundStream* soundStream, float volume); 371 372 void sfSoundStream_setPosition(sfSoundStream* soundStream, float positionX, float positionY, float positionZ); 373 374 void sfSoundStream_setRelativeToListener(sfSoundStream* soundStream, bool relative); 375 376 void sfSoundStream_setMinDistance(sfSoundStream* soundStream, float distance); 377 378 void sfSoundStream_setAttenuation(sfSoundStream* soundStream, float attenuation); 379 380 void sfSoundStream_setPlayingOffset(sfSoundStream* soundStream, long timeOffset); 381 382 void sfSoundStream_setLoop(sfSoundStream* soundStream, bool loop); 383 384 float sfSoundStream_getPitch(const sfSoundStream* soundStream); 385 386 float sfSoundStream_getVolume(const sfSoundStream* soundStream); 387 388 void sfSoundStream_getPosition(const sfSoundStream* soundStream, float* positionX, float* positionY, float* positionZ); 389 390 bool sfSoundStream_isRelativeToListener(const sfSoundStream* soundStream); 391 392 float sfSoundStream_getMinDistance(const sfSoundStream* soundStream); 393 394 float sfSoundStream_getAttenuation(const sfSoundStream* soundStream); 395 396 bool sfSoundStream_getLoop(const sfSoundStream* soundStream); 397 398 long sfSoundStream_getPlayingOffset(const sfSoundStream* soundStream); 399 400 const(char)* sfErr_getOutput();