Class: RbSDL2::Audio

Inherits:
Object
  • Object
show all
Includes:
AudioSpecReader
Defined in:
lib/rb_sdl2/audio/audio.rb,
lib/rb_sdl2/audio/audio_spec.rb,
lib/rb_sdl2/audio/audio_buffer.rb,
lib/rb_sdl2/audio/audio_device.rb,
lib/rb_sdl2/audio/audio_spec_reader.rb

Defined Under Namespace

Modules: AudioSpecReader Classes: AudioBuffer, AudioDevice, AudioSpec, Releaser

Constant Summary collapse

SDL_AUDIO_ALLOW_FREQUENCY_CHANGE =
0x00000001
SDL_AUDIO_ALLOW_FORMAT_CHANGE =
0x00000002
SDL_AUDIO_ALLOW_CHANNELS_CHANGE =
0x00000004
SDL_AUDIO_ALLOW_SAMPLES_CHANGE =
0x00000008
SDL_AUDIO_ALLOW_ANY_CHANGE =
SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_FORMAT_CHANGE |
SDL_AUDIO_ALLOW_CHANNELS_CHANGE | SDL_AUDIO_ALLOW_SAMPLES_CHANGE
SDL_AUDIO_STOPPED =
0
SDL_AUDIO_PLAYING =
1
SDL_AUDIO_PAUSED =
2
IS_CAPTURE =
-> (capture) { capture ? 1 : 0 }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device = nil, capture = false, allow_any_change: false, allow_channels_change: false, allow_frequency_change: false, allow_format_change: false, allow_samples_change: false, autoclose: true, spec: nil, **opts) ⇒ Audio

Returns a new instance of Audio.

Raises:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rb_sdl2/audio/audio.rb', line 66

def initialize(device = nil, capture = false, allow_any_change: false,
               allow_channels_change: false, allow_frequency_change: false,
               allow_format_change: false, allow_samples_change: false,
               autoclose: true, spec: nil, **opts)
  @capture = capture ? true : false
  @spec = AudioSpec.new
  allowed_changes = if allow_any_change
                      SDL_AUDIO_ALLOW_ANY_CHANGE
                    else
                      0 |
                        (allow_channels_change ? SDL_AUDIO_ALLOW_CHANNELS_CHANGE : 0) |
                        (allow_frequency_change ? SDL_AUDIO_ALLOW_FREQUENCY_CHANGE : 0) |
                        (allow_format_change ? SDL_AUDIO_ALLOW_FORMAT_CHANGE : 0) |
                        (allow_samples_change ? SDL_AUDIO_ALLOW_SAMPLES_CHANGE : 0)
                    end
  @id = ::SDL.OpenAudioDevice(device ? SDL.str_to_sdl(device) : nil, IS_CAPTURE.(capture),
                              spec || AudioSpec.new(**opts), @spec, allowed_changes)
  raise RbSDL2Error if @id == 0
  self.autoclose = autoclose
end

Instance Attribute Details

#specObject (readonly)

Returns the value of attribute spec.



150
151
152
# File 'lib/rb_sdl2/audio/audio.rb', line 150

def spec
  @spec
end

Class Method Details

.devicesObject



19
# File 'lib/rb_sdl2/audio/audio.rb', line 19

def devices = AudioDevice.devices

.driverObject

Raises:



21
22
23
24
25
# File 'lib/rb_sdl2/audio/audio.rb', line 21

def driver
  ptr = ::SDL.GetCurrentAudioDriver
  raise RbSDL2Error, "Audio subsystem has not been initialized" if ptr.null?
  ptr.read_string
end

.driversObject



27
28
29
30
31
32
33
# File 'lib/rb_sdl2/audio/audio.rb', line 27

def drivers
  ::SDL.GetNumAudioDrivers.times.map do |num|
    ptr = ::SDL.GetAudioDriver(num)
    raise RbSDL2Error if ptr.null?
    ptr.read_string
  end
end

.init(driver) ⇒ Object

Raises:



35
36
37
# File 'lib/rb_sdl2/audio/audio.rb', line 35

def init(driver)
  raise RbSDL2Error if ::SDL.AudioInit(driver) < 0
end

.loadObject



41
# File 'lib/rb_sdl2/audio/audio.rb', line 41

