Exemple #1
0
def get_notes_chords_list(note_strings: List[str],
                          offset_between: float) -> List[Union[Chord, Note]]:
    """
    :param note_strings: List of note strings 
    :param offset_between: offset between notes and chords 

    :return: List of music21 Chords and notes 
    """
    offset = 0
    output = []

    for pattern in note_strings:
        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.Piano()
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            output.append(new_chord)

        else:
            new_chord = note.Note(pattern)
            new_chord.offset = offset
            new_chord.storedInstrument = instrument.Piano()
            output.append(new_chord)

        offset += offset_between
    return output
Exemple #2
0
def decode_output(s):
    """Doing the opposite of the method `encode_score()`. Decodes `s`
    and builds a music21.stream.Stream with the model's predictions.
    The stream will have notes of fixed duration
    :param s: a list of classes (or pitches) that the model has predicted
    :return: a music21.stream.Stream stream of music21.note.Note notes
    """

    pos = 0  # position of the current note
    duration = 0.5  # duration of each note
    out = []

    # Decode notes to music21.note.Note object and give each 0.5 duration
    for x in s:
        if (' ' in x) or x.isdigit():  # x is a chord
            chord_notes = []
            for pitch_ in x.split(' '):
                note_ = note.Note(int(pitch_))
                note_.storedInstrument = instrument.Piano()
                chord_notes.append(note_)
            chord_ = chord.Chord(chord_notes)
            chord_.offset = pos
            out.append(chord_)
        else:  # x is a note
            note_ = note.Note(x)
            note_.offset = pos
            note_.storedInstrument = instrument.Piano()
            out.append(note_)
        pos += duration

    return stream.Stream(out)
Exemple #3
0
    def __init__(self):
        QMainWindow.__init__(self)
        self.resize(1300, 700)
        self.setFixedSize(self.size())
        self.setWindowTitle('PyChord')
        self.add_combo_boxes()
        self.add_buttons()
        self.sl = QSlider(Qt.Horizontal, self)
        self.volume = QLCDNumber(self)
        self.add_slider()

        self.actual_key = None
        self.actual_meter = 4
        self.actual_instrument = instrument.Piano()
        self.melody_stream = stream.Stream()
        self.harmonized_melody_stream = stream.Stream()
        self.reset_stream(self.melody_stream)
        self.reset_stream(self.harmonized_melody_stream)
        mixer.init(44100, -16, 2, 1024)

        self.tmpStream = stream.Stream()
        self.tmpStream.insert(instrument.Piano())
        self.lpc = lily.translate.LilypondConverter()
        self.lpc.context = lily.lilyObjects.LyMusicList()
        self.keyboard = Keyboard(self)
        self.melody = Melody(self, self.actual_key)
Exemple #4
0
def generate_mid(prediction_output, filename='generated.mid'):
    offset = 0
    output = []

    for element in prediction_output:
        # element is a chord
        if element.find('.') > -1 or element.isdigit():
            notes_in_chord = element.split('.')
            notes = []
            for current_note in notes_in_chord:
                new_note = note.Note(int(current_note))
                new_note.storedInstrument = instrument.Piano()
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            output.append(new_chord)
        # element is a note
        else:
            new_note = note.Note(element)
            new_note.storedInstrument = instrument.Piano()
            new_note.offset = offset
            output.append(new_note)
        offset += 0.5
    midi_stream = stream.Stream(output)
    midi_stream.write('midi', fp='./generated/' + filename)
    def __create_midi(self, prediction_output, output_file_name):

        offset = 0
        output_notes = []

        for pattern in prediction_output:

            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.Piano()
                    notes.append(new_note)

                new_chord = chord.Chord(notes)
                new_chord.offset = offset
                output_notes.append(new_chord)

            else:

                new_note = note.Note(pattern)
                new_note.offset = offset
                new_note.storedInstrument = instrument.Piano()
                output_notes.append(new_note)

            offset += 0.5

        midi_stream = stream.Stream(output_notes)
        midi_stream.write('midi', fp=output_file_name)
def generateMidiFrom(output):
    offSet = 0
    outputPitches = []
    
    for pattern in output:
        #NOTE:- Chords
        if ('.' in pattern) or pattern.isdigit():
            chordTones = pattern.split('.')
            pitches = []
            for tone in chordTones:
                pitch = note.Note(int(tone))
                pitch.storedInstrument = instrument.Piano()
                pitches.append(pitch)
            resultingChord = chord.Chord(pitches)
            resultingChord.offset = offSet
            outputPitches.append(resultingChord)
        #NOTE:- Pitches
        else:
            pitch = note.Note(pattern)
            pitch.offset = offSet
            pitch.storedInstrument = instrument.Piano()
            outputPitches.append(pitch)
        
        #NOTE:- offSet will change
        offSet += 0.5
    
    midiStream = stream.Stream(outputPitches)
    midiStream.write('midi', fp='output/composition.mid')
