All pastes #166528 Raw Edit

Stuff

public ruby v1 · immutable
·published 2006-09-10 23:57 UTC
rendered paste body
# Noise.rb# Makes noise come out of the speakers#  there is support for a multitude of sources (file, tcp, insertChoiseHere)require 'gst'Gst.initclass Noise  @pipeline = nil  @source =  nil  @spider = nil  @output = nil  @playing = false  @paused = false  attr_reader playing, paused  def initialize    @pipeline = Gst::Thread.new    @spider = Gst::ElementFactory.make("spider")    @output = Gst::ElementFactory.make("osssink")    signal_connect("eos"){stop}  end  def pause= condition    if condition && @playing      @pipeline.pause      @paused = true    else if !condition && @playing      @pipeline.play      @paused = false    end  end  def pause    pause= true  end  def paused?    @paused  end  def play source=nil    if source==nil && paused?      pause=false    else if source.downcase =~ /file:\/\/.*/      play_file source.gsub(/file:\/\//,"")    else if source.downcase =~ /tcp:\/\/.*/      play_stream_tcp source.gsub(/tcp:\/\//,"")    end  end  def play_file source    stop    @source = Gst::ElementFactory.make("filesrc")    @source.location = source    @pipeline.play  end  def play_stream_tcp source    #TODO implement playing from source    stop    puts "Warning:: playing from tcp is not supported yet!"  end  def stop    # stop the pipeline and clean it up    @pipeline.stop    @paused=false    @playing=false  end  # allow actions to be taken on special events  def signal_connect( signal, &handler )    case signal    when signal == "eos"  #end of stream      @pipeline.signal_connect("eos") {handler.call}    else      puts "Warning:: signal not recognised"    end  end  def seek_to    # seek to a given time in the stream    #  this feature is for a later version, it might be included only for local streams (that all depends :-)    #TODO implement seaking feature    puts "Warning:: seek to a time is not implemented yet!"  end  private  # sets up the pipeline  def pipeline_setup    @pipeline.clear    @source >> @spider >> @output    @pipeline.add(@source, @spider, @output)  endend