I made a code to randomly generate a score. Use it to create a dataset of machine learning sheet music.
--chromebook debian9 (stretch)
python library:
soft:
randomScore.py
import music21 as m21
from numpy.random import choice
def makeScore(symbleNum=200, scoreNum=20, noteRange=[3, 4, 5, 6, 7],
noteRangeProbs=[0.05, 0.4, 0.4, 0.1, 0.05], sharpProb=0.1,
restProb=0.2, exportDir='/home'):
'''
symbleNum:How many notes and rests per sheet music
scoreNum:How many scores to generate
noteRange:How many octaves to which notes are there (default is C3 to B)#Up to 7)
noteRangeProbs:A list of the probabilities of how much each note will come out by octave
sharpProb:Probability of sharpening notes
restProb:Probability of rest
exportDir:Where to save the output file
===========================
By default the file name is 1, 2, 3, ...It has become.
.In addition to the png file, various files will appear, but you can delete them.
'''
quarterLengths = [4, 2.5, 2, 1.5, 1, 0.75, 0.5, 0.25]
notes = ['C', 'D', 'E', 'F', 'G', 'A', 'B']
noteRange = list(map(lambda x: str(x), noteRange))
typeProbs = [1 - restProb, restProb]
sharpProbs = [sharpProb, 1 - sharpProb]
chooseThese = [True, False]
for i in range(scoreNum):
noteList = []
measure = m21.stream.Measure()
for j in range(symbleNum):
if choice(a=chooseThese, p=typeProbs):
if choice(a=chooseThese, p=sharpProbs):
pitchName = choice(notes) + '#' + choice(a=noteRange, p=noteRangeProbs)
else:
pitchName = choice(notes) + choice(a=noteRange, p=noteRangeProbs)
n = m21.note.Note(pitchName, quarterLength=choice(quarterLengths))
noteList.append(n)
else:
n = m21.note.Rest(quarterLength = choice(quarterLengths))
noteList.append(n)
measure.append(noteList)
fileName = exportDir + str(i)
measure.write('lily.png', fileName)
if __name__ == "__main__":
makeScore()
--Only four beats --The number of notes in one bar is strange --Musically (melody) funny --No flat --No title --No dynamics or articulation
etc
Recommended Posts