#!/usr/bin/python
# -*- coding: utf-8 -*-
# Mirat Can Bayrak
# Ver : 0.1 
# Todo
# show selected scale on guitar keyboard 
# show selected scale on piano keyboard
# generate midi file and play 

import sys

scale_formulas = {
    'maj':(0,2,4,5,7,9,11), #major
    'min':(0,2,3,5,7,8,10), #minor
  'min-a':(0,2,3,5,7,8,11), #armonic minor
  'min-m':(0,2,3,5,7,9,11)  #melodic minor
}

natural_notes = ['c','c#','d','d#','e','f','f#','g','g#','a','a#','b']

def formed_notes(scale):
    """ 
    returns notes that formed by scale 
    """
    tmp = scale[0]
    for i in range(1,7):
        tmp = tmp +', '+scale[i]
    return tmp

def keyboard(scale):
    """ returns a ascii pic of keyboard """
    board = "    1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9\
  0  1  2  3\n"
    for string in ('e','b','g','d','a','e'): # guitar strings
        board += string.upper()+ "|"
        for fret in range (1,24) :
            start = natural_notes.index(string)
            if natural_notes[(start+fret) % 12] in scale:
                board += "  O"
            else:
                board += "  -"
        board += "\n"
    return board

def calculate_scale(note,scale):
    tmp = list()
    try:
        basenote = natural_notes.index(note)
    except:
        print "nota bulunamadı"
        sys.exit()
    if scale in scale_formulas.keys():
        for walk in scale_formulas[scale]:
            tmp.append(natural_notes[(basenote + walk) % 12])
        return tmp
    else:
        print "gam bulunamadı"
if sys.argv[1] and sysargv[2]:
    scale = calculate_scale(sys.argv[1],sys.argv[2])
    print "The", sys.argv[1] , sys.argv[2] , "Scale is:" ,formed_notes(scale)
    print 
    print "On Guitar Keyboard :"
    print "--------------------"
    print keyboard(scale)