Exemple #7
0
def sequenceToMidi(prediction_output, artist):
    offset = 0
    output_notes = []

    for pattern in prediction_output:
        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.Piano()
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            output_notes.append(new_chord)
        else:
            new_note = note.Note(pattern)
            new_note.offset = offset
            new_note.storedInstrument = instrument.Piano()
            output_notes.append(new_note)

        offset += 0.5

    midi_stream = stream.Stream(output_notes)

    now = datetime.now()
    midiPath = '/static/ai/output/' + artist + " " + now.strftime(
        "%d-%m-%Y %H%M%S") + '.mid'
    midi_stream.write('midi', fp='app' + midiPath)
    return midiPath
Exemple #8
0
def create_midi(prediction_output):
    """
    Take list of ints from output of model, convert to notes and save to mid file.
    """
    offset = 0
    output_notes = []

    # make a note / chord based on ints from output
    for pattern in prediction_output:
        if ('.' in pattern) or pattern.isdigit():
            # parse chord
            notes_in_chord = pattern.split('.')
            notes = []
            for current_note in notes_in_chord:
                new_note = note.Note(int(current_note))
                new_note.storedInstrument = instrument.Piano()
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            output_notes.append(new_chord)
        else:
            # parse note
            new_note = note.Note(pattern)
            new_note.offset = offset
            new_note.storedInstrument = instrument.Piano()
            output_notes.append(new_note)

        # increment offset so notes don't stack
        offset += 0.5

    midi_stream = stream.Stream(output_notes)
    # save midi to a file
    midi_stream.write('midi', fp='GRU_test_output.mid')
Exemple #9
0
def createMidi(predictionOutput):
    # function to write the prediction output to a MIDI file
    offset = 0
    outputNotes = []

    for pattern in predictionOutput:
        if ('.' in pattern) or pattern.isdigit():
            notesInChord = pattern.split('.')
            notes = []
            for currentNote in notesInChord:
                newNote = note.Note(int(currentNote))
                newNote.storedInstrument = instrument.Piano()
                notes.append(newNote)
            newChord = chord.Chord(notes)
            newChord.offset = offset
            outputNotes.append(newChord)
        else:
            newNote = note.Note(pattern)
            newNote.offset = offset
            newNote.storedInstrument = instrument.Piano()
            outputNotes.append(newNote)

        offset += 0.5

    midiStream = stream.Stream(outputNotes)

    midiStream.write('midi', fp='test_output.mid')
    def save_midi_output(self, out, midi_out_file):
        offset = 0
        output_notes = []
        for _note in out:

            if ('.' in _note) or _note.isdigit():
                notes_in_chord = _note.split('.')
                notes = []
                for current_note in notes_in_chord:
                    new_note = note.Note(int(current_note))
                    new_note.storedInstrument = instrument.Piano()
                    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(_note)
                new_note.offset = offset
                new_note.storedInstrument = instrument.Piano()
                output_notes.append(new_note)

            offset += 0.5
        midi_stream = stream.Stream(output_notes)
        try:
            midi_stream.write('midi', fp=midi_out_file)
        except:
            print("ERROR: Could not write to MIDI File.")
def create_midi(prediction_output):

    offset = 0
    output_notes = []
    prediction_notes = []
    prediction_durations = []
    for i in range(len(prediction_output)):
        prediction_notes.append(prediction_output[i][0])
        prediction_durations.append(prediction_output[i][1])

    for i in range(len(prediction_notes)):

        if ('.' in prediction_notes[i]) or prediction_notes[i].isdigit():
            notes_in_chord = prediction_notes[i].split('.')
            notes = []
            for current_note in notes_in_chord:
                new_note = note.Note(int(current_note), quarterLength=float(prediction_durations[i]))
                new_note.storedInstrument = instrument.Piano()
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            output_notes.append(new_chord)
        else:
            new_note = note.Note(prediction_notes[i], quarterLength=float(prediction_durations[i]))
            new_note.offset = offset
            new_note.storedInstrument = instrument.Piano()
            output_notes.append(new_note)

        offset += prediction_durations[i]
    midi_stream = stream.Stream(output_notes)
    midi_stream.write('midi', fp='experiment/created_song.mid')
