#NEWLINE# def key_name_to_key_number( key_string ) : #NEWLINE# #INDENT# "Convert a correctly formated string key to key number\n\n    Parameters\n    ----------\n    key_string : str\n        Format is ``'key mode'``, where:\n            `key` is notaded using ABCDEFG with # or b;\n            `mode` is notated using 'major' or 'minor'.\n        Letter case is irrelevant for mode.\n\n    Returns\n    -------\n        key_number : int\n            Integer number representing the key and its mode\n    " #NEWLINE# if ( not isinstance ( key_string , str ) ) : #NEWLINE# #INDENT# raise ValueError ( 'key_string is not String' ) #NEWLINE# #UNINDENT# if ( not ( key_string [ 1 ] in [ '#' , 'b' , ' ' ] ) ) : #NEWLINE# #INDENT# raise ValueError ( '2nd character {} is not #, b nor blank_space' . format ( key_string [ 1 ] ) ) #NEWLINE# #UNINDENT# ( key_str , mode_str ) = key_string . split ( ) #NEWLINE# key_str = key_str . upper ( ) #NEWLINE# mode_str = mode_str . lower ( ) #NEWLINE# note_names_pc = { #NEWLINE# #INDENT# 'C' : 0 , #NEWLINE# 'D' : 2 , #NEWLINE# 'E' : 4 , #NEWLINE# 'F' : 5 , #NEWLINE# 'G' : 7 , #NEWLINE# 'A' : 9 , #NEWLINE# 'B' : 11 , #NEWLINE# #UNINDENT# } #NEWLINE# modes = [ 'major' , 'minor' ] #NEWLINE# if ( not ( key_str [ 0 ] in modes ) ) : #NEWLINE# #INDENT# raise ValueError ( 'Key {} is not recognized' . format ( key_str [ 0 ] ) ) #NEWLINE# #UNINDENT# if ( mode_str not in modes ) : #NEWLINE# #INDENT# raise ValueError ( 'Mode {} is not recognized' . format ( mode_str ) ) #NEWLINE# #UNINDENT# key_number = note_names_pc [ key_str [ 0 ] ] #NEWLINE# if ( len ( key_str ) == 2 ) : #NEWLINE# #INDENT# if ( key_str [ 1 ] == '#' ) : #NEWLINE# #INDENT# key_number += 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# key_number -= 1 #NEWLINE# #UNINDENT# #UNINDENT# key_number = ( key_number % 12 ) #NEWLINE# if ( mode_str == 'minor' ) : #NEWLINE# #INDENT# key_number += 12 #NEWLINE# #UNINDENT# return key_number