Skip to content

Compose sox_effects and torchaudio.transforms #889

@vincentqb

Description

@vincentqb

I'd like to suggest a way of composing sox_effects with torchaudio.transforms. The latter is made of torch.nn.Module so they can be composed using torch.nn.Sequential. This would also follow the design pattern we established in torchaudio with transforms/functionals.

class SoxEffectsTransform(torch.nn.Module):

    def __init__(self, effects, sample_rate):
        self.effects = effects
        self.sample_rate = sample_rate

    def forward(self, tensor):
        data, _ = torchaudio.sox_effects.apply_effects_tensor(tensor, self.sample_rate, self.effects)
        return data


tensor, sample_rate = ...
effects = [
    ['gain', '-n', '-10'],
    ['speed', '0.5'],  # duration of data is 0.5 seconds
    ['rate', f'{sample_rate}'],
    ['pad', '0', '1.5'],  # add 1.5 seconds silence at the end
    ['trim', '0', '2'],  # get the first 2 seconds
]
sox_effects_transform = SoxEffectsTransform(effects, sample_rate)
transform = torchaudio.transforms.Spectrogram()

torch.nn.Sequential(sox_effects_transform, transform)

Would it be valuable to offer something like SoxEffectsTransform directly in torchaudio? It is currently in the docstring.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions