トップ «前の日記(2007-09-22) 最新 次の日記(2007-09-27)» 編集

Route 477



2007-09-23

[ruby] 歌えPC

require 'Win32API'

def beep(freq, dur)
  Win32API.new('kernel32', 'Beep', %w(i i), "i").call(freq, dur)
end

FREQ_TBL = {
  "a" => 0, "a+" => 1, "b" => 2, "c" => 3, "c+" => 4, "d" => 5,
  "d+" => 6, "e" => 7, "f" => 8, "f+" => 9, "g" => 10, "g+" => 11,
}

def freq_oct(oct)
  440 * (2**(oct-4))
end

def freq_of(onp, oct)
  return 50 if onp == "r"
  oct-=1 unless %w(a a+ b- b).include? onp
  unit = (freq_oct(oct+1) - freq_oct(oct))/12
  freq_oct(oct) * (2.0**(FREQ_TBL[onp].to_f/12))
end

def say(onp, oct, len=100)
  p onp
  beep(freq_of(onp, oct), len)
end

if __FILE__ == $0
  oct=6
  "c<bagbagfgfedcdeg>1".each_byte{|c|
  #A>A<a>a<A>A<a>a<A>A<a>a<A>A<a>a<ArrrD
    case c
    when ?>
      oct+=1
    when ?<
      oct-=1
    when ?1
      l = 10
      (80/3).times do
        say("c", oct, l)
        say("g", oct-1, l)
        say("e", oct-1, l)
      end
    else
      if (?A..?Z).include? c
        say("#{c.chr.downcase}+", oct)
      else
        say(c.chr, oct)
      end
    end
  }
end

Windowsのひとはためしてみよう!

Q.コードがぐちゃぐちゃなんですが
A.でもそんなの関係ねえ
Q.合宿中じゃなかったんですか
A.でもそんなの以下略