示例#1
0
    def save_midi(self, midi_file_path, onehot_mat, ticksPerQuarterNote=1024):
        """

        :param midi_file_path: path to save midi file which is made after predicting
        :param onehot_mat: matrix having notes infomation made after predicting
        :param ticksPerQuarterNote: criterion of note`s length(duration)
        :return:
        """
        # tick step = 8th note, quaver
        tick_step = ticksPerQuarterNote / 2

        # extract note info from one-hot matrix
        result = []
        for i in range(onehot_mat.shape[0]):
            for j in range(onehot_mat.shape[1]):
                # if flow is started with new note info, then add new info to result list
                if i == 0:
                    if onehot_mat[i, j] == 1:
                        result.append({'time': i * tick_step, 'note': j})
                else:
                    if onehot_mat[i, j] == 1 and onehot_mat[i - 1, j] == 0:
                        result.append({'time': i * tick_step, 'note': j})

        # manufacture result list with unroll package`s Keystrike
        ks = KeyStrikes(result)
        # convert ks to have duration
        ks = ks.quantized(ticksPerQuarterNote)
        # convert ks to music21`s stream
        s = ks._to_music21stream()
        # convert s to midi format, again T^T
        mf = music21.midi.translate.streamToMidiFile(s)
        # save mf info with file
        mf.open(midi_file_path, 'wb')
        mf.write()
        mf.close()

        print '\tMidi file(=%s) successfully saved. Total 1/8 beats = %d' % (
            midi_file_path, onehot_mat.shape[0])
示例#2
0
    def save_midi(self, midi_file_path, onehot_mat, ticksPerQuarterNote=1024):
        """

        :param midi_file_path: predict 후 만들어진 midi file 을 저장할 path
        :param onehot_mat: predict 결과 만들어진 notes 정보를 가진 matrix
        :param ticksPerQuarterNote: note 의 길이 기준
        :return:
        """
        # 틱 간격은 8분의 1음표 단위로
        tick_step = ticksPerQuarterNote / 2

        # onehot matrix에서 note 정보 추출.
        result = []
        for i in range(onehot_mat.shape[0]):
            for j in range(onehot_mat.shape[1]):
                # 새로운 노트가 시작될 경우에 리스트에 추가.
                if i == 0:
                    if onehot_mat[i, j] == 1:
                        result.append({'time': i * tick_step, 'note': j})
                else:
                    if onehot_mat[i, j] == 1 and onehot_mat[i - 1, j] == 0:
                        result.append({'time': i * tick_step, 'note': j})

        # unroll 패키지의 Keystrike를 이용해서 가공.
        ks = KeyStrikes(result)
        # 리스트 형태를 duration 을 가지도록 변환.
        ks = ks.quantized(ticksPerQuarterNote)
        # music21 스트림으로 변환.
        s = ks._to_music21stream()
        # 다시 midi 파일 형식으로 변환. ( ㅠ.ㅠ )
        mf = music21.midi.translate.streamToMidiFile(s)
        # 파일로 저장.
        mf.open(midi_file_path, 'wb')
        mf.write()
        mf.close()

        print '\tMidi file(=%s) successfully saved. Total 1/8 beats = %d' % (midi_file_path, onehot_mat.shape[0])
示例#3
0
    def save_midi(self, midi_file_path, onehot_mat, ticksPerQuarterNote=1024):
        """

        :param midi_file_path: path to save midi file which is made after predicting
        :param onehot_mat: matrix having notes infomation made after predicting
        :param ticksPerQuarterNote: criterion of note`s length(duration)
        :return:
        """
        # tick step = 8th note, quaver
        tick_step = ticksPerQuarterNote / 2

        # extract note info from one-hot matrix
        result = []
        for i in range(onehot_mat.shape[0]):
            for j in range(onehot_mat.shape[1]):
                # if flow is started with new note info, then add new info to result list
                if i == 0:
                    if onehot_mat[i, j] == 1:
                        result.append({'time': i * tick_step, 'note': j})
                else:
                    if onehot_mat[i, j] == 1 and onehot_mat[i - 1, j] == 0:
                        result.append({'time': i * tick_step, 'note': j})

        # manufacture result list with unroll package`s Keystrike
        ks = KeyStrikes(result)
        # convert ks to have duration
        ks = ks.quantized(ticksPerQuarterNote)
        # convert ks to music21`s stream
        s = ks._to_music21stream()
        # convert s to midi format, again T^T
        mf = music21.midi.translate.streamToMidiFile(s)
        # save mf info with file
        mf.open(midi_file_path, 'wb')
        mf.write()
        mf.close()

        print '\tMidi file(=%s) successfully saved. Total 1/8 beats = %d' % (midi_file_path, onehot_mat.shape[0])