Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Project: Peter's Files
Views: 3893
Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu1804
Kernel: Python 3 (Ubuntu Linux)
Markov Chain Music\text{\Huge Markov Chain Music}
import numpy as np import random
def generate_random_array(n): out = np.random.rand(n,n) for i in np.arange(n): out[i] = out[i]/sum(out[i]) return out.T def simple_walk_melody(array,start, steps): vec = np.array([[0]]*len(array)) vec[start] = 1 notes = [] for i in np.arange(steps): vec = np.dot(array,vec) new_note = vec.argmax()+1 notes = notes + [new_note] print(notes) # matrix Tup natural def simple_walk_harmony(matrix,start,steps): out = np.array([[0]*len(matrix)]*steps, dtype = np.float64) for i in start: out[0][i] = 1/len(start) for j in np.arange(1,steps): unfiltered = np.dot(matrix,out[j-1]) temp = [] for k in start: temp = temp + [unfiltered.argmax()] unfiltered[unfiltered.argmax()] = 0 for l in temp: out[j][l] = 1/len(start) return out*len(start)

Random Selection By Adjacency Matrix\text{Random Selection By Adjacency Matrix}

def weighted_random_selection(array): # randomly selects an index of array weighted by values of the array array = array/sum(array) r = random.uniform(0, 1) j = 0 while r > sum(array[0:j]): j = j + 1 return j - 1 def note_from_number(number): return ['C','C#','D','D#','E','F','F#','G','G#','A','A#','B'][number] def weighted_walk(matrix, start, steps): out = np.array([[0]*len(matrix)]*steps) out[0][start] = 1 for j in np.arange(1,steps): choice = weighted_random_selection(np.dot(matrix,out[j-1])) out[j][choice] = 1 notes = '' for row in out: notes = notes + note_from_number(row.argmax()) + ' ' return notes
# ANTHONY # c c# d d# e f f# g g# a a# b Anthony = np.array([[ 0, 0, 0, .4, 0, .5, 0,.25,.18, 0, 0, 1], # c [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # c# [.3, .9, 0, .3, 0, 0, 0,.25, 0,.23, 0, 0], # d [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # d# [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # e [.4, 0, .6, 0, .6, 0, 0, 0, 0, 0, 0, 0], # f [ 0, 0, 0, 0, 0, 0, 0, 0, 0,.43,.15, 0], # f# [.3, 0, .4, .3, .2, .5, .9, 0, 0, 0, 0, 0], # g [ 0, .1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # g# [ 0, 0, 0, 0, 0, 0, 0,.25,.82, 0, 0, 0], # a [ 0, 0, 0, 0, 0, 0, .1, 0, 0, 0, 0, 0], # a# [ 0, 0, 0, 0, .2, 0, 0,.25, 0,.34,.85, 0]]) # b
weighted_walk(Anthony,0,50)
'C D F C G A B C D G C G C G C D F C F C D F C F G B C D F G D F C D F C F C F G B C F C G C F G A F# '
# ESTHER # c c# d d# e f f# g g# a a# b esther = np.array([[.1, .2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # c [.1, .2, .2, 0, 0, 0, 0, 0, 0, 0, 0, 0], # c# [.2, .2, .2, 0, 0, 0, 0, 0, 0, 0, 0, 0], # d [ 0, .2, .2, 0, 0, 0, 0, 0, 0, 0, 0, 0], # d# [.1, 0, .2, 0, 0, 0, 0, 0, 0, 0, 0, 0], # e [.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # f [.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # f# [.2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # g [ 0, .2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # g# [.1, 0, .2, 0, 0, 0, 0, 0, 0, 0, 0, 0], # a [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # a# [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) # b
weighted_walk(esther,0,50)
# BLANK # c c# d d# e f f# g g# a a# b Blank = np.array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # c [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # c# [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # d [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # d# [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # e [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # f [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # f# [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # g [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # g# [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # a [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # a# [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) # b