Exemplo n.º 1
0
def merge_tracks(tracks, dest_path):
    # Merging is easier with pianorollls
    quantization = 64
    T = 0
    for track in tracks:
        # Add a 4 quarter silence
        T += get_time(track, quantization) + quantization * 4

    t = 0
    flag_time_increment = True
    pr = {}
    for track in tracks:
        a = read_midi(track, quantization)
        for k in a.keys():
            if flag_time_increment:
                tt = t + a[k].shape[0]
                flag_time_increment = False
            if k not in pr.keys():
                pr[k] = np.zeros((T, 128))
            pr[k][t:tt] = a[k]
        t = tt + quantization * 4
        flag_time_increment = True

    write_midi(pr, quantization, dest_path, tempo=80)
    return
Exemplo n.º 2
0
def generate(model,
             piano, orchestra, indices,
             generation_length, seed_size, quantization_write,
             generated_folder, logger_generate):
    # Generate sequences from a trained model
    # piano, orchestra and index are data used to seed the generation
    # Note that generation length is fixed by the length of the piano input
    logger_generate.info("# Generating")

    generate_sequence = model.get_generate_function(
        piano=piano, orchestra=orchestra,
        generation_length=generation_length,
        seed_size=seed_size,
        batch_generation_size=len(indices),
        name="generate_sequence")

    # Load the mapping between pitch space and instrument
    metadata = pickle.load(open('../Data/metadata.pkl', 'rb'))
    instru_mapping = metadata['instru_mapping']

    # Given last indices, generate a batch of sequences
    (generated_sequence,) = generate_sequence(indices)
    if generated_folder is not None:
        for write_counter in xrange(generated_sequence.shape[0]):
            # Write midi
            pr_orchestra = reconstruct_pr(generated_sequence[write_counter], instru_mapping)
            write_path = generated_folder + '/' + str(write_counter) + '.mid'
            write_midi(pr_orchestra, quantization_write, write_path, tempo=80)

    return
Exemplo n.º 3
0
def check_orchestration_alignment(path_db, subfolder_names, temporal_granularity, quantization, unit_type, gapopen, gapextend):

    output_dir = 'DEBUG/' +\
                 'Grid_search_database_alignment/' + str(quantization) +\
                 '_' + temporal_granularity +\
                 '_' + unit_type +\
                 '_' + str(gapopen) +\
                 '_' + str(gapextend)

    if temporal_granularity == "event_level":
        quantization_write = 1
    else:
        quantization_write = quantization

    counter = 0
    sum_score = 0
    nbFrame = 0
    nbId = 0
    nbDiffs = 0

    for sub_db in subfolder_names:
        print '#' * 30
        print sub_db

        sub_db_path = path_db + '/' + sub_db
        if not os.path.isdir(sub_db_path):
            continue

        for folder_name in os.listdir(sub_db_path):
            print '# ' + sub_db + ' : ' + folder_name
            folder_path = sub_db_path + '/' + folder_name
            if not os.path.isdir(folder_path):
                continue

            # Skip already computed folders
            save_folder_name = output_dir +\
                '/' + sub_db + '_' + folder_name
            if os.path.isdir(save_folder_name):
                continue

            pr_piano_no_map, _, _, _, _, pr_orchestra_no_map, _, _, instru_orch, _, duration =\
                build_data_aux.process_folder(folder_path, quantization, binary_piano, binary_orch, temporal_granularity, gapopen, gapextend)

            # Apply the mapping
            pr_piano = {}
            pr_orchestra = {}
            for k, v in pr_piano_no_map.iteritems():
                if 'Piano' in pr_piano:
                    pr_piano['Piano'] = np.maximum(pr_piano['Piano'], v)
                else:
                    pr_piano['Piano'] = v

            for k, v in pr_orchestra_no_map.iteritems():
                # unmix instrus
                new_k = instru_orch[k.rstrip('\x00')]
                instru_names = build_data_aux.unmixed_instru(new_k)
                for instru_name in instru_names:
                    if instru_name in pr_orchestra:
                        pr_orchestra[instru_name] = np.maximum(pr_orchestra[instru_name], v)
                    else:
                        pr_orchestra[instru_name] = v

            # Sum all instrument
            piano_aligned = sum_along_instru_dim(pr_piano)
            orchestra_aligned = sum_along_instru_dim(pr_orchestra)
            OOO_aligned = np.zeros((duration, 30), dtype=np.int16)
            CCC_aligned = np.concatenate((piano_aligned, OOO_aligned, orchestra_aligned), axis=1)

            # Update statistics
            # nbFrame += duration
            # sum_score += this_sum_score
            # nbId += this_nbId
            # nbDiffs += this_nbDiffs

            # counter = counter + 1

            # Save every 100 example
            # if counter % 10 == 0:
            #     import pdb; pdb.set_trace()

            if not os.path.isdir(save_folder_name):
                os.makedirs(save_folder_name)

            visualize_mat(CCC_aligned, save_folder_name, 'aligned')
            # write_midi(pr={'piano1': sum_along_instru_dim(pr0)}, quantization=quantization, write_path=save_folder_name + '/0.mid', tempo=80)
            # write_midi(pr={'piano1': sum_along_instru_dim(pr1)}, quantization=quantization, write_path=save_folder_name + '/1.mid', tempo=80)
            write_midi(pr=pr_piano, quantization=quantization_write, write_path=save_folder_name + '/0.mid', tempo=80)
            write_midi(pr=pr_orchestra, quantization=quantization_write, write_path=save_folder_name + '/1.mid', tempo=80)
            write_midi(pr={'Piano': piano_aligned, 'Violin': orchestra_aligned}, quantization=quantization_write, write_path=save_folder_name + '/both_aligned.mid', tempo=80)
