# File lib/rubygame/sound.rb, line 168
  def play( options={} )

    fade_in    = (options[:fade_in]  or 0)
    repeats    = (options[:repeats]  or 0)
    stop_after = (options[:stop_after] or nil)


    fade_in =
      if( fade_in < 0 )
        raise ArgumentError, ":fade_in cannot be negative (got %.2f)"%fade_in
      elsif( fade_in < 0.05 )
        # Work-around for a bug with SDL_mixer not working with small
        # non-zero fade-ins
        0
      else
        (fade_in * 1000).to_i
      end


    repeats =
      if( repeats < -1 )
        raise( ArgumentError,
               ":repeats cannot be negative, except -1 (got #{repeats})" )
      else
        repeats
      end


    stop_after =
      if( stop_after.nil? )
        -1
      elsif( stop_after < 0 )
        raise( ArgumentError,
               ":stop_after cannot be negative, (got %.2f)"%stop_after )
      else
        (stop_after * 1000).to_i
      end



    Rubygame.open_audio


    # If it's already playing on a channel, stop it first.
    if channel_active?
      SDL::Mixer.HaltChannel( @channel )
    end


    # Find first available channel
    @channel = SDL::Mixer.GroupAvailable(-1)


    if @channel == -1
      # No channels were available, so make one more than there are now.
      # (Mix_AllocateChannels(-1) returns the current number of channels)
      SDL::Mixer.AllocateChannels( SDL::Mixer.AllocateChannels(-1) + 1 )

      # Try again
      @channel = SDL::Mixer.GroupAvailable(-1)
    end


    # Set sound channel volume before we play
    SDL::Mixer.Volume( @channel, (SDL::Mixer::MAX_VOLUME * @volume).to_i )


    result =
      if( fade_in <= 0 )
        # Play sound without fading in
        SDL::Mixer.PlayChannelTimed( @channel, @struct, repeats, stop_after )
      else
        # Play sound with fading in
        SDL::Mixer.FadeInChannelTimed( @channel, @struct,
                                       repeats, fade_in, stop_after )
      end


    if( result == -1 )
      raise Rubygame::SDLError, "Could not play Sound: #{SDL.GetError()}"
    end


    return self

  end