From 98bb4ec99c44d6a9b3c57a538ddfff0ff4d3fcbc Mon Sep 17 00:00:00 2001 From: Akihiro Sada Date: Sun, 10 Dec 2017 10:26:53 +0900 Subject: [PATCH 1/3] Add Sound#loop_count=. --- lib/dxruby_sdl/sound.rb | 17 ++++++++++++++--- spec/lib/dxruby_sdl/sound_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/dxruby_sdl/sound.rb b/lib/dxruby_sdl/sound.rb index da30ec2..d793db1 100644 --- a/lib/dxruby_sdl/sound.rb +++ b/lib/dxruby_sdl/sound.rb @@ -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 @@ -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) @@ -69,10 +75,11 @@ 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) @@ -80,6 +87,10 @@ def play 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)' diff --git a/spec/lib/dxruby_sdl/sound_spec.rb b/spec/lib/dxruby_sdl/sound_spec.rb index 152e3e4..19cc3ff 100644 --- a/spec/lib/dxruby_sdl/sound_spec.rb +++ b/spec/lib/dxruby_sdl/sound_spec.rb @@ -81,6 +81,28 @@ end end + describe '#loop_count=' do + context 'WAVE file', wave: true do + it 'SDL::Waveのループ回数を変更する' do + expect(sound.instance_variable_get('@sound') + .instance_variable_get('@loop_count')).to eq(0) + sound.loop_count = 1 + expect(sound.instance_variable_get('@sound') + .instance_variable_get('@loop_count')).to eq(1) + end + end + + context 'MIDI file', midi: true do + it 'SDL::Musicのループ回数を変更する' do + expect(sound.instance_variable_get('@sound') + .instance_variable_get('@loop_count')).to eq(-1) + sound.loop_count = 1 + expect(sound.instance_variable_get('@sound') + .instance_variable_get('@loop_count')).to eq(1) + end + end + end + describe '#stop' do it '再生していないサウンドを停止でエラーが発生しない' do sound = DXRubySDL::Sound.new(fixture_path('sound.wav')) From 91ea522c83edf17b9ad0270783872e9d13eb7182 Mon Sep 17 00:00:00 2001 From: Akihiro Sada Date: Mon, 11 Dec 2017 11:26:47 +0900 Subject: [PATCH 2/3] Rename @loop_count to @_loop_count. --- lib/dxruby_sdl/sound.rb | 12 ++++++------ spec/lib/dxruby_sdl/sound_spec.rb | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/dxruby_sdl/sound.rb b/lib/dxruby_sdl/sound.rb index d793db1..48cfb03 100644 --- a/lib/dxruby_sdl/sound.rb +++ b/lib/dxruby_sdl/sound.rb @@ -44,15 +44,15 @@ class Music def initialize(filename) @music = SDL::Mixer::Music.load(filename) - @loop_count = -1 + @_loop_count = -1 end def play - SDL::Mixer.play_music(@music, @loop_count) + SDL::Mixer.play_music(@music, @_loop_count) end def loop_count=(n) - @loop_count = n + @_loop_count = n end def set_volume(volume, time = 0) @@ -75,11 +75,11 @@ class Wave def initialize(filename) @wave = SDL::Mixer::Wave.load(filename) @last_played_channel = nil - @loop_count = 0 + @_loop_count = 0 end def play - @last_played_channel = SDL::Mixer.play_channel(-1, @wave, @loop_count) + @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) @@ -88,7 +88,7 @@ def play end def loop_count=(n) - @loop_count = n + @_loop_count = n end def set_volume(volume, time = 0) diff --git a/spec/lib/dxruby_sdl/sound_spec.rb b/spec/lib/dxruby_sdl/sound_spec.rb index 19cc3ff..b50b3ad 100644 --- a/spec/lib/dxruby_sdl/sound_spec.rb +++ b/spec/lib/dxruby_sdl/sound_spec.rb @@ -85,20 +85,20 @@ context 'WAVE file', wave: true do it 'SDL::Waveのループ回数を変更する' do expect(sound.instance_variable_get('@sound') - .instance_variable_get('@loop_count')).to eq(0) + .instance_variable_get('@_loop_count')).to eq(0) sound.loop_count = 1 expect(sound.instance_variable_get('@sound') - .instance_variable_get('@loop_count')).to eq(1) + .instance_variable_get('@_loop_count')).to eq(1) end end context 'MIDI file', midi: true do it 'SDL::Musicのループ回数を変更する' do expect(sound.instance_variable_get('@sound') - .instance_variable_get('@loop_count')).to eq(-1) + .instance_variable_get('@_loop_count')).to eq(-1) sound.loop_count = 1 expect(sound.instance_variable_get('@sound') - .instance_variable_get('@loop_count')).to eq(1) + .instance_variable_get('@_loop_count')).to eq(1) end end end From 01b510bc8e828788084193f724587943ef3f2f24 Mon Sep 17 00:00:00 2001 From: Akihiro Sada Date: Mon, 11 Dec 2017 21:14:17 +0900 Subject: [PATCH 3/3] Fix Sound#loop_count= spec. --- spec/lib/dxruby_sdl/sound_spec.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/lib/dxruby_sdl/sound_spec.rb b/spec/lib/dxruby_sdl/sound_spec.rb index b50b3ad..e89cb5f 100644 --- a/spec/lib/dxruby_sdl/sound_spec.rb +++ b/spec/lib/dxruby_sdl/sound_spec.rb @@ -84,21 +84,21 @@ describe '#loop_count=' do context 'WAVE file', wave: true do it 'SDL::Waveのループ回数を変更する' do - expect(sound.instance_variable_get('@sound') - .instance_variable_get('@_loop_count')).to eq(0) + 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 - expect(sound.instance_variable_get('@sound') - .instance_variable_get('@_loop_count')).to eq(1) + sound.play end end context 'MIDI file', midi: true do it 'SDL::Musicのループ回数を変更する' do - expect(sound.instance_variable_get('@sound') - .instance_variable_get('@_loop_count')).to eq(-1) + music = + sound.instance_variable_get('@sound').instance_variable_get('@music') + expect(SDL::Mixer).to receive(:play_music).with(music, 1) sound.loop_count = 1 - expect(sound.instance_variable_get('@sound') - .instance_variable_get('@_loop_count')).to eq(1) + sound.play end end end