Sha256: 83a5b7e2eea05029de8b622dad6159a8088f3ce7c24e498918c020da374cfd5d

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

# Ruby2D::Sound

module Ruby2D
  # Sounds are intended to be short samples, played without interruption, like an effect.
  class Sound
    attr_reader :path
    attr_accessor :loop, :data

    #
    # Load a sound from a file
    # @param [String] path File to load the sound from
    # @param [true, false] loop If +true+ playback will loop automatically, default is +false+
    # @raise [Error] if file cannot be found or music could not be successfully loaded.
    def initialize(path, loop: false)
      raise Error, "Cannot find audio file `#{path}`" unless File.exist? path

      @path = path
      @loop = loop
      raise Error, "Sound `#{@path}` cannot be created" unless ext_init(@path)
    end

    # Play the sound
    def play
      ext_play
    end

    # Stop the sound
    def stop
      ext_stop
    end

    # Returns the length in seconds
    def length
      ext_length
    end

    # Get the volume of the sound
    def volume
      ext_get_volume
    end

    # Set the volume of the sound
    def volume=(volume)
      # Clamp value to between 0-100
      ext_set_volume(volume.clamp(0, 100))
    end

    # Get the volume of the sound mixer
    def self.mix_volume
      ext_get_mix_volume
    end

    # Set the volume of the sound mixer
    def self.mix_volume=(volume)
      # Clamp value to between 0-100
      ext_set_mix_volume(volume.clamp(0, 100))
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby2d-0.12.1 lib/ruby2d/sound.rb
ruby2d-0.12.0 lib/ruby2d/sound.rb