# To obtain the predicted response, use .predict():
print("\n\n")
print("Tempory solution voor X_new")
X_new = X  # ToDo: create better way to fill X_new
Y_pred = model.predict(X_new)
print('\n\nPredicted response in numeric values Y_pred:', Y_pred, sep='\n')

print("\n\n")
print("Estimated output in notes (Notename, octave and quarterLength):")
for r in Y_pred:

    # toDo round to a 0.25 resolution because input used 1/4 notes
    t = r[2]
    BASE = 0.25
    v = t + (BASE - t) % BASE
    print(nc.getNoteName(int(round(r[0])), enharmonic=False), int(round(r[1])),
          v)

# *** Create the score with estimated notes ***
# *********************************************
# https://web.mit.edu/music21/doc/usersGuide/usersGuide_06_stream2.html
estimatedScore = m.stream.Stream()
timeSignature = m.meter.TimeSignature(TIME_SIGNATURE_STRING)
upperStaffClef = m.clef.TrebleClef()
lowerStaffClef = m.clef.BassClef()

myPart = m.stream.Part()
myPart_UpperStaff = m.stream.Part()
# set Clef UpperStaff
myPart_UpperStaff.append(upperStaffClef)
# set TimeSignature UpperStaff
Example #2
0
# Export de MuseScore File in musicxml (uncompressed music xml format musicxml extention)
museScoreFile = "C_major_scale_ascending.musicxml"  # in musicxml uncompressed
museScoreFile2 = "F_major_scale_ascending_8th_notes.musicxml"  # in musicxml uncompressed
museScoreFile3 = "C_major_scale_ascending_mixed_duration.musicxml"  # in musicxml uncompressed
keyC = m.key.Key('C')  #  lowercase = c minor. uppercase = C major
keyCmin = m.key.Key('c')  #  lowercase = c minor. uppercase = C major

# Test conversion Note to noteValue
# print("noteValues['C#']",nc.getNoteValue('C#'))
# print("noteValues['c#']",nc.getNoteValue('c#'))
#ns='d#'
#print("noteValues['"+ns+"']",nc.getNoteValue(ns))
#print("\n\n")
# Test conversion noteValue to Note
for v in range(0, 12, 1):
    n = nc.getNoteName(v, enharmonic=False)
    print(v, n)
    n = nc.getNoteName(v, enharmonic=True)
    print(v, n)

