コード例 #1
0
    def generate(self, seq_len, a_par=0):
        pattern = self.model_inp[self.start]
        prediction_output = []
        for note_index in range(seq_len):
            prediction_input = pattern.reshape(1, seq_len, 2,
                                               len(self.sorted_notes))
            prediction_input = prediction_input / float(len(self.sorted_notes))
            predictions = self.model.predict(prediction_input, verbose=0)[0]
            for prediction in predictions:
                index = np.argmax(prediction[0])
                duration_i = np.argmax(prediction[1])

                for name, value in self.sorted_notes.items():
                    if value == index:
                        result = name
                        break
                    else:
                        result = None

                for name, value in self.sorted_durations.items():
                    if value == duration_i:
                        duration = name
                        break
                    else:
                        duration = None

                prediction_output.append((result, Duration(duration)))
                result = np.zeros_like(prediction)
                result[0][index] = 1
                result[1][duration_i] = 1
                pattern = np.concatenate([pattern, [result]])
            pattern = pattern[len(pattern) - seq_len:len(pattern)]

        offset = 0
        output_notes = []
        for pattern, duration in prediction_output:
            if pattern.isdigit() or ('.' in pattern):
                notes_in_chord = pattern.split('.')
                notes = []
                for current_note in notes_in_chord:
                    new_note = Note(int(current_note))
                    new_note.duration = duration
                    new_note.storedInstrument = instrument.PanFlute()
                    notes.append(new_note)
                new_chord = Chord(notes)
                new_chord.offset = offset
                output_notes.append(new_chord)
            else:
                new_note = Note(pattern)
                new_note.offset = offset
                new_note.storedInstrument = instrument.Flute()
                output_notes.append(new_note)
            offset += 0.6

        midi_stream = stream.Stream(output_notes)
        midi_stream.write('midi',
                          fp=f'my_music/{self.model.name}_{self.start}.mid')