[ create a new paste ] login | about

Link: http://codepad.org/EQXviHPp    [ raw code | output | fork ]

bpgergo - Python, pasted on May 21:
#!/usr/bin/python
#dict of the <keyboard key> : <int> pairs
keys = {"C" : 0, "C#" : 1,  "Db" : 1, "D" : 2, "D#" : 3, "Eb" : 3, "E" : 4, "F" : 5, "F#" : 6, "Gb" : 6, "G" : 7, "G#" : 8, "Ab" : 8, "A" : 9, "A#" : 10, "Hb" : 10, "H" : 11}

#read lines of your file into a list of str
peace = ["C3E3G3C4E4", "C3D3A3D4F4", "H2D3G3D4F4", "C3E3G3C4E4", "C3E3A3E4A4", "C3D3F#3A3D4", "H2D3G3D4G4", "H2C3E3G3C4", "A2C3E3G3C4", "D2A2D3F#3C4", "G2H2D3G3H3"]
KEYCOUNT = 12

import re
patt = '([CDEFGAH])([#b]?)(\d)'
#print "pattern:", patt
p = re.compile('[CDEFGAH][#b]?\d')


'''Returns distance between keys
Raises a KeyError if key is not in the map
'''
def distance(key1, key2):
	s1, s2 = int(key1[-1]), int(key2[-1])
	k1, k2 = keys[key1[:-1]], keys[key2[:-1]]
	return (k2-k1)+KEYCOUNT*(s2-s1)

import itertools
'''s -> (s0,s1), (s1,s2), (s2, s3), ...
see http://docs.python.org/library/itertools.html'''
def pairwise(iterable):
	a, b = itertools.tee(iterable)
	b.next()
	return itertools.izip(a, b)


rev = {}
for item in keys.items():
	if item[1] in rev:
		rev[item[1]].append(item[0])
	else:
		rev[item[1]]= [item[0]]

def get_board_black(shift,notes=[]):
	board_black = ""
	for i in range(KEYCOUNT):
		if len(rev[i]) > 1:
			if (rev[i][0]+str(shift) in notes):
				board_black += rev[i][0]
			elif (rev[i][1]+str(shift) in notes):
				board_black += rev[i][1] + " "
			else:
				board_black += " | "
		else:
			board_black += "  "
	return board_black

def get_board(shift,notes=[]):
	board = ""
	for i in range(KEYCOUNT):
		if len(rev[i]) > 1:
			board += "   "
		else:
			if (rev[i][0]+str(shift) in notes):
				board += rev[i][0] + " "
			else:
				board += "[]"
			
	return board

def get_keyboard(shift):
	keyboard = ""
	for i in range(KEYCOUNT):
		keyboard += rev[i][0] + str(shift)
	return keyboard

def print_board(notes=None, size=5, print_keys=False):
	if notes is None:
		notes = []
	full_board_black = ""
	full_board = ""
	full_keyboard = ""
	for i in range(size):
		full_board_black += get_board_black(i, notes)
		full_board += get_board(i, notes)
		full_keyboard += get_keyboard(i)

	print full_board_black
	print full_board
	if print_keys:
		print full_keyboard
	print ""


############
print "keys:"
print keys
print "empty board:"
print_board(None, 5, True)

#also, suit = sweet
print "piece:"
print peace

print "play piece ..."
#print map(lambda chord : print_board(p.findall(chord), 5), peace);
for chord in peace:
	notes = p.findall(chord)
	print "chord and distances:", chord, map(lambda note_pair : distance(note_pair[0], note_pair[1]), pairwise(notes))
	print_board(notes, 5)
	


###############

def test():
	print ">>>>>>>>> test >>>>>>>>>"
	print 'keys.items():'
	print keys.items()
	print "rev:"
	print rev
	print "rev.keys():"
	print rev.keys()
	
	
	print 'distance("C#0", "D0")'
	print distance("C#0", "D0")
	print 'distance("C0", "D0")'
	print distance("C0", "D0")
	print 'distance("C0", "D1")'
	print distance("C0", "D1")
	print 'distance("C1", "D1")'
	print distance("C1", "D1")
	print 'distance("C1", "D0")'
	print distance("C1", "D0")
	
	chord1 = peace[1]
	print "chord1:"
	print chord1
	print "p.findall(chord1):"
	print p.findall(chord1)
	
	print "<<<<<<<<< test <<<<<<<<<"

#test()
	


