当前位置: 首页>>代码示例>>Python>>正文


Python torchaudio.load方法代码示例

本文整理汇总了Python中torchaudio.load方法的典型用法代码示例。如果您正苦于以下问题:Python torchaudio.load方法的具体用法?Python torchaudio.load怎么用?Python torchaudio.load使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在torchaudio的用法示例。


在下文中一共展示了torchaudio.load方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: load_cmuarctic_item

# 需要导入模块: import torchaudio [as 别名]
# 或者: from torchaudio import load [as 别名]
def load_cmuarctic_item(line: str,
                        path: str,
                        folder_audio: str,
                        ext_audio: str) -> Tuple[Tensor, int, str, str]:

    utterance_id, utterance = line[0].strip().split(" ", 2)[1:]

    # Remove space, double quote, and single parenthesis from utterance
    utterance = utterance[1:-3]

    file_audio = os.path.join(path, folder_audio, utterance_id + ext_audio)

    # Load audio
    waveform, sample_rate = torchaudio.load(file_audio)

    return (
        waveform,
        sample_rate,
        utterance,
        utterance_id.split("_")[1]
    ) 
开发者ID:pytorch,项目名称:audio,代码行数:23,代码来源:cmuarctic.py

示例2: set_audio_backend

# 需要导入模块: import torchaudio [as 别名]
# 或者: from torchaudio import load [as 别名]
def set_audio_backend(backend: Optional[str]) -> None:
    """Set the backend for I/O operation

    Args:
        backend (str): Name of the backend. One of "sox" or "soundfile",
            based on availability of the system.
    """
    if backend is not None and backend not in list_audio_backends():
        raise RuntimeError(
            f'Backend "{backend}" is not one of '
            f'available backends: {list_audio_backends()}.')

    if backend is None:
        module = no_backend
    elif backend == 'sox':
        module = sox_backend
    elif backend == 'sox_io':
        module = sox_io_backend
    elif backend == 'soundfile':
        module = soundfile_backend
    else:
        raise NotImplementedError(f'Unexpected backend "{backend}"')

    for func in ['save', 'load', 'load_wav', 'info']:
        setattr(torchaudio, func, getattr(module, func)) 
开发者ID:pytorch,项目名称:audio,代码行数:27,代码来源:utils.py

示例3: load_commonvoice_item

# 需要导入模块: import torchaudio [as 别名]
# 或者: from torchaudio import load [as 别名]
def load_commonvoice_item(line: List[str],
                          header: List[str],
                          path: str,
                          folder_audio: str) -> Tuple[Tensor, int, Dict[str, str]]:
    # Each line as the following data:
    # client_id, path, sentence, up_votes, down_votes, age, gender, accent

    assert header[1] == "path"
    fileid = line[1]

    filename = os.path.join(path, folder_audio, fileid)

    waveform, sample_rate = torchaudio.load(filename)

    dic = dict(zip(header, line))

    return waveform, sample_rate, dic 
开发者ID:pytorch,项目名称:audio,代码行数:19,代码来源:commonvoice.py

示例4: test_vad

# 需要导入模块: import torchaudio [as 别名]
# 或者: from torchaudio import load [as 别名]
def test_vad(self):
        sample_files = [
            common_utils.get_asset_path("vad-go-stereo-44100.wav"),
            common_utils.get_asset_path("vad-go-mono-32000.wav")
        ]

        for sample_file in sample_files:
            E = torchaudio.sox_effects.SoxEffectsChain()
            E.set_input_file(sample_file)
            E.append_effect_to_chain("vad")
            x, _ = E.sox_build_flow_effects()

            x_orig, sample_rate = torchaudio.load(sample_file)
            vad = torchaudio.transforms.Vad(sample_rate)

            y = vad(x_orig)
            self.assertTrue(x.allclose(y, rtol=1e-4, atol=1e-4)) 
