예제 #1
0
def plot_pianoroll(path="Sampled/4bar_samples/sample_4_0.midi"):
    midi = ppr.parse(path, beat_resolution=4)  # get Multitrack object
    midi = midi.tracks[0]  # get first/only track
    pianoroll = midi.pianoroll
    print(pianoroll.shape)

    ppr.plot(midi, filename="testppr.png", beat_resolution=4)
예제 #2
0
def plot_interpolation_sotw_to_lick():
    interpolations = []
    for i in range(0, 10, 3):
        path = "Sampled/interpolation_examples/SotW_to_lick/1/interpolate_" + str(
            i) + ".midi"
        midi = ppr.parse(path, beat_resolution=4)  # get Multitrack object
        midi = midi.tracks[0]  # get first/only track
        midi.name = ""
        if i == 0:
            midi.name = "start sequence"
        if i == 10:
            midi.name = "end sequence"

        interpolations.append(midi)

    mt = ppr.Multitrack(tracks=interpolations, beat_resolution=4)
    p, _ = ppr.plot(mt,
                    yticklabel="off",
                    xtick='beat',
                    xticklabel=True,
                    grid="both")
    #p.set_size_inches((8, 8), forward=True)

    filename = "interpolation_SotW_to_lick.png"
    p.savefig(filename)
예제 #3
0
def plot_interpolation_pianorolls(bars=16):
    interpolations = []
    for i in range(0, 6, 2):
        path = "Sampled/" + str(bars) + "bar_interpolation/interpolate_" + str(
            i) + ".midi"

        midi = ppr.parse(path, beat_resolution=4)  # get Multitrack object
        midi = midi.tracks[0]  # get first/only track
        midi.name = ""
        if i == 0:
            midi.name = "start sequence"
        if i == 4:
            midi.name = "end sequence"
        pr = midi.pianoroll

        # padding to full length in case MIDI file ends earlier
        if pr.shape[0] != bars * 16:
            padding = np.zeros((bars * 16 - pr.shape[0], pr.shape[1]))
            pr = np.concatenate((pr, padding), axis=0)
            midi.pianoroll = pr
        interpolations.append(midi)

    mt = ppr.Multitrack(tracks=interpolations, beat_resolution=4)

    if bars == 16:
        p, _ = ppr.plot(mt,
                        yticklabel="number",
                        xtick='beat',
                        xticklabel=False,
                        grid="off")
        # there seems to be a bug in ppr, despite xticklabel=False, the plot still has the labels for each x-axis value
    else:
        p, _ = ppr.plot(mt,
                        yticklabel="number",
                        xtick='beat',
                        xticklabel=True,
                        grid="both")
    p.set_size_inches((8, 8), forward=True)

    filename = str(bars) + "bar_interpolation.png"
    p.savefig(filename)
def reconstruct(file_path,
                model,
                start_bar,
                end_bar,
                temperature=0.5,
                smooth_threshold=0):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    if model.train():
        model.eval()

    with torch.no_grad():
        sample_np = getSlicedPianorollMatrixNp(file_path)
        sample_np = transposeNotesHigherLower(sample_np)
        sample_np = cutOctaves(sample_np)
        sample_np = sample_np[start_bar:end_bar]
        sample = torch.from_numpy(sample_np).float()
        recon, embed, logvar = model(sample.view(-1, 1, 96, 60).to(device))
        recon = torch.softmax(recon, dim=3)
        recon = recon.squeeze(1).cpu().numpy()
        # recon /= np.abs(np.max(recon))
        recon[recon < (1 - temperature)] = 0

        sample_play = debinarizeMidi(sample_np, prediction=False)
        sample_play = addCuttedOctaves(sample_play)
        recon = debinarizeMidi(recon, prediction=True)
        recon = addCuttedOctaves(recon)

        recon_out = recon[0]
        sample_out = sample_play[0]
        if recon.shape[0] > 1:
            for i in range(recon.shape[0] - 1):
                sample_out = np.concatenate((sample_out, sample_play[i + 1]),
                                            axis=0)
                recon_out = np.concatenate((recon_out, recon[i + 1]), axis=0)

    # plot with pypianoroll
    sample_plot = ppr.Track(sample_out)
    ppr.plot(sample_plot)
    recon_plot = ppr.Track(recon_out)
    ppr.plot(recon_plot)
    # smooth output
    smoother = NoteSmoother(recon_out, threshold=smooth_threshold)
    smoothed_seq = smoother.smooth()
    smoother_seq_plot = ppr.Track(smoothed_seq)
    ppr.plot(smoother_seq_plot)
예제 #5
0
    def plot_loss(self):
        plt.plot(self.disc_loss, c='red')
        plt.plot(self.gen_loss, c='blue')
        plt.title("GAN Loss per Epoch")
        plt.legend(['Discriminator', 'Generator'])
        plt.xlabel('Epoch')
        plt.ylabel('Loss')
        plt.savefig('GAN_Loss_per_Epoch_final_1000.png', transparent=True)
        plt.close()


if __name__ == '__main__':
    path = "C:\\Users\\10413\\Desktop\\deep_learning\\project\\midi-lstm-gan-master\\lpd_5\\lpd_5_cleansed"  # 文件夹目录
    listOfFiles = getListOfFiles(path)
    data_train = np.empty([len(listOfFiles), 4 * 96, 128])
    i = 0
    for files in listOfFiles:
        piano_roll = pypianoroll.load(files)
        data = piano_roll.tracks[1].pianoroll
        data2 = piano_roll.tracks[1]
        pypianoroll.plot(data2)  # 打开文件
        if data.shape[0] > (4 * 96 - 1):
            data_train[i, 0:4 * 96 - 1, 0:127] = data[0:4 * 96 - 1, 0:127]
            i = i + 1
        if i > 10:
            break
    gan = GAN(rows=4 * 96)
    gan.train(epochs=2,
              batch_size=32,
              train_data=data_train,
              sample_interval=1)