移調くん

ストラムスティックはキーG以外音が出ないのでキーGに移調せざるを得ないのですが、僕はキーCとかキーDから脳内でキーGに移調できません。移調の手助けをする簡単なツールを作ってみました。
ファイル: icho

#!/usr/bin/env ruby
# -*- ruby -*-

class Array
  def rotate(n)
    if n % self.size == 0
      self
    else
      self[n .. -1] + self[0 .. n - 1]
    end
  end
end

notes = [
  'C', 'C#', 'D', 'Eb', 'E', 'F',
  'F#', 'G', 'G#', 'A', 'Bb', 'B'
]

if ARGV.size < 2
  $stderr.print "usage: icho from to\n"
  exit 1
end

from_index = notes.index(ARGV[0][0].chr.upcase + ARGV[0][1 .. -1])
to_index = notes.index(ARGV[1][0].chr.upcase + ARGV[1][1 .. -1])
if from_index.nil? or to_index.nil?
  $stderr.print "invalid argument\n"
  exit 1
end
 
newnotes = notes.rotate(to_index - from_index)

notes.each_index do |i|
  printf "%-2s => %-2s\n", notes[i], newnotes[i]
end

実行例:

$ icho c g
C  => G 
C# => G#
D  => A 
Eb => Bb
E  => B 
F  => C 
F# => C#
G  => D 
G# => Eb
A  => E 
Bb => F 
B  => F#