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
10 changes: 9 additions & 1 deletion lib/dxruby_sdl/sound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def initialize(filename)
end
end

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

alias_method :setVolume, :set_volume

Expand All @@ -39,6 +39,10 @@ def play
def set_volume(volume, time = 0)
raise NotImplementedError, 'Sound#set_volume(volume, time) with MIDI'
end

def stop
SDL::Mixer.halt_music
end
end
private_constant :Music

Expand All @@ -65,6 +69,10 @@ def set_volume(volume, time = 0)
end
@wave.set_volume(volume)
end

def stop
SDL::Mixer.halt(@last_played_channel)
end
end
private_constant :Wave
end
Expand Down
56 changes: 48 additions & 8 deletions spec/lib/dxruby_sdl/sound_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
require 'spec_helper'

describe DXRubySDL::Sound, '音を表すクラス' do
shared_context 'WAVE file', wave: true do
let(:path) { fixture_path('sound.wav') }
let(:sound) { DXRubySDL::Sound.new(path) }
end

shared_context 'MIDI file', midi: true do
let(:path) { fixture_path('bgm.mid') }
let(:sound) { DXRubySDL::Sound.new(path) }
end

describe '.new' do
shared_context '.new' do
subject { DXRubySDL::Sound.new(fixture_path(filename)) }
Expand All @@ -25,10 +35,7 @@
end

describe '#play' do
context 'WAVE形式のファイルの場合' do
let(:path) { fixture_path('sound.wav') }
let(:sound) { DXRubySDL::Sound.new(path) }

context 'WAVE file', wave: true do
subject { sound.play }

it 'SDL::Mixer.play_channelを呼び出す' do
Expand Down Expand Up @@ -62,10 +69,7 @@
end
end

context 'MIDI形式のファイルの場合' do
let(:path) { fixture_path('bgm.mid') }
let(:sound) { DXRubySDL::Sound.new(path) }

context 'MIDI file', midi: true do
subject { sound.play }

it 'SDL::Mixer.play_musicを呼び出す' do
Expand All @@ -76,4 +80,40 @@
end
end
end

describe '#stop' do
context 'WAVE file', wave: true do
let(:path) { fixture_path('sound.wav') }
let(:sound) { DXRubySDL::Sound.new(path) }

subject { sound.stop }

before do
allow(SDL::Mixer).to receive(:halt)
sound.play
subject
end

describe SDL::Mixer do
it { expect(SDL::Mixer).to have_received(:halt).with(0).once }
end
end

context 'MIDI file', midi: true do
let(:path) { fixture_path('bgm.mid') }
let(:sound) { DXRubySDL::Sound.new(path) }

subject { sound.stop }

before do
allow(SDL::Mixer).to receive(:halt_music)
sound.play
subject
end

describe SDL::Mixer do
it { expect(SDL::Mixer).to have_received(:halt_music).with(no_args).once }
end
end
end
end