sfml3.rb

Class: SFML::SoundRecorder

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

Overview

Base class for a custom audio capture consumer. Subclasses must implement #on_process(samples), called from the audio thread whenever a chunk of captured samples is ready, and may implement +#on_start+/+#on_stop+ to react to recording starting/stopping. For simply capturing into a SoundBuffer without custom processing, use SoundBufferRecorder instead.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new ⇒ SoundRecorder

SoundRecorder must be subclassed: the subclass is required to implement #on_process(samples), called from the audio thread with an Array of captured Integer samples whenever a chunk is ready (return a truthy value to keep recording, falsy to stop), and may optionally implement #on_start and #on_stop.

Raises:

  • (NotImplementedError) —

    if the subclass does not define #on_process

  • (RuntimeError) —

    if no capture device is available



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'ext/audio/sound_recorder.c', line 187

static VALUE SoundRecorder_initialize(VALUE self) {
    SoundRecorder* ptr;
    sfSoundRecorder* handle;

    TypedData_Get_Struct(self, SoundRecorder, &SoundRecorder_data_type, ptr);

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

    handle = sfSoundRecorder_create(SoundRecorder_on_start, SoundRecorder_on_process,
                                    SoundRecorder_on_stop, ptr);

    if (handle == NULL) {
        rb_raise(rb_eRuntimeError, "failed to create sound recorder (no capture device?)");
    }

    ptr->handle = handle;

    return self;
}

Class Method Details

.available? ⇒ Boolean

Returns true if the audio backend supports capture.

Returns:

  • (Boolean)

Returns:

  • (Boolean) —

    whether the audio backend supports capture at all



248
249
250
# File 'ext/audio/sound_recorder.c', line 248

static VALUE SoundRecorder_available(VALUE klass) {
    return BOOL2RB(sfSoundRecorder_isAvailable());
}

.available_devices ⇒ Array<String>

Returns the names of the capture devices available on this system.

Returns:

  • (Array<String>)

Returns:

  • (Array<String>) —

    names of the capture devices available on this system



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'ext/audio/sound_recorder.c', line 259

static VALUE SoundRecorder_available_devices(VALUE klass) {
    size_t count = 0;
    const char* const* devices = sfSoundRecorder_getAvailableDevices(&count);
    VALUE rb_array;
    size_t i;

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

    rb_array = rb_ary_new_capa((long)count);

    for (i = 0; i < count; i++) {
        rb_ary_push(rb_array, rb_str_new_cstr(devices[i]));
    }

    return rb_array;
}

.default_device ⇒ String?

Returns the name of the system's default capture device.

Returns:

  • (String, nil)

Returns:

  • (String, nil) —

    the name of the system's default capture device, or nil when the system reports none



285
286
287
# File 'ext/audio/sound_recorder.c', line 285

static VALUE SoundRecorder_default_device(VALUE klass) {
    return SoundRecorder_nullable_string(sfSoundRecorder_getDefaultDevice());
}

Instance Method Details

#channel_count ⇒ Integer

Returns the number of capture channels.

Returns:

  • (Integer)

Returns:

  • (Integer)


319
320
321
# File 'ext/audio/sound_recorder.c', line 319

static VALUE SoundRecorder_channel_count(VALUE self) {
    return UINT2NUM(sfSoundRecorder_getChannelCount(Get_SoundRecorder_Struct(self)));
}

#channel_count=(value) ⇒ Integer

Must be called while not recording.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    value



330
331
332
333
334
# File 'ext/audio/sound_recorder.c', line 330

static VALUE SoundRecorder_set_channel_count(VALUE self, VALUE rb_count) {
    sfSoundRecorder_setChannelCount(Get_SoundRecorder_Struct(self),
                                    (unsigned int)NUM2INT(rb_count));
    return rb_count;
}

#channel_map ⇒ Array<Symbol>

Returns the channel layout of the recorder.

Returns:

  • (Array<Symbol>)

Returns:

  • (Array<Symbol>) —

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



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'ext/audio/sound_recorder.c', line 343

static VALUE SoundRecorder_channel_map(VALUE self) {
    size_t count = 0;
    sfSoundChannel* map = sfSoundRecorder_getChannelMap(Get_SoundRecorder_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;
}

#device ⇒ String?

Returns the name of the capture device in use.

Returns:

  • (String, nil)

Returns:

  • (String, nil) —

    the name of the capture device in use, or nil when the recorder has none



296
297
298
# File 'ext/audio/sound_recorder.c', line 296

static VALUE SoundRecorder_device(VALUE self) {
    return SoundRecorder_nullable_string(sfSoundRecorder_getDevice(Get_SoundRecorder_Struct(self)));
}

#device=(value) ⇒ Boolean

Must be called while not recording. Get available names from .available_devices.

Returns:

  • (Boolean)

Returns:

  • (Boolean) —

    whether the device was set successfully



308
309
310
311
# File 'ext/audio/sound_recorder.c', line 308

static VALUE SoundRecorder_set_device(VALUE self, VALUE rb_name) {
    return BOOL2RB(
        sfSoundRecorder_setDevice(Get_SoundRecorder_Struct(self), StringValueCStr(rb_name)));
}

#sample_rate ⇒ Integer

Returns the sample rate used for capture.

Returns:

  • (Integer)

Returns:

  • (Integer)


238
239
240
# File 'ext/audio/sound_recorder.c', line 238

static VALUE SoundRecorder_sample_rate(VALUE self) {
    return UINT2NUM(sfSoundRecorder_getSampleRate(Get_SoundRecorder_Struct(self)));
}

#start(sample_rate) ⇒ Boolean

Starts capturing audio at the given sample rate.

Returns:

  • (Boolean)

Returns:

  • (Boolean) —

    whether recording started successfully



216
217
218
219
# File 'ext/audio/sound_recorder.c', line 216

static VALUE SoundRecorder_start(VALUE self, VALUE rb_sample_rate) {
    return BOOL2RB(sfSoundRecorder_start(Get_SoundRecorder_Struct(self),
                                         (unsigned int)NUM2INT(rb_sample_rate)));
}

#stop ⇒ self

Stops capturing audio.

Returns:

  • (self)

Returns:

  • (self)


227
228
229
230
# File 'ext/audio/sound_recorder.c', line 227

static VALUE SoundRecorder_stop(VALUE self) {
    sfSoundRecorder_stop(Get_SoundRecorder_Struct(self));
    return self;
}