Exemple #12
0
def create_midi(outputs, filepath):
    """ convert the output to notes and create a midi file from the notes """
    offset = 0
    output_notes = []

    # create note and chord objects
    for output in outputs:
        # output is a chord
        if ('.' in output) or output.isdigit():
            notes_in_chord = output.split('.')
            notes = []
            for current_note in notes_in_chord:
                new_note = note.Note(int(current_note))
                new_note.storedInstrument = instrument.Piano()
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            output_notes.append(new_chord)
        # output is a note
        else:
            new_note = note.Note(output)
            new_note.offset = offset
            new_note.storedInstrument = instrument.Piano()
            output_notes.append(new_note)

        # increase offset each iteration so that notes do not stack
        offset += 0.5  #random.uniform(0.1,1.0)

    print(output_notes)
    midi_stream = stream.Stream(output_notes)

    midi_stream.write('midi', fp=filepath)
Exemple #13
0
def create_midi(prediction_output):
    """ convert the output from the prediction to notes and create a midi file
        from the notes """
    offset = 0
    output_notes = []

    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.Piano()
                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.Piano()
            output_notes.append(new_note)

        offset += 0.5

    midi_stream = stream.Stream(output_notes)

    midi_stream.write('midi', fp='lstm_outputs/test_output.mid')
def create_midi(pred_note, pred_dur, filename):
    """ convert 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 i in range(len(pred_note)):
        # pattern is a chord
        pattern = pred_note[i]
        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.Piano()
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            new_chord.duration.quarterLength = pred_dur[i]
            output_notes.append(new_chord)
        # pattern is a note
        else:
            new_note = note.Note(pattern)
            new_note.offset = offset
            new_note.storedInstrument = instrument.Piano()
            new_note.duration.quarterLength = pred_dur[i]
            output_notes.append(new_note)

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

    midi_stream = stream.Stream(output_notes)
    print('Saving Output file as midi....')
    midi_stream.write('midi', fp=filename)
def create_midi(notes_list, filename):
    """ Convert a list of notes into a MIDI file and save to .mid file"""
    offset = 0
    output_notes = []

    # create note and chord objects based on the values generated by the model
    for pattern in notes_list:
        # 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.Piano()
                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.Piano()
            output_notes.append(new_note)

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

    midi_stream = stream.Stream(output_notes)

    midi_stream.write("midi", fp=filename + ".mid")
def createMidiFile(newNotes):
    offset = 0
    midOutput = []

    # loop through output and convert to midi objects
    for prediction in newNotes:
        # if chord
        if ("." in prediction) or prediction.isdigit():
            # getting notes in chord
            chordNotes = []
            for n in prediction.split("."):
                noteObj = note.Note(int(n))
                noteObj.storedInstrument = instrument.Piano()
                chordNotes.append(noteObj)

            # creating new chord object, appended to the output
            chordObj = chord.Chord(chordNotes)
            chordObj.offset = offset
            midOutput.append(chordObj)
        # if note
        else:
            # creating new note object, appended to the output
            noteObj = note.Note(prediction)
            noteObj.offset = offset
            noteObj.storedInstrument = instrument.Piano()
            midOutput.append(noteObj)

        offset += 0.5

    # outputting to midi file
    midStream = stream.Stream(midOutput)
    midStream.write("midi", fp=outputFileName)
    print "file written out"
Exemple #17
0
def create_music(prediction):
    """
    用神经网络'预测'的音乐数据来生成 MIDI 文件,再转成 MP3 文件
    """
    offset = 0  # 偏移
    output_notes = []

    # 生成 Note(音符)或 Chord(和弦)对象
    for data in prediction:
        # 是 Chord。格式例如: 4.15.7
        if ('.' in data) or data.isdigit():
            notes_in_chord = data.split('.')
            notes = []
            for current_note in notes_in_chord:
                new_note = note.Note(int(current_note))
                new_note.storedInstrument = instrument.Piano()  # 乐器用钢琴 (piano)
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            output_notes.append(new_chord)
        # 是 Note
        else:
            new_note = note.Note(data)
            new_note.offset = offset
            new_note.storedInstrument = instrument.Piano()
            output_notes.append(new_note)

        # 每次迭代都将偏移增加,这样才不会交叠覆盖
        offset += 0.5

    # 创建音乐流(Stream)
    midi_stream = stream.Stream(output_notes)

    # 写入 MIDI 文件
    midi_stream.write('midi', fp='music/output.mid')
Exemple #18
0
def save_midi(pred_output):
    """ Save midi file with the composotion created """
    offset = 0
    output_notes = []

    for pattern in pred_output:
        # 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.Piano()
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            output_notes.append(new_chord)

        # Note
        else:
            new_note = note.Note(pattern)
            new_note.offset = offset
            new_note.storedInstrument = instrument.Piano()
            output_notes.append(new_note)

        # Increase offset with each iter
        offset += 0.5

    midi_stream = stream.Stream(output_notes)
    midi_stream.write('midi', fp='test_output.mid')
Exemple #19
0
 def convert_to_midi(self, notes):
     # given array of notes, convert to notes and then to midi
     B, S = notes.shape
     for i in range(B):
         # parse a sequence
         melody = []
         offset = 0
         for s in range(S):
             # Something like A#
             token = self.revvocab[notes[i, s]]
             if '.' in token or token.isdigit():
                 new_note = token.split('.')
                 new_note = [note.Note(int(x)) for x in new_note]
                 for _ in range(len(new_note)):
                     new_note[_].storedInstrument = instrument.Piano()
                 new_chord = chord.Chord(new_note)
                 new_chord.offset = offset
                 melody.append(new_chord)
             else:
                 # note
                 new_note = note.Note(token)
                 new_note.offset = offset
                 new_note.storedInstrument = instrument.Piano()
                 melody.append(new_note)
             offset += 0.5
         # Parsed entire sequence, save it
         midistream = stream.Stream(melody)
         filename = self.files[i].split('/')[-1]
         filename = filename.replace('.mid', '_output.mid')
         midistream.write('midi', filename)
         print("Written to {}".format(filename))
Exemple #20
0
def create_music(prediction, tag):
    offset = 0
    output_notes = []

    for data in prediction:
        if ('.' in data) or data.isdigit():
            notes_in_chord = data.split('.')
            notes = []
            for current_note in notes_in_chord:
                new_note = note.Note(int(current_note))
                new_note.storedInstrument = instrument.Piano()
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            output_notes.append(new_chord)
        else:
            new_note = note.Note(data)
            new_note.offset = offset
            new_note.storedInstrument = instrument.Piano()
            output_notes.append(new_note)

        offset += 0.5

    midi_stream = stream.Stream(output_notes)

    midi_stream.write('midi', fp='output_{}.mid'.format(tag))
Exemple #21
0
def convertAndSaveMidi(predictions, file_save_to):
    output_notes = []
    offset = 0

    for pattern in predictions:
        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.Piano()
                notes.append(new_note)

            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            output_notes.append(new_chord)

        else:
            new_note = note.Note(pattern)
            new_note.offset = offset
            new_note.storedInstrument = instrument.Piano()
            output_notes.append(new_note)
        offset = offset + 0.5

    midi_stream = stream.Stream(output_notes)
    midi_stream.write('midi', fp=file_save_to)
Exemple #22
0
def gen_midi(prediction_output, file_name):
    '''take model output and generate playable midi'''
    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.Piano()
                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.Piano()
            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=f'./playlist/{file_name}.mid')
Exemple #23
0
def test():
    from music21 import instrument as j
    sc1 = stream.Score()
    #    instruments = [Piccolo(), Glockenspiel(), 72, 69, 41, 27, 47, 1, 1, 1, 1, 34]
    instrument = [
        j.Piccolo(),
        j.Xylophone(),
        j.Clarinet(),
        j.Oboe(),
        j.Violin(),
        j.ElectricGuitar(),
        j.Harp(),
        j.Piano(),
        j.Piano(),
        j.Piano(),
        j.Piano(),
        j.ElectricBass()
    ]
    instrumentOctave = [3, 2, 2, 2, 1, 1, 1, 2, 1, 0, -1, -2]

    for i in range(12):
        inst = instrument[i]
        if i < 9:
            inst.midiChannel = i
        else:
            inst.midiChannel = i + 1
        part = addPart(instrument=inst)
        if instrumentOctave[i] != 0:
            part.transpose(12 * instrumentOctave[i], inPlace=True)
        sc1.insert(0, part)
    sc1.show()
def create_midi(prediction_output):
    offset = 0
    output_notes = []
    for pattern in prediction_output:
        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.Piano()
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            output_notes.append(new_chord)
        else:
            new_note = note.Note(pattern)
            new_note.offset = offset
            new_note.storedInstrument = instrument.Piano()
            output_notes.append(new_note)
        offset += 0.5

    midi_stream = stream.Stream(output_notes)

    print('Saving Output file as midi....')

    midi_stream.write('midi', fp='test_output.mid')
def create_midi(prediction_output):

    offset = 0
    output_notes = []

    for pattern in prediction_output:

        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.Piano()
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            output_notes.append(new_chord)

        else:
            new_note = note.Note(pattern)
            new_note.offset = offset
            new_note.storedInstrument = instrument.Piano()
            output_notes.append(new_note)

        offset += 0.5

    midi_stream = stream.Stream(output_notes)

    number = random.randint(1, 1000000)

    path = "static/" + str(number) + 'random.mid'

    midi_stream.write('midi', fp=path)

    return path
    def make_midi_file(sequence):
        """Creates a midi file for the given sequence.
        """
        output_folder = Path(
            __file__
        ).parent / 'data' / main.load_files_window.model_name_string / 'output'
        output_folder.mkdir(parents=True, exist_ok=True)
        midi_stream = stream.Stream()

        for pattern in sequence:
            note_pattern, duration_pattern = pattern
            if '.' in note_pattern:
                notes_in_chord = note_pattern.split('.')
                chord_notes = []
                for current_note in notes_in_chord:
                    new_note = note.Note(current_note)
                    new_note.duration = duration.Duration(duration_pattern)
                    new_note.storedInstrument = instrument.Piano()
                    chord_notes.append(new_note)
                new_chord = chord.Chord(chord_notes)
                midi_stream.append(new_chord)
            elif note_pattern == 'rest':
                new_note = note.Rest()
                new_note.duration = duration.Duration(duration_pattern)
                new_note.storedInstrument = instrument.Piano()
                midi_stream.append(new_note)
            elif note_pattern != 'START':
                new_note = note.Note(note_pattern)
                new_note.duration = duration.Duration(duration_pattern)
                new_note.storedInstrument = instrument.Piano()
                midi_stream.append(new_note)

        midi_stream = midi_stream.chordify()
        file_name_string = f'output-{main.load_files_window.model_name_string}-{time.strftime("%H%M%S")}.mid'
        midi_stream.write('midi', fp=str(output_folder / file_name_string))
def create_midi(predict_output, filename):

    offset = 0
    output_notes = []

    for item in predict_output:
        pattern = item[0]
        #pattern is 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.Piano()
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            output_notes.append(new_chord)
        #pattern is note
        else:
            new_note = note.Note(pattern)
            new_note.offset = offset
            new_note.storedInstrument = instrument.Piano()
            output_notes.append(new_note)

        offset += 0.5

    midi_stream = stream.Stream(output_notes)

    midi_stream.write('midi', fp='test_output.mid')
Exemple #28
0
def convert_to_midi(prediction_output):
    from music21 import stream
    #     from midi2audio import FluidSynth
    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:
                cn = int(current_note)
                new_note = note.Note(cn)
                new_note.storedInstrument = instrument.Piano()
                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.Piano()  # Piano
            output_notes.append(new_note)

        # increase offset each iteration so that notes do not stack
        offset += 1
    midi_stream = stream.Stream(output_notes)
    midi_stream.write('midi', fp='music_206.mid')
def create_midi(prediction_output):
    """ convert 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.Piano()
                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.Piano()
            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_output.mid')
