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

#initializeSoundBufferRecorder

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 Error, "sfSoundBufferRecorder_create returned NULL" if ptr.null?
  @handle = FFI::AutoPointer.new(ptr, C::Audio.method(:sfSoundBufferRecorder_destroy))
end

Instance Attribute Details

#handleObject (readonly)

:nodoc:



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

def handle
  @handle
end

Instance Method Details

#bufferObject

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:



48
49
50
51
52
53
54
55
# File 'lib/sfml/audio/sound_buffer_recorder.rb', line 48

def buffer
  ptr = C::Audio.sfSoundBufferRecorder_getBuffer(@handle)
  raise Error, "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_countObject



36
37
38
# File 'lib/sfml/audio/sound_buffer_recorder.rb', line 36

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

#channel_count=(value) ⇒ Object



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

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

#deviceObject

Currently selected input device name (or nil).



58
59
60
61
# File 'lib/sfml/audio/sound_buffer_recorder.rb', line 58

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

#device=(name) ⇒ Object

Raises:



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

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

#sample_rateObject



32
33
34
# File 'lib/sfml/audio/sound_buffer_recorder.rb', line 32

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

#stopObject



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

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