Class: Rubyboy::Audio

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

Constant Summary collapse

SAMPLE_RATE =
48000
SAMPLES =
512

Instance Method Summary collapse

Constructor Details

#initializeAudio

Returns a new instance of Audio.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubyboy/audio.rb', line 10

def initialize
  SDL.InitSubSystem(SDL::INIT_AUDIO)

  desired = SDL::AudioSpec.new
  desired[:freq] = SAMPLE_RATE
  desired[:format] = SDL::AUDIO_F32SYS
  desired[:channels] = 2
  desired[:samples] = SAMPLES * 2

  @device = SDL.OpenAudioDevice(nil, 0, desired, nil, 0)

  SDL.PauseAudioDevice(@device, 0)
end

Instance Method Details

#queue(buffer) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/rubyboy/audio.rb', line 24

def queue(buffer)
  # sleep(0.001) while SDL.GetQueuedAudioSize(@device) > 8192

  SDL.ClearQueuedAudio(@device) if SDL.GetQueuedAudioSize(@device) > 8192

  buf_ptr = FFI::MemoryPointer.new(:float, buffer.size)
  buf_ptr.put_array_of_float(0, buffer)

  SDL.QueueAudio(@device, buf_ptr, buffer.size * buf_ptr.type_size)
end