开发者ID:pytorch,项目名称:audio,代码行数:19,代码来源:test_sox_effects.py

示例5: test_batch_mulaw

# 需要导入模块: import torchaudio [as 别名]
# 或者: from torchaudio import load [as 别名]
def test_batch_mulaw(self):
        test_filepath = common_utils.get_asset_path('steam-train-whistle-daniel_simon.wav')
        waveform, _ = torchaudio.load(test_filepath)  # (2, 278756), 44100

        # Single then transform then batch
        waveform_encoded = torchaudio.transforms.MuLawEncoding()(waveform)
        expected = waveform_encoded.unsqueeze(0).repeat(3, 1, 1)

        # Batch then transform
        waveform_batched = waveform.unsqueeze(0).repeat(3, 1, 1)
        computed = torchaudio.transforms.MuLawEncoding()(waveform_batched)

        # shape = (3, 2, 201, 1394)
        self.assertEqual(computed, expected)

        # Single then transform then batch
        waveform_decoded = torchaudio.transforms.MuLawDecoding()(waveform_encoded)
        expected = waveform_decoded.unsqueeze(0).repeat(3, 1, 1)

        # Batch then transform
        computed = torchaudio.transforms.MuLawDecoding()(computed)

        # shape = (3, 2, 201, 1394)
        self.assertEqual(computed, expected) 
开发者ID:pytorch,项目名称:audio,代码行数:26,代码来源:test_batch_consistency.py

示例6: test_resample_size

