PageRenderTime 23ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/chingu/gosu_ext/sample.rb

http://github.com/ippa/chingu
Ruby | 79 lines | 57 code | 15 blank | 7 comment | 2 complexity | e52f2b10d6188faf1d98131ae214402c MD5 | raw file
Possible License(s): LGPL-2.1
 1module Gosu
 2  class Sample
 3    DEFAULT_VOLUME = 1.0 # Default volume of new samples.
 4
 5    class << self
 6      # Volume of all Samples.
 7      attr_reader :volume
 8
 9      public
10      # Volume of Samples, affected by Sample.volume and Window#volume and muting.
11      def effective_volume
12        @volume * $window.effective_volume
13      end
14
15      public
16      # Set the global volume of Samples.
17      def volume=(value)
18        raise "Bad volume setting" unless value.is_a? Numeric
19
20        @volume = [[value, 1.0].min, 0.0].max.to_f
21      end
22
23      public
24      def init_sound
25        @volume = DEFAULT_VOLUME
26        nil
27      end
28    end
29
30    init_sound
31
32    # Volume of this Sample. This is multiplied by the volume in #play.
33    attr_reader :volume
34
35    alias_method :old_initialize, :initialize
36    protected :old_initialize
37    public
38    # Accepts :volume (0.0..1.0) option, defaulting to 1.0.
39    def initialize(filename, options = {})
40      options = {
41          volume: DEFAULT_VOLUME,
42      }.merge! options
43
44      @volume = options[:volume]
45
46      old_initialize(filename)
47    end
48
49    public
50    # Set the volume of this Sample. This is multiplied by the volume in #play.
51    def volume=(value)
52      raise "Bad volume setting" unless value.is_a? Numeric
53
54      @volume = [[value, 1.0].min, 0.0].max.to_f
55    end
56
57    public
58    # Volume the Sample will actually be played at, affected by Sample.volume and Window#volume.
59    def effective_volume
60      @volume * self.class.effective_volume
61    end
62
63    alias_method :old_play, :play
64    protected :old_play
65    public
66    def play(volume = 1, speed = 1, looping = false)
67      volume *= effective_volume
68      old_play(volume, speed, looping) if volume > 0.0
69    end
70	
71    alias_method :old_play_pan, :play_pan
72    protected :old_play_pan
73    public
74    def play_pan(pan = 0, volume = 1, speed = 1, looping = false)
75      volume *= effective_volume
76      old_play_pan(pan, volume, speed, looping) if volume > 0.0
77    end
78  end
79end