audio_reader.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:the-wavenet-pianist 作者: 821760408-sp 项目源码 文件源码
def load_generic_audio(directory, sample_rate):
    """Generator that yields audio waveforms from the directory."""

    def randomize_files(fns):
        for _ in fns:
            file_index = random.randint(0, len(fns) - 1)
            yield fns[file_index]

    files = find_files(directory)
    id_reg_exp = re.compile(FILE_PATTERN)
    print("files length: {}".format(len(files)))
    randomized_files = randomize_files(files)
    for filename in randomized_files:
        ids = id_reg_exp.findall(filename)
        if not ids:
            # The file name does not match the pattern containing ids, so
            # there is no id.
            category_id = None
        else:
            # The file name matches the pattern for containing ids.
            category_id = int(ids[0][0])
        audio, _ = librosa.load(filename, sr=sample_rate, mono=True)
        # Normalize audio
        audio = librosa.util.normalize(audio) * 0.8
        # Trim the last 5 seconds to account for music rollout
        audio = audio[:-5 * sample_rate]
        audio = np.reshape(audio, (-1, 1))
        yield audio, filename, category_id
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号