Newer
Older
Intro_Quiz_2018 / mpg123.rb
#!/usr/bin/ruby
# -*- coding: utf-8 -*-



if !ARGV[0]
  STDERR.puts "Choose playback music file\n:"
  STDERR.puts "\t#{$0}"
  exit 1
end

frame = nil
e = IO.pipe

pid = fork do
  STDERR.printf("Hello %s playback now.", ARGV.join(" "))
  STDERR.reopen(e[1])
  e[0].close
  exec "mpg123", "-v", *ARGV
end






#ここから親の処理
e[1].close

def prompt(file)
  STDERR.printf("\n[%s] playback - command(next music:n、quit:q); ", file)
end

Thread.new do
  while not e[0].closed?
    line = ""
    while l = e[0].getc
      if l != "\r"[0] && l != "\n"[0]
        line << l.chr
      else
        break
      end
    end
    
    if /Frame\#\s*(\d+)/n =~ line
      STDERR.printf "%s ", $1 if $DEBUG
      frame =$1.to_i
    elsif /stream .* (.+) \.\.\./n =~ line
      prompt($1)
    end
  end
end

Thread.new do
  Process.wait
  printf("Stopping at frame %s\n", frame)
  exit 0
end


while true
  a = STDIN.gets
  if a.nil? || /q/ =~ a
    Process.kill(:INT, pid)
    Process.kill(:INT, pid)
    break
  elsif /n/=~ a
    Process.kill(:INT, pid)
  end
end