# 需要导入模块: import torchaudio [as 别名]
# 或者: from torchaudio import load [as 别名]
def test_resample_size(self):
        input_path = common_utils.get_asset_path('sinewave.wav')
        waveform, sample_rate = torchaudio.load(input_path)

        upsample_rate = sample_rate * 2
        downsample_rate = sample_rate // 2
        invalid_resample = torchaudio.transforms.Resample(sample_rate, upsample_rate, resampling_method='foo')

        self.assertRaises(ValueError, invalid_resample, waveform)

        upsample_resample = torchaudio.transforms.Resample(
            sample_rate, upsample_rate, resampling_method='sinc_interpolation')
        up_sampled = upsample_resample(waveform)

        # we expect the upsampled signal to have twice as many samples
        self.assertTrue(up_sampled.size(-1) == waveform.size(-1) * 2)

        downsample_resample = torchaudio.transforms.Resample(
            sample_rate, downsample_rate, resampling_method='sinc_interpolation')
        down_sampled = downsample_resample(waveform)

        # we expect the downsampled signal to have half as many samples
        self.assertTrue(down_sampled.size(-1) == waveform.size(-1) // 2) 
开发者ID:pytorch,项目名称:audio,代码行数:25,代码来源:test_transforms.py

示例7: _create_data_set

# 需要导入模块: import torchaudio [as 别名]
# 或者: from torchaudio import load [as 别名]
def _create_data_set(self):
        # used to generate the dataset to test on. this is not used in testing (offline procedure)
        test_filepath = common_utils.get_asset_path('kaldi_file.wav')
        sr = 16000
        x = torch.arange(0, 20).float()
        # between [-6,6]
        y = torch.cos(2 * math.pi * x) + 3 * torch.sin(math.pi * x) + 2 * torch.cos(x)
        # between [-2^30, 2^30]
        y = (y / 6 * (1 << 30)).long()
        # clear the last 16 bits because they aren't used anyways
        y = ((y >> 16) << 16).float()
        torchaudio.save(test_filepath, y, sr)
        sound, sample_rate = torchaudio.load(test_filepath, normalization=False)
        print(y >> 16)
        self.assertTrue(sample_rate == sr)
        torch.testing.assert_allclose(y, sound) 
开发者ID:pytorch,项目名称:audio,代码行数:18,代码来源:test_compliance_kaldi.py

示例8: test_dither

# 需要导入模块: import torchaudio [as 别名]
# 或者: from torchaudio import load [as 别名]
def test_dither(self):
        test_filepath = common_utils.get_asset_path('steam-train-whistle-daniel_simon.wav')
        waveform, _ = torchaudio.load(test_filepath)

        waveform_dithered = F.dither(waveform)
        waveform_dithered_noiseshaped = F.dither(waveform, noise_shaping=True)

        E = torchaudio.sox_effects.SoxEffectsChain()
        E.set_input_file(test_filepath)
        E.append_effect_to_chain("dither", [])
        sox_dither_waveform = E.sox_build_flow_effects()[0]

        self.assertEqual(waveform_dithered, sox_dither_waveform, atol=1e-04, rtol=1e-5)
        E.clear_chain()

        E.append_effect_to_chain("dither", ["-s"])
        sox_dither_waveform_ns = E.sox_build_flow_effects()[0]

        self.assertEqual(waveform_dithered_noiseshaped, sox_dither_waveform_ns, atol=1e-02, rtol=1e-5) 
开发者ID:pytorch,项目名称:audio,代码行数:21,代码来源:test_sox_compatibility.py

示例9: test_vctk_transform_pipeline

# 需要导入模块: import torchaudio [as 别名]
# 或者: from torchaudio import load [as 别名]
def test_vctk_transform_pipeline(self):
        test_filepath_vctk = common_utils.get_asset_path('VCTK-Corpus', 'wav48', 'p224', 'p224_002.wav')
        wf_vctk, sr_vctk = torchaudio.load(test_filepath_vctk)

        # rate
        sample = T.Resample(sr_vctk, 16000, resampling_method='sinc_interpolation')
        wf_vctk = sample(wf_vctk)
        # dither
        wf_vctk = F.dither(wf_vctk, noise_shaping=True)

        E = torchaudio.sox_effects.SoxEffectsChain()
        E.set_input_file(test_filepath_vctk)
        E.append_effect_to_chain("gain", ["-h"])
        E.append_effect_to_chain("channels", [1])
        E.append_effect_to_chain("rate", [16000])
        E.append_effect_to_chain("gain", ["-rh"])
        E.append_effect_to_chain("dither", ["-s"])
        wf_vctk_sox = E.sox_build_flow_effects()[0]

        self.assertEqual(wf_vctk, wf_vctk_sox, rtol=1e-03, atol=1e-03) 
开发者ID:pytorch,项目名称:audio,代码行数:22,代码来源:test_sox_compatibility.py

示例10: test_lowpass

# 需要导入模块: import torchaudio [as 别名]
# 或者: from torchaudio import load [as 别名]
def test_lowpass(self):
        """
        Test biquad lowpass filter, compare to SoX implementation
        """

        cutoff_freq = 3000

        noise_filepath = common_utils.get_asset_path('whitenoise.wav')
        E = torchaudio.sox_effects.SoxEffectsChain()
        E.set_input_file(noise_filepath)
        E.append_effect_to_chain("lowpass", [cutoff_freq])
        sox_output_waveform, sr = E.sox_build_flow_effects()

        waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
        output_waveform = F.lowpass_biquad(waveform, sample_rate, cutoff_freq)

        self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5) 
开发者ID:pytorch,项目名称:audio,代码行数:19,代码来源:test_sox_compatibility.py

示例11: test_allpass

# 需要导入模块: import torchaudio [as 别名]
# 或者: from torchaudio import load [as 别名]
def test_allpass(self):
        """
        Test biquad allpass filter, compare to SoX implementation
        """

        central_freq = 1000
        q = 0.707

        noise_filepath = common_utils.get_asset_path('whitenoise.wav')
        E = torchaudio.sox_effects.SoxEffectsChain()
        E.set_input_file(noise_filepath)
        E.append_effect_to_chain("allpass", [central_freq, str(q) + 'q'])
        sox_output_waveform, sr = E.sox_build_flow_effects()

        waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
        output_waveform = F.allpass_biquad(waveform, sample_rate, central_freq, q)

        self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5) 
