soundfile_backend.py 4.25 KB
Newer Older
Vincent QB's avatar
Vincent QB committed
1
import os
2
from typing import Any, Optional, Tuple
Vincent QB's avatar
Vincent QB committed
3
4

import torch
Tomás Osório's avatar
Tomás Osório committed
5
from torch import Tensor
Vincent QB's avatar
Vincent QB committed
6

7
8
9
10
11
12
13
from torchaudio._internal import (
    module_utils as _mod_utils,
    misc_ops as _misc_ops,
)

if _mod_utils.is_module_available('soundfile'):
    import soundfile
14
15


Vincent QB's avatar
Vincent QB committed
16
17
18
19
20
21
22
23
24
25
_subtype_to_precision = {
    'PCM_S8': 8,
    'PCM_16': 16,
    'PCM_24': 24,
    'PCM_32': 32,
    'PCM_U8': 8
}


class SignalInfo:
Tomás Osório's avatar
Tomás Osório committed
26
27
28
29
30
    def __init__(self,
                 channels: Optional[int] = None,
                 rate: Optional[float] = None,
                 precision: Optional[int] = None,
                 length: Optional[int] = None) -> None:
Vincent QB's avatar
Vincent QB committed
31
32
33
34
35
36
37
        self.channels = channels
        self.rate = rate
        self.precision = precision
        self.length = length


class EncodingInfo:
Tomás Osório's avatar
Tomás Osório committed
38
39
40
41
42
43
44
45
    def __init__(self,
                 encoding: Any = None,
                 bits_per_sample: Optional[int] = None,
                 compression: Optional[float] = None,
                 reverse_bytes: Any = None,
                 reverse_nibbles: Any = None,
                 reverse_bits: Any = None,
                 opposite_endian: Optional[bool] = None) -> None:
Vincent QB's avatar
Vincent QB committed
46
47
48
49
50
51
52
53
54
        self.encoding = encoding
        self.bits_per_sample = bits_per_sample
        self.compression = compression
        self.reverse_bytes = reverse_bytes
        self.reverse_nibbles = reverse_nibbles
        self.reverse_bits = reverse_bits
        self.opposite_endian = opposite_endian


55
@_mod_utils.requires_module('soundfile')
Tomás Osório's avatar
Tomás Osório committed
56
57
58
59
60
61
62
63
64
def load(filepath: str,
         out: Optional[Tensor] = None,
         normalization: Optional[bool] = True,
         channels_first: Optional[bool] = True,
         num_frames: int = 0,
         offset: int = 0,
         signalinfo: SignalInfo = None,
         encodinginfo: EncodingInfo = None,
         filetype: Optional[str] = None) -> Tuple[Tensor, int]:
Vincent QB's avatar
Vincent QB committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    r"""See torchaudio.load"""

    assert out is None
    assert normalization
    assert signalinfo is None
    assert encodinginfo is None

    # stringify if `pathlib.Path` (noop if already `str`)
    filepath = str(filepath)

    # check if valid file
    if not os.path.isfile(filepath):
        raise OSError("{} not found or is a directory".format(filepath))

    if num_frames < -1:
        raise ValueError("Expected value for num_samples -1 (entire file) or >=0")
    if num_frames == 0:
        num_frames = -1
    if offset < 0:
        raise ValueError("Expected positive offset value")

    # initialize output tensor
    # TODO call libsoundfile directly to avoid numpy
    out, sample_rate = soundfile.read(
        filepath, frames=num_frames, start=offset, dtype="float32", always_2d=True
    )
    out = torch.from_numpy(out).t()

    if not channels_first:
        out = out.t()

    # normalize if needed
    # _audio_normalization(out, normalization)

    return out, sample_rate


102
@_mod_utils.requires_module('soundfile')
Tomás Osório's avatar
Tomás Osório committed
103
def save(filepath: str, src: Tensor, sample_rate: int, precision: int = 16, channels_first: bool = True) -> None:
Vincent QB's avatar
Vincent QB committed
104
105
106
107
108
109
110
111
112
    r"""See torchaudio.save"""

    ch_idx, len_idx = (0, 1) if channels_first else (1, 0)

    # check if save directory exists
    abs_dirpath = os.path.dirname(os.path.abspath(filepath))
    if not os.path.isdir(abs_dirpath):
        raise OSError("Directory does not exist: {}".format(abs_dirpath))
    # check that src is a CPU tensor
113
    _misc_ops.check_input(src)
Vincent QB's avatar
Vincent QB committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    # Check/Fix shape of source data
    if src.dim() == 1:
        # 1d tensors as assumed to be mono signals
        src.unsqueeze_(ch_idx)
    elif src.dim() > 2 or src.size(ch_idx) > 16:
        # assumes num_channels < 16
        raise ValueError(
            "Expected format where C < 16, but found {}".format(src.size()))

    if channels_first:
        src = src.t()

    if src.dtype == torch.int64:
        # Soundfile doesn't support int64
        src = src.type(torch.int32)

    precision = "PCM_S8" if precision == 8 else "PCM_" + str(precision)

    return soundfile.write(filepath, src, sample_rate, precision)


135
@_mod_utils.requires_module('soundfile')
Tomás Osório's avatar
Tomás Osório committed
136
def info(filepath: str) -> Tuple[SignalInfo, EncodingInfo]:
Vincent QB's avatar
Vincent QB committed
137
138
139
140
141
142
143
144
    r"""See torchaudio.info"""

    sfi = soundfile.info(filepath)

    precision = _subtype_to_precision[sfi.subtype]
    si = SignalInfo(sfi.channels, sfi.samplerate, precision, sfi.frames)
    ei = EncodingInfo(bits_per_sample=precision)
    return si, ei