Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compose sox_effects and torchaudio.transforms #889

Closed
vincentqb opened this issue Aug 26, 2020 · 7 comments
Closed

Compose sox_effects and torchaudio.transforms #889

vincentqb opened this issue Aug 26, 2020 · 7 comments

Comments

@vincentqb
Copy link
Contributor

vincentqb commented Aug 26, 2020

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.

@lcances
Copy link

lcances commented Sep 5, 2020

Hello,
That would be an elegant solution to mix both sox_effects and torchaudio.transforms. I am particularly interested since I deal with waveforms of different lengths and sampling rates. Until now, I was pre-processing my dataset by re-sampling all audio files to the desired waveform and padding / cropping them to the desired length.

To start, I just tried to make your SoxEffectsTransform works but the following error is raised.

RuntimeError: torchaudio.sox_effects.sox_effects.apply_effects_tensor requires module: torchaudio._torchaudio

I carefully read the source code and couldn't find the torchaudio._torchaudio module which is a requirement.
Deactivating this requirement obviously doesn't solve the problem but raise the following error (if it is of any interest)

RuntimeError: Tried to instantiate class 'torchaudio.TensorSignal', but it does not exist! Ensure that it is registered via torch::class_

@mthrok
Copy link
Collaborator

mthrok commented Sep 6, 2020

Hi @leocances

Which OS and version of torchaudio are you using? sox-related functionalities are only available in Linux/macOS distribution. Windows is not supported.

@mthrok
Copy link
Collaborator

mthrok commented Sep 6, 2020

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.

I do not think adding it to transforms provide value for the maintenance cost. It is just a pass-through to the function form, which users can do as a part of their own transform easily.

@mthrok
Copy link
Collaborator

mthrok commented Sep 6, 2020

@leocances FYI: I am adding on-the-fly sample rate conversion here #816. Please give a +1 if you think this is useful.

@lcances
Copy link

lcances commented Sep 6, 2020

Hi @mthrok
I'm using Ubuntu 20.04.1 LTS.
I have created a dedicated environment for testing the current version of torchaudio, cloned the repo, and build it locally. So I should have the latest version available of torchaudio.

I am new to the PyTorch source code in general, and I may have missed something.

@mthrok
Copy link
Collaborator

mthrok commented Sep 6, 2020

@leocances
Hmm, that should work.
Could you try BUILD_SOX=1 python setup.py clean install (or develop)
If that does not help can you create a separate issue and follow the instruction to report your environment, thanks?

@lcances
Copy link

lcances commented Sep 6, 2020

BUILD_SOX=1 python setup.py clean install did not work at once.
I had to run the build command manually before

So, this did the trick.

python setup.py clean
BUILD_SOX=1 python setup.py build install

Thank a lot

@mthrok mthrok closed this as completed Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants