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

Handle SoX init/shutdown automatically #572

Merged
merged 2 commits into from Apr 24, 2020
Merged

Conversation

mthrok
Copy link
Collaborator

@mthrok mthrok commented Apr 22, 2020

This PR

  • Adds safeguard to SoX initialization/shutdown
  • When SoX is initialized, it register the exit hook to call shutdown using atexit
    • SoX is shutdown at the end of program execution.
    • Users can still manually shutdown.
  • Automatically initialize SoX when a building effects chain. This frees the users from need to call initialize_sox/shutdown_sox, yet they can still do so. This gives better backend abstraction.

# defined at
# https://fossies.org/dox/sox-14.4.2/sox_8h.html#a8e07e80cebeff3339265d89c387cea93a9ef2b87ec303edfe40751d9a85fadeeb


@_audio_backend_guard("sox")
def initialize_sox() -> int:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think the whole logic of initialize_sox/shutdown_sox can be moved to effect chain and mark them as internal, because users no longer need to worry about it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Essentially, you suggest closing sox on exit only, right? We can discuss that on a separate pull request.


_SOX_SUCCESS_CODE = 0
# defined at
# https://fossies.org/dox/sox-14.4.2/sox_8h.html#a8e07e80cebeff3339265d89c387cea93a9ef2b87ec303edfe40751d9a85fadeeb
Copy link
Collaborator Author

@mthrok mthrok Apr 22, 2020

Choose a reason for hiding this comment

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

It kind of bugs me to copy constant defined in somewhere else like this.
An alternative approach would be, changing the CPP impl of initialize_sox to return boolean, indicating success / failure by performing return code check on CPP side.

Copy link
Contributor

Choose a reason for hiding this comment

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

This can be done in a separate PR later.

"""
global _SOX_INITIALIZED
if _SOX_INITIALIZED is None:
raise RuntimeError('SoX effects chain has been already shut down. Can not initialize again.')
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We do not need to do this, but raising RuntimeError with a message would be better than segfault.

if _SOX_INITIALIZED is None:
raise RuntimeError('SoX effects chain has been already shut down. Can not initialize again.')
if not _SOX_INITIALIZED:
import _torch_sox
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It would be better to have debug log here.

logger.debug('Initializing SoX effects chain.')

Copy link
Contributor

Choose a reason for hiding this comment

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

We could make an explicit commitment to add debugging everywhere, but let's not do that here.

"""
global _SOX_INITIALIZED
if _SOX_INITIALIZED:
import _torch_sox
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Similarly, it would be better to have debug log here.

logger.debug('Shutting down SoX effects chain.')

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

@vincentqb vincentqb merged commit e5d99ed into pytorch:master Apr 24, 2020
@mthrok mthrok deleted the sox-finalize branch April 27, 2020 19:44
mthrok pushed a commit to mthrok/audio that referenced this pull request Feb 26, 2021
[WIP] Audio preprocessing tutorial. Yay!
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

2 participants