Guest User

Solution to https://redd.it/7ifbd5 in Python

a guest
Dec 29th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. #RANDOM "MUSIC" GENERATOR
  2. #Thanks to http://www.cyber-omelette.com/2017/01/markov.html for help with markov implementation.
  3.  
  4. import random
  5.  
  6. #so i can keep note info in one place
  7. class note:
  8.     def __init__(self, pitch='0', start='0', length='0'):
  9.         self.pitch = pitch
  10.         self.start = start
  11.         self.length = length
  12.  
  13. def readdata():
  14.     with open("data.txt") as rawdata:
  15.         #converts the data into something more usable.
  16.         #the data becomes a nested list. each item in the first list represents a note.
  17.         #each item in the second list represents (in order) the pitch, start, and length of the note.
  18.         data = str(rawdata.read()).split('\n')
  19.         for i in range(len(data)):
  20.             data[i] = data[i].split(' ')
  21.         return data
  22.  
  23. def makepitchrule(data):
  24.     #for now I'll only look one note back for reference
  25.     #the pitch and length will be calculated seperately. Start can be ignored.
  26.     #create reference for pitch of notes
  27.     location = 1
  28.     pitchrule = {}
  29.     for pitch in data[location:]:
  30.         key = data[location - 1][0]
  31.         if key in pitchrule:
  32.             pitchrule[key].append(pitch[0])
  33.         else:
  34.             pitchrule[key] = [pitch[0]]
  35.         location += 1
  36.     return pitchrule
  37.  
  38. def makelengthrule(data):
  39.     #do the exact same thing, but with length
  40.     #there's probably an easy way to combine these that I'm overlooking
  41.     location = 1
  42.     lengthrule = {}
  43.     for length in data[location:]:
  44.         key = data[location - 1][2]
  45.         if key in lengthrule:
  46.             lengthrule[key].append(length[2])
  47.         else:
  48.             lengthrule[key] = [length[2]]
  49.         location += 1
  50.     return lengthrule
  51.  
  52. def newnote(oldnote, pitchrule, lengthrule):
  53.     #create a new note given the old note and the markov rules
  54.     newnote = note()
  55.     newnote.start = str(float(oldnote.start) + float(oldnote.length))
  56.     newnote.pitch = random.choice(pitchrule[oldnote.pitch])
  57.     newnote.length = random.choice(lengthrule[oldnote.length])
  58.     return newnote
  59.  
  60. def writenote(currentnote, file):
  61.     notestring = str(currentnote.pitch)+' '+str(currentnote.start)+' '+str(currentnote.length)+'\n' #just converting everything to strings "just in case"
  62.     file.write(notestring)
  63.  
  64. #main program thing
  65. data = readdata()
  66. pitchrule = makepitchrule(data)
  67. lengthrule = makelengthrule(data)
  68. currentnote = note(random.choice(list(pitchrule.keys())), 0, random.choice(list(lengthrule.keys()))) #random first note
  69. with open('music.txt', mode='wt') as file: #was gonna open in the writing funtion but that's a bad idea
  70.     for i in range(100):
  71.         writenote(currentnote, file)
  72.         currentnote = newnote(currentnote, pitchrule, lengthrule)
Advertisement
Add Comment
Please, Sign In to add comment