def load(...) = AudioBuffer.load(...)

.openObject



43
44
45
46
47
48
49
50
51
# File 'lib/rb_sdl2/audio/audio.rb', line 43

def open(...)
  obj = new(...)
  return obj unless block_given?
  begin
    yield(obj)
  ensure
    obj.close
  end
end

.play(path) ⇒ Object



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

def play(path)
  buffer = load(path)
  obj = Audio.new(spec: buffer.spec)
  obj.write(buffer)
  obj.play
  obj
end

.quitObject



61
# File 'lib/rb_sdl2/audio/audio.rb', line 61

def quit = ::SDL.AudioQuit

Instance Method Details

#autoclose=(bool) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/rb_sdl2/audio/audio.rb', line 93

def autoclose=(bool)
  return if closed?
  if bool
    ObjectSpace.define_finalizer(self, Releaser.new(@id))
  else
    ObjectSpace.undefine_finalizer(self)
  end
  @autoclose = bool ? true : false
end

#autoclose?Boolean

Returns:

  • (Boolean)


103
# File 'lib/rb_sdl2/audio/audio.rb', line 103

def autoclose? = @autoclose

#capture?Boolean

Returns:

  • (Boolean)


105
# File 'lib/rb_sdl2/audio/audio.rb', line 105

def capture? = @capture

#clearObject



107
# File 'lib/rb_sdl2/audio/audio.rb', line 107

def clear = closed? ? nil : ::SDL.ClearQueuedAudio(self)

#closeObject



109
110
111
112
113
114
115
# File 'lib/rb_sdl2/audio/audio.rb', line 109

def close
  unless closed?
    self.autoclose = false
    @id = ::SDL.CloseAudioDevice(@id)
  end
  nil
end

#closed?Boolean

Returns:

  • (Boolean)


117
# File 'lib/rb_sdl2/audio/audio.rb', line 117

def closed? = SDL_AUDIO_STOPPED == ::SDL.GetAudioDeviceStatus(@id) || !@id

#idObject Also known as: audio_id

Raises:

  • (IOError)


119
120
121
122
# File 'lib/rb_sdl2/audio/audio.rb', line 119

def id
  raise IOError if closed?
  @id
end

#inspectObject



125
126
127
# File 'lib/rb_sdl2/audio/audio.rb', line 125

def inspect
  "#<#{self.class.name}:#{audio_id}#{closed? ? " (closed)" : nil}>"
end

#pauseObject



129
# File 'lib/rb_sdl2/audio/audio.rb', line 129

def pause = closed? ? nil : ::SDL.PauseAudioDevice(id, 1)

#paused?Boolean

Returns:

  • (Boolean)


131
# File 'lib/rb_sdl2/audio/audio.rb', line 131

def paused? = closed? ? false : SDL_AUDIO_PAUSED == ::SDL.GetAudioDeviceStatus(id)

#playObject



133
# File 'lib/rb_sdl2/audio/audio.rb', line 133

def play = closed? ? nil : ::SDL.PauseAudioDevice(id, 0)

#playing?Boolean

Returns:

  • (Boolean)


135
# File 'lib/rb_sdl2/audio/audio.rb', line 135

def playing? = closed? ? false : SDL_AUDIO_PLAYING == ::SDL.GetAudioDeviceStatus(id)

#read(len) ⇒ Object

Raises:

  • (IOError)


137
138
139
140
141
142
# File 'lib/rb_sdl2/audio/audio.rb', line 137

def read(len)
  raise IOError if closed?
  ptr = ::FFI::MemoryPointer.new(len)
  size = ::SDL.DequeueAudio(id, ptr, len)
  ptr.read_bytes(size)
end

#sizeObject Also known as: length

Raises:

  • (IOError)


144
145
146
147
# File 'lib/rb_sdl2/audio/audio.rb', line 144

def size
  raise IOError if closed?
  ::SDL.GetQueuedAudioSize(id)
end

#write(data) ⇒ Object

Raises:

  • (IOError)


155
156
157
158
159
160
# File 'lib/rb_sdl2/audio/audio.rb', line 155

def write(data)
  raise IOError if closed?
  err = ::SDL.QueueAudio(id, data, data.size)
  raise RbSDL2Error if err < 0
  data.size
end