Exemple #30
0
def create_music(prediction):  # 生成音乐函数,训练不用
    """ 用神经网络预测的音乐数据来生成mid文件 """
    offset = 0  # 偏移,防止数据覆盖
    output_notes = []
    # 生成Note或chord对象
    for data in prediction:
        # 如果是chord格式:45.21.78
        if ('.' in data) or data.isdigit():  # data中有.或者有数字
            note_in_chord = data.split('.')  # 用.分隔和弦中的每个音
            notes = []  # notes列表接收单音
            for current_note in note_in_chord:
                new_note = note.Note(int(current_note))  # 把当前音符化成整数,在对应midi_number转换成note
                new_note.storedInstrument = instrument.Piano()  # 乐器用钢琴
                notes.append(new_note)
            new_chord = chord.Chord(notes)  # 再把notes中的音化成新的和弦
            new_chord.offset = offset  # 初试定的偏移给和弦的偏移
            output_notes.append(new_chord)  # 把转化好的和弦传到output_notes中
        # 是note格式:
        else:
            new_note = note.Note(data)  # note直接可以把data变成新的note
            new_note.offset = offset
            new_note.storedInstrument = instrument.Piano()  # 乐器用钢琴
            output_notes.append(new_note)  # 把new_note传到output_notes中
        # 每次迭代都将偏移增加,防止交叠覆盖
        offset += 0.5

    # 创建音乐流(stream)
    midi_stream = stream.Stream(output_notes)  # 把上面的循环输出结果传到流

    # 写入midi文件
    midi_stream.write('midi', fp='output2.mid')  # 最终输出的文件名是output.mid,格式是mid