algoritmo euclideano para generar ritmos

This commit is contained in:
rgarcia-herrera 2024-10-25 18:59:21 -06:00
parent 968895aaaa
commit 58c6adfea6
3 changed files with 48 additions and 0 deletions

BIN
bridges2005-47.pdf Normal file

Binary file not shown.

43
euclidean.py Normal file
View File

@ -0,0 +1,43 @@
def distribute(pattern, remainder):
if len(remainder) <= 1:
# if remainder is 1 join into pattern
# if no remainder just return pattern
return ["".join(pattern + remainder)]
while remainder:
for i in range(0, len(pattern)):
if remainder:
pattern[i] += remainder.pop()
return pattern
def split(pattern):
""" split pattern by moving smaller sequences to remainder """
min_len = min([len(j) for j in pattern])
max_len = max([len(j) for j in pattern])
remainder = []
if max_len != min_len:
while len(pattern[-1]) == min_len:
remainder.append(pattern.pop())
# else means all sequences are of the same size so no remainder
return pattern, remainder
def euclidean_rythm(k, n):
""" k hits in measure of length n """
hits = list('X' * k)
silences = list('.' * (n - k))
while True:
hits = distribute(hits, silences)
if len(hits) == 1:
return hits.pop()
else:
hits, silences = split(hits)

5
test_euclidean.py Normal file
View File

@ -0,0 +1,5 @@
from euclidean import euclidean_rythm
def test_euclidean_rythm():
assert euclidean_rythm(k=3, n=12) == 'X...X...X...'
assert euclidean_rythm(k=5, n=13) == 'X..X.X..X.X..'