#NEWLINE# def key_number_to_key_name( key_number ) : #NEWLINE# #INDENT# "Convert a key number to a key string\n\n    Parameters\n    ----------\n    key_number : int\n        Uses pitch classes to represent major and minor keys.\n        For minor keys, adds a 12 offset.\n        For example, C major is 0 and C minor is 12.\n\n    Returns\n    -------\n    str\n        'Root mode', e.g. Gb minor.\n        Gives preference for keys with flats, with the\n        exception of F#, G# and C# minor.\n    " #NEWLINE# if ( not isinstance ( key_number , int ) ) : #NEWLINE# #INDENT# raise ValueError ( '`key_number` is not int!' ) #NEWLINE# #UNINDENT# if ( not ( ( key_number >= 0 ) and ( key_number < 24 ) ) ) : #NEWLINE# #INDENT# raise ValueError ( '`key_number` is larger than 24' ) #NEWLINE# #UNINDENT# keys = [ 'C' , 'Db' , 'D' , 'Eb' , 'E' , 'F' , 'Gb' , 'G' , 'Ab' , 'A' , 'Bb' , 'B' ] #NEWLINE# key_idx = ( key_number % 12 ) #NEWLINE# mode = ( key_number / 12 ) #NEWLINE# if ( mode == 0 ) : #NEWLINE# #INDENT# return ( keys [ key_idx ] + ' Major' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( mode == 1 ) : #NEWLINE# #INDENT# if ( key_idx in [ 1 , 6 , 8 ] ) : #NEWLINE# #INDENT# return ( keys [ ( key_idx - 1 ) ] + '# minor' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( keys [ key_idx ] + ' minor' )