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 25 import dsfml.audio.soundsource; 26 27 import dsfml.system.time; 28 29 import dsfml.system.vector3; 30 31 import dsfml.system.err; 32 33 34 /++ 35 + Abstract base class for streamed audio sources. 36 + 37 + Unlike audio buffers (see SoundBuffer), audio streams are never completely loaded in memory. 38 + 39 + 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. 40 + 41 + 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). 42 + 43 + 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. 44 + 45 + A derived class has to override two virtual functions: 46 + - onGetData fills a new chunk of audio data to be played. 47 + - onSeek changes the current playing position in the source 48 + 49 + 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. 50 + 51 + See_Also: http://sfml-dev.org/documentation/2.0/classsf_1_1SoundStream.php#details 52 + Authors: Laurent Gomila, Jeremy DeHaan 53 +/ 54 class SoundStream:SoundSource 55 { 56 57 58 59 package sfSoundStream* sfPtr; 60 61 private SoundStreamCallBacks callBacks; 62 63 protected this() 64 { 65 callBacks = new SoundStreamCallBacks(this); 66 sfPtr = sfSoundStream_construct(callBacks); 67 } 68 69 ~this() 70 { 71 import dsfml.system.config; 72 mixin(destructorOutput); 73 sfSoundStream_destroy(sfPtr); 74 } 75 76 protected void initialize(uint channelCount, uint sampleRate) 77 { 78 import dsfml.system..string; 79 80 sfSoundStream_initialize(sfPtr, channelCount, sampleRate); 81 82 err.write(toString(sfErr_getOutput())); 83 } 84 85 86 87 @property 88 { 89 void pitch(float newPitch) 90 { 91 sfSoundStream_setPitch(sfPtr, newPitch); 92 } 93 94 float pitch() 95 { 96 return sfSoundStream_getPitch(sfPtr); 97 } 98 } 99 100 /** 101 * The volume of the sound. 102 * 103 * The volume is a vlue between 0 (mute) and 100 (full volume). The default value for the volume is 100. 104 */ 105 @property 106 { 107 void volume(float newVolume) 108 { 109 sfSoundStream_setVolume(sfPtr, newVolume); 110 } 111 112 float volume() 113 { 114 return sfSoundStream_getVolume(sfPtr); 115 } 116 } 117 118 /** 119 * The 3D position of the sound in the audio scene. 120 * 121 * Only sounds with one channel (mono sounds) can be spatialized. The default position of a sound is (0, 0, 0). 122 */ 123 @property 124 { 125 void position(Vector3f newPosition) 126 { 127 sfSoundStream_setPosition(sfPtr, newPosition.x, newPosition.y, newPosition.z); 128 } 129 130 Vector3f position() 131 { 132 Vector3f temp; 133 sfSoundStream_getPosition(sfPtr, &temp.x, &temp.y, &temp.z); 134 return temp; 135 } 136 } 137 138 /** 139 * Whether or not the stream should loop after reaching the end. 140 * 141 * 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. 142 * 143 * Default looping state for streams is false. 144 */ 145 @property 146 { 147 void isLooping(bool loop) 148 { 149 sfSoundStream_setLoop(sfPtr, loop); 150 } 151 152 bool isLooping() 153 { 154 return sfSoundStream_getLoop(sfPtr); 155 } 156 } 157 158 /** 159 * The current playing position (from the beginning) of the stream. 160 * 161 * The playing position can be changed when the stream is either paused or playing. 162 */ 163 @property 164 { 165 void playingOffset(Time offset) 166 { 167 sfSoundStream_setPlayingOffset(sfPtr, offset.asMicroseconds()); 168 169 } 170 171 Time playingOffset() 172 { 173 return microseconds(sfSoundStream_getPlayingOffset(sfPtr)); 174 } 175 } 176 177 /** 178 * Make the sound's position relative to the listener (true) or absolute (false). 179 * 180 * 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). 181 */ 182 @property 183 { 184 void relativeToListener(bool relative) 185 { 186 sfSoundStream_setRelativeToListener(sfPtr, relative); 187 } 188 189 bool relativeToListener() 190 { 191 return sfSoundStream_isRelativeToListener(sfPtr); 192 } 193 } 194 195 /** 196 * The minimum distance of the sound. 197 * 198 * 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. 199 */ 200 @property 201 { 202 void minDistance(float distance) 203 { 204 sfSoundStream_setMinDistance(sfPtr, distance); 205 } 206 207 float minDistance() 208 { 209 return sfSoundStream_getMinDistance(sfPtr); 210 } 211 } 212 213 /** 214 * The attenuation factor of the sound. 215 * 216 * 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. 217 * 218 * 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. 219 */ 220 @property 221 { 222 void attenuation(float newAttenuation) 223 { 224 sfSoundStream_setAttenuation(sfPtr, newAttenuation); 225 } 226 227 float attenuation() 228 { 229 return sfSoundStream_getAttenuation(sfPtr); 230 } 231 } 232 233 234 /** 235 * The number of channels of the stream. 236 * 237 * 1 channel means mono sound, 2 means stereo, etc. 238 * 239 * Returns: Number of channels 240 */ 241 @property 242 { 243 uint channelCount() 244 { 245 return sfSoundStream_getChannelCount(sfPtr); 246 } 247 } 248 249 /** 250 * The stream sample rate of the stream 251 * 252 * The sample rate is the number of audio samples played per second. The higher, the better the quality. 253 * 254 * Returns: Sample rate, in number of samples per second. 255 */ 256 @property 257 { 258 uint sampleRate() 259 { 260 return sfSoundStream_getSampleRate(sfPtr); 261 } 262 } 263 264 @property 265 { 266 Status status() 267 { 268 return cast(Status)sfSoundStream_getStatus(sfPtr); 269 } 270 } 271 272 void play() 273 { 274 import dsfml.system..string; 275 276 sfSoundStream_play(sfPtr); 277 278 err.write(toString(sfErr_getOutput())); 279 } 280 281 void pause() 282 { 283 sfSoundStream_pause(sfPtr); 284 } 285 286 void stop() 287 { 288 sfSoundStream_stop(sfPtr); 289 } 290 291 abstract bool onGetData(ref const(short)[] samples); 292 293 abstract void onSeek(Time timeOffset); 294 295 296 } 297 298 private extern(C++) 299 { 300 struct Chunk 301 { 302 const(short)* samples; 303 size_t sampleCount; 304 } 305 } 306 307 private extern(C++) interface sfmlSoundStreamCallBacks 308 { 309 public: 310 bool onGetData(Chunk* chunk); 311 void onSeek(long time); 312 } 313 314 315 class SoundStreamCallBacks: sfmlSoundStreamCallBacks 316 { 317 SoundStream m_stream; 318 319 this(SoundStream stream) 320 { 321 m_stream = stream; 322 } 323 324 extern(C++) bool onGetData(Chunk* chunk) 325 { 326 const(short)[] samples; 327 328 auto ret = m_stream.onGetData(samples); 329 330 (*chunk).samples = samples.ptr; 331 (*chunk).sampleCount = samples.length; 332 333 return ret; 334 335 } 336 337 extern(C++) void onSeek(long time) 338 { 339 m_stream.onSeek(microseconds(time)); 340 } 341 342 343 344 } 345 346 private extern(C): 347 348 349 350 struct sfSoundStream; 351 352 353 sfSoundStream* sfSoundStream_construct(sfmlSoundStreamCallBacks callBacks); 354 355 void sfSoundStream_destroy(sfSoundStream* soundStream); 356 357 void sfSoundStream_initialize(sfSoundStream* soundStream, uint channelCount, uint sampleRate); 358 359 void sfSoundStream_play(sfSoundStream* soundStream); 360 361 void sfSoundStream_pause(sfSoundStream* soundStream); 362 363 void sfSoundStream_stop(sfSoundStream* soundStream); 364 365 int sfSoundStream_getStatus(const sfSoundStream* soundStream); 366 367 uint sfSoundStream_getChannelCount(const sfSoundStream* soundStream); 368 369 uint sfSoundStream_getSampleRate(const sfSoundStream* soundStream); 370 371 void sfSoundStream_setPitch(sfSoundStream* soundStream, float pitch); 372 373 void sfSoundStream_setVolume(sfSoundStream* soundStream, float volume); 374 375 void sfSoundStream_setPosition(sfSoundStream* soundStream, float positionX, float positionY, float positionZ); 376 377 void sfSoundStream_setRelativeToListener(sfSoundStream* soundStream, bool relative); 378 379 void sfSoundStream_setMinDistance(sfSoundStream* soundStream, float distance); 380 381 void sfSoundStream_setAttenuation(sfSoundStream* soundStream, float attenuation); 382 383 void sfSoundStream_setPlayingOffset(sfSoundStream* soundStream, long timeOffset); 384 385 void sfSoundStream_setLoop(sfSoundStream* soundStream, bool loop); 386 387 float sfSoundStream_getPitch(const sfSoundStream* soundStream); 388 389 float sfSoundStream_getVolume(const sfSoundStream* soundStream); 390 391 void sfSoundStream_getPosition(const sfSoundStream* soundStream, float* positionX, float* positionY, float* positionZ); 392 393 bool sfSoundStream_isRelativeToListener(const sfSoundStream* soundStream); 394 395 float sfSoundStream_getMinDistance(const sfSoundStream* soundStream); 396 397 float sfSoundStream_getAttenuation(const sfSoundStream* soundStream); 398 399 bool sfSoundStream_getLoop(const sfSoundStream* soundStream); 400 401 long sfSoundStream_getPlayingOffset(const sfSoundStream* soundStream); 402 403 const(char)* sfErr_getOutput();