summaryrefslogtreecommitdiff
path: root/rhythm.py
blob: 960f24743718a2ca87f9131e6f09efb142176126 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from utils import *
from math import ceil, floor

refractions = { # 1     2      3      4      5      6      7      8       9      10      11     12      13     14      15      16      17
	'double':	[(0,1),(1,16),(1,8), (3,16),(1,4), (5,16),(3,8), (7,16), (1,2), (9,16), (5,8), (11,16),(3,4), (13,16),(7,8),  (15,16),(1,1)],
	'trp_ls':	[(0,1),(1,12),(1,6), (1,4), (1,3), (5,12),(1,2), (7,12), (2,3), (17,24),(3,4), (19,24),(5,6), (7,8),  (11,12),(23,24),(1,1)],
	'qnt_ls':	[(0,1),(1,10),(2,10),(3,10),(4,10),(9,20),(1,2), (11,20),(3,5),(13,20),(7,10), (3,4),  (4,5), (17,20),(9,10), (19,20),(1,1)],
	'trp_ll':	[(0,1),(1,12),(1,6), (1,4), (1,3), (5,12),(1,2), (7,12), (2,3), (3,4),  (5,6), (11,12),(1,1), (13,12),(7,6),  (5,4),  (4,3)],
	'qnt_ll':	[(0,1),(1,10),(2,10),(3,10),(4,10),(1,2), (3,5), (7,10), (4,5), (9,10), (1,1), (11,10),(6,5), (13,10),(7,5),  (15,10),(8,5)],
	'trp_sl':	[(0,1),(1,24),(1,12),(1,8), (1,6), (5,24),(1,4), (7,24), (1,3), (5,12), (1,2), (7,12), (2,3), (3,4),  (5,6),  (11,12),(1,1)],
	'qnt_sl':	[(0,1),(1,20),(1,10),(3,20),(2,10),(1,4), (3,10),(7,20), (4,10),(9,20), (1,2), (11,20),(3,5), (7,10), (4,5),  (9,10), (1,1)],
	'trp_ss':	[(0,1),(1,24),(1,12),(1,8), (1,6), (5,24),(1,4), (7,24), (1,3), (3,8),  (5,12),(11,24),(1,2), (13,24),(7,12), (5,8),  (2,3)],
	'qnt_ss':	[(0,1),(1,20),(1,10),(3,20),(2,10),(1,4), (3,10),(7,20), (4,10),(9,20), (1,2), (11,20),(3,5), (13,20),(7,10), (3,4),  (4,5)],
	'qt2_ls':   [(0,1),(1,10),(2,10),(1,4), (3,10),(7,20),(4,10),(9,20), (1,2), (3,5),  (7,10),(3,4),  (4,5), (17,20),(9,10), (19,20),(1,1)],
	'qt2_sl':   [(0,1),(1,20),(1,10),(3,20),(2,10),(1,4), (3,10),(4,10), (1,2), (11,20),(3,5), (13,20),(7,10),(3,4),  (4,5),  (9,10), (1,1)],
	'tp2_ls':	[(0,1),(1,12),(1,6), (1,4), (1,3), (3,8), (5,12),(11,24),(1,2), (7,12), (2,3), (3,4),  (5,6), (7,8),  (11,12),(23,24),(1,1)],
	'tp2_sl':	[(0,1),(1,24),(1,12),(1,8), (1,6), (1,4), (1,3), (5,12), (1,2), (13,24),(7,12),(5,8),  (2,3), (3,4),  (5,6),  (11,12),(1,1)],
}

points = [0.0, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625]


metric_distances = {
	'double': [(0,1), (1,2), (1,4), (1,8), (1,16)],
	'trp_ls': [(0,1), (2,3), (1,3), (1,6), (1,12)],
	'qnt_ls': [(0,1), (3,5), (4,10), (2,10), (1,10)],
	'trp_ll': [(0,1), (2,3), (1,3), (1,6), (1,12)],
	'qnt_ll': [(0,1), (4,5), (4,10), (2,10), (1,10)],
	'trp_sl': [(0,1), (1,3), (1,6), (1,12), (1,24)],
	'qnt_sl': [(0,1), (4,10), (2,10), (1,10), (1,20)],
	'trp_ss': [(0,1), (1,3), (1,6), (1,12), (1,24)],
	'qnt_ss': [(0,1), (4,10), (2,10), (1,10), (1,20)],
	'qt2_ls': [(0,1), (1,2), (3,10), (2,10), (1,10)],
	'qt2_sl': [(0,1), (1,2), (2,10), (1,10), (1,20)],
	'tp2_ls': [(0,1), (1,2), (1,3), (1,6), (1,12)],
	'tp2_sl': [(0,1), (1,2), (1,6), (1,12), (1,24)],
}


def getOffsets(foot):
	""" calculate offsets for syncopated, refracted, scaled foot """
	syncedOffsets = syncOffsets(foot)
	finalOffsets = []
	for syncedOffset in syncedOffsets:
		finalOffset = getFinalOffset(syncedOffset, foot['gait'], foot['span'])
		finalOffsets.append(finalOffset)
	return finalOffsets


def syncOffsets(foot):
	""" syncopate logical offsets, desyncopate axis if crowded """
	syncedOffsets = []
	for i, offset in enumerate(foot['foot']):
		j = i%(len(foot['sync'])) # sync index wraps around
		if offset == 0:
			syncBasis = 1.0
		else:
			syncBasis = abs(offset)
		syncedOffset = offset + (syncBasis*foot['sync'][j])
		syncedOffsets.append(syncedOffset)
	# desyncopate axis if crowded in
	axisIndex = foot['foot'].index(0)
	beforeAxis = syncedOffsets[0:axisIndex]
	afterAxis = syncedOffsets[(axisIndex+1):len(syncedOffsets)]
	for offset in beforeAxis:
		if offset >= syncedOffsets[axisIndex]:
			syncedOffsets[axisIndex] = 0.0
	for offset in afterAxis:
		if offset <= syncedOffsets[axisIndex]:
			syncedOffsets[axisIndex] = 0.0
	syncedOffsets = resolveCollisions(syncedOffsets)
	return syncedOffsets


def resolveCollisions(syncedOffsets):
	# for now remove one - note lexeme object will reflect incorrect number of notes
	while len(syncedOffsets) != len(set(syncedOffsets)):
		neighborPairs = [ (x,y) for x, y in zip(syncedOffsets[:-1], syncedOffsets[1:]) ]
		for i, pair in enumerate(neighborPairs):
			if pair[0] == pair[1]:
				if pair[0] > 0:				
					if syncedOffsets[i+1] == ceil((syncedOffsets[i+1]*2)) / 2:
						syncedOffsets[i+1] += 0.5
					else:
						syncedOffsets[i+1] = fl(ceil((syncedOffsets[i+1]*2)) / 2)
				if pair[0] < 0:
					if syncedOffsets[i] == floor((syncedOffsets[i]*2)) / 2:
						syncedOffsets[i] -= 0.5
					else:
						syncedOffsets[i] = fl(floor((syncedOffsets[i]*2)) / 2)
	return syncedOffsets


def getFinalOffset(offset, refraction, scale):
	""" calculate refracted, scaled offset from 'logical' duple offset """
	if offset < 0:
		working_offset = -offset
	else:
		working_offset = offset
	offset_index = [x/16 for x in range(0,17)].index(working_offset)
	final = refractions[refraction][offset_index]

	if offset < 0:
		scale *= -1
	return fl((final[0]*scale)/(final[1]), 6)


# [0.0, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625]