def main(mode, src, dst, word_vectors_path, embedding_dict_path, transpose):
    to_numpy = mode == AVAILABLE_DIRECTIONS[0]
    input_paths, output_paths = parse_file_paths(src, dst, to_numpy, '.npy')
    print(input_paths, output_paths)

    wv = KeyedVectors.load(word_vectors_path, mmap='r')
    with open(embedding_dict_path) as fp:
        embedding_dict = json.load(fp)

    for input_p, output_p in zip(input_paths, output_paths):
        if to_numpy:
            mid = MidiFile(input_p)
            if transpose:
                for step in range(-12, 13):
                    transposed_mid = transpose_midi(step, mid)
                    numpy_track = mid2np(transposed_mid, wv, embedding_dict)
                    save_numpy_midi(output_p.replace(
                        '.npy', f'_{step}.npy'), numpy_track)
            else:
                numpy_track = mid2np(mid, wv, embedding_dict)
                save_numpy_midi(output_p, numpy_track)
        else:
            numpy_track = read_numpy_midi(input_p)
            mid = np2mid(numpy_track, wv)
            mid.save(output_p)
Exemplo n.º 2
0
def main(src, dst, low_step, high_step):
    to_numpy = True
    input_paths, output_paths = parse_file_paths(src, dst, to_numpy)

    for input_p, output_p in zip(input_paths, output_paths):
        track = read_numpy_midi(input_p)
        for step in range(low_step, high_step):
            output_path = output_p.replace('.', f'_tr_{str(step)}.')
            transposed = transpose(track, step)
            save_numpy_midi(output_path, transposed)
def main(src, dst, transposition, **kwargs):
    input_paths, output_paths = parse_file_paths(src, dst, True)

    for input_p, output_p in zip(input_paths, output_paths):
        mid = MidiFile(input_p)
        numpy_track = just_sparsify(mid)
        if not transposition:
            save_numpy_midi(output_p, numpy_track)
        else:
            for step in range(-12, 13):
                output_path = output_p.replace('.', f'_tr_{str(step)}.')
                transposed = transpose(numpy_track, step)
                save_numpy_midi(output_path, transposed)
Exemplo n.º 4
0
def main(mode, src, dst, **kwargs):
    clean_kwargs = {k: v for k, v in kwargs.items() if v is not None}

    to_numpy = mode == AVAILABLE_DIRECTIONS[0]
    input_paths, output_paths = parse_file_paths(src, dst, to_numpy)

    for input_p, output_p in zip(input_paths, output_paths):
        if to_numpy:
            mid = MidiFile(input_p)
            messages = [m for m in mid]
            numpy_track = mid2np(messages, **clean_kwargs)
            save_numpy_midi(output_p, numpy_track)
        else:
            numpy_track = read_numpy_midi(input_p)
            mid = np2mid(numpy_track, **clean_kwargs)
            mid.save(output_p)
Exemplo n.º 5
0
def main(src, limit_vector, **kwargs):
    clean_kwargs = {k: v for k, v in kwargs.items() if v is not None}

    files_in_folder = get_valid_files_in_dir(src)
    file_paths = [os.path.join(src, f) for f in files_in_folder]

    tracks = [sparse.load_npz(fp).tocsr() for fp in file_paths]
    if limit_vector:
        print(f'limiting to {limit_vector}')
        tracks = [t[:, :limit_vector] for t in tracks]

    counter, counter_notes = create_counters(tracks)
    embedding_dict = create_dict(counter_notes, **clean_kwargs)

    dicted_dataset, dicted_dataset_subsequents_removed = dictify_dataset(
        tracks, embedding_dict)

    output_dir = os.path.join(src, 'meta')
    os.makedirs(output_dir, exist_ok=True)

    options_str = option_kwargs_to_string(clean_kwargs)

    with open(os.path.join(output_dir, f'_counter_nums_{options_str}.json'),
              'w') as f:
        json.dump(counter, f, indent=4)

    with open(os.path.join(output_dir, f'_counter_notes_{options_str}.json'),
              'w') as f:
        json.dump(counter_notes, f, indent=4)

    with open(os.path.join(output_dir, f'_embedding_dict_{options_str}.json'),
              'w') as f:
        json.dump(embedding_dict, f, indent=4)

    save_numpy_midi(
        os.path.join(output_dir, f'_dicted_dataset_{options_str}.npy'),
        dicted_dataset)
    save_numpy_midi(
        os.path.join(output_dir,
                     f'_dicted_dataset_subsequents_removed_{options_str}.npy'),
        dicted_dataset_subsequents_removed)
def main(mode, src, dst, dur_dict, do_transpose, **kwargs):
    clean_kwargs = {k: v for k, v in kwargs.items() if v is not None}

    to_numpy = mode == AVAILABLE_DIRECTIONS[0]
    input_paths, output_paths = parse_file_paths(src, dst, to_numpy)

    if to_numpy:
        tracks, durations = zip(
            *[mid2np(MidiFile(input_p), **clean_kwargs) for input_p in input_paths])
        tracks_blob = np.concatenate(tracks, axis=0)
        durations = np.concatenate(durations)
        durations_ohe, _, durations_dict = cluster_and_ohe_durations(
            durations.reshape(-1, 1))

        meta_dir = os.path.join(dst, 'meta')
        os.makedirs(meta_dir, exist_ok=True)

        with open(os.path.join(meta_dir, 'durations_dict.json'), 'w+') as fp:
            json.dump(durations_dict, fp, indent=4, default=lambda x: float(
                x) if isinstance(x, np.float32) else x)

        track_w_duration_blob = np.concatenate(
            (tracks_blob, durations_ohe), axis=1)
        save_numpy_midi(os.path.join(
            meta_dir, 'tracks_blob_w_duration.npz'), track_w_duration_blob)

        track_lens = [t.shape[0] for t in tracks]
        durations_ohe_split = np.split(
            durations_ohe, np.cumsum(track_lens[:-1]))

        for output_p, t, d in zip(output_paths, tracks, durations_ohe_split):
            if do_transpose:
                for step in range(-12, 13):
                    transposed_t = transpose(t, step)
                    track_w_dur = np.concatenate((transposed_t, d), axis=-1)
                    save_numpy_midi(os.path.join(output_p.replace(
                        '.npz', f'_{step}.npz')), track_w_dur)
            else:
                track_w_dur = np.concatenate((t, d), axis=-1)
                save_numpy_midi(os.path.join(output_p), track_w_dur)
    else:
        meta_dir = os.path.join(dst, 'meta')

        assert dur_dict != '', 'Please specify duration dict'

        with open(dur_dict) as fp:
            durations_dict = json.load(fp)

        for input_p, output_p in zip(input_paths, output_paths):
            numpy_track = read_numpy_midi(input_p)
            track, durations = np.split(numpy_track, [128], axis=1)
            mid = np2mid(track, durations, durations_dict, **clean_kwargs)
            mid.save(output_p)