# See: https://web.mit.edu/music21/doc/usersGuide/usersGuide_24_environment.html#usersguide-24-environment
# See: https://web.mit.edu/music21/doc/usersGuide/usersGuide_24_environment.html
env = m.environment.UserSettings()
env.delete()
env.create()
# set environmment
env['autoDownload'] = 'allow'
#env['lilypondPath'] = '/usr/bin/lilypond'
#env['musescoreDirectPNGPath'] = '/usr/bin/musescore3'
env['musicxmlPath'] = '/home/claude/Applications/' + musescoreProg
Example #3
0
def create_estimated_score(X_new, Y_pred, base, time_signature_input_file,
                           key_signature_input_file, score_title, composer):
    """
    Create a music Score based on predicted music information

    Params
    X_new                       a numpy array with measure and offset information in a measure
    Y_pred                      a numpy array with note values, notevalue, octavenumber and quarternoteduration
    base                        a fraction to with noteduration a quarternoteduration has to rounded to (base is 0.25 for quarter note, 0,125 for a 8th note, etc)
    time_signature_input_file   a time signature object with time signaure information of the input file
    key_signature_input_file
    score_title                 a string which contains the score title meta data
    composer                    a string which contains the composer meta data
    """

    # Debug info:
    #print("\n\n")
    #print("Estimated output in notes (Notename, octave and quarterLength):")
    #for r in Y_pred:
    #
    #    # round to a 0.25 resolution because input used 1/4 notes
    #    t=r[2]
    #    BASE=0.25
    #    v=t + (BASE - t) % BASE
    #    print(nc.getNoteName(int(round(r[0])), enharmonic=False),int(round(r[1])), v)

    # *** Create the score with estimated notes ***
    # *********************************************
    # https://web.mit.edu/music21/doc/usersGuide/usersGuide_06_stream2.html
    estimatedScore = m.stream.Stream()

    # Set Meta Data in estimated score
    meta_data = m.metadata.Metadata()
    meta_data.title = score_title
    today = date.today()
    # YYYY/mm/dd
    d1 = today.strftime("%d/%m/%Y")
    meta_data.date = str(d1)
    meta_data.composer = composer + " (" + str(d1) + ")"

    upperStaffClef = m.clef.TrebleClef()
    lowerStaffClef = m.clef.BassClef()

    myPart = m.stream.Part()
    myPart_UpperStaff = m.stream.Part()
    # set Clef UpperStaff
    myPart_UpperStaff.append(upperStaffClef)

    # set TimeSignature UpperStaff
    myPart_UpperStaff.append(time_signature_input_file)

    # set keySignature UpperStaff
    myPart_UpperStaff.append(key_signature_input_file)

    myPart_LowerStaff = m.stream.Part()
    # set Clef UpperStaff
    myPart_LowerStaff.append(lowerStaffClef)

    # set TimeSignature LowerStaff
    myPart_LowerStaff.append(time_signature_input_file)

    # set keySignature LowerStaff
    myPart_LowerStaff.append(key_signature_input_file)

    # Do not use a Measure object
    # If you use a Time Signature object without a Measure object
    # when adding a notes, to a stream, measures are filled
    # automatically bases on note lengths
    myNote = m.note.Note()

    myPart_UpperStaff.partName = "Piano Upper"
    myPart_LowerStaff.partName = "Piano Lower"

    print("\n\n")
    print("process measures")
    itrNote = m.note.Note()
    if (X_new.shape[0] == Y_pred.shape[0]):
        # Normal Score
        cnt = 0  # counter to sync X and Y (sync time and Notes)
        curMeasure = 1
        noteCount = 0
        for e in X_new:

            # Decoding Y_pred: get note properies
            # Do the encoding as inverse of the decoding (see above)
            note_properties = Y_pred[cnt]
            #print("!!! note_properties[", cnt, "]", note_properties)
            curNoteName = nc.getNoteName(int(round(note_properties[0])),
                                         enharmonic=False)
            print("curNoteName", curNoteName)
            curNoteOctave = int(round(note_properties[1]))
            #print("curNoteOctave", curNoteOctave)

            # Process quarterDuration
            curNotequarterDuration = roundTo(note_properties[2], base)

            itrMeasure = int(e[0])
            itrOffset = e[1]
            print("ToDo itrMeasure=", itrMeasure, "itrOffset:", itrOffset)

            myNote = m.note.Note(
                name=curNoteName,
                quarterLength=curNotequarterDuration,
                octave=curNoteOctave,
                offset=itrOffset
                #,type="quarter"  # use quarterLength or type not both
            )

            # if you use a time signature object without a measure object then because of
            # the time signature measures are filled automatically by notes based on
            # its note duration
            myPart_UpperStaff.insert(cnt, myNote)
            noteCount = noteCount + 1
            cnt = cnt + 1
            print("cnt:", cnt)
    else:
        # Unbalanced Score
        print("Program error: Score not balanced")

    estimatedScore.insert(0, meta_data)
    estimatedScore.insert(1, myPart_UpperStaff)

    # *** Add a dummy LowerStaff with a dummy rest to create a Grand piano staff ***
    # When more staves are used, all staves
    # must be filled, before append to the total stream,
    # otherwise you get a corrupted stream when
    # empty staves are added to the total stream.
    dummyRest = m.note.Rest()
    dummyRest.duration.type = 'quarter'
    myPart_LowerStaff.insert(cnt, dummyRest)
    # If you do not want a grand staff comment statement below
    # ToDo problem with creating lowerStaff !!!!!!!!!!!!!!!!!!!!!!
    ##estimatedScore.insert(2, myPart_LowerStaff)

    return (estimatedScore)  # create_estimated_score