sfml3.rb

Class: SFML::SoundStream

Inherits:
Object
  • Object
show all
Defined in:
ext/audio/sound_stream.c,
ext/audio/sound_stream.c

Overview

Base class for a custom audio source that generates or decodes its own samples on demand. Subclasses must implement #on_get_data, returning an Array of Integer samples or a packed String of int16 samples for the next chunk (or +nil+/+false+ to signal end of stream), and may implement #on_seek(time) to support seeking.

Instance Method Summary collapse

Constructor Details

#new(channel_count, sample_rate) ⇒ SoundStream #new(channel_count, sample_rate, channel_map) ⇒ SoundStream

SoundStream must be subclassed: the subclass is required to implement #on_get_data, called from a foreign audio thread whenever more samples are needed, and may optionally implement #on_seek. channel_map, if given, is an Array of channel Symbols (see SoundChannel), one per channel.

Overloads:

  • #new(channel_count, sample_rate) ⇒ SoundStream
  • #new(channel_count, sample_rate, channel_map) ⇒ SoundStream

Raises:

  • (NotImplementedError) —

    if the subclass does not define #on_get_data

  • (RuntimeError) —

    if the underlying stream could not be created



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'ext/audio/sound_stream.c', line 229

static VALUE SoundStream_initialize(int argc, VALUE* argv, VALUE self) {
    VALUE rb_channel_count, rb_sample_rate, rb_channel_map;
    sfSoundChannel* channel_map = NULL;
    size_t channel_map_size = 0;
    SoundStream* ptr;
    long i;

    TypedData_Get_Struct(self, SoundStream, &SoundStream_data_type, ptr);

    rb_scan_args(argc, argv, "21", &rb_channel_count, &rb_sample_rate, &rb_channel_map);

    if (!NIL_P(rb_channel_map)) {
        if (!RB_TYPE_P(rb_channel_map, T_ARRAY)) {
            rb_raise(rb_eArgError, "channel map must be an Array");
        }

        channel_map_size = (size_t)RARRAY_LEN(rb_channel_map);
        channel_map = malloc(sizeof(sfSoundChannel) * channel_map_size);

        for (i = 0; i < (long)channel_map_size; i++) {
            channel_map[i] = sound_channel_from_rb(rb_ary_entry(rb_channel_map, i));
        }
    }

    if (!rb_respond_to(self, rb_intern("on_get_data"))) {
        free(channel_map);
        rb_raise(rb_eNotImpError, "subclass must define #on_get_data");
    }

    ptr->source.handle = sfSoundStream_create(
        SoundStream_on_get_data, SoundStream_on_seek, (unsigned int)NUM2INT(rb_channel_count),
        (unsigned int)NUM2INT(rb_sample_rate), channel_map, channel_map_size, ptr);

    free(channel_map);

    if (ptr->source.handle == NULL) {
        rb_raise(rb_eRuntimeError, "failed to create sound stream");
    }

    return self;
}

Instance Method Details

#attenuation ⇒ Float

Returns the distance-attenuation factor.

Returns:

  • (Float)

#attenuation=(value) ⇒ Float

Sets the distance-attenuation factor.

Returns:

  • (Float)

#channel_count ⇒ Integer

Returns the number of audio channels the stream produces.

Returns:

  • (Integer)

Returns:

  • (Integer)


277
278
279
# File 'ext/audio/sound_stream.c', line 277

static VALUE SoundStream_channel_count(VALUE self) {
    return UINT2NUM(sfSoundStream_getChannelCount(Get_SoundStream_Struct(self)));
}

#channel_map ⇒ Array<Symbol>

Returns the channel layout of the stream.

Returns:

  • (Array<Symbol>)

Returns:

  • (Array<Symbol>) —

    one entry per channel, e.g. [:front_left, :front_right]



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'ext/audio/sound_stream.c', line 298

static VALUE SoundStream_channel_map(VALUE self) {
    size_t count = 0;
    sfSoundChannel* map = sfSoundStream_getChannelMap(Get_SoundStream_Struct(self), &count);
    VALUE rb_array;
    size_t i;

    if (map == NULL) {
        return rb_ary_new();
    }

    rb_array = rb_ary_new_capa((long)count);

    for (i = 0; i < count; i++) {
        rb_ary_push(rb_array, ID2SYM(rb_intern(sound_channel_name(map[i]))));
    }

    return rb_array;
}

#cone ⇒ SoundSourceCone

Returns the source's directional attenuation cone.

Returns:

#cone=(value) ⇒ SoundSourceCone

Sets the source's directional attenuation cone.

Returns:

#direction ⇒ Vector3

Returns the direction the source is facing.

Returns:

#direction=(value) ⇒ Vector3

Sets the direction the source is facing.

Returns:

#directional_attenuation_factor ⇒ Float

Returns the factor controlling directional attenuation.

Returns:

  • (Float)

#directional_attenuation_factor=(value) ⇒ Float

Sets the factor controlling directional attenuation.

Returns:

  • (Float)

#doppler_factor ⇒ Float

Returns the factor by which the Doppler effect is scaled.

Returns:

  • (Float)

#doppler_factor=(value) ⇒ Float

Sets the factor by which the Doppler effect is scaled.

Returns:

  • (Float)

#effect_processor=(proc) ⇒ Proc

Installs a Proc that post-processes this source's audio in real time.

Returns:

  • (Proc) —

    proc

#looping=(value) ⇒ Boolean

Enables or disables looping.

Returns:

  • (Boolean)

#looping? ⇒ Boolean

Returns true if playback loops back to the start on completion.

Returns:

  • (Boolean)

#max_distance ⇒ Float

Returns the maximum distance of the distance-attenuation model.

Returns:

  • (Float)

#max_distance=(value) ⇒ Float

Sets the maximum distance of the distance-attenuation model.

Returns:

  • (Float)

#max_gain ⇒ Float

Returns the maximum gain of the distance-attenuation model.

Returns:

  • (Float)

#max_gain=(value) ⇒ Float

Sets the maximum gain of the distance-attenuation model.

Returns:

  • (Float)

#min_distance ⇒ Float

Returns the minimum distance of the distance-attenuation model.

Returns:

  • (Float)

#min_distance=(value) ⇒ Float

Sets the minimum distance of the distance-attenuation model.

Returns:

  • (Float)

#min_gain ⇒ Float

Returns the minimum gain of the distance-attenuation model.

Returns:

  • (Float)

#min_gain=(value) ⇒ Float

Sets the minimum gain of the distance-attenuation model.

Returns:

  • (Float)

#pan ⇒ Float

Returns the source's stereo pan.

Returns:

  • (Float) —

    stereo pan, -1 (left) to 1 (right)

#pan=(value) ⇒ Float

Sets the source's stereo pan.

Returns:

  • (Float)

#pause ⇒ self

Pauses playback, keeping the current playing offset.

Returns:

  • (self)

#pitch ⇒ Float

Returns the pitch scaling factor.

Returns:

  • (Float)

#pitch=(value) ⇒ Float

Sets the pitch scaling factor.

Returns:

  • (Float)

#play ⇒ self

Starts playback, or resumes it when paused.

Returns:

  • (self)

#playing_offset ⇒ Time

Returns the current playing offset.

Returns:

#playing_offset=(value) ⇒ Time

Seeks to the given playing offset.

Returns:

#position ⇒ Vector3

Returns the source's position in 3D space.

Returns:

#position=(value) ⇒ Vector3

Sets the source's position in 3D space.

Returns:

#relative_to_listener=(value) ⇒ Boolean

Makes the source relative to, or independent of, the listener.

Returns:

  • (Boolean)

#relative_to_listener? ⇒ Boolean

Returns true if the source is positioned relative to the listener.

Returns:

  • (Boolean)

#sample_rate ⇒ Integer

Returns the stream's sample rate in samples per second.

Returns:

  • (Integer)

Returns:

  • (Integer)


287
288
289
# File 'ext/audio/sound_stream.c', line 287

static VALUE SoundStream_sample_rate(VALUE self) {
    return UINT2NUM(sfSoundStream_getSampleRate(Get_SoundStream_Struct(self)));
}

#spatialization_enabled=(value) ⇒ Boolean

Enables or disables 3D spatialization.

Returns:

  • (Boolean)

#spatialization_enabled? ⇒ Boolean

Returns true if 3D spatialization is enabled.

Returns:

  • (Boolean)

#status ⇒ Symbol

Returns the current playback status.

Returns:

  • (Symbol) —

    one of :stopped, :paused, :playing

#stop ⇒ self

Stops playback and rewinds to the beginning. May briefly block the calling thread if an audio-thread callback for this source is in flight.

Returns:

  • (self)

#velocity ⇒ Vector3

Returns the source's velocity, used for Doppler calculations.

Returns:

#velocity=(value) ⇒ Vector3

Sets the source's velocity for Doppler calculations.

Returns:

#volume ⇒ Float

Returns the source's volume.

Returns:

  • (Float) —

    0 to 100

#volume=(value) ⇒ Float

Sets the source's volume.

Returns:

  • (Float)