link_wav.py 3.2 KB
Newer Older
小湉湉's avatar
小湉湉 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os
from operator import itemgetter
from pathlib import Path

import jsonlines
import numpy as np
21
from tqdm import tqdm
小湉湉's avatar
小湉湉 已提交
22

J
Jerryuhoo 已提交
23

小湉湉's avatar
小湉湉 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
def main():
    # parse config and args
    parser = argparse.ArgumentParser(
        description="Preprocess audio and then extract features .")

    parser.add_argument(
        "--old-dump-dir",
        default=None,
        type=str,
        help="directory to dump feature files.")
    parser.add_argument(
        "--dump-dir",
        type=str,
        required=True,
        help="directory to finetune dump feature files.")
    args = parser.parse_args()

    old_dump_dir = Path(args.old_dump_dir).expanduser()
    old_dump_dir = old_dump_dir.resolve()
    dump_dir = Path(args.dump_dir).expanduser()
    # use absolute path
    dump_dir = dump_dir.resolve()
    dump_dir.mkdir(parents=True, exist_ok=True)

    assert old_dump_dir.is_dir()
    assert dump_dir.is_dir()

    for sub in ["train", "dev", "test"]:
        # 把 old_dump_dir 里面的 *-wave.npy 软连接到 dump_dir 的对应位置
        output_dir = dump_dir / sub
        output_dir.mkdir(parents=True, exist_ok=True)
        results = []
56 57 58
        files = os.listdir(output_dir / "raw")
        for name in tqdm(files):
            utt_id = name.split("_feats.npy")[0]
小湉湉's avatar
小湉湉 已提交
59 60 61
            mel_path = output_dir / ("raw/" + name)
            gen_mel = np.load(mel_path)
            wave_name = utt_id + "_wave.npy"
J
Jerryuhoo 已提交
62 63 64
            try:
                wav = np.load(old_dump_dir / sub / ("raw/" + wave_name))
                os.symlink(old_dump_dir / sub / ("raw/" + wave_name),
J
Jerryuhoo 已提交
65
                           output_dir / ("raw/" + wave_name))
J
Jerryuhoo 已提交
66
            except FileNotFoundError:
J
Jerryuhoo 已提交
67 68
                print("delete " + name +
                      " because it cannot be found in the dump folder")
J
Jerryuhoo 已提交
69
                os.remove(output_dir / "raw" / name)
J
Jerryuhoo 已提交
70
                continue
J
Jerryuhoo 已提交
71
            except FileExistsError:
J
Jerryuhoo 已提交
72 73
                print("file " + name + " exists, skip.")
                continue
小湉湉's avatar
小湉湉 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
            num_sample = wav.shape[0]
            num_frames = gen_mel.shape[0]
            wav_path = output_dir / ("raw/" + wave_name)

            record = {
                "utt_id": utt_id,
                "num_samples": num_sample,
                "num_frames": num_frames,
                "feats": str(mel_path),
                "wave": str(wav_path),
            }
            results.append(record)

        results.sort(key=itemgetter("utt_id"))

        with jsonlines.open(output_dir / "raw/metadata.jsonl", 'w') as writer:
            for item in results:
                writer.write(item)


if __name__ == "__main__":
    main()