Skip to content

abstract class SF::SoundStream
inherits SF::SoundSource #

Abstract base class for streamed audio sources

Unlike audio buffers (see SF::SoundBuffer), audio streams are never completely loaded in memory. Instead, the audio data is acquired continuously while the stream is playing. This behavior 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).

SF::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 SF::Music). 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:

  • on_get_data fills a new chunk of audio data to be played
  • on_seek changes the current playing position in the source

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.

Usage example:

class CustomStream < SF::SoundStream
  def initialize(location : String)
    # Open the source and get audio settings
    # [...]

    # Initialize the stream -- important!
    super(channel_count, sample_rate)
  end

  def on_get_data
    # Return a slice with audio data from the stream source
    # (note: must not be empty if you want to continue playing)
    Slice.new(samples.to_unsafe, samples.size)
  end

  def on_seek(time_offset)
    # Change the current position in the stream source
  end
end

# Usage
stream = CustomStream.new("path/to/stream")
stream.play

See also: SF::Music

Direct known subclasses

SF::Music

Constants#

NoLoop = -1#

"Invalid" end_seeks value, telling us to continue uninterrupted

Constructors#

.new(channel_count : Int, sample_rate : Int)#

Define the audio stream parameters

This function must be called by derived classes as soon as they know the audio settings of the stream to play. Any attempt to manipulate the stream (play(), ...) before calling this function will fail. It can be called multiple times if the settings of the audio stream change, but only when the stream is stopped.

  • channel_count - Number of channels of the stream
  • sample_rate - Sample rate, in samples per second
View source

Methods#

#channel_count : Int32#

Return the number of channels of the stream

1 channel means a mono sound, 2 means stereo, etc.

Returns: Number of channels

View source

#finalize#

Destructor

View source

#loop : Bool#

Tell whether or not the stream is in loop mode

Returns: True if the stream is looping, false otherwise

See also: loop=

View source

#loop=(loop : Bool)#

Set whether or not the stream should loop after reaching the end

If set, the stream will restart from beginning after reaching the end and so on, until it is stopped or loop=(false) is called. The default looping state for streams is false.

  • loop - True to play in loop, false to play once

See also: loop

View source

abstract #on_get_data : Slice(Int16) | ::Nil#

Request a new chunk of audio samples from the stream source

This function must be overridden by derived classes to provide the audio samples to play. It is called continuously by the streaming loop, in a separate thread. The source can choose to stop the streaming loop at any time, by returning false to the caller. If you return true (i.e. continue streaming) it is important that the returned array of samples is not empty; this would stop the stream due to an internal limitation.

  • data - Chunk of data to fill

Returns: True to continue playback, false to stop

View source

#on_loop : Int64#

Change the current playing position in the stream source to the beginning of the loop

This function can be overridden by derived classes to allow implementation of custom loop points. Otherwise, it just calls on_seek(Time::Zero) and returns 0.

Returns: The seek position after looping (or -1 if there's no loop)

View source

abstract #on_seek(time_offset : Time)#

Change the current playing position in the stream source

This function must be overridden by derived classes to allow random seeking into the stream source.

  • time_offset - New playing position, relative to the beginning of the stream
View source

#pause#

Pause the audio stream

This function pauses the stream if it was playing, otherwise (stream already paused or stopped) it has no effect.

See also: play, stop

View source

#play#

Start or resume playing the audio stream

This function starts the stream if it was stopped, resumes it if it was paused, and restarts it from the beginning if it was already playing. This function uses its own thread so that it doesn't block the rest of the program while the stream is played.

See also: pause, stop

View source

#playing_offset : Time#

Get the current playing position of the stream

Returns: Current playing position, from the beginning of the stream

See also: playing_offset=

View source

#playing_offset=(time_offset : Time)#

Change the current playing position of the stream

The playing position can be changed when the stream is either paused or playing. Changing the playing position when the stream is stopped has no effect, since playing the stream would reset its position.

  • time_offset - New playing position, from the beginning of the stream

See also: playing_offset

View source

#processing_interval=(interval : Time)#

Set the processing interval

The processing interval controls the period at which the audio buffers are filled by calls to on_get_data. A smaller interval may be useful for low-latency streams. Note that the given period is only a hint and the actual period may vary. The default processing interval is 10 ms.

  • interval - Processing interval
View source

#sample_rate : Int32#

Get the stream sample rate of the stream

The sample rate is the number of audio samples played per second. The higher, the better the quality.

Returns: Sample rate, in number of samples per second

View source

#status : SoundSource::Status#

Get the current status of the stream (stopped, paused, playing)

Returns: Current status

View source

#stop#

Stop playing the audio stream

This function stops the stream if it was playing or paused, and does nothing if it was already stopped. It also resets the playing position (unlike pause()).

See also: play, pause

View source