abstract class SF::SoundStream

Overview

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:

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

Defined in:

audio/obj.cr

Constant Summary

NoLoop = -1

"Invalid" end_seeks value, telling us to continue uninterrupted

Constructors

Instance Method Summary

Instance methods inherited from class SF::SoundSource

attenuation : Float32 attenuation, attenuation=(attenuation : Number) attenuation=, finalize finalize, min_distance : Float32 min_distance, min_distance=(distance : Number) min_distance=, pause pause, pitch : Float32 pitch, pitch=(pitch : Number) pitch=, play play, position : Vector3f position, position=(position : Vector3f) position=, relative_to_listener=(relative : Bool) relative_to_listener=, relative_to_listener? : Bool relative_to_listener?, set_position(x : Number, y : Number, z : Number) set_position, status : SoundSource::Status status, stop stop, volume : Float32 volume, volume=(volume : Number) volume=

Constructor Detail

def self.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]

Instance Method Detail

def 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]
def finalize #

Destructor


[View source]
def 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]
def 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 def 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]
def 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 def 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]
def 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]
def 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]
def 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]
def 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]
def 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]
def status : SoundSource::Status #

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

Returns: Current status


[View source]
def 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]