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

[Not a Bug] How to time_stretch without changing the pitch? #27

Open
Jun711 opened this issue Sep 22, 2021 · 4 comments
Open

[Not a Bug] How to time_stretch without changing the pitch? #27

Jun711 opened this issue Sep 22, 2021 · 4 comments

Comments

@Jun711
Copy link

Jun711 commented Sep 22, 2021

I followed this example that I found on Medium but it doesn't seem to work. When the playbackRate is increased, how can I correct the pitch so that it is the same as the original audio?

Screen Shot 2021-09-21 at 7 55 27 PM

I have also tried the following by passing the output of time_stretch to the input of pitch_shift

# Play back at 1.5X speed
y_stretch = pyrb.time_stretch(audioData, sampleRate, audioSpeed)
# Play back two 1.5x tones
y_shift = pyrb.pitch_shift(y_stretch, sampleRate, audioSpeed)
soundfile.write(f'{tmp_name}.wav', y_shift, sampleRate, format='wav')
@bmcfee
Copy link
Owner

bmcfee commented Sep 22, 2021

In general, I wouldn't advise following code examples from medium posts.

Your example calls both time-stretch and pitch-shift. If you only want time stretching, then only call time-stretch.

@Jun711
Copy link
Author

Jun711 commented Sep 22, 2021

@bmcfee
I want to change the playbackRate of the audio without changing the pitch. I am trying to achieve what html5 audio.playbackRate does. When I use time_stretch alone, it seems to introduce some noise. is that expected?

Screen Shot 2021-09-22 at 9 33 09 AM

@bmcfee
Copy link
Owner

bmcfee commented Sep 22, 2021

When I use time_stretch alone, it seems to introduce some noise. is that expected?

I'm not sure how to answer that without more detail: "some noise" could mean a lot of things.

Time stretching is not a perfect process, and there will always be artifacts, but what kind of artifacts you see will depend on the properties of your signal. The default parameters might not be optimal for your particular use case, but you might be able to improve things by playing around with them. See https://breakfastquay.com/rubberband/documentation.html

pyrubberband is just a wrapper for the rubberband library, so you can pass through any parameters you like when calling time_stretch.

@Jun711
Copy link
Author

Jun711 commented Sep 22, 2021

@bmcfee

  1. You can use html5 audio in this repo:https://github.com/Jun711/html5-audio-rubberband to change an audio playbackRate.
    I used 1.5 rate. I also used pyrb.time_stretch to increase the audio playback rate to 1.5. You can listen to both audios in the repo to hear the difference.
y_stretch = pyrb.time_stretch(audioData, sampleRate, audioSpeed)
soundfile.write(f'{tmp_name}.wav', y_stretch, sampleRate, format='wav')
wavAudio = AudioSegment.from_wav(f'{tmp_name}.wav')
wavAudio.export(f'{tmp_name}.mp3', format="mp3")

This is how it looks like when you drag and drop the html file onto a Chrome browser
Screen Shot 2021-09-22 at 10 17 53 AM
Screen Shot 2021-09-22 at 10 18 25 AM

  1. regarding the documentation, which rubberband's function is time_stretch mapped to? I need some help with using the documentation

  2. I just installed rubberband and ran rubberband -h. If I want to turn on this smoothing option, is this the right way to pass it in?

y_stretch = pyrb.time_stretch(audioData, sampleRate, audioSpeed, {'--smoothing': True})
The remaining options fine-tune the processing mode and stretch algorithm.
These are mostly included for test purposes; the default settings and standard
crispness parameter are intended to provide the best sounding set of options
for most situations.  The default is to use none of these options.

         --smoothing      Apply window presum and time-domain smoothing

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

2 participants