开发者ID:pytorch,项目名称:audio,代码行数:20,代码来源:test_sox_compatibility.py

示例12: test_bandpass_with_csg

# 需要导入模块: import torchaudio [as 别名]
# 或者: from torchaudio import load [as 别名]
def test_bandpass_with_csg(self):
        """
        Test biquad bandpass filter, compare to SoX implementation
        """

        central_freq = 1000
        q = 0.707
        const_skirt_gain = True

        noise_filepath = common_utils.get_asset_path('whitenoise.wav')
        E = torchaudio.sox_effects.SoxEffectsChain()
        E.set_input_file(noise_filepath)
        E.append_effect_to_chain("bandpass", ["-c", central_freq, str(q) + 'q'])
        sox_output_waveform, sr = E.sox_build_flow_effects()

        waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
        output_waveform = F.bandpass_biquad(waveform, sample_rate, central_freq, q, const_skirt_gain)

        self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5) 
开发者ID:pytorch,项目名称:audio,代码行数:21,代码来源:test_sox_compatibility.py

示例13: test_bandpass_without_csg

# 需要导入模块: import torchaudio [as 别名]
# 或者: from torchaudio import load [as 别名]
def test_bandpass_without_csg(self):
        """
        Test biquad bandpass filter, compare to SoX implementation
        """

        central_freq = 1000
        q = 0.707
        const_skirt_gain = False

        noise_filepath = common_utils.get_asset_path('whitenoise.wav')
        E = torchaudio.sox_effects.SoxEffectsChain()
        E.set_input_file(noise_filepath)
        E.append_effect_to_chain("bandpass", [central_freq, str(q) + 'q'])
        sox_output_waveform, sr = E.sox_build_flow_effects()

        waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
        output_waveform = F.bandpass_biquad(waveform, sample_rate, central_freq, q, const_skirt_gain)

        self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5) 
开发者ID:pytorch,项目名称:audio,代码行数:21,代码来源:test_sox_compatibility.py

示例14: test_bandreject

# 需要导入模块: import torchaudio [as 别名]
# 或者: from torchaudio import load [as 别名]
def test_bandreject(self):
        """
        Test biquad bandreject filter, compare to SoX implementation
        """

        central_freq = 1000
        q = 0.707

        noise_filepath = common_utils.get_asset_path('whitenoise.wav')
        E = torchaudio.sox_effects.SoxEffectsChain()
        E.set_input_file(noise_filepath)
        E.append_effect_to_chain("bandreject", [central_freq, str(q) + 'q'])
        sox_output_waveform, sr = E.sox_build_flow_effects()

        waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
        output_waveform = F.bandreject_biquad(waveform, sample_rate, central_freq, q)

        self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5) 
开发者ID:pytorch,项目名称:audio,代码行数:20,代码来源:test_sox_compatibility.py

示例15: test_band_with_noise

# 需要导入模块: import torchaudio [as 别名]
# 或者: from torchaudio import load [as 别名]
def test_band_with_noise(self):
        """
        Test biquad band filter with noise mode, compare to SoX implementation
        """

        central_freq = 1000
        q = 0.707
        noise = True

        noise_filepath = common_utils.get_asset_path('whitenoise.wav')
        E = torchaudio.sox_effects.SoxEffectsChain()
        E.set_input_file(noise_filepath)
        E.append_effect_to_chain("band", ["-n", central_freq, str(q) + 'q'])
        sox_output_waveform, sr = E.sox_build_flow_effects()

        waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
        output_waveform = F.band_biquad(waveform, sample_rate, central_freq, q, noise)

        self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5) 
开发者ID:pytorch,项目名称:audio,代码行数:21,代码来源:test_sox_compatibility.py


注:本文中的torchaudio.load方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。