def write_notation_cell(music, path, event_index):
    score = Score()

    metadata = Metadata()
    metadata.title = ''
    metadata.composer = ''
    score.insert(0, metadata)

    layout = ScoreLayout()
    layout.scalingMillimeters = 1.25
    layout.scalingTenths = 40
    score.insert(0, layout)

    for musician in music:
        instrument_name = musician['instrument']
        instrument = get_instrument(instrument_name)
        instrument.partName = instrument.instrumentName
        if instrument.instrumentName is 'Violoncello':
            instrument.partName = 'Cello'
        instrument.partAbbreviation = instrument.instrumentAbbreviation

        parts = []
        part = Part()
        parts.append(part)
        part.insert(0, instrument)

        score.insert(0, part)
        # score.insert(0, StaffGroup(parts))

        for event in musician['music']:
            pitches = event['pitches']
            dur = event['duration']
            # if not pitches or pitches == 'stop':
            #     note = Rest()
            if len(pitches) == 1:
                pitch = Pitch(pitches[0] + 60)
                note = Note(pitch)
            else:
                note = Chord(notes=[Pitch(p + 60) for p in pitches])

            duration = Duration()
            duration.fill([dur])
            note.duration = duration

            part.append(note)

    file_path = os.path.join(path, str(event_index).zfill(2))
    musicxml_file_path = file_path + '.xml'
    png_output_file_path = file_path + '.png'

    score.write('musicxml', musicxml_file_path)

    write_png_with_musescore(musicxml_file_path, png_output_file_path, dpi=600)
    def predict(self, data, count, temp, length=500):

        songs = list(set([i.song for i in data]))

        bug = True
        while bug:
            try:
                condition = True
                while condition:
                    try:
                        random_song = random.choice(songs)
                        slice_by_instrument = dict(zip(self.target_instruments_str, [[] for i in self.target_instruments_str]))
                        for j in self.target_instruments_str:
                            for i in data:
                                if i.song == random_song and i.instrument == j:
                                    slice_by_instrument[j].append(i)

                        slice_by_instrument_without_rests = dict(zip(self.target_instruments_str, [[] for i in self.target_instruments_str]))

                        for i in slice_by_instrument.keys():
                            for song in slice_by_instrument[i]:
                                if not isinstance(song.chords[0], note.Rest):
                                    slice_by_instrument_without_rests[i].append(song)
                            if len(slice_by_instrument_without_rests[i]) != 0:
                                slice_by_instrument[i] = random.choice(slice_by_instrument_without_rests[i])
                            else:
                                slice_by_instrument[i] = random.choice(slice_by_instrument[i])

                        condition = False
                    except IndexError:
                        continue

                guitar_chords = slice_by_instrument['Electric Guitar'].chords
                guitar_durations = slice_by_instrument['Electric Guitar'].durations
                bass_chords = slice_by_instrument['Electric Bass'].chords
                drum_chords = slice_by_instrument['Piano'].chords


                starting_slice_notes = (np.asarray(encode_using_mapper(guitar_chords, self.guitar_mapper)) / len(self.guitar_mapper))[:20]
                starting_slice_durations = (np.asarray(encode_using_mapper(guitar_durations, self.guitar_durations_mapper)) / len(
                    self.guitar_durations_mapper))[:20]
                starting_slice_bass = (np.asarray(encode_using_mapper(bass_chords, self.bass_mapper)) / len(self.bass_mapper))[:20]
                starting_slice_drum = (np.asarray(encode_using_mapper(drum_chords, self.drum_mapper)) / len(self.drum_mapper))[:20]

                songs_in_db_cnt = len(get_songs_by_author(self.db_name))
                to_generate = count

                for j in range(songs_in_db_cnt, songs_in_db_cnt + to_generate):

                    generated_output = generate_multi_instrument_notes(self.model, starting_slice_notes, starting_slice_durations,
                                                                       starting_slice_bass, starting_slice_drum, self.guitar_mapper,
                                                                       self.guitar_durations_mapper, self.bass_mapper,
                                                                       self.drum_mapper, self.guitar_mapper_list, self.durations_mapper_list, temp=temp, length = length)

                    (guitar_output, bass_output, drum_output) = generated_output


                    guitar_part = create_midipart_with_durations(guitar_output, target_instrument=self.target_instruments[0])
                    bass_part = create_midipart_with_durations(bass_output, target_instrument=self.target_instruments[1])
                    #drum_part = create_drum_part_with_durations(drum_output)

                    # TODO fix drum sounds

                    guitar_part.insert(0, self.target_instruments[0])
                    bass_part.insert(0, self.target_instruments[1])
                    #drum_part.insert(0, self.target_instruments[2])

                    full_midi = Score()
                    full_midi.insert(0, guitar_part)
                    full_midi.insert(0, bass_part)
                    #full_midi.insert(0, drum_part)

                    midi_path = f'LSTM_{self.instrument_name}_{j}.mid'

                    full_midi.write('midi', fp=midi_path)
                    midi_to_wav(midi_path, f'static/songs/LSTM_{self.instrument_name}_{j}.wav')

                    self.save_song_to_db(f'LSTM_{self.instrument_name}_{j}.wav')
                bug = False
            except ValueError:
                continue
