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

ResizeSignal Always takes Random Chunk #96

Open
kevinbird15 opened this issue May 17, 2021 · 2 comments
Open

ResizeSignal Always takes Random Chunk #96

kevinbird15 opened this issue May 17, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@kevinbird15
Copy link
Collaborator

I was looking at using ResizeSignal to cut an input and output down to the same chunk of audio and realized that's not currently an easy thing to do. I was thinking that adding an option that would allow for crop_start to be set during initialization would be helpful. I haven't written anything yet, but wanted to see if that's something that would be helpful for anybody else.

Code in question:

class ResizeSignal(Transform):
    """Crops signal to be length specified in ms by duration, padding if needed"""

    def __init__(self, duration, pad_mode=AudioPadType.Zeros):
        self.duration = duration
        self.pad_mode = pad_mode
        if pad_mode not in [
            AudioPadType.Zeros,
            AudioPadType.Zeros_After,
            AudioPadType.Repeat,
        ]:
            raise ValueError(
                f"""pad_mode {pad_mode} not currently supported,
                only AudioPadType.Zeros, AudioPadType.Zeros_After,
                or AudioPadType.Repeat"""
            )

    def encodes(self, ai: AudioTensor) -> AudioTensor:
        sig = ai.data
        orig_samples = ai.nsamples
        crop_samples = int((self.duration / 1000) * ai.sr)
        if orig_samples == crop_samples:
            return ai
        elif orig_samples < crop_samples:
            ai.data = _tfm_pad_signal(sig, crop_samples, pad_mode=self.pad_mode)
        else:
            crop_start = random.randint(0, int(orig_samples - crop_samples)) ########################Always Random at the moment
            ai.data = sig[:, crop_start : crop_start + crop_samples]
        return ai

The other thing I though is that you could potentially set a random.seed but I wasn't able to make that solution to work in my code.

@scart97 scart97 added the enhancement New feature or request label May 22, 2021
@scart97
Copy link
Collaborator

scart97 commented May 22, 2021

It makes sense, and would be a good addition to the library. Also I just realized that this code is not using pytorch to create random numbers, that is a major silent bug:
https://tanelp.github.io/posts/a-bug-that-plagues-thousands-of-open-source-ml-projects/

@kevinbird15
Copy link
Collaborator Author

That bug is super interesting, thanks for sharing!

filipmu added a commit to filipmu/fastaudio that referenced this issue Jun 18, 2021
mogwai pushed a commit that referenced this issue Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants