Basic paper: The Euclidean Algorithm Generates Traditional Musical Rhythms

Python code

# Making Euclidean Rhythm with Bjorklund's distribution
# by hTaka
import sys

class EuclideanRhythm:
    def getRhythm(self,steps,hit,shift):
        self.result = ""
        if hit > steps:
            raise NameError("Number of hit must less than steps")
        pause = steps - hit
        leftUnit = "X"
        rightUnit = "."
        print "process>{0}|{1} / left:{2} right:{3}".format(("["+leftUnit+"]") * hit , ("["+rightUnit+"]") * pause , hit , pause)
        self.makeEuclidRhythm(hit,pause,leftUnit,rightUnit)
        if abs(shift)%steps != 0:
            print "Result> Steps:{0} hit:{1} shift:{2} [{3}] (before shift)".format(steps,hit,shift,self.result)
            self.shiftRhythm(steps,shift)
        return self.result
    
    def makeEuclidRhythm(self,numL,numR,leftUnit,rightUnit):
        unitBuffer = leftUnit
        if numR==0 or numR == 1:
            self.result = leftUnit * numL+ rightUnit * numR
            return
        if numR > numL:
            m,k = numR,numL
            isRightRemain = True
            leftUnit += (rightUnit * (m // k))
        else:
            m,k = numL,numR
            isRightRemain = False
            leftUnit += rightUnit
        if isRightRemain:
            print "process>{0}|{1}  / left:{2} right:{3} {4} {5}".format(("["+leftUnit+"]") * k, ("["+rightUnit+"]") * (m % k), k , m%k ,"RightUnit Remain with" , "[" + rightUnit + "]")
            self.makeEuclidRhythm(k,m%k,leftUnit,rightUnit)
        else:
            print "process>{0}|{1}  / left:{2} right:{3} {4} {5}".format(("["+leftUnit+"]") * k, ("["+unitBuffer+"]") * (m - k), k, m-k,"RightUnit Replace with Left" , "[" + unitBuffer + "]")
            self.makeEuclidRhythm(k,m-k,leftUnit,unitBuffer)

    def shiftRhythm(self,steps,shift):
        if shift > 0:
            shift = shift % steps
            self.result = "{0}{1}".format(self.result[steps - shift :],self.result[0 : -1 * shift])
        if shift < 0:
            shift = (abs(shift) % steps)
            self.result = "{0}{1}".format(self.result[shift :],self.result[0 : shift])
        return


def main(arg):
    try:
        steps = int(arg[1])
        hit = int(arg[2])
        shift = int(arg[3])
    except:
        print "Args error! usage: python EuclideanRhythms.py <STEPS> <HIT> <SHIFT>"
        print "using hard coded params."
        steps,hit,shift = 16,7,0
    rhythm = EuclideanRhythm()
    print "Result> Steps:{0} hit:{1} shift:{2} [{3}]".format(steps,hit,shift,rhythm.getRhythm(steps,hit,shift))

if __name__ == '__main__':
    main(sys.argv)

Result

process>[X][X][X][X][X][X][X]|[.][.][.][.][.][.][.][.][.] / left:7 right:9
process>[X.][X.][X.][X.][X.][X.][X.]|[.][.]  / left:7 right:2 RightUnit Remain with [.]
process>[X..][X..]|[X.][X.][X.][X.][X.]  / left:2 right:5 RightUnit Replace with Left [X.]
process>[X..X.X.][X..X.X.]|[X.]  / left:2 right:1 RightUnit Remain with [X.]
Result> Steps:16 hit:7 shift:0 [X..X.X.X..X.X.X.]

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です