Code to randomly generate a score

I made a code to randomly generate a score. Use it to create a dataset of machine learning sheet music.

You can make an image like this

0.png

1.png

2.png

3.png

environment

--chromebook debian9 (stretch)

Required libraries and software

python library:

soft:

code

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()

bad place

--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

Code to randomly generate a score
Command to generate QR code
Metaclass (wip) to generate a dictionary
Qiita (1) How to write a code name
How to code a drone using image recognition
Randomly sample MNIST data to create a dataset
How to generate a Python object from JSON
I tried to generate a random character string
I want to generate a UUID quickly (memorandum) ~ Python ~
Python Ver. To introduce WebPay with a little code.
Rewrite Python2 code to Python3 (2to3)
I made a code to convert illustration2vec to keras model
I want to randomly sample a file in Python
How to make a Python package using VS Code
I tried to automatically generate a password with Python3
Write code to Unit Test a Python web app
Generate a bash script to add Datadog monitor settings
How to create a kubernetes pod from python code
Good and bad code to compare on a minimap
[Python] Randomly generate a large number of English names
Generate a password that is easy to remember with apg
I want to automatically generate a modern metal band name
I made a command to generate a table comment in Django
How to generate a public key from an SSH private key
How to generate a query using the IN operator in Django
A quick introduction to pytest-mock
A road to intermediate Python
A super introduction to Linux
Part 1 Attempt to code mathematics (∈)
Add a dictionary to MeCab
How to call a function
Tool to check code style
Upload a file to Dropbox
Send a signal to subprocess
Set VS Code to PyCharm.
How to hack a terminal
Generate QR code in Python
Try to select a language
Convert python 3.x code to python 2.x
Generate error correction code to restore data corruption with zfec library
I tried to create a simple credit score by logistic regression.
Try to create a python environment with Visual Studio Code & WSL
Simple code to call a python program from Javascript on EC2
How to set Jupytext nicely when managing code as a team
I want to make a parameter list from CloudFormation code (yaml)