Ejemplo n.º 3
0
    def predict(self, data, count, temp, length=500):

        songs = list(set([i.song for i in data]))

        bug = True
        while bug:
            try:
                condition = True
                while condition:
                    try:
                        random_song = random.choice(songs)
                        slice_by_instrument = dict(
                            zip(self.target_instruments_str,
                                [[] for i in self.target_instruments_str]))
                        for j in self.target_instruments_str:
                            for i in data:
                                if i.song == random_song and i.instrument == j:
                                    slice_by_instrument[j].append(i)

                        slice_by_instrument_without_rests = dict(
                            zip(self.target_instruments_str,
                                [[] for i in self.target_instruments_str]))

                        for i in slice_by_instrument.keys():
                            for song in slice_by_instrument[i]:
                                if not isinstance(song.chords[0], note.Rest):
                                    slice_by_instrument_without_rests[
                                        i].append(song)
                            if len(slice_by_instrument_without_rests[i]) != 0:
                                slice_by_instrument[i] = random.choice(
                                    slice_by_instrument_without_rests[i])
                            else:
                                slice_by_instrument[i] = random.choice(
                                    slice_by_instrument[i])

                        condition = False
                    except IndexError:
                        continue

                guitar_chords = slice_by_instrument['Electric Guitar'].chords
                guitar_durations = slice_by_instrument[
                    'Electric Guitar'].durations
                bass_chords = slice_by_instrument['Electric Bass'].chords
                bass_durations = slice_by_instrument['Electric Bass'].durations

                combined_guitar = combine_chords_with_durations(
                    guitar_chords, guitar_durations)
                combined_bass = combine_chords_with_durations(
                    bass_chords, bass_durations)

                starting_slice_notes = (np.asarray(
                    encode_using_mapper(combined_guitar,
                                        self.guitar_mapper)))[:20]
                starting_slice_bass = (np.asarray(
                    encode_using_mapper(combined_bass, self.bass_mapper)))[:20]

                songs_in_db_cnt = len(get_songs_by_author(self.db_name))
                to_generate = count

                for j in range(songs_in_db_cnt, songs_in_db_cnt + to_generate):

                    generated_guitar = generate_notes(
                        self.guitar_model,
                        starting_slice_notes,
                        self.guitar_mapper,
                        mapperlist=self.guitar_mapper_list,
                        temp=temp,
                        length=length,
                        normalize=False,
                        random_start=False)
                    generated_bass = generate_notes(
                        self.bass_model,
                        starting_slice_bass,
                        self.bass_mapper,
                        mapperlist=self.bass_mapper_list,
                        temp=temp,
                        length=length,
                        normalize=False,
                        random_start=False)

                    guitar_part = create_midipart_with_durations(
                        generated_guitar,
                        target_instrument=self.target_instruments[0])
                    bass_part = create_midipart_with_durations(
                        generated_bass,
                        target_instrument=self.target_instruments[1])

                    guitar_part.insert(0, self.target_instruments[0])
                    bass_part.insert(0, self.target_instruments[1])

                    full_midi = Score()
                    full_midi.insert(0, guitar_part)
                    full_midi.insert(0, bass_part)

                    midi_path = f'Transformer_{self.instrument_name}_{j}.mid'

                    full_midi.write('midi', fp=midi_path)
                    midi_to_wav(
                        midi_path,
                        f'static/songs/Transformer_{self.instrument_name}_{j}.wav'
                    )

                    self.save_song_to_db(
                        f'Transformer_{self.instrument_name}_{j}.wav')
                bug = False
            except:
                continue