class SFML::SoundRecorder

Callback-based audio-capture base class. Subclass and override on_start (initialise capture state, return true to begin), on_process_samples(samples, channels) (called every audio chunk with an Array<Integer> of interleaved int16 PCM, return true to keep recording or false to stop), and on_stop (release any resources).

The simpler β€œrecord to a SoundBuffer” path lives in SFML::SoundBufferRecorder β€” reach for SoundRecorder only when you need to stream samples somewhere else (file, socket, DSP pipeline).

class LevelMeter < SFML::SoundRecorder def on_start @peak = 0 true end

def on_process_samples(samples, _channels)
  @peak = [@peak, *samples.map(&:abs)].max
  true
end

def on_stop
  puts "Peak: #{@peak}"
end

end

meter = LevelMeter.new meter.start(sample_rate: 44_100) sleep 2 meter.stop

CAVEATS * All three callbacks run on CSFML’s audio thread; heavy Ruby work on the audio thread will glitch the capture. * Always keep a reference to the SoundRecorder object β€” if the Ruby object is GC’d while CSFML is mid-capture, the process crashes.