def check_orchestration_alignment(path_db, subfolder_names, quantization, gapopen, gapextend):

    output_dir = 'DEBUG/' + str(quantization) +\
                 '_' + str(gapopen) +\
                 '_' + str(gapextend)

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    else:
        # Avoid re-running the algo on already tested parameters
        return

    counter = 0
    sum_score = 0
    nbFrame = 0
    nbId = 0
    nbDiffs = 0

    # num_track_browsed = 30
    for sub_db in subfolder_names:
        print '#' * 30
        print sub_db
        sub_db_path = path_db + '/' + sub_db
        if not os.path.isdir(sub_db_path):
            continue

        # list_tracks_dir = os.listdir(sub_db_path)
        # ind_folder = np.random.permutation(len(list_tracks_dir))
        # for ind in ind_folder[:num_track_browsed]:
        # for ind in list_tracks_dir:

            # folder_name = list_tracks_dir[ind]

        for folder_name in os.listdir(sub_db_path):

            print '#' * 20
            print '#' + folder_name + '\n'
            folder_path = sub_db_path + '/' + folder_name
            if not os.path.isdir(folder_path):
                continue

            # Get instrus and prs from a folder name name
            pr0, instru0, T0, path_0, pr1, instru1, T1, path_1 = build_data_aux.get_instru_and_pr_from_folder_path(folder_path, quantization=quantization, clip=True)
            # name_0 = re.split('/', path_0)[-1]
            # name_1 = re.split('/', path_1)[-1]

            ################################################
            ################################################
            # def auxiaux(pr, limit):
            #     pr_bis = pr
            #     pr = {}
            #     for k,v in pr_bis.iteritems():
            #         pr[k] = v[:limit,:]
            #     return pr
            # pr0 = auxiaux(pr0, 26)
            # pr1 = auxiaux(pr1, 48)
            ################################################
            ################################################

            # Get trace from needleman_wunsch algorithm
            # Traces are binary lists, 0 meaning a gap is inserted
            trace_0, trace_1, this_sum_score, this_nbId, this_nbDiffs = needleman_chord_wrapper(sum_along_instru_dim(pr0), sum_along_instru_dim(pr1))

            # Wrap dictionnaries according to the traces
            assert(len(trace_0) == len(trace_1)), "size mismatch"
            pr0_warp = warp_dictionnary_trace(pr0, trace_0)
            pr1_warp = warp_dictionnary_trace(pr1, trace_1)

            # In fact we just discard 0 in traces for both pr
            trace_prod = [e1 * e2 for (e1,e2) in zip(trace_0, trace_1)]
            if sum(trace_prod) == 0:
                # It's definitely not a match...
                # Check for the files : are they really an piano score and its orchestration ??
                with(open('log.txt', 'a')) as f:
                    f.write(folder_path + '\n')
                continue
            pr0_aligned = remove_zero_in_trace(pr0_warp, trace_prod)
            pr1_aligned = remove_zero_in_trace(pr1_warp, trace_prod)

            # Sum all instrument
            AAA_warp = sum_along_instru_dim(pr0_warp)
            BBB_warp = sum_along_instru_dim(pr1_warp)
            OOO_warp = np.zeros((BBB_warp.shape[0], 30), dtype=np.int16)
            CCC_warp = np.concatenate((AAA_warp, OOO_warp, BBB_warp), axis=1)
            AAA_aligned = sum_along_instru_dim(pr0_aligned)
            BBB_aligned = sum_along_instru_dim(pr1_aligned)
            OOO_aligned = np.zeros((BBB_aligned.shape[0], 30), dtype=np.int16)
            CCC_aligned = np.concatenate((AAA_aligned, OOO_aligned, BBB_aligned), axis=1)

            # Update statistics
            nbFrame += len(trace_0)
            sum_score += this_sum_score
            nbId += this_nbId
            nbDiffs += this_nbDiffs

            counter = counter + 1

            # Save every 100 example
            if not counter % 10 == 0:
                continue

            save_folder_name = output_dir +\
                '/' + sub_db + '_' + folder_name

            if not os.path.exists(save_folder_name):
                os.makedirs(save_folder_name)
            temp_csv = save_folder_name + '/warp.csv'
            np.savetxt(temp_csv, CCC_warp, delimiter=',')
            dump_to_csv(temp_csv, temp_csv)
            write_numpy_array_html(save_folder_name + "/pr_warp.html", "warp")

            temp_csv = save_folder_name + '/aligned.csv'
            np.savetxt(temp_csv, CCC_aligned, delimiter=',')
            dump_to_csv(temp_csv, temp_csv)
            write_numpy_array_html(save_folder_name + "/pr_aligned.html", "aligned")

            write_midi(pr={'piano1': sum_along_instru_dim(pr0)}, quantization=quantization, write_path=save_folder_name + '/0.mid', tempo=80)
            write_midi(pr={'piano1': sum_along_instru_dim(pr1)}, quantization=quantization, write_path=save_folder_name + '/1.mid', tempo=80)
            write_midi(pr={'piano1': AAA_warp, 'piano2': BBB_warp}, quantization=quantization, write_path=save_folder_name + '/both__warp.mid', tempo=80)
            write_midi(pr={'piano1': AAA_aligned, 'piano2': BBB_aligned}, quantization=quantization, write_path=save_folder_name + '/both__aligned.mid', tempo=80)

    # Write statistics
    mean_score = float(sum_score) / nbFrame
    nbId_norm = nbId / quantization
    nbDiffs_norm = nbDiffs / quantization

    with open(output_dir + '/log.txt', 'wb') as f:
        f.write("##########################\n" +
                "quantization = %d\n" % quantization +
                "Gapopen = %d\n" % gapopen +
                "Gapextend = %d\n" % gapextend +
                "Number frame = %d\n" % nbFrame +
                "\n\n\n" +
                "Sum score = %d\n" % sum_score+
                "Mean score = %f\n" % mean_score+
                "Number id = %d\n" % nbId +
                "Number id / quantization = %d\n" % nbId_norm+
                "Number diffs = %d\n" % nbDiffs+
                "Number diffs / quantization = %d\n" % nbDiffs_norm)