Class: SFML::SoundBufferRecorder

Inherits:
Object
  • Object
show all
Defined in:
lib/sfml/audio/sound_buffer_recorder.rb

Overview

Records audio from the system input (microphone) directly into a SoundBuffer. Quickest path for a "record audio" feature — start, speak, stop, save:

recorder = SFML::SoundBufferRecorder.new
recorder.start(sample_rate: 44100)
sleep 3
recorder.stop
recorder.buffer.save("memo.wav")

Recording requires a working input device; SFML::SoundRecorder.available? tells you whether one is present before you start.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ SoundBufferRecorder

Returns a new instance of SoundBufferRecorder.

Raises:



15
16
17
18
19
# File 'lib/sfml/audio/sound_buffer_recorder.rb', line 15

def initialize
  ptr = C::Audio.sfSoundBufferRecorder_create
  raise AudioError, "sfSoundBufferRecorder_create returned NULL" if ptr.null?
  @handle = FFI::AutoPointer.new(ptr, C::Audio.method(:sfSoundBufferRecorder_destroy))
end

Instance Attribute Details

#handle ⇒ Object (readonly)

:nodoc:



74
75
76
# File 'lib/sfml/audio/sound_buffer_recorder.rb', line 74

def handle
  @handle
end

Instance Method Details

#buffer ⇒ Object

The captured audio so far. Returned as a borrowed SoundBuffer owned by the recorder — copy it (via #save or by feeding to a Sound) before destroying the recorder, or build a Sound that outlives the recorder via the buffer's data.

Raises:



52
53
54
55
56
57
58
59
# File 'lib/sfml/audio/sound_buffer_recorder.rb', line 52

def buffer
  ptr = C::Audio.sfSoundBufferRecorder_getBuffer(@handle)
  raise AudioError, "sfSoundBufferRecorder_getBuffer returned NULL" if ptr.null?
  # Borrowed — recorder owns the underlying sf::SoundBuffer.
  buf = SoundBuffer.allocate
  buf.instance_variable_set(:@handle, ptr)
  buf
end

#channel_count ⇒ Object

Returns the channel count.



39
40
41
# File 'lib/sfml/audio/sound_buffer_recorder.rb', line 39

def channel_count
  C::Audio.sfSoundBufferRecorder_getChannelCount(@handle)
end

#channel_count=(value) ⇒ Object

Set the channel count.



44
45
46
# File 'lib/sfml/audio/sound_buffer_recorder.rb', line 44

def channel_count=(value)
  C::Audio.sfSoundBufferRecorder_setChannelCount(@handle, Integer(value))
end

#device ⇒ Object

Currently selected input device name (or nil).



62
63
64
65
# File 'lib/sfml/audio/sound_buffer_recorder.rb', line 62

def device
  name = C::Audio.sfSoundBufferRecorder_getDevice(@handle)
  name unless name.nil? || name.empty?
end

#device=(name) ⇒ Object

Set the device.

Raises:



68
69
70
71
72
# File 'lib/sfml/audio/sound_buffer_recorder.rb', line 68

def device=(name)
  ok = C::Audio.sfSoundBufferRecorder_setDevice(@handle, name.to_s)
  raise AudioError, "could not select recording device #{name.inspect}" unless ok
  name
end

#sample_rate ⇒ Object

Returns the sample rate.



34
35
36
# File 'lib/sfml/audio/sound_buffer_recorder.rb', line 34

def sample_rate
  C::Audio.sfSoundBufferRecorder_getSampleRate(@handle)
end

#start(sample_rate: 44_100) ⇒ Object

Begin capturing samples at the given sample rate. Returns true on success, false if the device couldn't be opened.



23
24
25
# File 'lib/sfml/audio/sound_buffer_recorder.rb', line 23

def start(sample_rate: 44_100)
  C::Audio.sfSoundBufferRecorder_start(@handle, Integer(sample_rate))
end

#stop ⇒ Object

Returns the stop.



28
29
30
31
# File 'lib/sfml/audio/sound_buffer_recorder.rb', line 28

def stop
  C::Audio.sfSoundBufferRecorder_stop(@handle)
  self
end