All pastes #155300 Raw Edit

Someone

public text v1 · immutable
#155300 ·published 2006-08-30 23:31 UTC
rendered paste body
##### PLAYER.RB #########

require 'gst'
require 'Timer.rb'

class BlockPlayer

  :low
  :medium
  :high
  @log_level = :high
  @log_play_interval = 1 

  # standard constructor
  def initialize( volume_block, frequency_block )
    @log_level = :high
    @log_play_interval = 1
    @volume = volume_block
    @frequency = frequency_block
    @play_state = false
    build_pipeline
  end

  # plays the function for a given time
  def play_time( max_time )
    # init values
    @audio_source.volume = @volume.call( 0 )
    @audio_source.freq = @frequency.call( 0 )

    # set up pipeline 
    @pipeline.play
    timer = Timer.new
    print_info :sound_start
    # iterate trough data
    elapsed = timer.elapsed
    while elapsed < max_time
     @pipeline.iterate do end 
     @audio_source.volume = @volume.call( elapsed )
     @audio_source.freq = @frequency.call( elapsed )
     print_info :sound_iter, timer
     elapsed = timer.elapsed
    end
    # finish up pipeline
    print_info :sound_stop
    @pipeline.stop
  end

  private
  # builds the elements and the pipeline
  def build_pipeline
    Gst.init
    @pipeline = Gst::Pipeline.new
    @audio_source = Gst::ElementFactory.make("sinesrc")  
    audio_sink = Gst::ElementFactory.make("osssink")
    @pipeline.add(@audio_source, audio_sink)
    @audio_source >> audio_sink
    print_info :create_pipeline # print info
  end 

  # provides the logging facilities
  def print_info( event, timer=nil, last_timer=nil )
    case event
    # standard play event
    when :sound_iter
      case @log_level
      # low log level
      when :low
      # medium log level
      when :medium
        timer = @timer unless timer
        if timer.elapsed > @log_play_interval
          puts "played #{timer.elapsed} seconds."
        end
      # highest log level
      when :high
        @ticks += 1
        timer = @timer unless timer
        if @last_output.elapsed >= @log_play_interval
          puts "played #{timer.elapsed} seconds, with #{@ticks} ticks. V: #{@audio_source.volume} :: F: #{@audio_source.freq}" 
          @last_output = Timer.new
        end
      end  

    # sound stream has started
    when :sound_start
      @timer = Timer.new
      @last_output = Timer.new
      case @log_level
        when :low
        when :medium
          puts "started soundstream"
        when :high
          @ticks = 0
          puts "started soundstream"
      end

    # sound stream has been stopped
    when :sound_stop
      case @log_level
        when :low
        when :medium
          @timer = nil
        when :high
          @timer = nil
          @ticks = nil
      end
    when :create_pipeline
        puts "pipeline creation done" unless @log_level ==  :low
    end 
  end
end



######## Timer.rb #############
class Timer
  def initialize
    self.start
  end

  def start
    @start_time = Time.now
  end

  def elapsed
    return (Time.now - @start_time)
  end

  def begin
    @start_time
  end
end


##### testHearing.rb ######
# This class represents a test case for the Player class
require 'Player.rb'

player = BlockPlayer.new lambda {|time| return_val = (time+30)/120<=1 ? (time+30)/120 : 1}, lambda {|time| 3000+(30+time)*time}
puts "starting player"
player.play_time(600)
puts "player has stopped"