summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorHeitor Carvalho <[email protected]>2018-10-24 18:10:17 -0300
committerTom Black <[email protected]>2018-10-24 14:10:16 -0700
commit830d99fde4555263967dfee94b4095ee300c12e1 (patch)
tree8360ba4b8bc60a45fd50c484f4de39dee13246cd /test
parent1b646f82c67e7c38df08113bcc3323c7aaf4639f (diff)
downloadruby2d-830d99fde4555263967dfee94b4095ee300c12e1.tar.gz
ruby2d-830d99fde4555263967dfee94b4095ee300c12e1.zip
Implements volume control to Music (#123)
Diffstat (limited to 'test')
-rw-r--r--test/audio.rb25
-rw-r--r--test/music_spec.rb27
2 files changed, 45 insertions, 7 deletions
diff --git a/test/audio.rb b/test/audio.rb
index 7d39569..8a0e8f6 100644
--- a/test/audio.rb
+++ b/test/audio.rb
@@ -11,28 +11,39 @@ set width: 300, height: 200, title: "Ruby 2D — Audio"
snd = Sound.new("#{media}/sound.wav")
mus = Music.new("#{media}/music.wav")
+volume_bar = Rectangle.new(color: 'green', width: 300, height: 50)
+
+on :mouse_down do |event|
+ Music.volume = event.x / Window.width.to_f * 100
+ volume_bar.width = Music.volume / 100.0 * Window.width
+ puts "Music volume: #{Music.volume}%"
+end
+
on :key_down do |event|
case event.key
when 'p'
- puts "Playing sound..."
+ puts "Playing sound"
snd.play
when 'm'
- puts "Playing music..."
+ puts "Playing music"
mus.play
+ mus.volume = 100
+ volume_bar.width = Window.width
+ puts "Music volume: #{mus.volume}"
when 'l'
- puts "Loop music true..."
+ puts "Looping music"
mus.loop = true
when 'a'
- puts "Pause music..."
+ puts "Music paused"
mus.pause
when 'r'
- puts "Resume music..."
+ puts "Music resumed"
mus.resume
when 's'
- puts "Stop music..."
+ puts "Music stopped"
mus.stop
when 'f'
- puts "fade out music..."
+ puts "Music fading out"
mus.fadeout 2000
when 'escape'
close
diff --git a/test/music_spec.rb b/test/music_spec.rb
index c3a1173..09b6f1c 100644
--- a/test/music_spec.rb
+++ b/test/music_spec.rb
@@ -6,6 +6,33 @@ RSpec.describe Ruby2D::Music do
it "raises exception if audio file doesn't exist" do
expect { Music.new("bad_music.mp3") }.to raise_error(Ruby2D::Error)
end
+
+ it "creates new music" do
+ Music.new("test/media/music.mp3")
+ end
+ end
+
+ describe "#volume" do
+ it "sets the volume on music instances" do
+ mus = Music.new("test/media/music.mp3")
+ expect(mus.volume).to eq(100)
+ mus.volume = 68
+ expect(mus.volume).to eq(68)
+ end
+
+ it "sets the volume using class methods" do
+ Music.volume = 27
+ expect(Music.volume).to eq(27)
+ end
+
+ it "sets volume to 0 or 100 if outside of range" do
+ Music.volume = 234
+ expect(Music.volume).to eq(100)
+ Music.volume = -312
+ expect(Music.volume).to eq(0)
+ Music.volume = -1
+ expect(Music.volume).to eq(0)
+ end
end
end