Advertisement
Guest User

Python Random Notes

a guest
Apr 22nd, 2018
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.90 KB | None | 0 0
  1. makePdf = True
  2. makePng = False
  3.  
  4. pageWidth = "8 in"
  5. pageHeight = "9 in"
  6. dotsPerInch = "120"
  7.  
  8. barsPerLine = 4
  9. linesPerPage = 6
  10. totalPages = 500
  11.  
  12. lowestMidiNote = 21
  13. highestMidiNote = 108
  14.  
  15. # 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]
  16. 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]
  17. 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]
  18. 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]
  19. 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]
  20. 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]
  21. clefIdentities = [["bottom", '\clef "bass_8" '], ["bottom", '\clef "bass" '], ["top", '\clef "treble" '], ["top", '\clef "treble^8" '], ["top", '\clef "treble^15" ']]
  22.  
  23. keySignatureProbabilitiesBySharpiness = [    4,     4,     4,     4,     4,     3,     2,     1,     1,     2,     3,     4,     4,     4,     4]
  24. keySignatureSharpinesses  =             [    0,     1,     2,     3,     4,     5,     6,     7,    -7,    -6,    -5,    -4,    -3,    -2,    -1]
  25. keySignatureTonicsBySharpiness =        [  "d",   "a",   "e",   "b", "fis", "cis", "gis", "dis", "des", "aes", "ees", "bes",   "f",   "c",   "g"]
  26. 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]]
  27.  
  28. familiarNoteProbabilities = [4, 3, 2, 1]
  29. familiarProbabilityRandomProbability = [1, 1]
  30. primeNoteGoal = 50
  31. oldKeyNewKeyProbability = [124, 1]
  32.  
  33. import random
  34. import os
  35.  
  36. random.seed(1)
  37.  
  38. def newNoteIdentity():
  39.     midiNote = random.randint(lowestMidiNote,highestMidiNote)
  40.     midiNoteAbsolutePositiveSharpiness = (midiNote - 2) * 7 % 12
  41.     midiNoteRelativePositiveSharpiness = (((midiNote - 2) * 7) - currentKeySignatureSharpiness) % 12
  42.     midiNoteRelativeSharpiness = midiNoteRelativePositiveSharpiness + random.choices([0, -12], weights=enharmonicTable[midiNoteRelativePositiveSharpiness])[0]
  43.     midiNoteAbsoluteSharpiness = midiNoteRelativeSharpiness + currentKeySignatureSharpiness
  44.     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"]
  45.     staffPositionsAboveMiddleC = int((7 * (midiNote - 60) - (midiNoteAbsoluteSharpiness + 2)) / 12)
  46.     scientificPitchNotationOctave = staffPositionsAboveMiddleC // 7 + 4
  47.     noteSuffixesByOctave = [",,,8", ",,8", ",8", "8", "'8", "''8", "'''8", "''''8", "'''''8"]
  48.     noteWord = "%s%s" % (noteSpellingsBySharpiness[midiNoteAbsoluteSharpiness], noteSuffixesByOctave[scientificPitchNotationOctave])
  49.     noteClefProbabilities = [clefBass8vb[staffPositionsAboveMiddleC], clefBass[staffPositionsAboveMiddleC], clefTreble[staffPositionsAboveMiddleC], clefTreble8va[staffPositionsAboveMiddleC], clefTreble15ma[staffPositionsAboveMiddleC]]
  50.     noteClefIdentity = random.choices(clefIdentities, weights=noteClefProbabilities)[0]
  51.     return([noteClefIdentity[0], noteClefIdentity[1], noteWord])
  52.  
  53. def concludedNoteIdentity():
  54.     global primeNoteCount
  55.     global currentFamiliarNoteIdentities
  56.     global currentClefTop
  57.     global currentClefBottom
  58.     currentNoteIdentity = random.choices([random.choices(currentFamiliarNoteIdentities, weights=familiarNoteProbabilities)[0], newNoteIdentity()], weights=familiarProbabilityRandomProbability)[0]
  59.     if currentNoteIdentity == currentFamiliarNoteIdentities[0]:
  60.         primeNoteCount +=1
  61.         if primeNoteCount == primeNoteGoal:
  62.             primeNoteCount = 0
  63.             del currentFamiliarNoteIdentities[-1]
  64.             currentFamiliarNoteIdentities.insert(0, newNoteIdentity())
  65.     if currentNoteIdentity[0] == "top":
  66.         currentClefTop = currentNoteIdentity[1]
  67.         noteTop = currentNoteIdentity[2]
  68.         noteBottom = "r8"
  69.     else:
  70.         currentClefBottom = currentNoteIdentity[1]
  71.         noteTop = "r8"
  72.         noteBottom = currentNoteIdentity[2]
  73.     return([[currentClefTop, noteTop], [currentClefBottom, noteBottom]])
  74.  
  75. def concludedBarIdentity():
  76.     global currentKeySignatureSharpiness
  77.     global previousKeySignatureSharpiness
  78.     global barCount
  79.     global lineCount
  80.     previousKeySignatureSharpiness = currentKeySignatureSharpiness
  81.     currentKeySignatureSharpiness = random.choices([currentKeySignatureSharpiness, random.choices(keySignatureSharpinesses, weights=keySignatureProbabilitiesBySharpiness)[0]], weights=oldKeyNewKeyProbability)[0]
  82.     keySignature = "\key " + keySignatureTonicsBySharpiness[currentKeySignatureSharpiness] + " \dorian "
  83.     if currentKeySignatureSharpiness == previousKeySignatureSharpiness:
  84.         explicitKeySignature = ""
  85.     else:
  86.         explicitKeySignature = keySignature
  87.     barMap = [concludedNoteIdentity(), concludedNoteIdentity(), concludedNoteIdentity(), concludedNoteIdentity(), concludedNoteIdentity()]
  88.     if barMap[0][1][1] == "r8":
  89.         if barMap[1][1][1] == "r8":
  90.             beat1 = [explicitKeySignature + barMap[0][0][0] + barMap[0][0][1] + "[ " + barMap[1][0][0] + barMap[1][0][1] + "]", explicitKeySignature + barMap[0][1][0] + "r4"]
  91.         else:
  92.             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]]
  93.     else:
  94.         if barMap[1][1][1] == "r8":
  95.             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]]
  96.         else:
  97.             beat1 = [explicitKeySignature + barMap[0][0][0] + "r4", explicitKeySignature + barMap[0][1][0] + barMap[0][1][1] + "[ " + barMap[1][1][0] + barMap[1][1][1] + "]"]
  98.     if barMap[2][1][1] == "r8":
  99.         if barMap[3][1][1] == "r8":
  100.             if barMap[4][1][1] == "r8":
  101.                 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."]
  102.             else:
  103.                 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]]
  104.         else:
  105.             if barMap[4][1][1] == "r8":
  106.                 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]]
  107.             else:
  108.                 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] + "]"]
  109.     else:
  110.         if barMap[3][1][1] == "r8":
  111.             if barMap[4][1][1] == "r8":
  112.                 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"]
  113.             else:
  114.                 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] + "]"]
  115.         else:
  116.             if barMap[4][1][1] == "r8":
  117.                 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"]
  118.             else:
  119.                 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] + "]"]
  120.     currentBarTop = beat1[0] + " " + beat2[0]
  121.     currentBarBottom = beat1[1] + " " + beat2[1]
  122.     if barMap[0][0][1] == barMap[1][0][1] == barMap[2][0][1] == barMap[3][0][1] == barMap[4][0][1] == "r8":
  123.         currentBarTop = explicitKeySignature + barMap[0][0][0] + " R8*5"
  124.     if barMap[0][1][1] == barMap[1][1][1] == barMap[2][1][1] == barMap[3][1][1] == barMap[4][1][1] == "r8":
  125.         currentBarBottom = explicitKeySignature + barMap[0][1][0] + " R8*5"
  126.     barCount +=1
  127.     if barCount == barsPerLine:
  128.         lineCount +=1
  129.         barCount = 0
  130.         if lineCount == linesPerPage:
  131.             lineCount = 0
  132.             barCount = 0
  133.             breakCommand = " \pageBreak"
  134.         else:
  135.             breakCommand = " \\break \\noPageBreak"
  136.     else:
  137.         breakCommand = " \\noBreak"
  138.     return([keySignature, explicitKeySignature, [barMap[0][0][0], barMap[0][1][0]], [currentBarTop, currentBarBottom], breakCommand])
  139.  
  140. def currentPage():
  141.     global pageCount
  142.     global currentBarIdentity
  143.     pageCount = pageCount + 1
  144.     if pageCount == 1:
  145.         subsequentPageOverride = ""
  146.     else:
  147.         subsequentPageOverride = "\once \override Staff.TimeSignature #'stencil = ##f "
  148.     pageInProgress = [subsequentPageOverride + currentBarIdentity[0] + currentBarIdentity[3][0] + currentBarIdentity[4], subsequentPageOverride + currentBarIdentity[0] + currentBarIdentity[3][1] + currentBarIdentity[4]]
  149.     if pageCount == totalPages:
  150.         for repeater in range(1, barsPerLine * linesPerPage):
  151.             currentBarIdentity = concludedBarIdentity()
  152.             pageInProgress[0] += currentBarIdentity[1] + currentBarIdentity[3][0] + currentBarIdentity[4]
  153.             pageInProgress[1] += currentBarIdentity[1] + currentBarIdentity[3][1] + currentBarIdentity[4]
  154.         pageInProgress[0] += ' \\bar "|." r8'
  155.         pageInProgress[1] += ' \\bar "|." r8'
  156.     else:
  157.         for repeater in range(0, barsPerLine * linesPerPage):
  158.             currentBarIdentity = concludedBarIdentity()
  159.             pageInProgress[0] += currentBarIdentity[1] + currentBarIdentity[3][0] + currentBarIdentity[4]
  160.             pageInProgress[1] += currentBarIdentity[1] + currentBarIdentity[3][1] + currentBarIdentity[4]
  161.     return(pageInProgress)
  162.  
  163. def completePage():
  164.     pageInformation = currentPage()
  165.     emptyPage = """\\version "2.18.2"
  166.  
  167. #(set! paper-alist (cons '("res" . (cons (* %s) (* %s))) paper-alist))
  168. \paper { #(set-paper-size "res") }
  169.  
  170. \header {
  171.  copyright = "page %d of %d"
  172. }
  173.  
  174. \layout {
  175.  \context {
  176.    \Score
  177.    \\remove "Bar_number_engraver"
  178.  }
  179.  \context {
  180.    \Voice
  181.    \consists "Melody_engraver"
  182.    \override Stem #'neutral-direction = #'()
  183.  }
  184. }
  185.  
  186. \score {
  187.  \\new PianoStaff <<
  188.    \\new Staff {
  189.      \\autoBeamOff \\time 5/8
  190.      %s
  191.    }
  192.    \\new Staff {
  193.      \\autoBeamOff \\time 5/8
  194.      %s
  195.    }
  196.  >>
  197.  \layout {
  198.    \context { \Staff \override TimeSignature.style = #'numbered }
  199.    indent = #0
  200.  }
  201. }""" % (pageWidth, pageHeight, pageCount, totalPages, pageInformation[0], pageInformation[1])
  202.     pageNumberPadded = "{:0>4}".format(pageCount)
  203.     file = open("/tmp/PythonRandomNotes_Page%s.ly" % (pageNumberPadded), "w")
  204.     file.write(emptyPage)
  205.     file.close()
  206.     if makePdf == True:
  207.         pdf = "--pdf "
  208.     else:
  209.         pdf = ""
  210.     if makePng == True:
  211.         png = "--png "
  212.     else:
  213.         png = ""
  214.     os.system("lilypond -s %s%s-dresolution=%s -o /tmp/PythonRandomNotes_Page%s /tmp/PythonRandomNotes_Page%s.ly" % (pdf, png, dotsPerInch, pageNumberPadded, pageNumberPadded))
  215.     if makePdf == True:
  216.         os.system("pdftk /tmp/PythonRandomNotes_Page%s.pdf cat 1 output /tmp/PythonRandomNotes_Page%s-single.pdf" % (pageNumberPadded, pageNumberPadded))
  217.         os.system("rm /tmp/PythonRandomNotes_Page%s.pdf" % (pageNumberPadded))
  218.     if makePng == True:
  219.         os.system("mv /tmp/PythonRandomNotes_Page%s-page1.png /tmp/PythonRandomNotes_Page%s.png" % (pageNumberPadded, pageNumberPadded))
  220.         os.system("rm /tmp/PythonRandomNotes_Page%s-page2.png" % (pageNumberPadded))
  221.     os.system("rm /tmp/PythonRandomNotes_Page%s.ly" % (pageNumberPadded))
  222.     print("page %d of %d completed" % (pageCount, totalPages))
  223.  
  224. # just to define them to begin with
  225. currentKeySignatureSharpiness = random.choices(keySignatureSharpinesses, weights=keySignatureProbabilitiesBySharpiness)[0]
  226. previousKeySignatureSharpiness = ""
  227. currentClefTop = '\clef "treble" '
  228. currentClefBottom = '\clef "bass" '
  229. currentFamiliarNoteIdentities = [newNoteIdentity(), newNoteIdentity(), newNoteIdentity(), newNoteIdentity()]
  230. primeNoteCount = 0
  231. barCount = 0
  232. lineCount = 0
  233. pageCount = 0
  234. currentBarIdentity = concludedBarIdentity()
  235.  
  236. for repeater in range(0, totalPages):
  237.     completePage()
  238. if makePdf == True:
  239.     os.system("pdftk /tmp/PythonRandomNotes_Page*-single.pdf cat output /tmp/PythonRandomNotes.pdf")
  240.     os.system("rm /tmp/PythonRandomNotes_Page*-single.pdf")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement