Commit d8a47f4a authored by jamarshon's avatar jamarshon Committed by Vincent QB
Browse files

Removal of torchaudio.legacy

parent 3d4f256a
......@@ -34,7 +34,6 @@ test:
- torchaudio
- torchaudio.datasets
- torchaudio.kaldi_io
- torchaudio.legacy
- torchaudio.sox_effects
- torchaudio.transforms
......
......@@ -13,7 +13,6 @@ The :mod:`torchaudio` package consists of I/O, popular datasets and common audio
kaldi_io
transforms
functional
legacy
.. automodule:: torchaudio
:members:
.. role:: hidden
:class: hidden-section
torchaudio.legacy
======================
.. currentmodule:: torchaudio.legacy
Legacy loading and save functions.
:hidden:`load`
~~~~~~~~~~~~~~~~~~~~~~~~~~
.. autofunction:: load
:hidden:`save`
~~~~~~~~~~~~~~~~~~~~~~~~~~
.. autofunction:: save
import unittest
import common_utils
import torch
import torchaudio
from torchaudio.legacy import save, load
import math
import os
class Test_LoadSave(unittest.TestCase):
test_dirpath, test_dir = common_utils.create_temp_assets_dir()
test_filepath = os.path.join(test_dirpath, "assets",
"steam-train-whistle-daniel_simon.mp3")
def test_load(self):
# check normal loading
x, sr = load(self.test_filepath)
self.assertEqual(sr, 44100)
self.assertEqual(x.size(), (278756, 2))
self.assertGreater(x.sum(), 0)
# check normalizing
x, sr = load(self.test_filepath, normalization=True)
self.assertEqual(x.dtype, torch.float32)
self.assertTrue(x.min() >= -1.0)
self.assertTrue(x.max() <= 1.0)
# check raising errors
with self.assertRaises(OSError):
load("file-does-not-exist.mp3")
with self.assertRaises(OSError):
tdir = os.path.join(
os.path.dirname(self.test_dirpath), "torchaudio")
load(tdir)
def test_save(self):
# load signal
x, sr = load(self.test_filepath)
# check save
new_filepath = os.path.join(self.test_dirpath, "test.wav")
save(new_filepath, x, sr)
self.assertTrue(os.path.isfile(new_filepath))
os.unlink(new_filepath)
# check automatic normalization
x /= 1 << 31
save(new_filepath, x, sr)
self.assertTrue(os.path.isfile(new_filepath))
os.unlink(new_filepath)
# test save 1d tensor
x = x[:, 0] # get mono signal
x.squeeze_() # remove channel dim
save(new_filepath, x, sr)
self.assertTrue(os.path.isfile(new_filepath))
os.unlink(new_filepath)
# don't allow invalid sizes as inputs
with self.assertRaises(ValueError):
x.unsqueeze_(0) # N x L not L x N
save(new_filepath, x, sr)
with self.assertRaises(ValueError):
x.squeeze_()
x.unsqueeze_(1)
x.unsqueeze_(0) # 1 x L x 1
save(new_filepath, x, sr)
# automatically convert sr from floating point to int
x.squeeze_(0)
save(new_filepath, x, float(sr))
self.assertTrue(os.path.isfile(new_filepath))
os.unlink(new_filepath)
# don't save to folders that don't exist
with self.assertRaises(OSError):
new_filepath = os.path.join(self.test_dirpath, "no-path",
"test.wav")
save(new_filepath, x, sr)
# save created file
sinewave_filepath = os.path.join(self.test_dirpath, "assets",
"sinewave.wav")
sr = 16000
freq = 440
volume = 0.3
y = (torch.cos(
2 * math.pi * torch.arange(0, 4 * sr).float() * freq / sr))
y.unsqueeze_(1)
# y is between -1 and 1, so must scale
y = (y * volume * 2**31).long()
save(sinewave_filepath, y, sr)
self.assertTrue(os.path.isfile(sinewave_filepath))
# test precision
new_filepath = os.path.join(self.test_dirpath, "test.wav")
si, ei = torchaudio.info(sinewave_filepath)
save(new_filepath, y, sr, precision=16)
si16, ei16 = torchaudio.info(new_filepath)
self.assertEqual(si.precision, 32)
self.assertEqual(si16.precision, 16)
os.unlink(new_filepath)
def test_load_and_save_is_identity(self):
input_path = os.path.join(self.test_dirpath, 'assets', 'sinewave.wav')
tensor, sample_rate = load(input_path)
output_path = os.path.join(self.test_dirpath, 'test.wav')
save(output_path, tensor, sample_rate, 32)
tensor2, sample_rate2 = load(output_path)
self.assertTrue(tensor.allclose(tensor2))
self.assertEqual(sample_rate, sample_rate2)
os.unlink(output_path)
def test_load_partial(self):
num_frames = 100
offset = 200
# load entire mono sinewave wav file, load a partial copy and then compare
input_sine_path = os.path.join(self.test_dirpath, 'assets', 'sinewave.wav')
x_sine_full, sr_sine = load(input_sine_path)
x_sine_part, _ = load(input_sine_path, num_frames=num_frames, offset=offset)
l1_error = x_sine_full[offset:(num_frames + offset)].sub(x_sine_part).abs().sum().item()
# test for the correct number of samples and that the correct portion was loaded
self.assertEqual(x_sine_part.size(0), num_frames)
self.assertEqual(l1_error, 0.)
# create a two channel version of this wavefile
x_2ch_sine = x_sine_full.repeat(1, 2)
out_2ch_sine_path = os.path.join(self.test_dirpath, 'assets', '2ch_sinewave.wav')
save(out_2ch_sine_path, x_2ch_sine, sr_sine)
x_2ch_sine_load, _ = load(out_2ch_sine_path, num_frames=num_frames, offset=offset)
os.unlink(out_2ch_sine_path)
l1_error = x_2ch_sine_load.sub(x_2ch_sine[offset:(offset + num_frames)]).abs().sum().item()
self.assertEqual(l1_error, 0.)
# test with two channel mp3
x_2ch_full, sr_2ch = load(self.test_filepath, normalization=True)
x_2ch_part, _ = load(self.test_filepath, normalization=True, num_frames=num_frames, offset=offset)
l1_error = x_2ch_full[offset:(offset + num_frames)].sub(x_2ch_part).abs().sum().item()
self.assertEqual(x_2ch_part.size(0), num_frames)
self.assertEqual(l1_error, 0.)
# check behavior if number of samples would exceed file length
offset_ns = 300
x_ns, _ = load(input_sine_path, num_frames=100000, offset=offset_ns)
self.assertEqual(x_ns.size(0), x_sine_full.size(0) - offset_ns)
# check when offset is beyond the end of the file
with self.assertRaises(RuntimeError):
load(input_sine_path, offset=100000)
def test_z_get_info(self):
input_path = os.path.join(self.test_dirpath, 'assets', 'sinewave.wav')
channels, samples, rate, precision = (1, 64000, 16000, 32)
si, ei = torchaudio.info(input_path)
self.assertEqual(si.channels, channels)
self.assertEqual(si.length, samples)
self.assertEqual(si.rate, rate)
self.assertEqual(ei.bits_per_sample, precision)
if __name__ == '__main__':
unittest.main()
......@@ -5,7 +5,7 @@ import torch
import _torch_sox
from .version import __version__, git_version
from torchaudio import transforms, datasets, kaldi_io, sox_effects, legacy, compliance, _docs
from torchaudio import transforms, datasets, kaldi_io, sox_effects, compliance, _docs
def check_input(src):
......
from __future__ import division, print_function
import os.path
import torch
import _torch_sox
import torchaudio
def load(filepath, out=None, normalization=None, num_frames=0, offset=0):
r"""Loads an audio file from disk into a Tensor. The default options have
changed as of torchaudio 0.2 and this function maintains option defaults
from version 0.1.
Args:
filepath (str): Path to audio file
out (torch.Tensor, optional): An output Tensor to use instead of creating one. (Default: ``None``)
normalization (bool or number, optional): If boolean `True`, then output is divided by `1 << 31`
(assumes 16-bit depth audio, and normalizes to `[0, 1]`. If `number`, then output is divided by that
number. (Default: ``None``)
num_frames (int, optional): Number of frames to load. -1 to load everything after the
offset. (Default: ``0``)
offset (int, optional): Number of frames from the start of the file to begin data
loading. (Default: ``0``)
Returns:
Tuple[torch.Tensor, int]: The output tensor is of size `[L x C]` where L is the number of audio frames,
C is the number of channels. The integer is sample-rate of the audio (as listed in the metadata of
the file)
Example
>>> data, sample_rate = torchaudio.legacy.load('foo.mp3')
>>> print(data.size())
torch.Size([278756, 2])
>>> print(sample_rate)
44100
"""
return torchaudio.load(filepath, out, normalization, False, num_frames, offset)
def save(filepath, src, sample_rate, precision=32):
r"""Saves a Tensor with audio signal to disk as a standard format like mp3, wav, etc.
The default options have changed as of torchaudio 0.2 and this function maintains
option defaults from version 0.1.
Args:
filepath (str): Path to audio file
src (torch.Tensor): An input 2D Tensor of shape `[L x C]` where L is
the number of audio frames, C is the number of channels
sample_rate (int): The sample-rate of the audio to be saved
precision (int, optional): The bit-precision of the audio to be saved. (Default: ``32``)
Example
>>> data, sample_rate = torchaudio.legacy.load('foo.mp3')
>>> torchaudio.legacy.save('foo.wav', data, sample_rate)
"""
torchaudio.save(filepath, src, sample_rate, precision, False)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment