User:Danny van der Kleij/Markov Piano: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "<source lang="python"> import os, random samplelist = ['Piano_A', 'Piano_B', 'Piano_C', 'Piano_D', 'Piano_E', 'Piano_F', 'Piano_G', 'Piano_F', 'Piano_E', 'Piano_D', 'Piano_C', 'P...")
 
No edit summary
Line 26: Line 26:


print markovlist
print markovlist
# now after having the new markov chain list we play it using mplayer and system calls.
for (k, s) in enumerate(markovlist):
for (k, s) in enumerate(markovlist):
   cmd = 'mplayer %s.wav' % (markovlist[k])   
   cmd = 'mplayer %s.wav' % (markovlist[k])   

Revision as of 09:53, 24 May 2011

import os, random
samplelist = ['Piano_A', 'Piano_B', 'Piano_C', 'Piano_D', 'Piano_E', 'Piano_F', 'Piano_G', 'Piano_F', 'Piano_E', 'Piano_D', 'Piano_C', 'Piano_B', 'Piano_A', 'end']


#need to know, which sample comes after another sample. for example after Piano_B can come Piano_A or Piano_C.
dictionary = {}

# i is what counts through the list with numbers(0-12) r = the corresponding elements in the list, so we get
# i = 2, r = 'piano_C'
for (i, r) in enumerate(samplelist):
  if i < len(samplelist) - 1:
    if samplelist[i] not in dictionary:
      dictionary[r] = []
    dictionary[r].append(samplelist[i+1])

#now walk through the dictionary each time chosing a new element 
#from the list of choices and storing that in a new list.

for j in range(0, 1):
  markovlist = []
  start = random.choice(dictionary['Piano_A'])
  while start != 'end':
    markovlist.append(start)
    start = random.choice(dictionary[start])

print markovlist
# now after having the new markov chain list we play it using mplayer and system calls.
for (k, s) in enumerate(markovlist):
  cmd = 'mplayer %s.wav' % (markovlist[k])  
  os.system(cmd)