Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- makePdf = True
- makePng = False
- pageWidth = "8 in"
- pageHeight = "9 in"
- dotsPerInch = "120"
- barsPerLine = 4
- linesPerPage = 6
- totalPages = 500
- lowestMidiNote = 21
- highestMidiNote = 108
- # staff position and clef probabilities [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, -40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
- clefBass8vb = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 18, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0, 0, 0, 0, 0, 0, 0]
- clefBass = [ 40, 35, 30, 25, 20, 15, 10, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45]
- clefTreble = [ 40, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 40, 35, 30, 25, 20, 15, 10, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 10, 15, 20, 25, 30, 35]
- clefTreble8va = [ 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 18, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- clefTreble15ma = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- clefIdentities = [["bottom", '\clef "bass_8" '], ["bottom", '\clef "bass" '], ["top", '\clef "treble" '], ["top", '\clef "treble^8" '], ["top", '\clef "treble^15" ']]
- keySignatureProbabilitiesBySharpiness = [ 4, 4, 4, 4, 4, 3, 2, 1, 1, 2, 3, 4, 4, 4, 4]
- keySignatureSharpinesses = [ 0, 1, 2, 3, 4, 5, 6, 7, -7, -6, -5, -4, -3, -2, -1]
- keySignatureTonicsBySharpiness = [ "d", "a", "e", "b", "fis", "cis", "gis", "dis", "des", "aes", "ees", "bes", "f", "c", "g"]
- enharmonicTable = [[50, 0], [50, 0], [49, 1], [46, 4], [41, 9], [34, 16], [25, 25], [16, 34], [9, 41], [4, 46], [1, 49], [0, 50]]
- familiarNoteProbabilities = [4, 3, 2, 1]
- familiarProbabilityRandomProbability = [1, 1]
- primeNoteGoal = 50
- oldKeyNewKeyProbability = [124, 1]
- import random
- import os
- random.seed(1)
- def newNoteIdentity():
- midiNote = random.randint(lowestMidiNote,highestMidiNote)
- midiNoteAbsolutePositiveSharpiness = (midiNote - 2) * 7 % 12
- midiNoteRelativePositiveSharpiness = (((midiNote - 2) * 7) - currentKeySignatureSharpiness) % 12
- midiNoteRelativeSharpiness = midiNoteRelativePositiveSharpiness + random.choices([0, -12], weights=enharmonicTable[midiNoteRelativePositiveSharpiness])[0]
- midiNoteAbsoluteSharpiness = midiNoteRelativeSharpiness + currentKeySignatureSharpiness
- noteSpellingsBySharpiness = ["d", "a", "e", "b", "fis", "cis", "gis", "dis", "ais", "eis", "bis", "fisis", "cisis", "gisis", "disis", "aisis", "eisis", "bisis", "feses", "ceses", "geses", "deses", "aeses", "eeses", "beses", "fes", "ces", "ges", "des", "aes", "ees", "bes", "f", "c", "g"]
- staffPositionsAboveMiddleC = int((7 * (midiNote - 60) - (midiNoteAbsoluteSharpiness + 2)) / 12)
- scientificPitchNotationOctave = staffPositionsAboveMiddleC // 7 + 4
- noteSuffixesByOctave = [",,,8", ",,8", ",8", "8", "'8", "''8", "'''8", "''''8", "'''''8"]
- noteWord = "%s%s" % (noteSpellingsBySharpiness[midiNoteAbsoluteSharpiness], noteSuffixesByOctave[scientificPitchNotationOctave])
- noteClefProbabilities = [clefBass8vb[staffPositionsAboveMiddleC], clefBass[staffPositionsAboveMiddleC], clefTreble[staffPositionsAboveMiddleC], clefTreble8va[staffPositionsAboveMiddleC], clefTreble15ma[staffPositionsAboveMiddleC]]
- noteClefIdentity = random.choices(clefIdentities, weights=noteClefProbabilities)[0]
- return([noteClefIdentity[0], noteClefIdentity[1], noteWord])
- def concludedNoteIdentity():
- global primeNoteCount
- global currentFamiliarNoteIdentities
- global currentClefTop
- global currentClefBottom
- currentNoteIdentity = random.choices([random.choices(currentFamiliarNoteIdentities, weights=familiarNoteProbabilities)[0], newNoteIdentity()], weights=familiarProbabilityRandomProbability)[0]
- if currentNoteIdentity == currentFamiliarNoteIdentities[0]:
- primeNoteCount +=1
- if primeNoteCount == primeNoteGoal:
- primeNoteCount = 0
- del currentFamiliarNoteIdentities[-1]
- currentFamiliarNoteIdentities.insert(0, newNoteIdentity())
- if currentNoteIdentity[0] == "top":
- currentClefTop = currentNoteIdentity[1]
- noteTop = currentNoteIdentity[2]
- noteBottom = "r8"
- else:
- currentClefBottom = currentNoteIdentity[1]
- noteTop = "r8"
- noteBottom = currentNoteIdentity[2]
- return([[currentClefTop, noteTop], [currentClefBottom, noteBottom]])
- def concludedBarIdentity():
- global currentKeySignatureSharpiness
- global previousKeySignatureSharpiness
- global barCount
- global lineCount
- previousKeySignatureSharpiness = currentKeySignatureSharpiness
- currentKeySignatureSharpiness = random.choices([currentKeySignatureSharpiness, random.choices(keySignatureSharpinesses, weights=keySignatureProbabilitiesBySharpiness)[0]], weights=oldKeyNewKeyProbability)[0]
- keySignature = "\key " + keySignatureTonicsBySharpiness[currentKeySignatureSharpiness] + " \dorian "
- if currentKeySignatureSharpiness == previousKeySignatureSharpiness:
- explicitKeySignature = ""
- else:
- explicitKeySignature = keySignature
- barMap = [concludedNoteIdentity(), concludedNoteIdentity(), concludedNoteIdentity(), concludedNoteIdentity(), concludedNoteIdentity()]
- if barMap[0][1][1] == "r8":
- if barMap[1][1][1] == "r8":
- beat1 = [explicitKeySignature + barMap[0][0][0] + barMap[0][0][1] + "[ " + barMap[1][0][0] + barMap[1][0][1] + "]", explicitKeySignature + barMap[0][1][0] + "r4"]
- else:
- beat1 = [explicitKeySignature + barMap[0][0][0] + barMap[0][0][1] + " " + barMap[1][0][0] + barMap[1][0][1], explicitKeySignature + barMap[0][1][0] + barMap[0][1][1] + " " + barMap[1][1][0] + barMap[1][1][1]]
- else:
- if barMap[1][1][1] == "r8":
- beat1 = [explicitKeySignature + barMap[0][0][0] + barMap[0][0][1] + " " + barMap[1][0][0] + barMap[1][0][1], explicitKeySignature + barMap[0][1][0] + barMap[0][1][1] + " " + barMap[1][1][0] + barMap[1][1][1]]
- else:
- beat1 = [explicitKeySignature + barMap[0][0][0] + "r4", explicitKeySignature + barMap[0][1][0] + barMap[0][1][1] + "[ " + barMap[1][1][0] + barMap[1][1][1] + "]"]
- if barMap[2][1][1] == "r8":
- if barMap[3][1][1] == "r8":
- if barMap[4][1][1] == "r8":
- beat2 = [barMap[2][0][0] + barMap[2][0][1] + "[ " + barMap[3][0][0] + barMap[3][0][1] + " " + barMap[4][0][0] + barMap[4][0][1] + "]", barMap[2][1][0] + "r4."]
- else:
- beat2 = [barMap[2][0][0] + barMap[2][0][1] + "[ " + barMap[3][0][0] + barMap[3][0][1] + "] r8", barMap[2][1][0] + "r4 " + barMap[4][1][0] + barMap[4][1][1]]
- else:
- if barMap[4][1][1] == "r8":
- beat2 = [barMap[2][0][0] + barMap[2][0][1] + "[ " + barMap[3][0][0] + barMap[3][0][1] + " " + barMap[4][0][0] + barMap[4][0][1] + "]", barMap[2][1][0] + barMap[2][1][1] + " " + barMap[3][1][0] + barMap[3][1][1] + " " + barMap[4][1][0] + barMap[4][1][1]]
- else:
- beat2 = [barMap[2][0][0] + barMap[2][0][1] + " " + barMap[3][0][0] + "r4", barMap[2][1][0] + barMap[2][1][1] + " " + barMap[3][1][0] + barMap[3][1][1] + "[ " + barMap[4][1][0] + barMap[4][1][1] + "]"]
- else:
- if barMap[3][1][1] == "r8":
- if barMap[4][1][1] == "r8":
- beat2 = [barMap[2][0][0] + barMap[2][0][1] + " " + barMap[3][0][0] + barMap[3][0][1] + "[ " + barMap[4][0][0] + barMap[4][0][1] + "]", barMap[2][1][0] + barMap[2][1][1] + " " + "r4"]
- else:
- beat2 = [barMap[2][0][0] + barMap[2][0][1] + " " + barMap[3][0][0] + barMap[3][0][1] + " " + barMap[4][0][0] + barMap[4][0][1], barMap[2][1][0] + barMap[2][1][1] + "[ " + barMap[3][1][0] + barMap[3][1][1] + " " + barMap[4][1][0] + barMap[4][1][1] + "]"]
- else:
- if barMap[4][1][1] == "r8":
- beat2 = [barMap[2][0][0] + "r4 " + barMap[4][0][0] + barMap[4][0][1], barMap[2][1][0] + barMap[2][1][1] + "[ " + barMap[3][1][0] + barMap[3][1][1] + "] r8"]
- else:
- beat2 = [barMap[2][0][0] + "r4.", barMap[2][1][0] + barMap[2][1][1] + "[ " + barMap[3][1][0] + barMap[3][1][1] + " " + barMap[4][1][0] + barMap[4][1][1] + "]"]
- currentBarTop = beat1[0] + " " + beat2[0]
- currentBarBottom = beat1[1] + " " + beat2[1]
- if barMap[0][0][1] == barMap[1][0][1] == barMap[2][0][1] == barMap[3][0][1] == barMap[4][0][1] == "r8":
- currentBarTop = explicitKeySignature + barMap[0][0][0] + " R8*5"
- if barMap[0][1][1] == barMap[1][1][1] == barMap[2][1][1] == barMap[3][1][1] == barMap[4][1][1] == "r8":
- currentBarBottom = explicitKeySignature + barMap[0][1][0] + " R8*5"
- barCount +=1
- if barCount == barsPerLine:
- lineCount +=1
- barCount = 0
- if lineCount == linesPerPage:
- lineCount = 0
- barCount = 0
- breakCommand = " \pageBreak"
- else:
- breakCommand = " \\break \\noPageBreak"
- else:
- breakCommand = " \\noBreak"
- return([keySignature, explicitKeySignature, [barMap[0][0][0], barMap[0][1][0]], [currentBarTop, currentBarBottom], breakCommand])
- def currentPage():
- global pageCount
- global currentBarIdentity
- pageCount = pageCount + 1
- if pageCount == 1:
- subsequentPageOverride = ""
- else:
- subsequentPageOverride = "\once \override Staff.TimeSignature #'stencil = ##f "
- pageInProgress = [subsequentPageOverride + currentBarIdentity[0] + currentBarIdentity[3][0] + currentBarIdentity[4], subsequentPageOverride + currentBarIdentity[0] + currentBarIdentity[3][1] + currentBarIdentity[4]]
- if pageCount == totalPages:
- for repeater in range(1, barsPerLine * linesPerPage):
- currentBarIdentity = concludedBarIdentity()
- pageInProgress[0] += currentBarIdentity[1] + currentBarIdentity[3][0] + currentBarIdentity[4]
- pageInProgress[1] += currentBarIdentity[1] + currentBarIdentity[3][1] + currentBarIdentity[4]
- pageInProgress[0] += ' \\bar "|." r8'
- pageInProgress[1] += ' \\bar "|." r8'
- else:
- for repeater in range(0, barsPerLine * linesPerPage):
- currentBarIdentity = concludedBarIdentity()
- pageInProgress[0] += currentBarIdentity[1] + currentBarIdentity[3][0] + currentBarIdentity[4]
- pageInProgress[1] += currentBarIdentity[1] + currentBarIdentity[3][1] + currentBarIdentity[4]
- return(pageInProgress)
- def completePage():
- pageInformation = currentPage()
- emptyPage = """\\version "2.18.2"
- #(set! paper-alist (cons '("res" . (cons (* %s) (* %s))) paper-alist))
- \paper { #(set-paper-size "res") }
- \header {
- copyright = "page %d of %d"
- }
- \layout {
- \context {
- \Score
- \\remove "Bar_number_engraver"
- }
- \context {
- \Voice
- \consists "Melody_engraver"
- \override Stem #'neutral-direction = #'()
- }
- }
- \score {
- \\new PianoStaff <<
- \\new Staff {
- \\autoBeamOff \\time 5/8
- %s
- }
- \\new Staff {
- \\autoBeamOff \\time 5/8
- %s
- }
- >>
- \layout {
- \context { \Staff \override TimeSignature.style = #'numbered }
- indent = #0
- }
- }""" % (pageWidth, pageHeight, pageCount, totalPages, pageInformation[0], pageInformation[1])
- pageNumberPadded = "{:0>4}".format(pageCount)
- file = open("/tmp/PythonRandomNotes_Page%s.ly" % (pageNumberPadded), "w")
- file.write(emptyPage)
- file.close()
- if makePdf == True:
- pdf = "--pdf "
- else:
- pdf = ""
- if makePng == True:
- png = "--png "
- else:
- png = ""
- os.system("lilypond -s %s%s-dresolution=%s -o /tmp/PythonRandomNotes_Page%s /tmp/PythonRandomNotes_Page%s.ly" % (pdf, png, dotsPerInch, pageNumberPadded, pageNumberPadded))
- if makePdf == True:
- os.system("pdftk /tmp/PythonRandomNotes_Page%s.pdf cat 1 output /tmp/PythonRandomNotes_Page%s-single.pdf" % (pageNumberPadded, pageNumberPadded))
- os.system("rm /tmp/PythonRandomNotes_Page%s.pdf" % (pageNumberPadded))
- if makePng == True:
- os.system("mv /tmp/PythonRandomNotes_Page%s-page1.png /tmp/PythonRandomNotes_Page%s.png" % (pageNumberPadded, pageNumberPadded))
- os.system("rm /tmp/PythonRandomNotes_Page%s-page2.png" % (pageNumberPadded))
- os.system("rm /tmp/PythonRandomNotes_Page%s.ly" % (pageNumberPadded))
- print("page %d of %d completed" % (pageCount, totalPages))
- # just to define them to begin with
- currentKeySignatureSharpiness = random.choices(keySignatureSharpinesses, weights=keySignatureProbabilitiesBySharpiness)[0]
- previousKeySignatureSharpiness = ""
- currentClefTop = '\clef "treble" '
- currentClefBottom = '\clef "bass" '
- currentFamiliarNoteIdentities = [newNoteIdentity(), newNoteIdentity(), newNoteIdentity(), newNoteIdentity()]
- primeNoteCount = 0
- barCount = 0
- lineCount = 0
- pageCount = 0
- currentBarIdentity = concludedBarIdentity()
- for repeater in range(0, totalPages):
- completePage()
- if makePdf == True:
- os.system("pdftk /tmp/PythonRandomNotes_Page*-single.pdf cat output /tmp/PythonRandomNotes.pdf")
- os.system("rm /tmp/PythonRandomNotes_Page*-single.pdf")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement