Beispiel #1
0
def create_midi(prediction_output):
    
    """ Converts the output from the prediction to notes and create a midi file
        from the notes """
    
    offset = 0
    output_notes = []

    # create note and chord objects based on the values generated by the model
    for pattern in prediction_output:
        # pattern is a chord
        if ('.' in pattern) or pattern.isdigit():
            notes_in_chord = pattern.split('.')
            notes = []
            for current_note in notes_in_chord:
                new_note = note.Note(int(current_note))
                new_note.storedInstrument = instrument.SnareDrum()
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            output_notes.append(new_chord)
        # pattern is a note
        else:
            new_note = note.Note(pattern)
            new_note.offset = offset
            new_note.storedInstrument = instrument.SnareDrum()
            output_notes.append(new_note)

        # increase offset each iteration so that notes do not stack
        offset += 0.5

    midi_stream = stream.Stream(output_notes)

    midi_stream.write('midi', fp='test_output2.mid')
Beispiel #2
0
def predict_and_create_midi(model, X, unique_info, unique_size):

    int_to_note = dict((number, note) for number, note in enumerate(unique_info))
    Y_pred = [] 

    random = np.random.randint(0, len(X) - 1)
    seq = X[random] #sequence of music information
    
    for number in range(300):
        X_pred = np.reshape(seq, (1, len(seq), 1))
        X_pred = X_pred / float(unique_size)
        pred = model.predict(X_pred, verbose=0)
        best = np.argmax(pred) #taking the  muisic info with the highest probability  
        best_info = int_to_note[best]  # un-encoding
        Y_pred.append(best_info)
        seq.append(best)
        seq = seq[1:len(seq)] #create the next sequence

    song = []
    offset= 0.5
    # create note and chord objects based on the values generated by the model
    for info in predictions:
        # info=chord
        if ('.' in info) or info.isdigit():
            chord_notes = info.split('.')
            notes = []
            for current in chord_notes:
                new_note = note.Note(int(current))
                new_note.offset = offset
                new_note.storedInstrument = instrument.SnareDrum()
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            song.append(new_chord)
        # info = Rest
        elif ('Re' in info):
            new_rest= note.Rest()
            new_rest.offset = offset
            new_rest.quarterLength = float(info[1:])
            new_rest.storedInstrument = instrument.SnareDrum()
            song.append(new_rest)
        # info = note
        else:
            new_note = note.Note(info)
            new_note.offset = offset
            new_note.storedInstrument = instrument.SnareDrum()
            song.append(new_note)
        offset += 0.5
    midi = stream.Stream(song)
    midi.write('midi', fp='C:/Users/PycharmProjects/AI-music/generate-music/generated_song.mid')
Beispiel #3
0
def create_midi(pred_note, pred_duration, name='out.mid'):
	offset = 0
	output_notes = []

	for i in range(0,len(pred_note)):
		d = duration.Duration()
		d.quarterLength = pred_duration[i]

		new_note = note.Note(pred_note[i])
		new_note.duration = d
		new_note.offset = offset
		new_note.storedInstrument = instrument.SnareDrum()
		output_notes.append(new_note)

		offset += pred_duration[i]

	midi_stream = stream.Stream(output_notes)
	midi_stream.write('midi', fp=name)