summaryrefslogtreecommitdiffhomepage
path: root/test/audio.rb
blob: 64e580ce1b4ad091962de89c470c1ab53c39bd6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require 'ruby2d'

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"
    snd.play
  when 'm'
    puts "Playing music"
    mus.play
    mus.volume = 100
    volume_bar.width = Window.width
    puts "Music volume: #{mus.volume}"
  when 'l'
    puts "Looping music"
    mus.loop = true
  when 'a'
    puts "Music paused"
    mus.pause
  when 'r'
    puts "Music resumed"
    mus.resume
  when 's'
    puts "Music stopped"
    mus.stop
  when 'f'
    puts "Music fading out"
    mus.fadeout 2000
  when 'escape'
    close
  end
end

show