Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/dxruby_sdl/sound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ def initialize(filename)
end
end

def_delegators :@sound, :play, :set_volume, :stop
def_delegators :@sound, :play, :loop_count=, :set_volume, :stop

alias_method :loopCount=, :loop_count=
alias_method :setVolume, :set_volume

private
Expand All @@ -43,10 +44,15 @@ class Music

def initialize(filename)
@music = SDL::Mixer::Music.load(filename)
@_loop_count = -1
end

def play
SDL::Mixer.play_music(@music, -1)
SDL::Mixer.play_music(@music, @_loop_count)
end

def loop_count=(n)
@_loop_count = n
end

def set_volume(volume, time = 0)
Expand All @@ -69,17 +75,22 @@ class Wave
def initialize(filename)
@wave = SDL::Mixer::Wave.load(filename)
@last_played_channel = nil
@_loop_count = 0
end

def play
@last_played_channel = SDL::Mixer.play_channel(-1, @wave, 0)
@last_played_channel = SDL::Mixer.play_channel(-1, @wave, @_loop_count)
rescue SDL::Error => e
if /No free channels available/ =~ e.message
SDL::Mixer.halt(@last_played_channel == 0 ? 1 : 0)
retry
end
end

def loop_count=(n)
@_loop_count = n
end

def set_volume(volume, time = 0)
if time > 0
raise NotImplementedError, 'Sound#set_volume(volume, time != 0)'
Expand Down
22 changes: 22 additions & 0 deletions spec/lib/dxruby_sdl/sound_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@
end
end

describe '#loop_count=' do
context 'WAVE file', wave: true do
it 'SDL::Waveのループ回数を変更する' do
wave =
sound.instance_variable_get('@sound').instance_variable_get('@wave')
expect(SDL::Mixer).to receive(:play_channel).with(-1, wave, 1)
sound.loop_count = 1
sound.play
end
end

context 'MIDI file', midi: true do
it 'SDL::Musicのループ回数を変更する' do
music =
sound.instance_variable_get('@sound').instance_variable_get('@music')
expect(SDL::Mixer).to receive(:play_music).with(music, 1)
sound.loop_count = 1
sound.play
end
end
end

describe '#stop' do
it '再生していないサウンドを停止でエラーが発生しない' do
sound = DXRubySDL::Sound.new(fixture_path('sound.wav'))
Expand Down