Output:
keys:
{'A': 9, 'C': 0, 'Ab': 8, 'E': 4, 'D': 2, 'G': 7, 'F': 5, 'C#': 1, 'H': 11, 'Db': 1, 'D#': 3, 'Eb': 3, 'F#': 6, 'G#': 8, 'Gb': 6, 'Hb': 10, 'A#': 10}
empty board:
   |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |   
[]   []   [][]   []   []   [][]   []   [][]   []   []   [][]   []   [][]   []   []   [][]   []   [][]   []   []   [][]   []   [][]   []   []   []
C0C#0D0D#0E0F0F#0G0Ab0A0Hb0H0C1C#1D1D#1E1F1F#1G1Ab1A1Hb1H1C2C#2D2D#2E2F2F#2G2Ab2A2Hb2H2C3C#3D3D#3E3F3F#3G3Ab3A3Hb3H3C4C#4D4D#4E4F4F#4G4Ab4A4Hb4H4

piece:
['C3E3G3C4E4', 'C3D3A3D4F4', 'H2D3G3D4F4', 'C3E3G3C4E4', 'C3E3A3E4A4', 'C3D3F#3A3D4', 'H2D3G3D4G4', 'H2C3E3G3C4', 'A2C3E3G3C4', 'D2A2D3F#3C4', 'G2H2D3G3H3']
play piece ...
chord and distances: C3E3G3C4E4 [4, 3, 5, 4]
   |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |   
[]   []   [][]   []   []   [][]   []   [][]   []   []   [][]   []   [][]   []   []   []C    []   E []   G    []   []C    []   E []   []   []   []

chord and distances: C3D3A3D4F4 [2, 7, 5, 3]
   |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |   
[]   []   [][]   []   []   [][]   []   [][]   []   []   [][]   []   [][]   []   []   []C    D    [][]   []   A    [][]   D    []F    []   []   []

chord and distances: H2D3G3D4F4 [3, 5, 7, 3]
   |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |   
[]   []   [][]   []   []   [][]   []   [][]   []   []   [][]   []   [][]   []   []   H []   D    [][]   G    []   [][]   D    []F    []   []   []

chord and distances: C3E3G3C4E4 [4, 3, 5, 4]
   |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |   
[]   []   [][]   []   []   [][]   []   [][]   []   []   [][]   []   [][]   []   []   []C    []   E []   G    []   []C    []   E []   []   []   []

chord and distances: C3E3A3E4A4 [4, 5, 7, 5]
   |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |   
[]   []   [][]   []   []   [][]   []   [][]   []   []   [][]   []   [][]   []   []   []C    []   E []   []   A    [][]   []   E []   []   A    []

chord and distances: C3D3F#3A3D4 [2, 4, 3, 5]
   |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |     F#   |    |      |    |      |    |    |   
[]   []   [][]   []   []   [][]   []   [][]   []   []   [][]   []   [][]   []   []   []C    D    [][]   []   A    [][]   D    [][]   []   []   []

chord and distances: H2D3G3D4G4 [3, 5, 7, 5]
   |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |   
[]   []   [][]   []   []   [][]   []   [][]   []   []   [][]   []   [][]   []   []   H []   D    [][]   G    []   [][]   D    [][]   G    []   []

chord and distances: H2C3E3G3C4 [1, 4, 3, 5]
   |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |   
[]   []   [][]   []   []   [][]   []   [][]   []   []   [][]   []   [][]   []   []   H C    []   E []   G    []   []C    []   [][]   []   []   []

chord and distances: A2C3E3G3C4 [3, 4, 3, 5]
   |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |   
[]   []   [][]   []   []   [][]   []   [][]   []   []   [][]   []   [][]   []   A    []C    []   E []   G    []   []C    []   [][]   []   []   []

chord and distances: D2A2D3F#3C4 [7, 5, 4, 6]
   |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |     F#   |    |      |    |      |    |    |   
[]   []   [][]   []   []   [][]   []   [][]   []   []   [][]   D    [][]   []   A    [][]   D    [][]   []   []   []C    []   [][]   []   []   []

chord and distances: G2H2D3G3H3 [4, 3, 5, 4]
   |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |      |    |      |    |    |   
[]   []   [][]   []   []   [][]   []   [][]   []   []   [][]   []   [][]   G    []   H []   D    [][]   G    []   H []   []   [][]   []   []   []



Create a new paste based on this one


Comments: