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

Add Torchscript sox effects #760

Merged
merged 6 commits into from Jul 16, 2020
Merged

Conversation

mthrok
Copy link
Collaborator

@mthrok mthrok commented Jul 1, 2020

This PR add new sox effects functions torchaudio.sox_effects.apply_effects_tensor and torchaudio.sox_effects.apply_effects_file, which applies sox effects to Tensor object and file object respectively.

This new functions are written from scratch to take advantage of Torchscript, and tested on various effects by comparing against the results from sox command. The existing torchaudio.sox_effects.SoxEffectsChain does not have this and it does not work correctly on certain format (see #771 )

Also this PR adds torchaudio.utils.sox_utils module, which allows users to set verbosity, multi-threading option, buffer size and get the list of supported effects/formats.

Off topic: @cpuhrsch I was adding some tests on data loader with num_workers > 1. This test example applies speed perturbation, which alters the length of input Tensor. It would be interesting if we can combine nestedtensor there.
https://github.com/mthrok/audio/blob/sox-effects-chain/test/sox_effect/test_dataset.py#L17-L63

@mthrok mthrok force-pushed the sox-effects-chain branch 6 times, most recently from 96cbfb4 to a53314e Compare July 2, 2020 01:20
@codecov
Copy link

codecov bot commented Jul 2, 2020

Codecov Report

Merging #760 into master will increase coverage by 0.13%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #760      +/-   ##
==========================================
+ Coverage   89.53%   89.66%   +0.13%     
==========================================
  Files          32       34       +2     
  Lines        2617     2652      +35     
==========================================
+ Hits         2343     2378      +35     
  Misses        274      274              
Impacted Files Coverage Δ
torchaudio/__init__.py 73.33% <ø> (ø)
torchaudio/sox_effects/__init__.py 100.00% <ø> (ø)
torchaudio/sox_effects/sox_effects.py 95.12% <100.00%> (+0.67%) ⬆️
torchaudio/utils/__init__.py 100.00% <100.00%> (ø)
torchaudio/utils/sox_utils.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 131e48b...a17fffb. Read the comment docs.

@cpuhrsch
Copy link
Contributor

Is it possible to split this up a bit more into smaller PRs or would that result in unreasonable amount of work?

@mthrok mthrok force-pushed the sox-effects-chain branch 4 times, most recently from ea0b9e0 to b2ba4dc Compare July 14, 2020 19:52
@mthrok
Copy link
Collaborator Author

mthrok commented Jul 14, 2020

Is it possible to split this up a bit more into smaller PRs or would that result in unreasonable amount of work?

@cpuhrsch I spliced the PR into logical commits. I can still make them separate PRs if necessary, but this way, we do not need to worry about merge conflict between them. Let me know if you feel separate PRs is better.

The commits are basically about

  1. 70763f4 Add sox utilities. Add functions that can control verbosity, multithreading, buffer size of sox effects, list format/effect functions.
  2. 7b7b4d2 Make init/shutdown_sox_effects functions thread safe by adding lock/guard mechanism
  3. b3d4f38 Add sox_effects implementation.
  4. 4ef41ad Add tests for sox_effects implementation.

@vincentqb
Copy link
Contributor

vincentqb commented Jul 14, 2020

btw, could you add a example code snippet to the description to showcase how to use the new interface? with old-new syntax comparison?

@mthrok
Copy link
Collaborator Author

mthrok commented Jul 15, 2020

btw, could you add a example code snippet to the description to showcase how to use the new interface? with old-new syntax comparison?

@vincentqb Updated the docstring and added examples c4dafdb a17fffb.

I will add migration message in the next PR in which deprecation messages are added to the existing SoxEffectsChain class. I do not think migration step should belong to the docsrtings of the new functions.

@vincentqb
Copy link
Contributor

vincentqb commented Jul 15, 2020

btw, could you add a example code snippet to the description to showcase how to use the new interface? with old-new syntax comparison?

@vincentqb Updated the docstring and added examples c4dafdb a17fffb.

Cool!

I will add migration message in the next PR in which deprecation messages are added to the existing SoxEffectsChain class. I do not think migration step should belong to the docsrtings of the new functions.

That's even better. I initially meant to simply add instructions in the description of this PR as a first step.

@vincentqb
Copy link
Contributor

cc @eugene-kharitonov for interest in applying sox effects to tensors directly without using files :)

@vincentqb
Copy link
Contributor

This PR add new sox effects functions torchaudio.sox_effects.apply_effects_tensor and torchaudio.sox_effects.apply_effects_file, which applies sox effects to Tensor object and file object respectively.

Is there value in having an overloaded function that switched between the two depending on input?



@_mod_utils.requires_module('torchaudio._torchaudio')
def set_buffer_size(buffer_size: int):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I see functions like these, I understand that there are global parameters behind the scene to manage the sox_effects_chain. Is that the case? Is there a way here to have many chains with different such settings?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I see functions like these, I understand that there are global parameters behind the scene to manage the sox_effects_chain. Is that the case?

Yes

Is there a way here to have many chains with different such settings?

Nope

namespace torchaudio {
namespace sox_effects {

namespace {

enum SoxEffectsResourceState { NotInitialized, Initialized, ShutDown };
SoxEffectsResourceState SOX_RESOURCE_STATE = NotInitialized;
std::mutex SOX_RESOUCE_STATE_MUTEX;
Copy link
Contributor

@vincentqb vincentqb Jul 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the code is thread safe through this global lock, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the code is thread safe through this global lock, right?

Yes.

@@ -125,14 +125,12 @@ void save_audio_file(
const c10::intrusive_ptr<TensorSignal>& signal,
const double compression) {
const auto tensor = signal->getTensor();
const auto sample_rate = signal->getSampleRate();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: what do people do to get sample rate then?

Copy link
Contributor

@vincentqb vincentqb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a few clarifying questions. Thanks for grouping the changes into logical commits. Separate PR would have made it easier to associate descriptions with each changes though, at least for the reader :)

@mthrok
Copy link
Collaborator Author

mthrok commented Jul 16, 2020

This PR add new sox effects functions torchaudio.sox_effects.apply_effects_tensor and torchaudio.sox_effects.apply_effects_file, which applies sox effects to Tensor object and file object respectively.

Is there value in having an overloaded function that switched between the two depending on input?

No.

@mthrok mthrok merged commit 60a8e23 into pytorch:master Jul 16, 2020
@mthrok mthrok deleted the sox-effects-chain branch July 16, 2020 02:48
@mthrok
Copy link
Collaborator Author

mthrok commented Jul 16, 2020

Thanks!

mthrok pushed a commit to mthrok/audio that referenced this pull request Feb 26, 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

Successfully merging this pull request may close these issues.

None yet

3 participants