Abstract base class for streamed audio sources.
1 class CustomStream : SoundStream 2 { 3 4 bool open(const(char)[] location) 5 { 6 // Open the source and get audio settings 7 ... 8 uint channelCount = ...; 9 unint sampleRate = ...; 10 11 // Initialize the stream -- important! 12 initialize(channelCount, sampleRate); 13 } 14 15 protected: 16 override bool onGetData(ref const(short)[] samples) 17 { 18 // Fill the chunk with audio data from the stream source 19 // (note: must not be empty if you want to continue playing) 20 21 // Return true to continue playing 22 return true; 23 } 24 25 override void onSeek(Uint32 timeOffset) 26 { 27 // Change the current position in the stream source 28 ... 29 } 30 } 31 32 // Usage 33 auto stream = CustomStream(); 34 stream.open("path/to/stream"); 35 stream.play();
$(MUSIC_LINK)
Unlike audio buffers (see $(SOUNDBUFFER_LINK)), audio streams are never completely loaded in memory. 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.
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).
$(U 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_LINK)). No network stream source is provided, but you can write your own by combining this class with the network module.
A derived class has to override two virtual functions:
$(PARA It is important to note that each $(U 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.)